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
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_mul_pow_sub_sum_0(in_ptr0, in_ptr1, 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 % 4
    x1 = xindex // 4 % 4
    x3 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp3 = tl.load(in_ptr0 + (4 + x0 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp6 = tl.load(in_ptr0 + (8 + x0 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp9 = tl.load(in_ptr0 + (12 + x0 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp14 = tl.load(in_ptr1 + (x0 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp17 = tl.load(in_ptr1 + (4 + x0 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp21 = tl.load(in_ptr1 + (8 + x0 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp25 = tl.load(in_ptr1 + (12 + x0 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp1 = 1.0
    tmp2 = tmp0 * tmp1
    tmp4 = tmp3 * tmp1
    tmp5 = tmp2 + tmp4
    tmp7 = tmp6 * tmp1
    tmp8 = tmp5 + tmp7
    tmp10 = tmp9 * tmp1
    tmp11 = tmp8 + tmp10
    tmp12 = 4.0
    tmp13 = tmp11 / tmp12
    tmp15 = tmp13 - tmp14
    tmp16 = tmp15 * tmp15
    tmp18 = tmp13 - tmp17
    tmp19 = tmp18 * tmp18
    tmp20 = tmp16 + tmp19
    tmp22 = tmp13 - tmp21
    tmp23 = tmp22 * tmp22
    tmp24 = tmp20 + tmp23
    tmp26 = tmp13 - tmp25
    tmp27 = tmp26 * tmp26
    tmp28 = tmp24 + tmp27
    tmp29 = tmp28 * tmp1
    tmp30 = -1.0
    tmp31 = tmp29 * tmp30
    tl.store(out_ptr0 + x3, tmp31, xmask)


@triton.jit
def triton_poi_fused_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
    x2 = xindex
    x0 = xindex % 16
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (16 + x0), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (32 + x0), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (48 + x0), 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 = 1.0
    tmp10 = tmp8 * tmp9
    tl.store(out_ptr0 + x2, tmp10, xmask)


@triton.jit
def triton_poi_fused__log_softmax_2(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
    x2 = xindex
    x0 = xindex % 16
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (16 + x0), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (32 + x0), xmask, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr0 + (48 + x0), xmask, eviction_policy='evict_last')
    tmp2 = tl_math.exp(tmp1)
    tmp4 = tl_math.exp(tmp3)
    tmp5 = tmp2 + tmp4
    tmp7 = tl_math.exp(tmp6)
    tmp8 = tmp5 + tmp7
    tmp10 = tl_math.exp(tmp9)
    tmp11 = tmp8 + tmp10
    tmp12 = tl_math.log(tmp11)
    tmp13 = tmp0 - tmp12
    tl.store(out_ptr0 + x2, tmp13, xmask)


@triton.jit
def triton_per_fused_mean_mul_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)
    r0 = rindex % 4
    r1 = rindex // 4 % 4
    r2 = rindex // 16
    r3 = rindex % 16
    tmp0 = tl.load(in_ptr0 + (r0 + 16 * r1), None, eviction_policy='evict_last'
        )
    tmp1 = tl.load(in_ptr1 + (r3 + 64 * r2), None)
    tmp3 = tl.load(in_ptr0 + (4 + r0 + 16 * r1), None, eviction_policy=
        'evict_last')
    tmp4 = tl.load(in_ptr1 + (16 + r3 + 64 * r2), None)
    tmp7 = tl.load(in_ptr0 + (8 + r0 + 16 * r1), None, eviction_policy=
        'evict_last')
    tmp8 = tl.load(in_ptr1 + (32 + r3 + 64 * r2), None)
    tmp11 = tl.load(in_ptr0 + (12 + r0 + 16 * r1), None, eviction_policy=
        'evict_last')
    tmp12 = tl.load(in_ptr1 + (48 + r3 + 64 * r2), None)
    tmp2 = tmp0 * tmp1
    tmp5 = tmp3 * tmp4
    tmp6 = tmp2 + tmp5
    tmp9 = tmp7 * tmp8
    tmp10 = tmp6 + tmp9
    tmp13 = tmp11 * tmp12
    tmp14 = tmp10 + tmp13
    tmp15 = 4.0
    tmp16 = tmp14 / tmp15
    tmp17 = tl.broadcast_to(tmp16, [XBLOCK, RBLOCK])
    tmp19 = tl.sum(tmp17, 1)[:, None]
    tmp20 = 64.0
    tmp21 = tmp19 / tmp20
    tmp22 = -1.0
    tmp23 = tmp21 * tmp22
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp23, None)


@triton.jit
def triton_poi_fused_argmax_4(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp17 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp32 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp2 = tmp0 > tmp1
    tmp3 = tmp0 == tmp1
    tmp4 = tmp0 != tmp0
    tmp5 = tmp1 != tmp1
    tmp6 = tmp4 > tmp5
    tmp7 = tmp2 | tmp6
    tmp8 = tmp4 & tmp5
    tmp9 = tmp3 | tmp8
    tmp10 = tl.full([1], 0, tl.int64)
    tmp11 = tl.full([1], 1, tl.int64)
    tmp12 = tmp10 < tmp11
    tmp13 = tmp9 & tmp12
    tmp14 = tmp7 | tmp13
    tmp15 = tl.where(tmp14, tmp0, tmp1)
    tmp16 = tl.where(tmp14, tmp10, tmp11)
    tmp18 = tmp15 > tmp17
    tmp19 = tmp15 == tmp17
    tmp20 = tmp15 != tmp15
    tmp21 = tmp17 != tmp17
    tmp22 = tmp20 > tmp21
    tmp23 = tmp18 | tmp22
    tmp24 = tmp20 & tmp21
    tmp25 = tmp19 | tmp24
    tmp26 = tl.full([1], 2, tl.int64)
    tmp27 = tmp16 < tmp26
    tmp28 = tmp25 & tmp27
    tmp29 = tmp23 | tmp28
    tmp30 = tl.where(tmp29, tmp15, tmp17)
    tmp31 = tl.where(tmp29, tmp16, tmp26)
    tmp33 = tmp30 > tmp32
    tmp34 = tmp30 == tmp32
    tmp35 = tmp30 != tmp30
    tmp36 = tmp32 != tmp32
    tmp37 = tmp35 > tmp36
    tmp38 = tmp33 | tmp37
    tmp39 = tmp35 & tmp36
    tmp40 = tmp34 | tmp39
    tmp41 = tl.full([1], 3, tl.int64)
    tmp42 = tmp31 < tmp41
    tmp43 = tmp40 & tmp42
    tmp44 = tmp38 | tmp43
    tl.where(tmp44, tmp30, tmp32)
    tmp46 = tl.where(tmp44, tmp31, tmp41)
    tl.store(out_ptr0 + x0, tmp46, xmask)


@triton.jit
def triton_per_fused__to_copy_argmax_eq_mean_5(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)
    r0 = rindex
    r1 = rindex % 4
    r2 = rindex // 4 % 4
    tmp0 = tl.load(in_ptr0 + 4 * r0, None, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr0 + (1 + 4 * r0), None, eviction_policy='evict_last')
    tmp17 = tl.load(in_ptr0 + (2 + 4 * r0), None, eviction_policy='evict_last')
    tmp32 = tl.load(in_ptr0 + (3 + 4 * r0), None, eviction_policy='evict_last')
    tmp48 = tl.load(in_ptr1 + (r2 + 4 * r1), None, eviction_policy='evict_last'
        )
    tmp2 = tmp0 > tmp1
    tmp3 = tmp0 == tmp1
    tmp4 = tmp0 != tmp0
    tmp5 = tmp1 != tmp1
    tmp6 = tmp4 > tmp5
    tmp7 = tmp2 | tmp6
    tmp8 = tmp4 & tmp5
    tmp9 = tmp3 | tmp8
    tmp10 = tl.full([1, 1], 0, tl.int64)
    tmp11 = tl.full([1, 1], 1, tl.int64)
    tmp12 = tmp10 < tmp11
    tmp13 = tmp9 & tmp12
    tmp14 = tmp7 | tmp13
    tmp15 = tl.where(tmp14, tmp0, tmp1)
    tmp16 = tl.where(tmp14, tmp10, tmp11)
    tmp18 = tmp15 > tmp17
    tmp19 = tmp15 == tmp17
    tmp20 = tmp15 != tmp15
    tmp21 = tmp17 != tmp17
    tmp22 = tmp20 > tmp21
    tmp23 = tmp18 | tmp22
    tmp24 = tmp20 & tmp21
    tmp25 = tmp19 | tmp24
    tmp26 = tl.full([1, 1], 2, tl.int64)
    tmp27 = tmp16 < tmp26
    tmp28 = tmp25 & tmp27
    tmp29 = tmp23 | tmp28
    tmp30 = tl.where(tmp29, tmp15, tmp17)
    tmp31 = tl.where(tmp29, tmp16, tmp26)
    tmp33 = tmp30 > tmp32
    tmp34 = tmp30 == tmp32
    tmp35 = tmp30 != tmp30
    tmp36 = tmp32 != tmp32
    tmp37 = tmp35 > tmp36
    tmp38 = tmp33 | tmp37
    tmp39 = tmp35 & tmp36
    tmp40 = tmp34 | tmp39
    tmp41 = tl.full([1, 1], 3, tl.int64)
    tmp42 = tmp31 < tmp41
    tmp43 = tmp40 & tmp42
    tmp44 = tmp38 | tmp43
    tl.where(tmp44, tmp30, tmp32)
    tmp46 = tl.where(tmp44, tmp31, tmp41)
    tmp47 = tmp46.to(tl.float32)
    tmp49 = tmp48.to(tl.float32)
    tmp50 = tmp47 == tmp49
    tmp51 = tmp50.to(tl.float32)
    tmp52 = tl.broadcast_to(tmp51, [XBLOCK, RBLOCK])
    tmp54 = tl.sum(tmp52, 1)[:, None]
    tmp55 = 64.0
    tmp56 = tmp54 / tmp55
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp56, None)


def call(args):
    arg0_1, arg1_1, arg2_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4), (16, 4, 1))
    assert_size_stride(arg1_1, (4, 4, 4), (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), (16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_mul_pow_sub_sum_0[grid(64)](arg0_1, arg1_1, buf0, 
            64, XBLOCK=64, num_warps=1, num_stages=1)
        del arg0_1
        del arg1_1
        buf1 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused_1[grid(64)](buf0, buf1, 64, XBLOCK=64, num_warps=1,
            num_stages=1)
        buf2 = buf0
        del buf0
        triton_poi_fused__log_softmax_2[grid(64)](buf1, buf2, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        del buf1
        buf3 = empty_strided_cuda((), (), torch.float32)
        buf7 = buf3
        del buf3
        triton_per_fused_mean_mul_3[grid(1)](buf7, buf2, arg2_1, 1, 64,
            XBLOCK=1, num_warps=2, num_stages=1)
        buf5 = empty_strided_cuda((4, 4), (1, 4), torch.int64)
        triton_poi_fused_argmax_4[grid(16)](buf2, buf5, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        del buf2
        buf6 = empty_strided_cuda((), (), torch.float32)
        buf8 = buf6
        del buf6
        triton_per_fused__to_copy_argmax_eq_mean_5[grid(1)](buf8, arg2_1,
            buf5, 1, 64, XBLOCK=1, num_warps=2, num_stages=1)
        del arg2_1
        del buf5
    return buf7, buf8


class ProtoLossNew(torch.nn.Module):

    def __init__(self, num_classes, num_support, num_queries, ndim):
        super(ProtoLossNew, self).__init__()
        self.num_classes = num_classes
        self.num_support = num_support
        self.num_queries = num_queries
        self.ndim = ndim

    def euclidean_distance(self, a, b):
        N, _D = a.shape[0], a.shape[1]
        M = b.shape[0]
        a = torch.repeat_interleave(a.unsqueeze(1), repeats=M, dim=1)
        b = torch.repeat_interleave(b.unsqueeze(0), repeats=N, dim=0)
        return 1.0 * torch.sum(torch.pow(a - b, 2), 2)

    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], output[1]


Measured latency_ms: 0.078362