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


@triton.jit
def triton_poi_fused__softmax_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
    ):
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    tl.full([XBLOCK], True, tl.int1)
    x3 = xindex
    x0 = xindex % 16
    x2 = xindex // 64
    tmp0 = tl.load(in_ptr0 + x3, None)
    tmp1 = tl.load(in_ptr0 + (x0 + 64 * x2), None, eviction_policy='evict_last'
        )
    tmp2 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), None, eviction_policy=
        'evict_last')
    tmp4 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), None, eviction_policy=
        'evict_last')
    tmp6 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), None, 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, None)


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


@triton.jit
def triton_poi_fused_index_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    tl.full([XBLOCK], True, tl.int1)
    x1 = xindex // 4
    x0 = xindex % 4
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last')
    tmp1 = tl.full([XBLOCK], 4, tl.int32)
    tmp2 = tmp0 + tmp1
    tmp3 = tmp0 < 0
    tmp4 = tl.where(tmp3, tmp2, tmp0)
    tl.device_assert((0 <= tmp4) & (tmp4 < 4),
        'index out of bounds: 0 <= tmp4 < 4')
    tmp6 = tmp4
    tmp7 = tl.full([1], 3, tl.int32)
    tmp8 = tmp6 == tmp7
    tmp9 = x0
    tmp10 = tmp9 == tmp7
    tmp11 = tl.full([1], 2, tl.int32)
    tmp12 = tmp7 == tmp11
    tmp13 = tmp9 == tmp11
    tmp14 = tl.full([1], 1, tl.int32)
    tmp15 = tmp11 == tmp14
    tmp16 = tmp9 == tmp14
    tmp17 = tl.full([1], 0, tl.int32)
    tmp18 = tmp14 == tmp17
    tmp19 = tmp9 == tmp17
    tmp20 = tl.full([1], 1, tl.int64)
    tmp21 = tl.full([1], 0, tl.int64)
    tmp22 = tl.where(tmp19, tmp20, tmp21)
    tmp23 = tl.where(tmp18, tmp22, tmp21)
    tmp24 = tl.where(tmp16, tmp20, tmp23)
    tmp25 = tmp11 == tmp17
    tmp26 = tl.where(tmp25, tmp22, tmp21)
    tmp27 = tl.where(tmp15, tmp24, tmp26)
    tmp28 = tl.where(tmp13, tmp20, tmp27)
    tmp29 = tmp7 == tmp14
    tmp30 = tmp7 == tmp17
    tmp31 = tl.where(tmp30, tmp22, tmp21)
    tmp32 = tl.where(tmp29, tmp24, tmp31)
    tmp33 = tl.where(tmp12, tmp28, tmp32)
    tmp34 = tl.where(tmp10, tmp20, tmp33)
    tmp35 = tmp6 == tmp11
    tmp36 = tmp6 == tmp14
    tmp37 = tmp6 == tmp17
    tmp38 = tl.where(tmp37, tmp22, tmp21)
    tmp39 = tl.where(tmp36, tmp24, tmp38)
    tmp40 = tl.where(tmp35, tmp28, tmp39)
    tmp41 = tl.where(tmp8, tmp34, tmp40)
    tl.store(out_ptr0 + x2, tmp41, None)


