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, math as tl_math
import 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


@triton.jit
def triton_poi_fused__to_copy_add_arange_eq_gt_mul_pow_rsub_sqrt_sub_where_0(
    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
    x2 = xindex
    x0 = xindex % 64
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp6 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last')
    tmp30 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp50 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp70 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp1 = tmp0.to(tl.int64)
    tmp2 = tl.full([1], 0, tl.int64)
    tmp3 = tmp1 == tmp2
    tmp4 = tmp3.to(tl.int64)
    tmp5 = tmp4.to(tl.float32)
    tmp7 = -0.8775825618903726
    tmp8 = tmp6 > tmp7
    tmp9 = 0.8775825618903728
    tmp10 = tmp6 * tmp9
    tmp11 = tmp6 * tmp6
    tmp12 = 1.0
    tmp13 = tmp12 - tmp11
    tmp14 = libdevice.sqrt(tmp13)
    tmp15 = 0.479425538604203
    tmp16 = tmp14 * tmp15
    tmp17 = tmp10 - tmp16
    tmp18 = 0.23971276930210156
    tmp19 = tmp6 - tmp18
    tmp20 = tl.where(tmp8, tmp17, tmp19)
    tmp21 = tmp5 * tmp20
    tmp22 = tmp12 - tmp5
    tmp23 = tmp22 * tmp6
    tmp24 = tmp21 + tmp23
    tmp25 = tmp24 * tmp12
    tmp26 = tl.full([1], 1, tl.int64)
    tmp27 = tmp1 == tmp26
    tmp28 = tmp27.to(tl.int64)
    tmp29 = tmp28.to(tl.float32)
    tmp31 = tmp30 > tmp7
    tmp32 = tmp30 * tmp9
    tmp33 = tmp30 * tmp30
    tmp34 = tmp12 - tmp33
    tmp35 = libdevice.sqrt(tmp34)
    tmp36 = tmp35 * tmp15
    tmp37 = tmp32 - tmp36
    tmp38 = tmp30 - tmp18
    tmp39 = tl.where(tmp31, tmp37, tmp38)
    tmp40 = tmp29 * tmp39
    tmp41 = tmp12 - tmp29
    tmp42 = tmp41 * tmp30
    tmp43 = tmp40 + tmp42
    tmp44 = tmp43 * tmp12
    tmp45 = triton_helpers.maximum(tmp25, tmp44)
    tmp46 = tl.full([1], 2, tl.int64)
    tmp47 = tmp1 == tmp46
    tmp48 = tmp47.to(tl.int64)
    tmp49 = tmp48.to(tl.float32)
    tmp51 = tmp50 > tmp7
    tmp52 = tmp50 * tmp9
    tmp53 = tmp50 * tmp50
    tmp54 = tmp12 - tmp53
    tmp55 = libdevice.sqrt(tmp54)
    tmp56 = tmp55 * tmp15
    tmp57 = tmp52 - tmp56
    tmp58 = tmp50 - tmp18
    tmp59 = tl.where(tmp51, tmp57, tmp58)
    tmp60 = tmp49 * tmp59
    tmp61 = tmp12 - tmp49
    tmp62 = tmp61 * tmp50
    tmp63 = tmp60 + tmp62
    tmp64 = tmp63 * tmp12
    tmp65 = triton_helpers.maximum(tmp45, tmp64)
    tmp66 = tl.full([1], 3, tl.int64)
    tmp67 = tmp1 == tmp66
    tmp68 = tmp67.to(tl.int64)
    tmp69 = tmp68.to(tl.float32)
    tmp71 = tmp70 > tmp7
    tmp72 = tmp70 * tmp9
    tmp73 = tmp70 * tmp70
    tmp74 = tmp12 - tmp73
    tmp75 = libdevice.sqrt(tmp74)
    tmp76 = tmp75 * tmp15
    tmp77 = tmp72 - tmp76
    tmp78 = tmp70 - tmp18
    tmp79 = tl.where(tmp71, tmp77, tmp78)
    tmp80 = tmp69 * tmp79
    tmp81 = tmp12 - tmp69
    tmp82 = tmp81 * tmp70
    tmp83 = tmp80 + tmp82
    tmp84 = tmp83 * tmp12
    tmp85 = triton_helpers.maximum(tmp65, tmp84)
    tl.store(out_ptr0 + x2, tmp85, xmask)


@triton.jit
def triton_poi_fused__to_copy_add_arange_eq_gt_mul_pow_rsub_sqrt_sub_where_1(
    in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 1024
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex // 4
    x0 = xindex % 4
    x4 = xindex % 256
    x5 = xindex
    tmp0 = tl.load(in_ptr0 + x3, xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr1 + x4, xmask, eviction_policy='evict_last')
    tmp26 = tl.load(in_ptr2 + x3, xmask, eviction_policy='evict_last')
    tmp1 = tmp0.to(tl.int64)
    tmp2 = x0
    tmp3 = tmp1 == tmp2
    tmp4 = tmp3.to(tl.int64)
    tmp5 = tmp4.to(tl.float32)
    tmp7 = -0.8775825618903726
    tmp8 = tmp6 > tmp7
    tmp9 = 0.8775825618903728
    tmp10 = tmp6 * tmp9
    tmp11 = tmp6 * tmp6
    tmp12 = 1.0
    tmp13 = tmp12 - tmp11
    tmp14 = libdevice.sqrt(tmp13)
    tmp15 = 0.479425538604203
    tmp16 = tmp14 * tmp15
    tmp17 = tmp10 - tmp16
    tmp18 = 0.23971276930210156
    tmp19 = tmp6 - tmp18
    tmp20 = tl.where(tmp8, tmp17, tmp19)
    tmp21 = tmp5 * tmp20
    tmp22 = tmp12 - tmp5
    tmp23 = tmp22 * tmp6
    tmp24 = tmp21 + tmp23
    tmp25 = tmp24 * tmp12
    tmp27 = tmp25 - tmp26
    tmp28 = 30.0
    tmp29 = tmp27 * tmp28
    tl.store(out_ptr0 + x5, tmp29, xmask)


@triton.jit
def triton_red_fused__log_softmax__to_copy_arange_eq_mean_mul_neg_sum_2(
    in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr,
    RBLOCK: tl.constexpr):
    rnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xoffset + tl.arange(0, XBLOCK)[:, None]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rbase = tl.arange(0, RBLOCK)[None, :]
    _tmp46 = tl.full([XBLOCK, RBLOCK], 0, tl.float32)
    for roffset in range(0, rnumel, RBLOCK):
        rindex = roffset + rbase
        rmask = rindex < rnumel
        r0 = rindex
        tmp0 = tl.load(in_ptr0 + 4 * r0, rmask, eviction_policy=
            'evict_last', other=0.0)
        tmp2 = tl.load(in_ptr0 + (1 + 4 * r0), rmask, eviction_policy=
            'evict_last', other=0.0)
        tmp5 = tl.load(in_ptr0 + (2 + 4 * r0), rmask, eviction_policy=
            'evict_last', other=0.0)
        tmp8 = tl.load(in_ptr0 + (3 + 4 * r0), rmask, eviction_policy=
            'evict_last', other=0.0)
        tmp14 = tl.load(in_ptr1 + r0, rmask, eviction_policy='evict_first',
            other=0.0)
        tmp1 = tl_math.exp(tmp0)
        tmp3 = tl_math.exp(tmp2)
        tmp4 = tmp1 + tmp3
        tmp6 = tl_math.exp(tmp5)
        tmp7 = tmp4 + tmp6
        tmp9 = tl_math.exp(tmp8)
        tmp10 = tmp7 + tmp9
        tmp11 = tl_math.log(tmp10)
        tmp12 = tmp0 - tmp11
        tmp13 = -tmp12
        tmp15 = tmp14.to(tl.int64)
        tmp16 = tl.full([1, 1], 0, tl.int64)
        tmp17 = tmp15 == tmp16
        tmp18 = tmp17.to(tl.int64)
        tmp19 = tmp18.to(tl.float32)
        tmp20 = tmp13 * tmp19
        tmp21 = tmp2 - tmp11
        tmp22 = -tmp21
        tmp23 = tl.full([1, 1], 1, tl.int64)
        tmp24 = tmp15 == tmp23
        tmp25 = tmp24.to(tl.int64)
        tmp26 = tmp25.to(tl.float32)
        tmp27 = tmp22 * tmp26
        tmp28 = tmp20 + tmp27
        tmp29 = tmp5 - tmp11
        tmp30 = -tmp29
        tmp31 = tl.full([1, 1], 2, tl.int64)
        tmp32 = tmp15 == tmp31
        tmp33 = tmp32.to(tl.int64)
        tmp34 = tmp33.to(tl.float32)
        tmp35 = tmp30 * tmp34
        tmp36 = tmp28 + tmp35
        tmp37 = tmp8 - tmp11
        tmp38 = -tmp37
        tmp39 = tl.full([1, 1], 3, tl.int64)
        tmp40 = tmp15 == tmp39
        tmp41 = tmp40.to(tl.int64)
        tmp42 = tmp41.to(tl.float32)
        tmp43 = tmp38 * tmp42
        tmp44 = tmp36 + tmp43
        tmp45 = tl.broadcast_to(tmp44, [XBLOCK, RBLOCK])
        tmp47 = _tmp46 + tmp45
        _tmp46 = tl.where(rmask, tmp47, _tmp46)
    tmp46 = tl.sum(_tmp46, 1)[:, None]
    tmp48 = 256.0
    tmp49 = tmp46 / tmp48
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp49, None)


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, 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, 1), (64, 16, 4, 1, 256),
            torch.float32)
        get_raw_stream(0)
        triton_poi_fused__to_copy_add_arange_eq_gt_mul_pow_rsub_sqrt_sub_where_0[
            grid(256)](arg0_1, arg1_1, buf0, 256, XBLOCK=256, num_warps=4,
            num_stages=1)
        buf1 = empty_strided_cuda((4, 4, 4, 4, 4), (256, 64, 16, 4, 1),
            torch.float32)
        triton_poi_fused__to_copy_add_arange_eq_gt_mul_pow_rsub_sqrt_sub_where_1[
            grid(1024)](arg0_1, arg1_1, buf0, buf1, 1024, XBLOCK=256,
            num_warps=4, num_stages=1)
        del arg1_1
        del buf0
        buf3 = empty_strided_cuda((), (), torch.float32)
        buf4 = buf3
        del buf3
        triton_red_fused__log_softmax__to_copy_arange_eq_mean_mul_neg_sum_2[
            grid(1)](buf4, buf1, arg0_1, 1, 256, XBLOCK=1, RBLOCK=256,
            num_warps=8, num_stages=1)
        del arg0_1
        del buf1
    return buf4,


class DenseCrossEntropy(nn.Module):

    def forward(self, x, target):
        x = x.float()
        target = target.float()
        logprobs = torch.nn.functional.log_softmax(x, dim=-1)
        loss = -logprobs * target
        loss = loss.sum(-1)
        return loss.mean()


class ArcFaceLossNew(nn.Module):

    def __init__(self, s=30.0, m=0.5):
        super().__init__()
        self.crit = DenseCrossEntropy()
        self.s = s
        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.043701