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 libdevice, math as tl_math
import torch.nn.functional as F
from torch import nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda


@triton.jit
def triton_per_fused_mul_sum_0(in_ptr0, in_ptr1, out_ptr0, out_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
    tmp0 = tl.load(in_ptr0 + r0, None)
    tmp4 = tl.load(in_ptr1 + r0, None)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = tl.sum(tmp1, 1)[:, None]
    tmp5 = tl.broadcast_to(tmp4, [XBLOCK, RBLOCK])
    tmp7 = tl.sum(tmp5, 1)[:, None]
    tmp8 = tmp0 * tmp4
    tmp9 = tl.broadcast_to(tmp8, [XBLOCK, RBLOCK])
    tmp11 = tl.sum(tmp9, 1)[:, None]
    tl.store(out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp3, None)
    tl.store(out_ptr1 + tl.full([XBLOCK, 1], 0, tl.int32), tmp7, None)
    tl.store(out_ptr2 + tl.full([XBLOCK, 1], 0, tl.int32), tmp11, None)


@triton.jit
def triton_per_fused_mul_sum_1(in_ptr0, in_ptr1, out_ptr0, out_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
    tmp0 = tl.load(in_ptr0 + (64 + r0), None)
    tmp4 = tl.load(in_ptr1 + (64 + r0), None)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = tl.sum(tmp1, 1)[:, None]
    tmp5 = tl.broadcast_to(tmp4, [XBLOCK, RBLOCK])
    tmp7 = tl.sum(tmp5, 1)[:, None]
    tmp8 = tmp0 * tmp4
    tmp9 = tl.broadcast_to(tmp8, [XBLOCK, RBLOCK])
    tmp11 = tl.sum(tmp9, 1)[:, None]
    tl.store(out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp3, None)
    tl.store(out_ptr1 + tl.full([XBLOCK, 1], 0, tl.int32), tmp7, None)
    tl.store(out_ptr2 + tl.full([XBLOCK, 1], 0, tl.int32), tmp11, None)


@triton.jit
def triton_per_fused_mul_sum_2(in_ptr0, in_ptr1, out_ptr0, out_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
    tmp0 = tl.load(in_ptr0 + (128 + r0), None)
    tmp4 = tl.load(in_ptr1 + (128 + r0), None)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = tl.sum(tmp1, 1)[:, None]
    tmp5 = tl.broadcast_to(tmp4, [XBLOCK, RBLOCK])
    tmp7 = tl.sum(tmp5, 1)[:, None]
    tmp8 = tmp0 * tmp4
    tmp9 = tl.broadcast_to(tmp8, [XBLOCK, RBLOCK])
    tmp11 = tl.sum(tmp9, 1)[:, None]
    tl.store(out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp3, None)
    tl.store(out_ptr1 + tl.full([XBLOCK, 1], 0, tl.int32), tmp7, None)
    tl.store(out_ptr2 + tl.full([XBLOCK, 1], 0, tl.int32), tmp11, None)


@triton.jit
def triton_per_fused_mul_sum_3(in_ptr0, in_ptr1, out_ptr0, out_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
    tmp0 = tl.load(in_ptr0 + (192 + r0), None)
    tmp4 = tl.load(in_ptr1 + (192 + r0), None)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = tl.sum(tmp1, 1)[:, None]
    tmp5 = tl.broadcast_to(tmp4, [XBLOCK, RBLOCK])
    tmp7 = tl.sum(tmp5, 1)[:, None]
    tmp8 = tmp0 * tmp4
    tmp9 = tl.broadcast_to(tmp8, [XBLOCK, RBLOCK])
    tmp11 = tl.sum(tmp9, 1)[:, None]
    tl.store(out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp3, None)
    tl.store(out_ptr1 + tl.full([XBLOCK, 1], 0, tl.int32), tmp7, None)
    tl.store(out_ptr2 + tl.full([XBLOCK, 1], 0, tl.int32), tmp11, None)


@triton.jit
def triton_per_fused__to_copy_add_binary_cross_entropy_div_eq_index_put_mul_rsub_smooth_l1_loss_sub_sum_zeros_like_4(
    in_out_ptr1, in_out_ptr2, in_out_ptr3, in_out_ptr4, in_ptr0, in_ptr1,
    in_ptr2, in_ptr3, in_ptr4, in_ptr5, in_ptr6, in_ptr7, in_ptr8, in_ptr9,
    in_ptr10, in_ptr11, in_ptr12, in_ptr13, in_ptr14, out_ptr3, xnumel, rnumel
    ):
    XBLOCK: tl.constexpr = 1
    RBLOCK: tl.constexpr = 256
    xoffset = tl.program_id(0) * XBLOCK
    tl.full([1], xoffset, tl.int32)
    tl.full([RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[:]
    tl.full([RBLOCK], True, tl.int1)
    r0 = rindex
    tmp0 = tl.load(in_ptr0 + r0, None)
    tmp3 = tl.load(in_ptr1 + r0, None)
    tmp16 = tl.load(in_ptr2 + r0, None)
    tmp34 = tl.load(in_ptr3 + r0, None)
    tmp66 = tl.load(in_ptr4 + 0)
    tmp67 = tl.broadcast_to(tmp66, [1])
    tmp68 = tl.load(in_out_ptr4 + 0)
    tmp69 = tl.broadcast_to(tmp68, [1])
    tmp70 = tl.load(in_ptr5 + 0)
    tmp71 = tl.broadcast_to(tmp70, [1])
    tmp77 = tl.load(in_ptr6 + 0)
    tmp78 = tl.broadcast_to(tmp77, [1])
    tmp79 = tl.load(in_ptr7 + 0)
    tmp80 = tl.broadcast_to(tmp79, [1])
    tmp81 = tl.load(in_ptr8 + 0)
    tmp82 = tl.broadcast_to(tmp81, [1])
    tmp88 = tl.load(in_ptr9 + 0)
    tmp89 = tl.broadcast_to(tmp88, [1])
    tmp90 = tl.load(in_ptr10 + 0)
    tmp91 = tl.broadcast_to(tmp90, [1])
    tmp92 = tl.load(in_ptr11 + 0)
    tmp93 = tl.broadcast_to(tmp92, [1])
    tmp99 = tl.load(in_ptr12 + 0)
    tmp100 = tl.broadcast_to(tmp99, [1])
    tmp101 = tl.load(in_ptr13 + 0)
    tmp102 = tl.broadcast_to(tmp101, [1])
    tmp103 = tl.load(in_ptr14 + 0)
    tmp104 = tl.broadcast_to(tmp103, [1])
    tmp1 = 1.0
    tmp2 = tmp0 - tmp1
    tmp4 = -tmp3
    tmp5 = libdevice.log1p(tmp4)
    tmp6 = -100.0
    tmp7 = triton_helpers.maximum(tmp5, tmp6)
    tmp8 = tmp2 * tmp7
    tmp9 = tl_math.log(tmp3)
    tmp10 = triton_helpers.maximum(tmp9, tmp6)
    tmp11 = tmp0 * tmp10
    tmp12 = tmp8 - tmp11
    tmp13 = tl.broadcast_to(tmp12, [RBLOCK])
    tmp15 = triton_helpers.promote_to_tensor(tl.sum(tmp13, 0))
    tmp17 = tmp16 == tmp1
    tmp18 = tmp17.to(tl.float32)
    tmp19 = tl.broadcast_to(tmp18, [RBLOCK])
    tmp21 = triton_helpers.promote_to_tensor(tl.sum(tmp19, 0))
    tmp22 = 0.0
    tmp23 = tmp16 == tmp22
    tmp24 = tmp23.to(tl.float32)
    tmp25 = tl.broadcast_to(tmp24, [RBLOCK])
    tmp27 = triton_helpers.promote_to_tensor(tl.sum(tmp25, 0))
    tmp28 = tmp21 + tmp27
    tmp29 = tmp21 / tmp28
    tmp30 = tl.where(tmp17, tmp29, tmp22)
    tmp31 = tmp1 - tmp29
    tmp32 = tl.where(tmp23, tmp31, tmp30)
    tmp33 = tmp16 - tmp1
    tmp35 = -tmp34
    tmp36 = libdevice.log1p(tmp35)
    tmp37 = triton_helpers.maximum(tmp36, tmp6)
    tmp38 = tmp33 * tmp37
    tmp39 = tl_math.log(tmp34)
    tmp40 = triton_helpers.maximum(tmp39, tmp6)
    tmp41 = tmp16 * tmp40
    tmp42 = tmp38 - tmp41
    tmp43 = tmp42 * tmp32
    tmp44 = tl.broadcast_to(tmp43, [RBLOCK])
    tmp46 = triton_helpers.promote_to_tensor(tl.sum(tmp44, 0))
    tmp47 = tmp34 - tmp16
    tmp48 = tl_math.abs(tmp47)
    tmp49 = tmp48 < tmp1
    tmp50 = tmp48 * tmp48
    tmp51 = 0.5
    tmp52 = tmp50 * tmp51
    tmp53 = tmp52 * tmp1
    tmp54 = tmp48 - tmp51
    tmp55 = tl.where(tmp49, tmp53, tmp54)
    tmp56 = tl.broadcast_to(tmp55, [RBLOCK])
    tmp58 = triton_helpers.promote_to_tensor(tl.sum(tmp56, 0))
    tmp59 = 256.0
    tmp60 = tmp46 / tmp59
    tmp61 = tmp60 * tmp1
    tmp62 = tmp15 / tmp59
    tmp63 = tmp62 * tmp1
    tmp64 = tmp58 / tmp59
    tmp65 = tmp64 * tmp1
    tmp72 = tmp69 + tmp71
    tmp73 = tmp72 - tmp67
    tmp74 = tmp67 / tmp73
    tmp75 = tmp1 - tmp74
    tmp76 = tmp75 + tmp22
    tmp83 = tmp80 + tmp82
    tmp84 = tmp83 - tmp78
    tmp85 = tmp78 / tmp84
    tmp86 = tmp1 - tmp85
    tmp87 = tmp76 + tmp86
    tmp94 = tmp91 + tmp93
    tmp95 = tmp94 - tmp89
    tmp96 = tmp89 / tmp95
    tmp97 = tmp1 - tmp96
    tmp98 = tmp87 + tmp97
    tmp105 = tmp102 + tmp104
    tmp106 = tmp105 - tmp100
    tmp107 = tmp100 / tmp106
    tmp108 = tmp1 - tmp107
    tmp109 = tmp98 + tmp108
    tmp110 = 0.25
    tmp111 = tmp109 * tmp110
    tmp112 = tmp111 * tmp1
    tmp113 = tmp61 + tmp63
    tmp114 = tmp113 + tmp65
    tmp115 = tmp114 + tmp112
    tl.debug_barrier()
    tl.store(in_out_ptr1 + tl.full([1], 0, tl.int32), tmp61, None)
    tl.debug_barrier()
    tl.store(in_out_ptr2 + tl.full([1], 0, tl.int32), tmp63, None)
    tl.debug_barrier()
    tl.store(in_out_ptr3 + tl.full([1], 0, tl.int32), tmp65, None)
    tl.debug_barrier()
    tl.store(in_out_ptr4 + tl.full([1], 0, tl.int32), tmp112, None)
    tl.store(out_ptr3 + tl.full([1], 0, tl.int32), tmp115, None)


def call(args):
    arg0_1, arg1_1, arg2_1, arg3_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))
    assert_size_stride(arg2_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg3_1, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf11 = empty_strided_cuda((), (), torch.float32)
        buf12 = empty_strided_cuda((), (), torch.float32)
        buf13 = empty_strided_cuda((), (), torch.float32)
        get_raw_stream(0)
        triton_per_fused_mul_sum_0[grid(1)](arg1_1, arg0_1, buf11, buf12,
            buf13, 1, 64, XBLOCK=1, num_warps=2, num_stages=1)
        buf14 = empty_strided_cuda((), (), torch.float32)
        buf15 = empty_strided_cuda((), (), torch.float32)
        buf16 = empty_strided_cuda((), (), torch.float32)
        triton_per_fused_mul_sum_1[grid(1)](arg1_1, arg0_1, buf14, buf15,
            buf16, 1, 64, XBLOCK=1, num_warps=2, num_stages=1)
        buf17 = empty_strided_cuda((), (), torch.float32)
        buf18 = empty_strided_cuda((), (), torch.float32)
        buf19 = empty_strided_cuda((), (), torch.float32)
        triton_per_fused_mul_sum_2[grid(1)](arg1_1, arg0_1, buf17, buf18,
            buf19, 1, 64, XBLOCK=1, num_warps=2, num_stages=1)
        buf20 = empty_strided_cuda((), (), torch.float32)
        buf21 = empty_strided_cuda((), (), torch.float32)
        buf22 = empty_strided_cuda((), (), torch.float32)
        triton_per_fused_mul_sum_3[grid(1)](arg1_1, arg0_1, buf20, buf21,
            buf22, 1, 64, XBLOCK=1, num_warps=2, num_stages=1)
        buf7 = empty_strided_cuda((), (), torch.float32)
        buf5 = empty_strided_cuda((), (), torch.float32)
        buf9 = empty_strided_cuda((), (), torch.float32)
        buf6 = buf5
        del buf5
        buf8 = buf7
        del buf7
        buf10 = buf9
        del buf9
        buf23 = buf11
        del buf11
        buf24 = buf23
        del buf23
        buf25 = empty_strided_cuda((), (), torch.float32)
        triton_per_fused__to_copy_add_binary_cross_entropy_div_eq_index_put_mul_rsub_smooth_l1_loss_sub_sum_zeros_like_4[
            grid(1)](buf6, buf8, buf10, buf24, arg2_1, arg3_1, arg1_1,
            arg0_1, buf13, buf12, buf16, buf14, buf15, buf19, buf17, buf18,
            buf22, buf20, buf21, buf25, 1, 256, num_warps=2, num_stages=1)
        del arg0_1
        del arg1_1
        del arg2_1
        del arg3_1
        del buf12
        del buf13
        del buf14
        del buf15
        del buf16
        del buf17
        del buf18
        del buf19
        del buf20
        del buf21
        del buf22
    return buf25, buf6, buf8, buf10, buf24


def _iou(pred, target):
    b = pred.shape[0]
    IoU = 0.0
    for i in range(0, b):
        Iand1 = torch.sum(target[i, :, :] * pred[i, :, :])
        Ior1 = torch.sum(target[i, :, :]) + torch.sum(pred[i, :, :]) - Iand1
        IoU1 = Iand1 / Ior1
        IoU = IoU + (1 - IoU1)
    return IoU / b


class IOU(torch.nn.Module):

    def __init__(self):
        super(IOU, self).__init__()

    def forward(self, pred, target):
        return _iou(pred, target)


class Weighed_Bce_Loss(nn.Module):

    def __init__(self):
        super(Weighed_Bce_Loss, self).__init__()

    def forward(self, x, label):
        x = x.view(-1, 1, x.shape[1], x.shape[2])
        label = label.view(-1, 1, label.shape[1], label.shape[2])
        label_t = (label == 1).float()
        label_f = (label == 0).float()
        p = torch.sum(label_t) / (torch.sum(label_t) + torch.sum(label_f))
        w = torch.zeros_like(label)
        w[label == 1] = p
        w[label == 0] = 1 - p
        loss = F.binary_cross_entropy(x, label, weight=w)
        return loss


class Cls_Loss(nn.Module):

    def __init__(self):
        super(Cls_Loss, self).__init__()

    def forward(self, x, label):
        loss = F.binary_cross_entropy(x, label)
        return loss


class S_Loss(nn.Module):

    def __init__(self):
        super(S_Loss, self).__init__()

    def forward(self, x, label):
        loss = F.smooth_l1_loss(x, label)
        return loss


class LossNew(nn.Module):

    def __init__(self):
        super(LossNew, self).__init__()
        self.loss_wbce = Weighed_Bce_Loss()
        self.loss_cls = Cls_Loss()
        self.loss_s = S_Loss()
        self.loss_i = IOU()
        self.w_wbce = 1
        self.w_cls = 1
        self.w_smooth = 1
        self.w_iou = 1

    def forward(self, input_0, input_1, input_2, input_3):
        arg0_1 = input_0
        arg1_1 = input_1
        arg2_1 = input_2
        arg3_1 = input_3
        output = call([arg0_1, arg1_1, arg2_1, arg3_1])
        return output[0], output[1], output[2], output[3], output[4]


Measured latency_ms: 0.099978