@triton.jit
def triton_per_fused_add_clone_div_lift_fresh_mul_rsub_sum_view_3(in_out_ptr0,
    in_ptr0, in_ptr1, xnumel, rnumel):
    XBLOCK: tl.constexpr = 1
    RBLOCK: tl.constexpr = 1024
    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 + (64 * (r0 // 16) + r0 % 16), None)
    tmp1 = tl.load(in_ptr1 + 4 * r0, None, eviction_policy='evict_last')
    tmp13 = tl.load(in_ptr0 + (16 + 64 * (r0 // 16) + r0 % 16), None)
    tmp14 = tl.load(in_ptr1 + (1 + 4 * r0), None, eviction_policy='evict_last')
    tmp26 = tl.load(in_ptr0 + (48 + 64 * (r0 // 16) + r0 % 16), None)
    tmp27 = tl.load(in_ptr1 + (3 + 4 * r0), None, eviction_policy='evict_last')
    tmp39 = tl.load(in_ptr0 + (32 + 64 * (r0 // 16) + r0 % 16), None)
    tmp40 = tl.load(in_ptr1 + (2 + 4 * r0), None, eviction_policy='evict_last')
    tmp2 = tmp1.to(tl.float32)
    tmp3 = tmp0 * tmp2
    tmp4 = tl.broadcast_to(tmp3, [RBLOCK])
    tmp6 = triton_helpers.promote_to_tensor(tl.sum(tmp4, 0))
    tmp7 = tl.broadcast_to(tmp2, [RBLOCK])
    tmp9 = triton_helpers.promote_to_tensor(tl.sum(tmp7, 0))
    tmp10 = tl.broadcast_to(tmp0, [RBLOCK])
    tmp12 = triton_helpers.promote_to_tensor(tl.sum(tmp10, 0))
    tmp15 = tmp14.to(tl.float32)
    tmp16 = tmp13 * tmp15
    tmp17 = tl.broadcast_to(tmp16, [RBLOCK])
    tmp19 = triton_helpers.promote_to_tensor(tl.sum(tmp17, 0))
    tmp20 = tl.broadcast_to(tmp15, [RBLOCK])
    tmp22 = triton_helpers.promote_to_tensor(tl.sum(tmp20, 0))
    tmp23 = tl.broadcast_to(tmp13, [RBLOCK])
    tmp25 = triton_helpers.promote_to_tensor(tl.sum(tmp23, 0))
    tmp28 = tmp27.to(tl.float32)
    tmp29 = tmp26 * tmp28
    tmp30 = tl.broadcast_to(tmp29, [RBLOCK])
    tmp32 = triton_helpers.promote_to_tensor(tl.sum(tmp30, 0))
    tmp33 = tl.broadcast_to(tmp28, [RBLOCK])
    tmp35 = triton_helpers.promote_to_tensor(tl.sum(tmp33, 0))
    tmp36 = tl.broadcast_to(tmp26, [RBLOCK])
    tmp38 = triton_helpers.promote_to_tensor(tl.sum(tmp36, 0))
    tmp41 = tmp40.to(tl.float32)
    tmp42 = tmp39 * tmp41
    tmp43 = tl.broadcast_to(tmp42, [RBLOCK])
    tmp45 = triton_helpers.promote_to_tensor(tl.sum(tmp43, 0))
    tmp46 = tl.broadcast_to(tmp41, [RBLOCK])
    tmp48 = triton_helpers.promote_to_tensor(tl.sum(tmp46, 0))
    tmp49 = tl.broadcast_to(tmp39, [RBLOCK])
    tmp51 = triton_helpers.promote_to_tensor(tl.sum(tmp49, 0))
    tmp52 = 2.0
    tmp53 = tmp6 * tmp52
    tmp54 = 1.0
    tmp55 = tmp53 + tmp54
    tmp56 = tmp12 + tmp9
    tmp57 = tmp56 + tmp54
    tmp58 = tmp55 / tmp57
    tmp59 = tmp54 - tmp58
    tmp60 = 0.25
    tmp61 = tmp60 * tmp59
    tmp62 = 0.0
    tmp63 = tmp61 + tmp62
    tmp64 = tmp19 * tmp52
    tmp65 = tmp64 + tmp54
    tmp66 = tmp25 + tmp22
    tmp67 = tmp66 + tmp54
    tmp68 = tmp65 / tmp67
    tmp69 = tmp54 - tmp68
    tmp70 = tmp60 * tmp69
    tmp71 = tmp63 + tmp70
    tmp72 = tmp45 * tmp52
    tmp73 = tmp72 + tmp54
    tmp74 = tmp51 + tmp48
    tmp75 = tmp74 + tmp54
    tmp76 = tmp73 / tmp75
    tmp77 = tmp54 - tmp76
    tmp78 = tmp60 * tmp77
    tmp79 = tmp71 + tmp78
    tmp80 = tmp32 * tmp52
    tmp81 = tmp80 + tmp54
    tmp82 = tmp38 + tmp35
    tmp83 = tmp82 + tmp54
    tmp84 = tmp81 / tmp83
    tmp85 = tmp54 - tmp84
    tmp86 = tmp60 * tmp85
    tmp87 = tmp79 + tmp86
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp87, None)


def call(args):
    arg0_1, arg1_1 = args
    args.clear()
    assert_size_stride(arg0_1, (64, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg1_1, (1024,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((64, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused__softmax_0[grid(4096)](arg0_1, buf0, 4096, XBLOCK=
            256, num_warps=4, num_stages=1)
        del arg0_1
        buf1 = empty_strided_cuda((64, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused__softmax_1[grid(4096)](buf0, buf1, 4096, XBLOCK=
            256, num_warps=4, num_stages=1)
        del buf0
        buf3 = empty_strided_cuda((1024, 4), (4, 1), torch.int64)
        triton_poi_fused_index_2[grid(4096)](arg1_1, buf3, 4096, XBLOCK=128,
            num_warps=4, num_stages=1)
        del arg1_1
        buf10 = empty_strided_cuda((), (), torch.float32)
        buf13 = buf10
        del buf10
        buf17 = buf13
        del buf13
        triton_per_fused_add_clone_div_lift_fresh_mul_rsub_sum_view_3[grid(1)](
            buf17, buf1, buf3, 1, 1024, num_warps=8, num_stages=1)
        del buf1
        del buf3
    return buf17,


class DiceLossNew(nn.Module):

    def __init__(self, size_average=True, ignore_index=-100, reduce=True):
        super(DiceLossNew, self).__init__()
        self.size_average = size_average
        self.ignore_index = ignore_index
        self.reduce = reduce
        self.softMax = nn.Softmax(dim=1)

    def dice_loss(self, input, target, weight=None):
        smooth = 1.0
        loss = 0.0
        n_classes = input.size(1)
        for c in range(n_classes):
            iflat = input[:, c].contiguous().view(-1)
            tflat = target[:, c].contiguous().view(-1)
            intersection = (iflat * tflat).sum()
            if weight is not None:
                w = weight[c]
            else:
                w = 1 / n_classes
            w = torch.tensor(w)
            w = w.float()
            loss += w * (1 - (2.0 * intersection + smooth) / (iflat.sum() +
                tflat.sum() + smooth))
        return loss

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