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
from torch import nn
from torchvision import models as models
import torch.onnx
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_0(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
    x2 = xindex
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr0 + (3 + 4 * x1), 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-12
    tmp14 = triton_helpers.maximum(tmp12, tmp13)
    tmp15 = tmp0 / tmp14
    tl.store(out_ptr0 + x2, tmp15, xmask)


@triton.jit
def triton_poi_fused_div_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
    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 = 1e-12
    tmp14 = triton_helpers.maximum(tmp12, tmp13)
    tmp15 = tmp0 / tmp14
    tl.store(out_ptr0 + x3, tmp15, xmask)


@triton.jit
def triton_per_fused_clamp_min_div_linalg_vector_norm_mul_rsub_sum_2(
    in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, 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
    r0 = rindex % 4
    tmp0 = tl.load(in_ptr0 + (r4 + 64 * r2), None)
    tmp2 = tl.load(in_ptr0 + (16 + r4 + 64 * r2), None)
    tmp5 = tl.load(in_ptr0 + (32 + r4 + 64 * r2), None)
    tmp8 = tl.load(in_ptr0 + (48 + r4 + 64 * r2), None)
    tmp15 = tl.load(in_ptr1 + r1, None, eviction_policy='evict_last')
    tmp1 = tmp0 * tmp0
    tmp3 = tmp2 * tmp2
    tmp4 = tmp1 + tmp3
    tmp6 = tmp5 * tmp5
    tmp7 = tmp4 + tmp6
    tmp9 = tmp8 * tmp8
    tmp10 = tmp7 + tmp9
    tmp11 = libdevice.sqrt(tmp10)
    tmp12 = 1e-08
    tmp13 = triton_helpers.maximum(tmp11, tmp12)
    tmp14 = tmp0 / tmp13
    tmp16 = tl.full([XBLOCK, RBLOCK], 4, tl.int32)
    tmp17 = tmp15 + tmp16
    tmp18 = tmp15 < 0
    tmp19 = tl.where(tmp18, tmp17, tmp15)
    tl.device_assert((0 <= tmp19) & (tmp19 < 4),
        'index out of bounds: 0 <= tmp19 < 4')
    tmp21 = tl.load(in_ptr2 + (r0 + 4 * tmp19), None)
    tmp22 = tmp21 * tmp21
    tmp23 = tmp22 + tmp22
    tmp24 = tmp23 + tmp22
    tmp25 = tmp24 + tmp22
    tmp26 = libdevice.sqrt(tmp25)
    tmp27 = triton_helpers.maximum(tmp26, tmp12)
    tmp28 = tmp21 / tmp27
    tmp29 = tmp14 * tmp28
    tmp30 = tmp2 / tmp13
    tmp31 = tmp30 * tmp28
    tmp32 = tmp29 + tmp31
    tmp33 = tmp5 / tmp13
    tmp34 = tmp33 * tmp28
    tmp35 = tmp32 + tmp34
    tmp36 = tmp8 / tmp13
    tmp37 = tmp36 * tmp28
    tmp38 = tmp35 + tmp37
    tmp39 = 1.0
    tmp40 = tmp39 - tmp38
    tmp41 = tl.broadcast_to(tmp40, [XBLOCK, RBLOCK])
    tmp43 = tl.sum(tmp41, 1)[:, None]
    tmp44 = 0.25
    tmp45 = tmp43 * tmp44
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp45, None)


def call(args):
    primals_1, primals_2, primals_3 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_2, (4,), (1,))
    assert_size_stride(primals_3, (4, 4), (4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_div_0[grid(16)](primals_3, buf0, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused_div_1[grid(256)](primals_1, buf1, 256, XBLOCK=256,
            num_warps=4, num_stages=1)
        buf3 = empty_strided_cuda((), (), torch.float32)
        buf18 = buf3
        del buf3
        triton_per_fused_clamp_min_div_linalg_vector_norm_mul_rsub_sum_2[grid
            (1)](buf18, buf1, primals_2, buf0, 1, 64, XBLOCK=1, num_warps=2,
            num_stages=1)
        del buf1
        buf4 = torch.ops.aten.set_.source_Tensor(primals_3, buf0)
        assert_size_stride(buf4, (4, 4), (4, 1))
        del primals_3
    return buf18, primals_1, primals_2, buf0


class CenterLossNew(nn.Module):
    """Implements the Center loss from https://ydwen.github.io/papers/WenECCV16.pdf"""

    def __init__(self, num_classes, embed_size, cos_dist=True):
        super().__init__()
        self.cos_dist = cos_dist
        self.num_classes = num_classes
        self.centers = nn.Parameter(torch.randn(self.num_classes, embed_size))
        self.embed_size = embed_size
        self.mse = nn.MSELoss(reduction='elementwise_mean')

    def get_centers(self):
        """Returns estimated centers"""
        return self.centers

    def forward(self, input_0, input_1):
        primals_3 = self.centers
        primals_1 = input_0
        primals_2 = input_1
        output = call([primals_1, primals_2, primals_3])
        return output[0]


Measured latency_ms: 0.043133