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_poi_fused__softmax_0(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
    x2 = xindex
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp3 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp1 = 1.0
    tmp2 = tmp0 * tmp1
    tmp4 = tmp3 * tmp1
    tmp6 = tmp5 * tmp1
    tmp7 = triton_helpers.maximum(tmp4, tmp6)
    tmp9 = tmp8 * tmp1
    tmp10 = triton_helpers.maximum(tmp7, tmp9)
    tmp12 = tmp11 * tmp1
    tmp13 = triton_helpers.maximum(tmp10, tmp12)
    tmp14 = tmp2 - tmp13
    tmp15 = 0.25
    tmp16 = tmp14 * tmp15
    tmp17 = tl_math.exp(tmp16)
    tl.store(out_ptr0 + x2, tmp17, 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
    x2 = xindex
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp3 = tmp1 + tmp2
    tmp5 = tmp3 + tmp4
    tmp7 = tmp5 + tmp6
    tmp8 = tmp0 / tmp7
    tl.store(out_ptr0 + x2, tmp8, xmask)


@triton.jit
def triton_poi_fused_2(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
    x2 = xindex
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp3 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp1 = 1.0
    tmp2 = tmp0 * tmp1
    tmp4 = tmp3 * tmp1
    tmp6 = tmp5 * tmp1
    tmp7 = triton_helpers.maximum(tmp4, tmp6)
    tmp9 = tmp8 * tmp1
    tmp10 = triton_helpers.maximum(tmp7, tmp9)
    tmp12 = tmp11 * tmp1
    tmp13 = triton_helpers.maximum(tmp10, tmp12)
    tmp14 = tmp2 - tmp13
    tmp15 = 0.25
    tmp16 = tmp14 * tmp15
    tl.store(out_ptr0 + x2, tmp16, xmask)


@triton.jit
def triton_per_fused__log_softmax_mean_mul_sub_sum_xlogy_3(in_out_ptr0,
    in_ptr0, in_ptr1, 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)
    r2 = rindex // 16
    r4 = rindex % 16
    r1 = rindex // 4 % 4
    tmp0 = tl.load(in_ptr0 + (r4 + 64 * r2), None)
    tmp9 = tl.load(in_ptr1 + (r4 + 64 * r2), None)
    tmp10 = tl.load(in_ptr1 + (4 * r1 + 64 * r2), None, eviction_policy=
        'evict_last')
    tmp12 = tl.load(in_ptr1 + (1 + 4 * r1 + 64 * r2), None, eviction_policy
        ='evict_last')
    tmp15 = tl.load(in_ptr1 + (2 + 4 * r1 + 64 * r2), None, eviction_policy
        ='evict_last')
    tmp18 = tl.load(in_ptr1 + (3 + 4 * r1 + 64 * r2), None, eviction_policy
        ='evict_last')
    tmp25 = tl.load(in_ptr0 + (16 + r4 + 64 * r2), None)
    tmp32 = tl.load(in_ptr1 + (16 + r4 + 64 * r2), None)
    tmp33 = tl.load(in_ptr1 + (16 + 4 * r1 + 64 * r2), None,
        eviction_policy='evict_last')
    tmp35 = tl.load(in_ptr1 + (17 + 4 * r1 + 64 * r2), None,
        eviction_policy='evict_last')
    tmp38 = tl.load(in_ptr1 + (18 + 4 * r1 + 64 * r2), None,
        eviction_policy='evict_last')
    tmp41 = tl.load(in_ptr1 + (19 + 4 * r1 + 64 * r2), None,
        eviction_policy='evict_last')
    tmp49 = tl.load(in_ptr0 + (32 + r4 + 64 * r2), None)
    tmp56 = tl.load(in_ptr1 + (32 + r4 + 64 * r2), None)
    tmp57 = tl.load(in_ptr1 + (32 + 4 * r1 + 64 * r2), None,
        eviction_policy='evict_last')
    tmp59 = tl.load(in_ptr1 + (33 + 4 * r1 + 64 * r2), None,
        eviction_policy='evict_last')
    tmp62 = tl.load(in_ptr1 + (34 + 4 * r1 + 64 * r2), None,
        eviction_policy='evict_last')
    tmp65 = tl.load(in_ptr1 + (35 + 4 * r1 + 64 * r2), None,
        eviction_policy='evict_last')
    tmp73 = tl.load(in_ptr0 + (48 + r4 + 64 * r2), None)
    tmp80 = tl.load(in_ptr1 + (48 + r4 + 64 * r2), None)
    tmp81 = tl.load(in_ptr1 + (48 + 4 * r1 + 64 * r2), None,
        eviction_policy='evict_last')
    tmp83 = tl.load(in_ptr1 + (49 + 4 * r1 + 64 * r2), None,
        eviction_policy='evict_last')
    tmp86 = tl.load(in_ptr1 + (50 + 4 * r1 + 64 * r2), None,
        eviction_policy='evict_last')
    tmp89 = tl.load(in_ptr1 + (51 + 4 * r1 + 64 * r2), None,
        eviction_policy='evict_last')
    tmp1 = libdevice.isnan(tmp0).to(tl.int1)
    tmp2 = 0.0
    tmp3 = tmp0 == tmp2
    tmp4 = tl_math.log(tmp0)
    tmp5 = tmp0 * tmp4
    tmp6 = tl.where(tmp3, tmp2, tmp5)
    tmp7 = float('nan')
    tmp8 = tl.where(tmp1, tmp7, tmp6)
    tmp11 = tl_math.exp(tmp10)
    tmp13 = tl_math.exp(tmp12)
    tmp14 = tmp11 + tmp13
    tmp16 = tl_math.exp(tmp15)
    tmp17 = tmp14 + tmp16
    tmp19 = tl_math.exp(tmp18)
    tmp20 = tmp17 + tmp19
    tmp21 = tl_math.log(tmp20)
    tmp22 = tmp9 - tmp21
    tmp23 = tmp0 * tmp22
    tmp24 = tmp8 - tmp23
    tmp26 = libdevice.isnan(tmp25).to(tl.int1)
    tmp27 = tmp25 == tmp2
    tmp28 = tl_math.log(tmp25)
    tmp29 = tmp25 * tmp28
    tmp30 = tl.where(tmp27, tmp2, tmp29)
    tmp31 = tl.where(tmp26, tmp7, tmp30)
    tmp34 = tl_math.exp(tmp33)
    tmp36 = tl_math.exp(tmp35)
    tmp37 = tmp34 + tmp36
    tmp39 = tl_math.exp(tmp38)
    tmp40 = tmp37 + tmp39
    tmp42 = tl_math.exp(tmp41)
    tmp43 = tmp40 + tmp42
    tmp44 = tl_math.log(tmp43)
    tmp45 = tmp32 - tmp44
    tmp46 = tmp25 * tmp45
    tmp47 = tmp31 - tmp46
    tmp48 = tmp24 + tmp47
    tmp50 = libdevice.isnan(tmp49).to(tl.int1)
    tmp51 = tmp49 == tmp2
    tmp52 = tl_math.log(tmp49)
    tmp53 = tmp49 * tmp52
    tmp54 = tl.where(tmp51, tmp2, tmp53)
    tmp55 = tl.where(tmp50, tmp7, tmp54)
    tmp58 = tl_math.exp(tmp57)
    tmp60 = tl_math.exp(tmp59)
    tmp61 = tmp58 + tmp60
    tmp63 = tl_math.exp(tmp62)
    tmp64 = tmp61 + tmp63
    tmp66 = tl_math.exp(tmp65)
    tmp67 = tmp64 + tmp66
    tmp68 = tl_math.log(tmp67)
    tmp69 = tmp56 - tmp68
    tmp70 = tmp49 * tmp69
    tmp71 = tmp55 - tmp70
    tmp72 = tmp48 + tmp71
    tmp74 = libdevice.isnan(tmp73).to(tl.int1)
    tmp75 = tmp73 == tmp2
    tmp76 = tl_math.log(tmp73)
    tmp77 = tmp73 * tmp76
    tmp78 = tl.where(tmp75, tmp2, tmp77)
    tmp79 = tl.where(tmp74, tmp7, tmp78)
    tmp82 = tl_math.exp(tmp81)
    tmp84 = tl_math.exp(tmp83)
    tmp85 = tmp82 + tmp84
    tmp87 = tl_math.exp(tmp86)
    tmp88 = tmp85 + tmp87
    tmp90 = tl_math.exp(tmp89)
    tmp91 = tmp88 + tmp90
    tmp92 = tl_math.log(tmp91)
    tmp93 = tmp80 - tmp92
    tmp94 = tmp73 * tmp93
    tmp95 = tmp79 - tmp94
    tmp96 = tmp72 + tmp95
    tmp97 = tl.broadcast_to(tmp96, [XBLOCK, RBLOCK])
    tmp99 = tl.sum(tmp97, 1)[:, None]
    tmp100 = 64.0
    tmp101 = tmp99 / tmp100
    tmp102 = 16.0
    tmp103 = tmp101 * tmp102
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp103, 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)
        get_raw_stream(0)
        triton_poi_fused__softmax_0[grid(256)](arg1_1, buf0, 256, XBLOCK=
            256, num_warps=4, num_stages=1)
        del arg1_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=256,
            num_warps=4, num_stages=1)
        buf2 = buf0
        del buf0
        triton_poi_fused_2[grid(256)](arg0_1, buf2, 256, XBLOCK=256,
            num_warps=4, num_stages=1)
        del arg0_1
        buf4 = empty_strided_cuda((), (), torch.float32)
        buf5 = buf4
        del buf4
        triton_per_fused__log_softmax_mean_mul_sub_sum_xlogy_3[grid(1)](buf5,
            buf1, buf2, 1, 64, XBLOCK=1, num_warps=2, num_stages=1)
        del buf1
        del buf2
    return buf5,


class KDLossNew(nn.Module):

    def __init__(self, temp: 'float', reduction: 'str'):
        super(KDLossNew, self).__init__()
        self.temp = temp
        self.reduction = reduction
        self.kl_loss = nn.KLDivLoss(reduction=reduction)

    def __call__(self, *args, **kwargs):
        return super(KDLossNew, self).__call__(*args, **kwargs)

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