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


@triton.jit
def triton_poi_fused__log_softmax_add_gt_mul_neg_pow_rsub_sqrt_sub_where_1(
    in_ptr0, in_ptr1, in_ptr2, in_ptr3, 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
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr1 + x2, xmask)
    tmp21 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last')
    tmp25 = tl.load(in_ptr3 + x1, xmask, eviction_policy='evict_last')
    tmp2 = -0.8775825618903726
    tmp3 = tmp1 > tmp2
    tmp4 = 0.8775825618903728
    tmp5 = tmp1 * tmp4
    tmp6 = tmp1 * tmp1
    tmp7 = 1.0
    tmp8 = tmp7 - tmp6
    tmp9 = libdevice.sqrt(tmp8)
    tmp10 = 0.479425538604203
    tmp11 = tmp9 * tmp10
    tmp12 = tmp5 - tmp11
    tmp13 = 0.23971276930210156
    tmp14 = tmp1 - tmp13
    tmp15 = tl.where(tmp3, tmp12, tmp14)
    tmp16 = tmp0 * tmp15
    tmp17 = tmp7 - tmp0
    tmp18 = tmp17 * tmp1
    tmp19 = tmp16 + tmp18
    tmp20 = tmp19 * tmp7
    tmp22 = tmp20 - tmp21
    tmp23 = 30.0
    tmp24 = tmp22 * tmp23
    tmp26 = tl_math.log(tmp25)
    tmp27 = tmp24 - tmp26
    tmp28 = -tmp27
    tmp29 = tmp28 * tmp0
    tl.store(out_ptr0 + x2, tmp29, xmask)


@triton.jit
def triton_per_fused_div_mean_sum_2(in_out_ptr0, in_ptr0, 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
    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')
    tmp3 = tl.load(in_ptr0 + (2 + 4 * r0), None, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr0 + (3 + 4 * r0), None, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 + tmp3
    tmp6 = tmp4 + tmp5
    tmp7 = tl.broadcast_to(tmp6, [XBLOCK, RBLOCK])
    tmp9 = tl.sum(tmp7, 1)[:, None]
    tmp10 = 64.0
    tmp11 = tmp9 / tmp10
    tmp12 = 0.5
    tmp13 = tmp11 * tmp12
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp13, 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, 1), (16, 4, 1, 64), torch.float32)
        buf1 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32)
        get_raw_stream(0)
        triton_poi_fused__log_softmax_add_gt_mul_pow_rsub_sqrt_sub_where_0[grid
            (64)](arg1_1, arg0_1, buf0, buf1, 64, XBLOCK=64, num_warps=1,
            num_stages=1)
        buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused__log_softmax_add_gt_mul_neg_pow_rsub_sqrt_sub_where_1[
            grid(256)](arg1_1, arg0_1, buf0, buf1, buf2, 256, XBLOCK=128,
            num_warps=4, num_stages=1)
        del arg0_1
        del arg1_1
        del buf0
        del buf1
        buf3 = empty_strided_cuda((), (), torch.float32)
        buf4 = buf3
        del buf3
        triton_per_fused_div_mean_sum_2[grid(1)](buf4, buf2, 1, 64, XBLOCK=
            1, num_warps=2, num_stages=1)
        del buf2
    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.modules.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.043612