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
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_div_linalg_vector_norm_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
    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')
    tmp3 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp6 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp9 = tl.load(in_ptr0 + (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 = tmp0 / tmp12
    tl.store(out_ptr0 + x3, tmp13, xmask)


@triton.jit
def triton_poi_fused_add_pow_sqrt_sub_sum_1(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
    x2 = xindex // 64
    x1 = xindex // 16 % 4
    x3 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp1 = tl.load(in_ptr0 + (x0 + 64 * x1), xmask, eviction_policy=
        'evict_last')
    tmp4 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp5 = tl.load(in_ptr0 + (16 + x0 + 64 * x1), xmask, eviction_policy=
        'evict_last')
    tmp9 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp10 = tl.load(in_ptr0 + (32 + x0 + 64 * x1), xmask, eviction_policy=
        'evict_last')
    tmp14 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp15 = tl.load(in_ptr0 + (48 + x0 + 64 * x1), xmask, eviction_policy=
        'evict_last')
    tmp22 = tl.load(in_ptr1 + (x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp23 = tl.load(in_ptr1 + (x0 + 64 * x1), xmask, eviction_policy=
        'evict_last')
    tmp26 = tl.load(in_ptr1 + (16 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp27 = tl.load(in_ptr1 + (16 + x0 + 64 * x1), xmask, eviction_policy=
        'evict_last')
    tmp31 = tl.load(in_ptr1 + (32 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp32 = tl.load(in_ptr1 + (32 + x0 + 64 * x1), xmask, eviction_policy=
        'evict_last')
    tmp36 = tl.load(in_ptr1 + (48 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp37 = tl.load(in_ptr1 + (48 + x0 + 64 * x1), xmask, eviction_policy=
        'evict_last')
    tmp2 = tmp0 - tmp1
    tmp3 = tmp2 * tmp2
    tmp6 = tmp4 - tmp5
    tmp7 = tmp6 * tmp6
    tmp8 = tmp3 + tmp7
    tmp11 = tmp9 - tmp10
    tmp12 = tmp11 * tmp11
    tmp13 = tmp8 + tmp12
    tmp16 = tmp14 - tmp15
    tmp17 = tmp16 * tmp16
    tmp18 = tmp13 + tmp17
    tmp19 = 1e-12
    tmp20 = tmp18 + tmp19
    tmp21 = libdevice.sqrt(tmp20)
    tmp24 = tmp22 - tmp23
    tmp25 = tmp24 * tmp24
    tmp28 = tmp26 - tmp27
    tmp29 = tmp28 * tmp28
    tmp30 = tmp25 + tmp29
    tmp33 = tmp31 - tmp32
    tmp34 = tmp33 * tmp33
    tmp35 = tmp30 + tmp34
    tmp38 = tmp36 - tmp37
    tmp39 = tmp38 * tmp38
    tmp40 = tmp35 + tmp39
    tmp41 = tmp40 + tmp19
    tmp42 = libdevice.sqrt(tmp41)
    tmp43 = tmp21 - tmp42
    tl.store(out_ptr0 + x3, tmp43, xmask)


@triton.jit
def triton_poi_fused__to_copy_eq_fill_lift_fresh_2(in_ptr0, out_ptr0,
    xnumel, XBLOCK: tl.constexpr):
    xnumel = 1024
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex // 256
    x1 = xindex // 64 % 4
    x0 = xindex % 64
    x3 = xindex % 256
    x5 = xindex
    tmp11 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp12 = tl.load(in_ptr0 + x3, xmask, eviction_policy='evict_last')
    tmp17 = tl.load(in_ptr0 + (64 + x0), xmask, eviction_policy='evict_last')
    tmp23 = tl.load(in_ptr0 + (128 + x0), xmask, eviction_policy='evict_last')
    tmp31 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp0 = x2
    tmp1 = tl.full([1], 2, tl.int32)
    tmp2 = tmp0 == tmp1
    tmp3 = x1
    tmp4 = tmp3 == tmp1
    tmp5 = tl.full([1], 1, tl.int32)
    tmp6 = tmp1 == tmp5
    tmp7 = tmp3 == tmp5
    tmp8 = tl.full([1], 0, tl.int32)
    tmp9 = tmp5 == tmp8
    tmp10 = tmp3 == tmp8
    tmp13 = tmp11 == tmp12
    tmp14 = tmp13.to(tl.float32)
    tmp15 = 0.0
    tmp16 = tl.where(tmp10, tmp15, tmp14)
    tmp18 = tmp17 == tmp12
    tmp19 = tmp18.to(tl.float32)
    tmp20 = tl.where(tmp9, tmp16, tmp19)
    tmp21 = tl.where(tmp7, tmp15, tmp20)
    tmp22 = tmp1 == tmp8
    tmp24 = tmp23 == tmp12
    tmp25 = tmp24.to(tl.float32)
    tmp26 = tl.where(tmp22, tmp16, tmp25)
    tmp27 = tl.where(tmp6, tmp21, tmp26)
    tmp28 = tl.where(tmp4, tmp15, tmp27)
    tmp29 = tmp0 == tmp5
    tmp30 = tmp0 == tmp8
    tmp32 = tmp31 == tmp12
    tmp33 = tmp32.to(tl.float32)
    tmp34 = tl.where(tmp30, tmp16, tmp33)
    tmp35 = tl.where(tmp29, tmp21, tmp34)
    tmp36 = tl.where(tmp2, tmp28, tmp35)
    tl.store(out_ptr0 + x5, tmp36, xmask)


@triton.jit
def triton_per_fused_div_fill_lift_fresh_linalg_vector_norm_mul_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)
    r3 = rindex % 256
    r2 = rindex // 256
    r1 = rindex // 64 % 4
    r4 = rindex
    tmp0 = tl.load(in_ptr0 + r3, None, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr1 + (768 + r3), None, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr1 + r4, None)
    tmp1 = r2
    tmp2 = tl.full([1], 3, tl.int32)
    tmp3 = tmp1 == tmp2
    tmp4 = r1
    tmp5 = tmp4 == tmp2
    tmp7 = 0.0
    tmp8 = tl.where(tmp5, tmp7, tmp6)
    tmp10 = tl.where(tmp3, tmp8, tmp9)
    tmp11 = tmp0 * tmp10
    tmp12 = tmp11 * tmp11
    tmp13 = tl.broadcast_to(tmp12, [RBLOCK])
    tmp15 = triton_helpers.promote_to_tensor(tl.sum(tmp13, 0))
    tmp16 = libdevice.sqrt(tmp15)
    tmp17 = 0.25
    tmp18 = tmp16 * tmp17
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp18, None)


def call(args):
    arg0_1, arg1_1, arg2_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))
    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_div_linalg_vector_norm_0[grid(256)](arg2_1, buf0, 
            256, XBLOCK=128, num_warps=4, num_stages=1)
        del arg2_1
        buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused_div_linalg_vector_norm_0[grid(256)](arg1_1, buf1, 
            256, XBLOCK=128, num_warps=4, num_stages=1)
        del arg1_1
        buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused_add_pow_sqrt_sub_sum_1[grid(256)](buf0, buf1, buf2,
            256, XBLOCK=128, num_warps=4, num_stages=1)
        del buf0
        del buf1
        buf3 = empty_strided_cuda((4, 4, 4, 4, 4), (256, 64, 16, 4, 1),
            torch.float32)
        triton_poi_fused__to_copy_eq_fill_lift_fresh_2[grid(1024)](arg0_1,
            buf3, 1024, XBLOCK=256, num_warps=4, num_stages=1)
        del arg0_1
        buf4 = empty_strided_cuda((), (), torch.float32)
        buf5 = buf4
        del buf4
        triton_per_fused_div_fill_lift_fresh_linalg_vector_norm_mul_3[grid(1)](
            buf5, buf2, buf3, 1, 1024, num_warps=8, num_stages=1)
        del buf2
        del buf3
    return buf5,


class DistillLossNew(nn.Module):

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

    def cdist(self, a, b):
        """
        Returns euclidean distance between (all feature pairs) in a and b

        Args:
             a (2D Tensor): A batch of vectors shaped (B1, D)
             b (2D Tensor): A batch of vectors shaped (B2, D)
        Returns:
             A matrix of all pairwise distance between all vectors in a and b,
             will be shape of (B1, B2)
        """
        diff = a.unsqueeze(1) - b.unsqueeze(0)
        return ((diff ** 2).sum(2) + 1e-12).sqrt()

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


Measured latency_ms: 0.06575