import math
import torch
import torch.nn as nn


class Loss(nn.Module):

    def __init__(self, device, type_in='pred_intervals', alpha=0.1,
        loss_type='qd_soft', censor_R=False, soften=100.0, lambda_in=10.0,
        sigma_in=0.5):
        super().__init__()
        self.alpha = alpha
        self.lambda_in = lambda_in
        self.soften = soften
        self.loss_type = loss_type
        self.type_in = type_in
        self.censor_R = censor_R
        self.sigma_in = sigma_in
        self.device = device

    def forward(self, y_pred, y_true):
        if self.type_in == 'pred_intervals':
            metric = []
            metric_name = []
            y_U = y_pred[:, 0]
            y_L = y_pred[:, 1]
            y_T = y_true[:, 0]
            N_ = y_T.shape[0]
            alpha_ = self.alpha
            lambda_ = self.lambda_in
            torch.mean(y_pred, dim=1)
            MPIW = torch.mean(y_U - y_L)
            gamma_U = torch.sigmoid((y_U - y_T) * self.soften)
            gamma_L = torch.sigmoid((y_T - y_L) * self.soften)
            gamma_ = torch.mul(gamma_U, gamma_L)
            torch.ones_like(gamma_)
            zeros = torch.zeros_like(y_U)
            gamma_U_hard = torch.max(zeros, torch.sign(y_U - y_T))
            gamma_L_hard = torch.max(zeros, torch.sign(y_T - y_L))
            gamma_hard = torch.mul(gamma_U_hard, gamma_L_hard)
            qd_lhs_hard = torch.div(torch.mean(torch.abs(y_U - y_L) *
                gamma_hard), torch.mean(gamma_hard) + 0.001)
            torch.div(torch.mean(torch.abs(y_U - y_L) * gamma_), torch.mean
                (gamma_) + 0.001)
            PICP_soft = torch.mean(gamma_)
            PICP_hard = torch.mean(gamma_hard)
            zero = torch.tensor(0.0)
            qd_rhs_soft = lambda_ * math.sqrt(N_) * torch.pow(torch.max(
                zero, 1.0 - alpha_ - PICP_soft), 2)
            qd_rhs_hard = lambda_ * math.sqrt(N_) * torch.pow(torch.max(
                zero, 1.0 - alpha_ - PICP_hard), 2)
            qd_loss_soft = qd_lhs_hard + qd_rhs_soft
            qd_loss_hard = qd_lhs_hard + qd_rhs_hard
            y_mean = y_U
            y_var_limited = torch.min(y_L, torch.tensor(10.0))
            y_var = torch.max(torch.log(1.0 + torch.exp(y_var_limited)),
                torch.tensor(1e-05))
            self.y_mean = y_mean
            self.y_var = y_var
            gauss_loss = torch.log(y_var) / 2.0 + torch.div(torch.pow(y_T -
                y_mean, 2), 2.0 * y_var)
            gauss_loss = torch.mean(gauss_loss)
            if self.loss_type == 'qd_soft':
                loss = qd_loss_soft
            elif self.loss_type == 'qd_hard':
                loss = qd_loss_hard
            elif self.loss_type == 'gauss_like':
                loss = gauss_loss
            elif self.loss_type == 'picp':
                loss = PICP_hard
            elif self.loss_type == 'mse':
                loss = torch.mean(torch.pow(y_U - y_T, 2))
            torch.mean(gamma_U_hard)
            torch.mean(gamma_L_hard)
            PICP = torch.mean(gamma_hard)
            metric.append(PICP)
            metric_name.append('PICP')
            metric.append(MPIW)
            metric_name.append('MPIW')
        return loss, PICP, MPIW


def get_inputs():
    return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]


def get_init_inputs():
    return [[], {'device': 0}]

import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import math as tl_math
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor


