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_clamp_min_div_linalg_vector_norm_mul_0(in_ptr0,
    in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 1024
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex // 256
    x4 = xindex % 64
    x0 = xindex % 16
    x6 = xindex % 256
    x2 = xindex // 64 % 4
    x7 = xindex
    tmp0 = tl.load(in_ptr0 + (x4 + 64 * x3), xmask, eviction_policy=
        'evict_last')
    tmp1 = tl.load(in_ptr0 + (x0 + 64 * x3), xmask, eviction_policy=
        'evict_last')
    tmp3 = tl.load(in_ptr0 + (16 + x0 + 64 * x3), xmask, eviction_policy=
        'evict_last')
    tmp6 = tl.load(in_ptr0 + (32 + x0 + 64 * x3), xmask, eviction_policy=
        'evict_last')
    tmp9 = tl.load(in_ptr0 + (48 + x0 + 64 * x3), xmask, eviction_policy=
        'evict_last')
    tmp16 = tl.load(in_ptr1 + x6, xmask, eviction_policy='evict_last')
    tmp17 = tl.load(in_ptr1 + (x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp19 = tl.load(in_ptr1 + (16 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp22 = tl.load(in_ptr1 + (32 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp25 = tl.load(in_ptr1 + (48 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp2 = tmp1 * tmp1
    tmp4 = tmp3 * tmp3
    tmp5 = tmp2 + tmp4
    tmp7 = tmp6 * tmp6
    tmp8 = tmp5 + tmp7
    tmp10 = tmp9 * tmp9
    tmp11 = tmp8 + tmp10
    tmp12 = libdevice.sqrt(tmp11)
    tmp13 = 1e-08
    tmp14 = triton_helpers.maximum(tmp12, tmp13)
    tmp15 = tmp0 / tmp14
    tmp18 = tmp17 * tmp17
    tmp20 = tmp19 * tmp19
    tmp21 = tmp18 + tmp20
    tmp23 = tmp22 * tmp22
    tmp24 = tmp21 + tmp23
    tmp26 = tmp25 * tmp25
    tmp27 = tmp24 + tmp26
    tmp28 = libdevice.sqrt(tmp27)
    tmp29 = triton_helpers.maximum(tmp28, tmp13)
    tmp30 = tmp16 / tmp29
    tmp31 = tmp15 * tmp30
    tl.store(out_ptr0 + x7, tmp31, xmask)


@triton.jit
def triton_poi_fused_sum_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 16
    x1 = xindex // 16
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 256 * x1), xmask)
    tmp1 = tl.load(in_ptr0 + (16 + x0 + 256 * x1), xmask)
    tmp3 = tl.load(in_ptr0 + (32 + x0 + 256 * x1), xmask)
    tmp5 = tl.load(in_ptr0 + (48 + x0 + 256 * x1), xmask)
    tmp9 = tl.load(in_ptr0 + (64 + x0 + 256 * x1), xmask)
    tmp10 = tl.load(in_ptr0 + (80 + x0 + 256 * x1), xmask)
    tmp12 = tl.load(in_ptr0 + (96 + x0 + 256 * x1), xmask)
    tmp14 = tl.load(in_ptr0 + (112 + x0 + 256 * x1), xmask)
    tmp18 = tl.load(in_ptr0 + (128 + x0 + 256 * x1), xmask)
    tmp19 = tl.load(in_ptr0 + (144 + x0 + 256 * x1), xmask)
    tmp21 = tl.load(in_ptr0 + (160 + x0 + 256 * x1), xmask)
    tmp23 = tl.load(in_ptr0 + (176 + x0 + 256 * x1), xmask)
    tmp27 = tl.load(in_ptr0 + (192 + x0 + 256 * x1), xmask)
    tmp28 = tl.load(in_ptr0 + (208 + x0 + 256 * x1), xmask)
    tmp30 = tl.load(in_ptr0 + (224 + x0 + 256 * x1), xmask)
    tmp32 = tl.load(in_ptr0 + (240 + x0 + 256 * x1), xmask)
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 + tmp3
    tmp6 = tmp4 + tmp5
    tmp7 = 1.0
    tmp8 = tmp6 * tmp7
    tmp11 = tmp9 + tmp10
    tmp13 = tmp11 + tmp12
    tmp15 = tmp13 + tmp14
    tmp16 = tmp15 * tmp7
    tmp17 = triton_helpers.maximum(tmp8, tmp16)
    tmp20 = tmp18 + tmp19
    tmp22 = tmp20 + tmp21
    tmp24 = tmp22 + tmp23
    tmp25 = tmp24 * tmp7
    tmp26 = triton_helpers.maximum(tmp17, tmp25)
    tmp29 = tmp27 + tmp28
    tmp31 = tmp29 + tmp30
    tmp33 = tmp31 + tmp32
    tmp34 = tmp33 * tmp7
    tmp35 = triton_helpers.maximum(tmp26, tmp34)
    tl.store(out_ptr0 + x2, tmp35, xmask)


@triton.jit
def triton_poi_fused__softmax_sum_2(in_ptr0, in_ptr1, out_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 16
    x3 = xindex // 16
    x2 = xindex // 64
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 64 * x3), xmask)
    tmp1 = tl.load(in_ptr0 + (16 + x0 + 64 * x3), xmask)
    tmp3 = tl.load(in_ptr0 + (32 + x0 + 64 * x3), xmask)
    tmp5 = tl.load(in_ptr0 + (48 + x0 + 64 * x3), xmask)
    tmp9 = tl.load(in_ptr1 + (x0 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 + tmp3
    tmp6 = tmp4 + tmp5
    tmp7 = 1.0
    tmp8 = tmp6 * tmp7
    tmp10 = tmp8 - tmp9
    tmp11 = 10.0
    tmp12 = tmp10 * tmp11
    tmp13 = tl_math.exp(tmp12)
    tl.store(out_ptr0 + x4, tmp13, xmask)


@triton.jit
def triton_per_fused__softmax_log_mean_mul_sub_xlogy_3(in_out_ptr0,
    in_out_ptr1, in_ptr0, in_ptr1, 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)
    r3 = rindex
    r0 = rindex % 16
    r2 = rindex // 64
    tmp0 = tl.load(in_ptr0 + r3, None)
    tmp1 = tl.load(in_ptr0 + (r0 + 64 * r2), None, eviction_policy='evict_last'
        )
    tmp2 = tl.load(in_ptr0 + (16 + r0 + 64 * r2), None, eviction_policy=
        'evict_last')
    tmp4 = tl.load(in_ptr0 + (32 + r0 + 64 * r2), None, eviction_policy=
        'evict_last')
    tmp6 = tl.load(in_ptr0 + (48 + r0 + 64 * r2), None, eviction_policy=
        'evict_last')
    tmp9 = tl.load(in_ptr1 + r3, None)
    tmp10 = tl.load(in_ptr1 + (r0 + 64 * r2), None, eviction_policy=
        'evict_last')
    tmp11 = tl.load(in_ptr1 + (16 + r0 + 64 * r2), None, eviction_policy=
        'evict_last')
    tmp13 = tl.load(in_ptr1 + (32 + r0 + 64 * r2), None, eviction_policy=
        'evict_last')
    tmp15 = tl.load(in_ptr1 + (48 + r0 + 64 * r2), None, eviction_policy=
        'evict_last')
    tmp3 = tmp1 + tmp2
    tmp5 = tmp3 + tmp4
    tmp7 = tmp5 + tmp6
    tmp8 = tmp0 / tmp7
    tmp12 = tmp10 + tmp11
    tmp14 = tmp12 + tmp13
    tmp16 = tmp14 + tmp15
    tmp17 = tmp9 / tmp16
    tmp18 = libdevice.isnan(tmp8).to(tl.int1)
    tmp19 = 0.0
    tmp20 = tmp8 == tmp19
    tmp21 = tl_math.log(tmp8)
    tmp22 = tmp8 * tmp21
    tmp23 = tl.where(tmp20, tmp19, tmp22)
    tmp24 = float('nan')
    tmp25 = tl.where(tmp18, tmp24, tmp23)
    tmp26 = tl_math.log(tmp17)
    tmp27 = tmp8 * tmp26
    tmp28 = tmp25 - tmp27
    tmp29 = tl.broadcast_to(tmp28, [RBLOCK])
    tmp31 = triton_helpers.promote_to_tensor(tl.sum(tmp29, 0))
    tmp32 = libdevice.isnan(tmp17).to(tl.int1)
    tmp33 = tmp17 == tmp19
    tmp34 = tmp17 * tmp26
    tmp35 = tl.where(tmp33, tmp19, tmp34)
    tmp36 = tl.where(tmp32, tmp24, tmp35)
    tmp37 = tmp17 * tmp21
    tmp38 = tmp36 - tmp37
    tmp39 = tl.broadcast_to(tmp38, [RBLOCK])
    tmp41 = triton_helpers.promote_to_tensor(tl.sum(tmp39, 0))
    tmp42 = 256.0
    tmp43 = tmp31 / tmp42
    tmp44 = tmp41 / tmp42
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp43, None)
    tl.debug_barrier()
    tl.store(in_out_ptr1 + tl.full([1], 0, tl.int32), tmp44, 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)
        buf0 = empty_strided_cuda((4, 4, 4, 4, 4), (256, 64, 16, 4, 1),
            torch.float32)
        get_raw_stream(0)
        triton_poi_fused_clamp_min_div_linalg_vector_norm_mul_0[grid(1024)](
            arg2_1, arg3_1, buf0, 1024, XBLOCK=128, num_warps=4, num_stages=1)
        del arg2_1
        del arg3_1
        buf1 = empty_strided_cuda((4, 1, 4, 4), (16, 64, 4, 1), torch.float32)
        triton_poi_fused_sum_1[grid(64)](buf0, buf1, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused__softmax_sum_2[grid(256)](buf0, buf1, buf2, 256,
            XBLOCK=256, num_warps=4, num_stages=1)
        buf4 = buf0
        del buf0
        triton_poi_fused_clamp_min_div_linalg_vector_norm_mul_0[grid(1024)](
            arg0_1, arg1_1, buf4, 1024, XBLOCK=128, num_warps=4, num_stages=1)
        del arg0_1
        del arg1_1
        buf5 = buf1
        del buf1
        triton_poi_fused_sum_1[grid(64)](buf4, buf5, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        buf6 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused__softmax_sum_2[grid(256)](buf4, buf5, buf6, 256,
            XBLOCK=256, num_warps=4, num_stages=1)
        del buf4
        del buf5
        buf8 = empty_strided_cuda((), (), torch.float32)
        buf9 = empty_strided_cuda((), (), torch.float32)
        buf10 = buf8
        del buf8
        buf11 = buf9
        del buf9
        triton_per_fused__softmax_log_mean_mul_sub_xlogy_3[grid(1)](buf10,
            buf11, buf2, buf6, 1, 256, num_warps=2, num_stages=1)
        del buf2
        del buf6
    return buf10, buf11


class KLLossNew(nn.Module):
    """
    KL-Divergence symmetric loss between two distributions
    Used in here for knowledge distillation
    """

    def __init__(self):
        super(KLLossNew, self).__init__()
        self.similarity_f = nn.CosineSimilarity(dim=2)

    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]


Measured latency_ms: 0.017142