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


@triton.jit
def triton_poi_fused__softmax_0(in_ptr0, out_ptr0, out_ptr1, out_ptr2,
    out_ptr3, xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex
    x0 = xindex % 16
    x2 = xindex // 64
    tmp0 = tl.load(in_ptr0 + x3, xmask)
    tmp1 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp2 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp4 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp6 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp3 = triton_helpers.maximum(tmp1, tmp2)
    tmp5 = triton_helpers.maximum(tmp3, tmp4)
    tmp7 = triton_helpers.maximum(tmp5, tmp6)
    tmp8 = tmp0 - tmp7
    tmp9 = tl_math.exp(tmp8)
    tl.store(out_ptr0 + x3, tmp9, xmask)
    tl.store(out_ptr1 + x3, tmp9, xmask)
    tl.store(out_ptr2 + x3, tmp9, xmask)
    tl.store(out_ptr3 + x3, tmp9, xmask)


@triton.jit
def triton_poi_fused__softmax_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
    ):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex
    x0 = xindex % 16
    x2 = xindex // 64
    tmp0 = tl.load(in_ptr0 + x3, xmask)
    tmp1 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp2 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp4 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp6 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp3 = tmp1 + tmp2
    tmp5 = tmp3 + tmp4
    tmp7 = tmp5 + tmp6
    tmp8 = tmp0 / tmp7
    tl.store(out_ptr0 + x3, tmp8, xmask)