@triton.jit
def triton_per_fused_abs_add_div_exp_lift_fresh_log_maximum_mean_minimum_mul_pow_rsub_sigmoid_sign_sub_zeros_like_0(
    in_out_ptr0, in_out_ptr1, in_out_ptr2, in_ptr0, in_ptr1, out_ptr2,
    xnumel, rnumel, XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 64
    xoffset = tl.program_id(0) * XBLOCK
    xoffset + tl.arange(0, XBLOCK)[:, None]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r0 = rindex % 16
    r1 = rindex // 16
    r2 = rindex
    tmp0 = tl.load(in_ptr0 + (r0 + 64 * r1), None)
    tmp1 = tl.load(in_ptr0 + (16 + r0 + 64 * r1), None)
    tmp4 = tl.load(in_ptr1 + (r0 + 64 * r1), None)
    tmp2 = tmp0 - tmp1
    tmp3 = tl_math.abs(tmp2)
    tmp5 = tmp0 - tmp4
    tmp6 = tl.full([1, 1], 0, tl.int32)
    tmp7 = tmp6 < tmp5
    tmp8 = tmp7.to(tl.int8)
    tmp9 = tmp5 < tmp6
    tmp10 = tmp9.to(tl.int8)
    tmp11 = tmp8 - tmp10
    tmp12 = tmp11.to(tmp5.dtype)
    tmp13 = 0.0
    tmp14 = triton_helpers.maximum(tmp13, tmp12)
    tmp15 = tmp4 - tmp1
    tmp16 = tmp6 < tmp15
    tmp17 = tmp16.to(tl.int8)
    tmp18 = tmp15 < tmp6
    tmp19 = tmp18.to(tl.int8)
    tmp20 = tmp17 - tmp19
    tmp21 = tmp20.to(tmp15.dtype)
    tmp22 = triton_helpers.maximum(tmp13, tmp21)
    tmp23 = tmp14 * tmp22
    tmp24 = tmp3 * tmp23
    tmp25 = tl.broadcast_to(tmp24, [XBLOCK, RBLOCK])
    tmp27 = tl.sum(tmp25, 1)[:, None]
    tmp28 = tl.broadcast_to(tmp23, [XBLOCK, RBLOCK])
    tmp30 = tl.sum(tmp28, 1)[:, None]
    tmp31 = 100.0
    tmp32 = tmp5 * tmp31
    tmp33 = tl.sigmoid(tmp32)
    tmp34 = tmp15 * tmp31
    tmp35 = tl.sigmoid(tmp34)
    tmp36 = tmp33 * tmp35
    tmp37 = tl.broadcast_to(tmp36, [XBLOCK, RBLOCK])
    tmp39 = tl.sum(tmp37, 1)[:, None]
    tmp40 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
    tmp42 = tl.sum(tmp40, 1)[:, None]
    tmp43 = 10.0
    tmp44 = triton_helpers.minimum(tmp1, tmp43)
    tmp45 = tl_math.exp(tmp44)
    tmp46 = 1.0
    tmp47 = tmp45 + tmp46
    tmp48 = tl_math.log(tmp47)
    tmp49 = 9.999999747378752e-06
    tmp50 = triton_helpers.maximum(tmp48, tmp49)
    tmp51 = 64.0
    tmp52 = tmp27 / tmp51
    tmp53 = tmp30 / tmp51
    tmp54 = 0.001
    tmp55 = tmp53 + tmp54
    tmp56 = tmp52 / tmp55
    tmp57 = tmp39 / tmp51
    tmp58 = 0.9
    tmp59 = tmp58 - tmp57
    tmp60 = triton_helpers.maximum(tmp13, tmp59)
    tmp61 = tmp60 * tmp60
    tmp62 = 20.0
    tmp63 = tmp61 * tmp62
    tmp64 = tmp56 + tmp63
    tmp65 = tmp42 / tmp51
    tl.store(out_ptr2 + tl.broadcast_to(r2, [XBLOCK, RBLOCK]), tmp50, None)
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp64, None)
    tl.debug_barrier()
    tl.store(in_out_ptr1 + tl.full([XBLOCK, 1], 0, tl.int32), tmp53, None)
    tl.debug_barrier()
    tl.store(in_out_ptr2 + tl.full([XBLOCK, 1], 0, tl.int32), tmp65, None)


def call(args):
    arg0_1, arg1_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((), (), torch.float32)
        buf3 = empty_strided_cuda((), (), torch.float32)
        buf4 = empty_strided_cuda((), (), torch.float32)
        buf5 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        buf6 = buf0
        del buf0
        buf7 = buf3
        del buf3
        buf8 = buf4
        del buf4
        get_raw_stream(0)
        triton_per_fused_abs_add_div_exp_lift_fresh_log_maximum_mean_minimum_mul_pow_rsub_sigmoid_sign_sub_zeros_like_0[
            grid(1)](buf6, buf7, buf8, arg0_1, arg1_1, buf5, 1, 64, XBLOCK=
            1, num_warps=2, num_stages=1)
        del arg1_1
    return buf6, buf7, buf8, buf5, reinterpret_tensor(arg0_1, (4, 4, 4), (
        64, 4, 1), 0)


class LossNew(nn.Module):

    def __init__(self, device, type_in='pred_intervals', alpha=0.1,
        loss_type='qd_soft', censor_R=False, soften=100.0, lambda_in=10.0,
        sigma_in=0.5):
        super().__init__()
        self.alpha = alpha
        self.lambda_in = lambda_in
        self.soften = soften
        self.loss_type = loss_type
        self.type_in = type_in
        self.censor_R = censor_R
        self.sigma_in = sigma_in
        self.device = device

    def forward(self, input_0, input_1):
        arg0_1 = input_0
        arg1_1 = input_1
        output = call([arg0_1, arg1_1])
        return output[0], output[1], output[2]
