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 as 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_max_0(in_ptr0, out_ptr0, xnumel, rnumel, XBLOCK: tl.
    constexpr):
    xnumel = 16
    RBLOCK: tl.constexpr = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r1 = rindex
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (r1 + 16 * x0), xmask, other=0.0)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = tl.where(xmask, tmp1, float('-inf'))
    tmp4 = triton_helpers.max2(tmp3, 1)[:, None]
    tl.store(out_ptr0 + x0, tmp4, xmask)


@triton.jit
def triton_per_fused_sum_1(in_ptr0, out_ptr0, xnumel, rnumel, XBLOCK: tl.
    constexpr):
    xnumel = 16
    RBLOCK: tl.constexpr = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r1 = rindex
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (r1 + 16 * x0), xmask, other=0.0)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = tl.where(xmask, tmp1, 0)
    tmp4 = tl.sum(tmp3, 1)[:, None]
    tl.store(out_ptr0 + x0, tmp4, xmask)


@triton.jit
def triton_per_fused__to_copy_binary_cross_entropy_with_logits_gt_2(in_ptr0,
    in_ptr1, out_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 16
    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)
    tmp6 = tl.load(in_ptr1 + r0, None)
    tmp1 = 0.0
    tmp2 = tmp0 > tmp1
    tmp3 = tmp2.to(tl.float32)
    tmp4 = 1.0
    tmp5 = tmp4 - tmp3
    tmp7 = tmp5 * tmp6
    tmp8 = triton_helpers.minimum(tmp1, tmp6)
    tmp9 = tl_math.abs(tmp6)
    tmp10 = -tmp9
    tmp11 = tl_math.exp(tmp10)
    tmp12 = libdevice.log1p(tmp11)
    tmp13 = tmp8 - tmp12
    tmp14 = tmp7 - tmp13
    tmp15 = tl.broadcast_to(tmp14, [XBLOCK, RBLOCK])
    tmp17 = tl.sum(tmp15, 1)[:, None]
    tl.store(out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp17, None)


@triton.jit
def triton_per_fused_binary_cross_entropy_with_logits_3(in_ptr0, in_ptr1,
    out_ptr0, 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)
    tmp1 = 1.0
    tmp2 = tmp1 - tmp0
    tmp4 = tmp2 * tmp3
    tmp5 = 0.0
    tmp6 = triton_helpers.minimum(tmp5, tmp3)
    tmp7 = tl_math.abs(tmp3)
    tmp8 = -tmp7
    tmp9 = tl_math.exp(tmp8)
    tmp10 = libdevice.log1p(tmp9)
    tmp11 = tmp6 - tmp10
    tmp12 = tmp4 - tmp11
    tmp13 = tl.broadcast_to(tmp12, [RBLOCK])
    tmp15 = triton_helpers.promote_to_tensor(tl.sum(tmp13, 0))
    tl.store(out_ptr0 + tl.full([1], 0, tl.int32), tmp15, None)


@triton.jit
def triton_per_fused_mul_sum_4(in_ptr0, in_ptr1, out_ptr0, out_ptr1,
    out_ptr2, xnumel, rnumel, XBLOCK: tl.constexpr):
    xnumel = 4
    RBLOCK: tl.constexpr = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r1 = rindex
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (r1 + 64 * x0), xmask, other=0.0)
    tmp2 = tl.load(in_ptr1 + (r1 + 64 * x0), xmask, other=0.0)
    tmp1 = tl.sigmoid(tmp0)
    tmp3 = tmp1 * tmp2
    tmp4 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK])
    tmp6 = tl.where(xmask, tmp4, 0)
    tmp7 = tl.sum(tmp6, 1)[:, None]
    tmp8 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK])
    tmp10 = tl.where(xmask, tmp8, 0)
    tmp11 = tl.sum(tmp10, 1)[:, None]
    tmp12 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
    tmp14 = tl.where(xmask, tmp12, 0)
    tmp15 = tl.sum(tmp14, 1)[:, None]
    tl.store(out_ptr0 + x0, tmp7, xmask)
    tl.store(out_ptr1 + x0, tmp11, xmask)
    tl.store(out_ptr2 + x0, tmp15, xmask)