@triton.jit
def triton_per_fused_add_div_mean_mul_rsub_sum_2(in_out_ptr0, in_ptr0,
    in_ptr1, in_ptr2, in_ptr3, in_ptr4, 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
    r1 = rindex % 16
    r2 = rindex // 16
    r3 = rindex % 4
    r4 = rindex // 4
    tmp0 = tl.load(in_ptr0 + r0, None)
    tmp1 = tl.load(in_ptr1 + r0, None)
    tmp3 = tl.load(in_ptr0 + (64 + r0), None)
    tmp4 = tl.load(in_ptr1 + (64 + r0), None)
    tmp7 = tl.load(in_ptr0 + (128 + r0), None)
    tmp8 = tl.load(in_ptr1 + (128 + r0), None)
    tmp11 = tl.load(in_ptr0 + (192 + r0), None)
    tmp12 = tl.load(in_ptr1 + (192 + r0), None)
    tmp34 = tl.load(in_ptr2 + (r1 + 64 * r2), None)
    tmp35 = tl.load(in_ptr1 + (r1 + 64 * r2), None)
    tmp37 = tl.load(in_ptr2 + (16 + r1 + 64 * r2), None)
    tmp38 = tl.load(in_ptr1 + (16 + r1 + 64 * r2), None)
    tmp41 = tl.load(in_ptr2 + (32 + r1 + 64 * r2), None)
    tmp42 = tl.load(in_ptr1 + (32 + r1 + 64 * r2), None)
    tmp45 = tl.load(in_ptr2 + (48 + r1 + 64 * r2), None)
    tmp46 = tl.load(in_ptr1 + (48 + r1 + 64 * r2), None)
    tmp65 = tl.load(in_ptr3 + (r3 + 16 * r4), None)
    tmp66 = tl.load(in_ptr1 + (r3 + 16 * r4), None)
    tmp68 = tl.load(in_ptr3 + (4 + r3 + 16 * r4), None)
    tmp69 = tl.load(in_ptr1 + (4 + r3 + 16 * r4), None)
    tmp72 = tl.load(in_ptr3 + (8 + r3 + 16 * r4), None)
    tmp73 = tl.load(in_ptr1 + (8 + r3 + 16 * r4), None)
    tmp76 = tl.load(in_ptr3 + (12 + r3 + 16 * r4), None)
    tmp77 = tl.load(in_ptr1 + (12 + r3 + 16 * r4), None)
    tmp96 = tl.load(in_ptr4 + 4 * r0, None, eviction_policy='evict_last')
    tmp97 = tl.load(in_ptr1 + 4 * r0, None, eviction_policy='evict_last')
    tmp99 = tl.load(in_ptr4 + (1 + 4 * r0), None, eviction_policy='evict_last')
    tmp100 = tl.load(in_ptr1 + (1 + 4 * r0), None, eviction_policy='evict_last'
        )
    tmp103 = tl.load(in_ptr4 + (2 + 4 * r0), None, eviction_policy='evict_last'
        )
    tmp104 = tl.load(in_ptr1 + (2 + 4 * r0), None, eviction_policy='evict_last'
        )
    tmp107 = tl.load(in_ptr4 + (3 + 4 * r0), None, eviction_policy='evict_last'
        )
    tmp108 = tl.load(in_ptr1 + (3 + 4 * r0), None, eviction_policy='evict_last'
        )
    tmp2 = tmp0 * tmp1
    tmp5 = tmp3 * tmp4
    tmp6 = tmp2 + tmp5
    tmp9 = tmp7 * tmp8
    tmp10 = tmp6 + tmp9
    tmp13 = tmp11 * tmp12
    tmp14 = tmp10 + tmp13
    tmp15 = 2.0
    tmp16 = tmp14 * tmp15
    tmp17 = 1.0
    tmp18 = tmp16 + tmp17
    tmp19 = tmp0 + tmp1
    tmp20 = tmp3 + tmp4
    tmp21 = tmp19 + tmp20
    tmp22 = tmp7 + tmp8
    tmp23 = tmp21 + tmp22
    tmp24 = tmp11 + tmp12
    tmp25 = tmp23 + tmp24
    tmp26 = tmp25 + tmp17
    tmp27 = 1e-06
    tmp28 = tmp26 + tmp27
    tmp29 = tmp18 / tmp28
    tmp30 = tmp17 - tmp29
    tmp31 = tl.broadcast_to(tmp30, [XBLOCK, RBLOCK])
    tmp33 = tl.sum(tmp31, 1)[:, None]
    tmp36 = tmp34 * tmp35
    tmp39 = tmp37 * tmp38
    tmp40 = tmp36 + tmp39
    tmp43 = tmp41 * tmp42
    tmp44 = tmp40 + tmp43
    tmp47 = tmp45 * tmp46
    tmp48 = tmp44 + tmp47
    tmp49 = tmp48 * tmp15
    tmp50 = tmp49 + tmp17
    tmp51 = tmp34 + tmp35
    tmp52 = tmp37 + tmp38
    tmp53 = tmp51 + tmp52
    tmp54 = tmp41 + tmp42
    tmp55 = tmp53 + tmp54
    tmp56 = tmp45 + tmp46
    tmp57 = tmp55 + tmp56
    tmp58 = tmp57 + tmp17
    tmp59 = tmp58 + tmp27
    tmp60 = tmp50 / tmp59
    tmp61 = tmp17 - tmp60
    tmp62 = tl.broadcast_to(tmp61, [XBLOCK, RBLOCK])
    tmp64 = tl.sum(tmp62, 1)[:, None]
    tmp67 = tmp65 * tmp66
    tmp70 = tmp68 * tmp69
    tmp71 = tmp67 + tmp70
    tmp74 = tmp72 * tmp73
    tmp75 = tmp71 + tmp74
    tmp78 = tmp76 * tmp77
    tmp79 = tmp75 + tmp78
    tmp80 = tmp79 * tmp15
    tmp81 = tmp80 + tmp17
    tmp82 = tmp65 + tmp66
    tmp83 = tmp68 + tmp69
    tmp84 = tmp82 + tmp83
    tmp85 = tmp72 + tmp73
    tmp86 = tmp84 + tmp85
    tmp87 = tmp76 + tmp77
    tmp88 = tmp86 + tmp87
    tmp89 = tmp88 + tmp17
    tmp90 = tmp89 + tmp27
    tmp91 = tmp81 / tmp90
    tmp92 = tmp17 - tmp91
    tmp93 = tl.broadcast_to(tmp92, [XBLOCK, RBLOCK])
    tmp95 = tl.sum(tmp93, 1)[:, None]
    tmp98 = tmp96 * tmp97
    tmp101 = tmp99 * tmp100
    tmp102 = tmp98 + tmp101
    tmp105 = tmp103 * tmp104
    tmp106 = tmp102 + tmp105
    tmp109 = tmp107 * tmp108
    tmp110 = tmp106 + tmp109
    tmp111 = tmp110 * tmp15
    tmp112 = tmp111 + tmp17
    tmp113 = tmp96 + tmp97
    tmp114 = tmp99 + tmp100
    tmp115 = tmp113 + tmp114
    tmp116 = tmp103 + tmp104
    tmp117 = tmp115 + tmp116
    tmp118 = tmp107 + tmp108
    tmp119 = tmp117 + tmp118
    tmp120 = tmp119 + tmp17
    tmp121 = tmp120 + tmp27
    tmp122 = tmp112 / tmp121
    tmp123 = tmp17 - tmp122
    tmp124 = tl.broadcast_to(tmp123, [XBLOCK, RBLOCK])
    tmp126 = tl.sum(tmp124, 1)[:, None]
    tmp127 = 64.0
    tmp128 = tmp33 / tmp127
    tmp129 = 0.2
    tmp130 = tmp128 * tmp129
    tmp131 = tmp64 / tmp127
    tmp132 = 1.1
    tmp133 = tmp131 * tmp132
    tmp134 = tmp130 + tmp133
    tmp135 = tmp95 / tmp127
    tmp136 = 3.0
    tmp137 = tmp135 * tmp136
    tmp138 = tmp134 + tmp137
    tmp139 = tmp126 / tmp127
    tmp140 = tmp139 * tmp136
    tmp141 = tmp138 + tmp140
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp141, 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, 4), (64, 16, 4, 1), torch.float32)
        buf4 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        buf8 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        buf12 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused__softmax_0[grid(256)](arg0_1, buf0, buf4, buf8,
            buf12, 256, XBLOCK=256, num_warps=4, num_stages=1)
        del arg0_1
        buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused__softmax_1[grid(256)](buf0, buf1, 256, XBLOCK=128,
            num_warps=4, num_stages=1)
        buf13 = buf0
        del buf0
        triton_poi_fused__softmax_1[grid(256)](buf12, buf13, 256, XBLOCK=
            128, num_warps=4, num_stages=1)
        buf5 = buf12
        del buf12
        triton_poi_fused__softmax_1[grid(256)](buf4, buf5, 256, XBLOCK=128,
            num_warps=4, num_stages=1)
        buf9 = buf4
        del buf4
        triton_poi_fused__softmax_1[grid(256)](buf8, buf9, 256, XBLOCK=128,
            num_warps=4, num_stages=1)
        del buf8
        buf11 = empty_strided_cuda((), (), torch.float32)
        buf16 = buf11
        del buf11
        triton_per_fused_add_div_mean_mul_rsub_sum_2[grid(1)](buf16, buf1,
            arg1_1, buf5, buf9, buf13, 1, 64, XBLOCK=1, num_warps=2,
            num_stages=1)
        del arg1_1
        del buf1
        del buf13
        del buf5
        del buf9
    return buf16,


