import torch
from torch._inductor.select_algorithm import extern_kernels
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
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor


@triton.jit
def triton_poi_fused_div_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 = 1e-12
    tmp14 = triton_helpers.maximum(tmp12, tmp13)
    tmp15 = tmp0 / tmp14
    tl.store(out_ptr0 + x3, tmp15, 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
    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')
    tmp2 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp4 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp6 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), 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 = tl_math.exp(tmp8)
    tl.store(out_ptr0 + x3, tmp9, xmask)


@triton.jit
def triton_poi_fused__softmax_max_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
    x0 = xindex % 16
    x1 = xindex // 16
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 64 * x1), xmask)
    tmp1 = tl.load(in_ptr0 + (16 + x0 + 64 * x1), xmask)
    tmp3 = tl.load(in_ptr0 + (32 + x0 + 64 * x1), xmask)
    tmp5 = tl.load(in_ptr0 + (48 + x0 + 64 * x1), xmask)
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 + tmp3
    tmp6 = tmp4 + tmp5
    tmp7 = tmp0 / tmp6
    tmp8 = tmp1 / tmp6
    tmp9 = tmp7 > tmp8
    tmp10 = tmp7 == tmp8
    tmp11 = tmp7 != tmp7
    tmp12 = tmp8 != tmp8
    tmp13 = tmp11 > tmp12
    tmp14 = tmp9 | tmp13
    tmp15 = tmp11 & tmp12
    tmp16 = tmp10 | tmp15
    tmp17 = tl.full([1], 0, tl.int64)
    tmp18 = tl.full([1], 1, tl.int64)
    tmp19 = tmp17 < tmp18
    tmp20 = tmp16 & tmp19
    tmp21 = tmp14 | tmp20
    tmp22 = tl.where(tmp21, tmp7, tmp8)
    tmp23 = tl.where(tmp21, tmp17, tmp18)
    tmp24 = tmp3 / tmp6
    tmp25 = tmp22 > tmp24
    tmp26 = tmp22 == tmp24
    tmp27 = tmp22 != tmp22
    tmp28 = tmp24 != tmp24
    tmp29 = tmp27 > tmp28
    tmp30 = tmp25 | tmp29
    tmp31 = tmp27 & tmp28
    tmp32 = tmp26 | tmp31
    tmp33 = tl.full([1], 2, tl.int64)
    tmp34 = tmp23 < tmp33
    tmp35 = tmp32 & tmp34
    tmp36 = tmp30 | tmp35
    tmp37 = tl.where(tmp36, tmp22, tmp24)
    tmp38 = tl.where(tmp36, tmp23, tmp33)
    tmp39 = tmp5 / tmp6
    tmp40 = tmp37 > tmp39
    tmp41 = tmp37 == tmp39
    tmp42 = tmp37 != tmp37
    tmp43 = tmp39 != tmp39
    tmp44 = tmp42 > tmp43
    tmp45 = tmp40 | tmp44
    tmp46 = tmp42 & tmp43
    tmp47 = tmp41 | tmp46
    tmp48 = tl.full([1], 3, tl.int64)
    tmp49 = tmp38 < tmp48
    tmp50 = tmp47 & tmp49
    tmp51 = tmp45 | tmp50
    tl.where(tmp51, tmp37, tmp39)
    tmp53 = tl.where(tmp51, tmp38, tmp48)
    tl.store(out_ptr0 + x2, tmp53, xmask)