@triton.jit
def triton_per_fused_mul_sum_5(in_ptr0, in_ptr1, out_ptr0, out_ptr1,
    out_ptr2, xnumel, rnumel, XBLOCK: tl.constexpr):
    xnumel = 4
    RBLOCK: tl.constexpr = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r1 = rindex
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (16 + r1 + 64 * x0), xmask, other=0.0)
    tmp2 = tl.load(in_ptr1 + (16 + r1 + 64 * x0), xmask, other=0.0)
    tmp1 = tl.sigmoid(tmp0)
    tmp3 = tmp1 * tmp2
    tmp4 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK])
    tmp6 = tl.where(xmask, tmp4, 0)
    tmp7 = tl.sum(tmp6, 1)[:, None]
    tmp8 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK])
    tmp10 = tl.where(xmask, tmp8, 0)
    tmp11 = tl.sum(tmp10, 1)[:, None]
    tmp12 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
    tmp14 = tl.where(xmask, tmp12, 0)
    tmp15 = tl.sum(tmp14, 1)[:, None]
    tl.store(out_ptr0 + x0, tmp7, xmask)
    tl.store(out_ptr1 + x0, tmp11, xmask)
    tl.store(out_ptr2 + x0, tmp15, xmask)


@triton.jit
def triton_per_fused_mul_sum_6(in_ptr0, in_ptr1, out_ptr0, out_ptr1,
    out_ptr2, xnumel, rnumel, XBLOCK: tl.constexpr):
    xnumel = 4
    RBLOCK: tl.constexpr = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r1 = rindex
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (32 + r1 + 64 * x0), xmask, other=0.0)
    tmp2 = tl.load(in_ptr1 + (32 + r1 + 64 * x0), xmask, other=0.0)
    tmp1 = tl.sigmoid(tmp0)
    tmp3 = tmp1 * tmp2
    tmp4 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK])
    tmp6 = tl.where(xmask, tmp4, 0)
    tmp7 = tl.sum(tmp6, 1)[:, None]
    tmp8 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK])
    tmp10 = tl.where(xmask, tmp8, 0)
    tmp11 = tl.sum(tmp10, 1)[:, None]
    tmp12 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
    tmp14 = tl.where(xmask, tmp12, 0)
    tmp15 = tl.sum(tmp14, 1)[:, None]
    tl.store(out_ptr0 + x0, tmp7, xmask)
    tl.store(out_ptr1 + x0, tmp11, xmask)
    tl.store(out_ptr2 + x0, tmp15, xmask)


@triton.jit
def triton_per_fused_mul_sum_7(in_ptr0, in_ptr1, out_ptr0, out_ptr1,
    out_ptr2, xnumel, rnumel, XBLOCK: tl.constexpr):
    xnumel = 4
    RBLOCK: tl.constexpr = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r1 = rindex
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (48 + r1 + 64 * x0), xmask, other=0.0)
    tmp2 = tl.load(in_ptr1 + (48 + r1 + 64 * x0), xmask, other=0.0)
    tmp1 = tl.sigmoid(tmp0)
    tmp3 = tmp1 * tmp2
    tmp4 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK])
    tmp6 = tl.where(xmask, tmp4, 0)
    tmp7 = tl.sum(tmp6, 1)[:, None]
    tmp8 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK])
    tmp10 = tl.where(xmask, tmp8, 0)
    tmp11 = tl.sum(tmp10, 1)[:, None]
    tmp12 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
    tmp14 = tl.where(xmask, tmp12, 0)
    tmp15 = tl.sum(tmp14, 1)[:, None]
    tl.store(out_ptr0 + x0, tmp7, xmask)
    tl.store(out_ptr1 + x0, tmp11, xmask)
    tl.store(out_ptr2 + x0, tmp15, xmask)