class DiceLoss(nn.Module):

    def __init__(self, dims=(1, 2, 3)) ->None:
        super(DiceLoss, self).__init__()
        self.eps: 'float' = 1e-06
        self.dims = dims

    def forward(self, input: 'torch.Tensor', target: 'torch.Tensor',
        weights=None) ->torch.Tensor:
        if not torch.is_tensor(input):
            raise TypeError('Input type is not a torch.Tensor. Got {}'.
                format(type(input)))
        if not len(input.shape) == 4:
            raise ValueError('Invalid input shape, we expect BxNxHxW. Got: {}'
                .format(input.shape))
        if not input.shape[-2:] == target.shape[-2:]:
            raise ValueError(
                'input and target shapes must be the same. Got: {}'.format(
                input.shape))
        if not input.device == target.device:
            raise ValueError(
                'input and target must be in the same device. Got: {}'.
                format(input.device))
        smooth = 1
        input_soft = F.softmax(input, dim=1)
        intersection = torch.sum(input_soft * target, self.dims)
        cardinality = torch.sum(input_soft + target, self.dims)
        dice_score = (2.0 * intersection + smooth) / (cardinality + smooth +
            self.eps)
        return torch.mean(1.0 - dice_score)


class MDiceLossNew(nn.Module):

    def __init__(self) ->None:
        super(MDiceLossNew, self).__init__()
        self.dice_loss_bg = DiceLoss(dims=0)
        self.dice_loss_fg = DiceLoss(dims=1)
        self.dice_loss_th = DiceLoss(dims=2)
        self.dice_loss_gp = DiceLoss(dims=3)

    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.041236