@triton.jit
def triton_per_fused_acos_clamp_eq_mean_mul_pow_relu_rsub_sum_where_3(
    in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 32
    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 % 16
    r1 = rindex // 16
    r2 = rindex
    tmp0 = tl.load(in_ptr0 + (r0 + 64 * r1), None)
    tmp1 = tl.load(in_ptr0 + (128 + r0 + 64 * r1), None)
    tmp3 = tl.load(in_ptr0 + (16 + r0 + 64 * r1), None)
    tmp4 = tl.load(in_ptr0 + (144 + r0 + 64 * r1), None)
    tmp7 = tl.load(in_ptr0 + (32 + r0 + 64 * r1), None)
    tmp8 = tl.load(in_ptr0 + (160 + r0 + 64 * r1), None)
    tmp11 = tl.load(in_ptr0 + (48 + r0 + 64 * r1), None)
    tmp12 = tl.load(in_ptr0 + (176 + r0 + 64 * r1), None)
    tmp22 = tl.load(in_ptr1 + r2, None)
    tmp23 = tl.load(in_ptr1 + (32 + r2), None)
    tmp2 = tmp0 * tmp1
    tmp5 = tmp3 * tmp4
    tmp6 = tmp2 + tmp5
    tmp9 = tmp7 * tmp8
    tmp10 = tmp6 + tmp9
    tmp13 = tmp11 * tmp12
    tmp14 = tmp10 + tmp13
    tmp15 = -1e-07
    tmp16 = triton_helpers.maximum(tmp14, tmp15)
    tmp17 = 1e-07
    tmp18 = triton_helpers.minimum(tmp16, tmp17)
    tmp19 = libdevice.acos(tmp18)
    tmp20 = 1.0
    tmp21 = tmp19 * tmp20
    tmp24 = tmp22 == tmp23
    tmp25 = tmp21 * tmp21
    tmp26 = 0.5
    tmp27 = tmp26 - tmp21
    tmp28 = tl.full([1, 1], 0, tl.int32)
    tmp29 = triton_helpers.maximum(tmp28, tmp27)
    tmp30 = tmp29 * tmp29
    tmp31 = tl.where(tmp24, tmp25, tmp30)
    tmp32 = 1e-12
    tmp33 = triton_helpers.maximum(tmp31, tmp32)
    tmp34 = tl.broadcast_to(tmp33, [XBLOCK, RBLOCK])
    tmp36 = tl.sum(tmp34, 1)[:, None]
    tmp37 = 32.0
    tmp38 = tmp36 / tmp37
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp38, None)


def call(args):
    primals_1, primals_2 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_2, (4, 4), (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_0[grid(256)](primals_1, buf0, 256, XBLOCK=128,
            num_warps=4, num_stages=1)
        del primals_1
        buf1 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf0, (64, 4), (4, 1), 0),
            reinterpret_tensor(primals_2, (4, 4), (1, 4), 0), out=buf1)
        del primals_2
        buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused__softmax_1[grid(256)](buf1, buf2, 256, XBLOCK=128,
            num_warps=4, num_stages=1)
        buf3 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.int64)
        triton_poi_fused__softmax_max_2[grid(64)](buf2, buf3, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        del buf2
        buf5 = empty_strided_cuda((), (), torch.float32)
        buf6 = buf5
        del buf5
        triton_per_fused_acos_clamp_eq_mean_mul_pow_relu_rsub_sum_where_3[grid
            (1)](buf6, buf0, buf3, 1, 32, XBLOCK=1, num_warps=2, num_stages=1)
        del buf3
    return buf6, reinterpret_tensor(buf1, (4, 4, 4, 4), (64, 16, 4, 1), 0
        ), reinterpret_tensor(buf0, (64, 4), (4, 1), 0)


class AMCLossNew(nn.Module):

    def __init__(self, in_features, out_features, s=None, m=None, device='cuda'
        ):
        """
        Angular Margin Contrastive Loss

        https://arxiv.org/pdf/2004.09805.pdf
        
        Code converted over from Tensorflow to Pytorch

        """
        super(AMCLossNew, self).__init__()
        self.m = 0.5 if not m else m
        self.s = 1.0 if not s else s
        self.in_features = in_features
        self.out_features = out_features
        self.fc = nn.Linear(in_features, out_features, bias=False)
        self.device = device

    def forward(self, input_0):
        primals_2 = self.fc.weight
        primals_1 = input_0
        output = call([primals_1, primals_2])
        return output[0], output[1]


Measured latency_ms: 0.061505