@triton.jit
def triton_per_fused__to_copy_add_binary_cross_entropy_with_logits_div_gt_mean_mul_rsub_8(
    in_out_ptr0, 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, xnumel, rnumel, XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 4
    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)
    tmp5 = tl.load(in_ptr1 + r0, None)
    tmp6 = tl.load(in_ptr2 + r0, None)
    tmp15 = tl.load(in_ptr3 + r0, None)
    tmp18 = tl.load(in_ptr4 + r0, None)
    tmp19 = tl.load(in_ptr5 + r0, None)
    tmp27 = tl.load(in_ptr6 + r0, None)
    tmp30 = tl.load(in_ptr7 + r0, None)
    tmp31 = tl.load(in_ptr8 + r0, None)
    tmp39 = tl.load(in_ptr9 + r0, None)
    tmp42 = tl.load(in_ptr10 + r0, None)
    tmp43 = tl.load(in_ptr11 + r0, None)
    tmp51 = tl.load(in_ptr12 + 0)
    tmp52 = tl.broadcast_to(tmp51, [XBLOCK, 1])
    tmp57 = tl.load(in_ptr13 + 0)
    tmp58 = tl.broadcast_to(tmp57, [XBLOCK, 1])
    tmp1 = 2.0
    tmp2 = tmp0 * tmp1
    tmp3 = 1e-09
    tmp4 = tmp2 + tmp3
    tmp7 = tmp5 + tmp6
    tmp8 = tmp7 + tmp3
    tmp9 = tmp4 / tmp8
    tmp10 = 1.0
    tmp11 = tmp10 - tmp9
    tmp12 = tl.broadcast_to(tmp11, [XBLOCK, RBLOCK])
    tmp14 = tl.sum(tmp12, 1)[:, None]
    tmp16 = tmp15 * tmp1
    tmp17 = tmp16 + tmp3
    tmp20 = tmp18 + tmp19
    tmp21 = tmp20 + tmp3
    tmp22 = tmp17 / tmp21
    tmp23 = tmp10 - tmp22
    tmp24 = tl.broadcast_to(tmp23, [XBLOCK, RBLOCK])
    tmp26 = tl.sum(tmp24, 1)[:, None]
    tmp28 = tmp27 * tmp1
    tmp29 = tmp28 + tmp3
    tmp32 = tmp30 + tmp31
    tmp33 = tmp32 + tmp3
    tmp34 = tmp29 / tmp33
    tmp35 = tmp10 - tmp34
    tmp36 = tl.broadcast_to(tmp35, [XBLOCK, RBLOCK])
    tmp38 = tl.sum(tmp36, 1)[:, None]
    tmp40 = tmp39 * tmp1
    tmp41 = tmp40 + tmp3
    tmp44 = tmp42 + tmp43
    tmp45 = tmp44 + tmp3
    tmp46 = tmp41 / tmp45
    tmp47 = tmp10 - tmp46
    tmp48 = tl.broadcast_to(tmp47, [XBLOCK, RBLOCK])
    tmp50 = tl.sum(tmp48, 1)[:, None]
    tmp53 = 16.0
    tmp54 = tmp52 / tmp53
    tmp55 = 0.1
    tmp56 = tmp54 * tmp55
    tmp59 = 256.0
    tmp60 = tmp58 / tmp59
    tmp61 = tmp60 * tmp10
    tmp62 = tmp56 + tmp61
    tmp63 = 4.0
    tmp64 = tmp14 / tmp63
    tmp65 = 0.0
    tmp66 = tmp64 + tmp65
    tmp67 = tmp26 / tmp63
    tmp68 = tmp66 + tmp67
    tmp69 = tmp38 / tmp63
    tmp70 = tmp68 + tmp69
    tmp71 = tmp50 / tmp63
    tmp72 = tmp70 + tmp71
    tmp73 = 0.25
    tmp74 = tmp72 * tmp73
    tmp75 = tmp74 * tmp65
    tmp76 = tmp62 + tmp75
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp76, 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((4, 4), (4, 1), torch.float32)
        get_raw_stream(0)
        triton_per_fused_max_0[grid(16)](arg1_1, buf0, 16, 16, XBLOCK=1,
            num_warps=2, num_stages=1)
        buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_per_fused_sum_1[grid(16)](arg0_1, buf2, 16, 16, XBLOCK=1,
            num_warps=2, num_stages=1)
        buf3 = empty_strided_cuda((), (), torch.float32)
        triton_per_fused__to_copy_binary_cross_entropy_with_logits_gt_2[grid(1)
            ](buf2, buf0, buf3, 1, 16, XBLOCK=1, num_warps=2, num_stages=1)
        del buf0
        del buf2
        buf4 = empty_strided_cuda((), (), torch.float32)
        triton_per_fused_binary_cross_entropy_with_logits_3[grid(1)](arg0_1,
            arg1_1, buf4, 1, 256, num_warps=2, num_stages=1)
        buf5 = empty_strided_cuda((4,), (1,), torch.float32)
        buf6 = empty_strided_cuda((4,), (1,), torch.float32)
        buf7 = empty_strided_cuda((4,), (1,), torch.float32)
        triton_per_fused_mul_sum_4[grid(4)](arg1_1, arg0_1, buf5, buf6,
            buf7, 4, 16, XBLOCK=1, num_warps=2, num_stages=1)
        buf9 = empty_strided_cuda((4,), (1,), torch.float32)
        buf10 = empty_strided_cuda((4,), (1,), torch.float32)
        buf11 = empty_strided_cuda((4,), (1,), torch.float32)
        triton_per_fused_mul_sum_5[grid(4)](arg1_1, arg0_1, buf9, buf10,
            buf11, 4, 16, XBLOCK=1, num_warps=2, num_stages=1)
        buf13 = empty_strided_cuda((4,), (1,), torch.float32)
        buf14 = empty_strided_cuda((4,), (1,), torch.float32)
        buf15 = empty_strided_cuda((4,), (1,), torch.float32)
        triton_per_fused_mul_sum_6[grid(4)](arg1_1, arg0_1, buf13, buf14,
            buf15, 4, 16, XBLOCK=1, num_warps=2, num_stages=1)
        buf17 = empty_strided_cuda((4,), (1,), torch.float32)
        buf18 = empty_strided_cuda((4,), (1,), torch.float32)
        buf19 = empty_strided_cuda((4,), (1,), torch.float32)
        triton_per_fused_mul_sum_7[grid(4)](arg1_1, arg0_1, buf17, buf18,
            buf19, 4, 16, XBLOCK=1, num_warps=2, num_stages=1)
        del arg0_1
        del arg1_1
        buf12 = empty_strided_cuda((), (), torch.float32)
        buf21 = buf12
        del buf12
        triton_per_fused__to_copy_add_binary_cross_entropy_with_logits_div_gt_mean_mul_rsub_8[
            grid(1)](buf21, buf5, buf6, buf7, buf9, buf10, buf11, buf13,
            buf14, buf15, buf17, buf18, buf19, buf3, buf4, 1, 4, XBLOCK=1,
            num_warps=2, num_stages=1)
        del buf10
        del buf11
        del buf13
        del buf14
        del buf15
        del buf17
        del buf18
        del buf19
        del buf3
        del buf4
        del buf5
        del buf6
        del buf7
        del buf9
    return buf21,


