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 math
from itertools import product as product
import torch.nn as nn
import torch.utils.data.distributed
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_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
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + x0, xmask)
    tmp1 = 1.0
    tmp2 = tmp0 * tmp1
    tl.store(out_ptr0 + x0, tmp2, xmask)


@triton.jit
def triton_poi_fused_clamp_gt_index_index_put_mul_pow_rsub_sqrt_sub_where_1(
    in_ptr0, in_ptr1, out_ptr1, xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 16
    x0 = xindex % 16
    tmp11 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
    tmp0 = x1
    tmp1 = tl.full([1], 2, tl.int64)
    tmp2 = tmp0 < tmp1
    tmp3 = tl.full([1], 1, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.full([1], 0, tl.int64)
    tmp6 = tl.where(tmp4, tmp5, tmp3)
    tmp7 = tl.full([1], 3, tl.int64)
    tmp8 = tmp0 < tmp7
    tmp9 = tl.where(tmp8, tmp1, tmp7)
    tmp10 = tl.where(tmp2, tmp6, tmp9)
    tmp12 = tl.full([XBLOCK], 4, tl.int32)
    tmp13 = tmp11 + tmp12
    tmp14 = tmp11 < 0
    tmp15 = tl.where(tmp14, tmp13, tmp11)
    tl.device_assert((0 <= tmp15) & (tmp15 < 4) | ~xmask,
        'index out of bounds: 0 <= tmp15 < 4')
    tmp17 = tl.load(in_ptr1 + (x0 + 16 * tmp15 + 64 * tmp10), xmask)
    tmp18 = -0.8775825618903726
    tmp19 = tmp17 > tmp18
    tmp20 = 0.8775825618903728
    tmp21 = tmp17 * tmp20
    tmp22 = tmp17 * tmp17
    tmp23 = 1.0
    tmp24 = tmp23 - tmp22
    tmp25 = 0.0
    tmp26 = triton_helpers.maximum(tmp24, tmp25)
    tmp27 = triton_helpers.minimum(tmp26, tmp23)
    tmp28 = libdevice.sqrt(tmp27)
    tmp29 = 0.479425538604203
    tmp30 = tmp28 * tmp29
    tmp31 = tmp21 - tmp30
    tmp32 = 0.23971276930210156
    tmp33 = tmp17 - tmp32
    tmp34 = tl.where(tmp19, tmp31, tmp33)
    tl.store(out_ptr1 + (x0 + 16 * tmp15 + 64 * tmp10), tmp34, xmask)


@triton.jit
def triton_poi_fused_mul_2(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
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + x0, xmask)
    tmp1 = 64.0
    tmp2 = tmp0 * tmp1
    tl.store(out_ptr0 + x0, tmp2, xmask)


def call(args):
    arg0_1, arg1_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg1_1, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_mul_0[grid(256)](arg0_1, buf1, 256, XBLOCK=256,
            num_warps=4, num_stages=1)
        triton_poi_fused_clamp_gt_index_index_put_mul_pow_rsub_sqrt_sub_where_1[
            grid(64)](arg1_1, arg0_1, buf1, 64, XBLOCK=64, num_warps=1,
            num_stages=1)
        del arg0_1
        del arg1_1
        buf3 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused_mul_2[grid(256)](buf1, buf3, 256, XBLOCK=256,
            num_warps=4, num_stages=1)
        del buf1
    return buf3,


class ArcFaceNew(nn.Module):

    def __init__(self, s=64.0, m=0.5):
        """ArcFace formula:
            cos(m + theta) = cos(m)cos(theta) - sin(m)sin(theta)
        Note that:
            0 <= m + theta <= Pi
        So if (m + theta) >= Pi, then theta >= Pi - m. In [0, Pi]
        we have:
            cos(theta) < cos(Pi - m)
        So we can use cos(Pi - m) as threshold to check whether
        (m + theta) go out of [0, Pi]
        Args:
            embedding_size: usually 128, 256, 512 ...
            class_num: num of people when training
            s: scale, see normface https://arxiv.org/abs/1704.06369
            m: margin, see SphereFace, CosFace, and ArcFace paper
        https://github.com/siriusdemon/Build-Your-Own-Face-Model/blob/master/recognition/model/metric.py
        """
        super().__init__()
        self.s = s
        self.m = m
        self.cos_m = math.cos(m)
        self.sin_m = math.sin(m)
        self.th = math.cos(math.pi - m)
        self.mm = math.sin(math.pi - m) * m

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


Measured latency_ms: 0.038788