class SoftDiceLoss(nn.Module):
    """Differentiable soft dice loss.

    Note: Sigmoid is automatically applied here!
    """

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

    def forward(self, logits, targets):
        eps = 1e-09
        num = targets.size(0)
        probs = torch.sigmoid(logits)
        m1 = probs.view(num, -1)
        m2 = targets.view(num, -1).float()
        intersection = torch.sum(m1 * m2, 1)
        union = torch.sum(m1, dim=1) + torch.sum(m2, dim=1)
        score = (2 * intersection + eps) / (union + eps)
        score = (1 - score).mean()
        return score


class MultiLabelDiceLoss(nn.Module):
    """The average dice across multiple classes.

    Note: Sigmoid is automatically applied here!
    """

    def __init__(self):
        super(MultiLabelDiceLoss, self).__init__()
        self.dice_loss = SoftDiceLoss()

    def forward(self, logits, targets):
        loss = 0
        num_classes = targets.size(1)
        for class_nr in range(num_classes):
            loss += self.dice_loss(logits[:, class_nr, :, :], targets[:,
                class_nr, :, :])
        return loss / num_classes


class ComboLossNew(nn.Module):
    """Weighted classification and segmentation loss.

    Attributes:
        weights (list):
        activation:
        bce: with logits loss
        dice_loss: soft dice loss (all classes)

    """

    def __init__(self, weights=[0.1, 0, 1], activation=None):
        """
        Args:
            weights (list): [image_cls, pixel_seg, pixel_cls]
            activation: One of ['sigmoid', None]
        """
        super(ComboLossNew, self).__init__()
        self.weights = weights
        self.activation = activation
        assert self.activation in ['sigmoid', None
            ], "`activation` must be one of ['sigmoid', None]."
        self.bce = nn.BCEWithLogitsLoss(reduce=True)
        self.dice_loss = MultiLabelDiceLoss()

    def create_fc_tensors(self, logits, targets):
        """Creates the classification tensors from the segmentation ones.
        """
        batch_size, num_classes, _, _ = targets.shape
        summed = targets.view(batch_size, num_classes, -1).sum(-1)
        targets_fc = (summed > 0).float()
        logits_fc = logits.view(batch_size, num_classes, -1)
        logits_fc = torch.max(logits_fc, -1)[0]
        return logits_fc, targets_fc

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


Measured latency_ms: 0.08892