# AOT ID: ['0_inference']
from ctypes import c_void_p, c_long, c_int
import torch
import math
import random
import os
import tempfile
from math import inf, nan
from torch._inductor.hooks import run_intermediate_hooks
from torch._inductor.utils import maybe_profile
from torch._inductor.codegen.memory_planning import _align as align
from torch import device, empty_strided
from torch._inductor.async_compile import AsyncCompile
from torch._inductor.select_algorithm import extern_kernels
from torch._inductor.codegen.multi_kernel import MultiKernelCall
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid, split_scan_grid, grid_combo_kernels, start_graph, end_graph
from torch._C import _cuda_getCurrentRawStream as get_raw_stream

aten = torch.ops.aten
inductor_ops = torch.ops.inductor
_quantized = torch.ops._quantized
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cpu = torch._C._dynamo.guards._empty_strided_cpu
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
empty_strided_xpu = torch._C._dynamo.guards._empty_strided_xpu
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
alloc_from_pool = torch.ops.inductor._alloc_from_pool
async_compile = AsyncCompile()


# kernel path: runs/run_shard_4/inductor_cache/d4/cd4nn236j2iacn6ceojlrv7a3i4iyguhy6f5thudh5pspewffmz6.py
# Topologically Sorted Source Nodes: [gt, mul, pow_1, sub, sine, mul_1, phi, sub_2, phi_1, mul_2, sub_3, mul_3, output, logprobs], Original ATen: [aten.gt, aten.mul, aten.pow, aten.rsub, aten.sqrt, aten.sub, aten.where, aten.add, aten._log_softmax]
# Source node to ATen node mapping:
#   gt => gt
#   logprobs => exp, sum_1
#   mul => mul
#   mul_1 => mul_1
#   mul_2 => mul_2
#   mul_3 => mul_3
#   output => add
#   phi => sub_1
#   phi_1 => where
#   pow_1 => pow_1
#   sine => sqrt
#   sub => sub
#   sub_2 => sub_2
#   sub_3 => sub_3
# Graph fragment:
#   %gt : [num_users=1] = call_function[target=torch.ops.aten.gt.Scalar](args = (%arg0_1, -0.8775825618903726), kwargs = {})
#   %mul : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%arg0_1, 0.8775825618903728), kwargs = {})
#   %pow_1 : [num_users=1] = call_function[target=torch.ops.aten.pow.Tensor_Scalar](args = (%arg0_1, 2), kwargs = {})
#   %sub : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (1.0, %pow_1), kwargs = {})
#   %sqrt : [num_users=1] = call_function[target=torch.ops.aten.sqrt.default](args = (%sub,), kwargs = {})
#   %mul_1 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%sqrt, 0.479425538604203), kwargs = {})
#   %sub_1 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%mul, %mul_1), kwargs = {})
#   %sub_2 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%arg0_1, 0.23971276930210156), kwargs = {})
#   %where : [num_users=1] = call_function[target=torch.ops.aten.where.self](args = (%gt, %sub_1, %sub_2), kwargs = {})
#   %mul_2 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%arg1_1, %where), kwargs = {})
#   %sub_3 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (1.0, %arg1_1), kwargs = {})
#   %mul_3 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%sub_3, %arg0_1), kwargs = {})
#   %add : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%mul_2, %mul_3), kwargs = {})
#   %mul_tensor : [num_users=2] = call_function[target=torch.ops.aten.mul.Tensor](args = (%add, 1), kwargs = {})
#   %amax_default : [num_users=1] = call_function[target=torch.ops.aten.amax.default](args = (%mul_tensor, [-1], True), kwargs = {})
#   %sub_tensor : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%mul_tensor, %amax_default), kwargs = {})
#   %mul_tensor_1 : [num_users=2] = call_function[target=torch.ops.aten.mul.Tensor](args = (%sub_tensor, 30.0), kwargs = {})
#   %exp : [num_users=1] = call_function[target=torch.ops.aten.exp.default](args = (%mul_tensor_1,), kwargs = {})
#   %sum_1 : [num_users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%exp, [-1], True), kwargs = {})
triton_poi_fused__log_softmax_add_gt_mul_pow_rsub_sqrt_sub_where_0 = async_compile.triton('triton_poi_fused__log_softmax_add_gt_mul_pow_rsub_sqrt_sub_where_0', '''
import triton
import triton.language as tl
from triton.compiler.compiler import AttrsDescriptor

from torch._inductor.runtime import triton_helpers, triton_heuristics
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
from torch._inductor.runtime.hints import AutotuneHint, ReductionHint, TileHint, instance_descriptor, DeviceProperties

@triton_heuristics.pointwise(
    size_hints=[64], 
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '*fp32', 3: '*fp32', 4: 'i32'}, 'device': DeviceProperties(type='cuda', index=0, cc=80, major=8, regs_per_multiprocessor=65536, max_threads_per_multi_processor=2048, multi_processor_count=108), 'constants': {}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2, 3, 4), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused__log_softmax_add_gt_mul_pow_rsub_sqrt_sub_where_0', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 8, 'num_reduction': 0, 'backend_hash': 'A9C866B4A14FD3277824029365D703C2427B2E685E54EC9B3EF4ADC8D1EEAC1D', 'are_deterministic_algorithms_enabled': False, 'assert_indirect_indexing': True, 'autotune_local_cache': True, 'autotune_pointwise': True, 'autotune_remote_cache': None, 'force_disable_caches': False, 'dynamic_scale_rblock': True, 'max_autotune': False, 'max_autotune_pointwise': False, 'min_split_scan_rblock': 256, 'spill_threshold': 16, 'store_cubin': False},
    min_elem_per_thread=0
)
@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)
''', device_str='cuda')


# kernel path: runs/run_shard_4/inductor_cache/7p/c7pnycif2hdh2j6dm2ov3e34xtzilxnepnaqyhbcnxfhqyxqgf7u.py
# Topologically Sorted Source Nodes: [gt, mul, pow_1, sub, sine, mul_1, phi, sub_2, phi_1, mul_2, sub_3, mul_3, output, logprobs, neg, loss], Original ATen: [aten.gt, aten.mul, aten.pow, aten.rsub, aten.sqrt, aten.sub, aten.where, aten.add, aten._log_softmax, aten.neg]
# Source node to ATen node mapping:
#   gt => gt
#   logprobs => log, sub_5
#   loss => mul_5
#   mul => mul
#   mul_1 => mul_1
#   mul_2 => mul_2
#   mul_3 => mul_3
#   neg => neg
#   output => add
#   phi => sub_1
#   phi_1 => where
#   pow_1 => pow_1
#   sine => sqrt
#   sub => sub
#   sub_2 => sub_2
#   sub_3 => sub_3
# Graph fragment:
#   %gt : [num_users=1] = call_function[target=torch.ops.aten.gt.Scalar](args = (%arg0_1, -0.8775825618903726), kwargs = {})
#   %mul : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%arg0_1, 0.8775825618903728), kwargs = {})
#   %pow_1 : [num_users=1] = call_function[target=torch.ops.aten.pow.Tensor_Scalar](args = (%arg0_1, 2), kwargs = {})
#   %sub : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (1.0, %pow_1), kwargs = {})
#   %sqrt : [num_users=1] = call_function[target=torch.ops.aten.sqrt.default](args = (%sub,), kwargs = {})
#   %mul_1 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%sqrt, 0.479425538604203), kwargs = {})
#   %sub_1 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%mul, %mul_1), kwargs = {})
#   %sub_2 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%arg0_1, 0.23971276930210156), kwargs = {})
#   %where : [num_users=1] = call_function[target=torch.ops.aten.where.self](args = (%gt, %sub_1, %sub_2), kwargs = {})
#   %mul_2 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%arg1_1, %where), kwargs = {})
#   %sub_3 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (1.0, %arg1_1), kwargs = {})
#   %mul_3 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%sub_3, %arg0_1), kwargs = {})
#   %add : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%mul_2, %mul_3), kwargs = {})
#   %mul_tensor : [num_users=2] = call_function[target=torch.ops.aten.mul.Tensor](args = (%add, 1), kwargs = {})
#   %sub_tensor : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%mul_tensor, %amax_default), kwargs = {})
#   %mul_tensor_1 : [num_users=2] = call_function[target=torch.ops.aten.mul.Tensor](args = (%sub_tensor, 30.0), kwargs = {})
#   %log : [num_users=1] = call_function[target=torch.ops.aten.log.default](args = (%sum_1,), kwargs = {})
#   %sub_5 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%mul_tensor_1, %log), kwargs = {})
#   %neg : [num_users=1] = call_function[target=torch.ops.aten.neg.default](args = (%sub_5,), kwargs = {})
#   %mul_5 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%neg, %arg1_1), kwargs = {})
triton_poi_fused__log_softmax_add_gt_mul_neg_pow_rsub_sqrt_sub_where_1 = async_compile.triton('triton_poi_fused__log_softmax_add_gt_mul_neg_pow_rsub_sqrt_sub_where_1', '''
import triton
import triton.language as tl
from triton.compiler.compiler import AttrsDescriptor

from torch._inductor.runtime import triton_helpers, triton_heuristics
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
from torch._inductor.runtime.hints import AutotuneHint, ReductionHint, TileHint, instance_descriptor, DeviceProperties

@triton_heuristics.pointwise(
    size_hints=[256], 
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '*fp32', 3: '*fp32', 4: '*fp32', 5: 'i32'}, 'device': DeviceProperties(type='cuda', index=0, cc=80, major=8, regs_per_multiprocessor=65536, max_threads_per_multi_processor=2048, multi_processor_count=108), 'constants': {}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2, 3, 4, 5), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused__log_softmax_add_gt_mul_neg_pow_rsub_sqrt_sub_where_1', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 4, 'num_reduction': 0, 'backend_hash': 'A9C866B4A14FD3277824029365D703C2427B2E685E54EC9B3EF4ADC8D1EEAC1D', 'are_deterministic_algorithms_enabled': False, 'assert_indirect_indexing': True, 'autotune_local_cache': True, 'autotune_pointwise': True, 'autotune_remote_cache': None, 'force_disable_caches': False, 'dynamic_scale_rblock': True, 'max_autotune': False, 'max_autotune_pointwise': False, 'min_split_scan_rblock': 256, 'spill_threshold': 16, 'store_cubin': False},
    min_elem_per_thread=0
)
@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)
''', device_str='cuda')


# kernel path: runs/run_shard_4/inductor_cache/ws/cwsssktu52blpd3xccw4jscrdh35mswkecob5i6wbmoj6bffiyeb.py
# Topologically Sorted Source Nodes: [loss_1, loss_2, truediv], Original ATen: [aten.sum, aten.mean, aten.div]
# Source node to ATen node mapping:
#   loss_1 => sum_2
#   loss_2 => mean
#   truediv => div
# Graph fragment:
#   %sum_2 : [num_users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%mul_5, [-1]), kwargs = {})
#   %mean : [num_users=1] = call_function[target=torch.ops.aten.mean.default](args = (%sum_2,), kwargs = {})
#   %div : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%mean, 2), kwargs = {})
triton_per_fused_div_mean_sum_2 = async_compile.triton('triton_per_fused_div_mean_sum_2', '''
import triton
import triton.language as tl
from triton.compiler.compiler import AttrsDescriptor

from torch._inductor.runtime import triton_helpers, triton_heuristics
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
from torch._inductor.runtime.hints import AutotuneHint, ReductionHint, TileHint, instance_descriptor, DeviceProperties

@triton_heuristics.persistent_reduction(
    size_hints=[1, 64],
    reduction_hint=ReductionHint.INNER,
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: 'i32', 3: 'i32'}, 'device': DeviceProperties(type='cuda', index=0, cc=80, major=8, regs_per_multiprocessor=65536, max_threads_per_multi_processor=2048, multi_processor_count=108), 'constants': {2: 1}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 3), equal_to_1=(2,))]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_per_fused_div_mean_sum_2', 'mutated_arg_names': ['in_out_ptr0'], 'no_x_dim': False, 'num_load': 4, 'num_reduction': 1, 'backend_hash': 'A9C866B4A14FD3277824029365D703C2427B2E685E54EC9B3EF4ADC8D1EEAC1D', 'are_deterministic_algorithms_enabled': False, 'assert_indirect_indexing': True, 'autotune_local_cache': True, 'autotune_pointwise': True, 'autotune_remote_cache': None, 'force_disable_caches': False, 'dynamic_scale_rblock': True, 'max_autotune': False, 'max_autotune_pointwise': False, 'min_split_scan_rblock': 256, 'spill_threshold': 16, 'store_cubin': False}
)
@triton.jit
def triton_per_fused_div_mean_sum_2(in_out_ptr0, in_ptr0, xnumel, rnumel, XBLOCK : tl.constexpr):
    xnumel = 1
    rnumel = 64
    RBLOCK: tl.constexpr = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[None, :]
    roffset = 0
    rmask = 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)
''', device_str='cuda')


async_compile.wait(globals())
del async_compile

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)
        # Topologically Sorted Source Nodes: [gt, mul, pow_1, sub, sine, mul_1, phi, sub_2, phi_1, mul_2, sub_3, mul_3, output, logprobs], Original ATen: [aten.gt, aten.mul, aten.pow, aten.rsub, aten.sqrt, aten.sub, aten.where, aten.add, aten._log_softmax]
        stream0 = get_raw_stream(0)
        triton_poi_fused__log_softmax_add_gt_mul_pow_rsub_sqrt_sub_where_0.run(arg1_1, arg0_1, buf0, buf1, 64, grid=grid(64), stream=stream0)
        buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        # Topologically Sorted Source Nodes: [gt, mul, pow_1, sub, sine, mul_1, phi, sub_2, phi_1, mul_2, sub_3, mul_3, output, logprobs, neg, loss], Original ATen: [aten.gt, aten.mul, aten.pow, aten.rsub, aten.sqrt, aten.sub, aten.where, aten.add, aten._log_softmax, aten.neg]
        triton_poi_fused__log_softmax_add_gt_mul_neg_pow_rsub_sqrt_sub_where_1.run(arg1_1, arg0_1, buf0, buf1, buf2, 256, grid=grid(256), stream=stream0)
        del arg0_1
        del arg1_1
        del buf0
        del buf1
        buf3 = empty_strided_cuda((), (), torch.float32)
        buf4 = buf3; del buf3  # reuse
        # Topologically Sorted Source Nodes: [loss_1, loss_2, truediv], Original ATen: [aten.sum, aten.mean, aten.div]
        triton_per_fused_div_mean_sum_2.run(buf4, buf2, 1, 64, grid=grid(1), stream=stream0)
        del buf2
    return (buf4, )


def benchmark_compiled_module(times=10, repeat=10):
    from torch._dynamo.testing import rand_strided
    from torch._inductor.utils import print_performance
    arg0_1 = rand_strided((4, 4, 4, 4), (64, 16, 4, 1), device='cuda:0', dtype=torch.float32)
    arg1_1 = rand_strided((4, 4, 4, 4), (64, 16, 4, 1), device='cuda:0', dtype=torch.float32)
    fn = lambda: call([arg0_1, arg1_1])
    return print_performance(fn, times=times, repeat=repeat)


if __name__ == "__main__":
    from torch._inductor.wrapper_benchmark import compiled_module_main
    compiled_module_main('None', benchmark_compiled_module)

import math
import torch
from torch import nn


class DenseCrossEntropy(nn.Module):
    """ The CrossEntropy loss that takes the one-hot
    vector of the gt label as the input, should be equivalent to the 
    standard CrossEntropy implementation. The one-hot vector
    is meant for the ArcFaceLoss and CutMix augmentation

    Args:
        x: the output of the model.
        target: the one-hot ground-truth label
    """

    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 ArcFaceLoss(nn.modules.Module):
    """ ArcFaceLoss, see the Fig.2 and Eq.3 in 
    https://arxiv.org/pdf/1801.07698.pdf

    Args:
        s: the scale factor on the output for computing
            CrossEntropy
        m: the margin penalty on the target (ground-truth label)
    """

    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, logits, labels):
        logits = logits.float()
        cosine = logits
        sine = torch.sqrt(1.0 - torch.pow(cosine, 2))
        phi = cosine * self.cos_m - sine * self.sin_m
        phi = torch.where(cosine > self.th, phi, cosine - self.mm)
        output = labels * phi + (1.0 - labels) * cosine
        output *= self.s
        loss = self.crit(output, labels)
        return loss / 2


def get_inputs():
    return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]


def get_init_inputs():
    return [[], {}]

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):
    """ The CrossEntropy loss that takes the one-hot
    vector of the gt label as the input, should be equivalent to the 
    standard CrossEntropy implementation. The one-hot vector
    is meant for the ArcFaceLoss and CutMix augmentation

    Args:
        x: the output of the model.
        target: the one-hot ground-truth label
    """

    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):
    """ ArcFaceLoss, see the Fig.2 and Eq.3 in 
    https://arxiv.org/pdf/1801.07698.pdf

    Args:
        s: the scale factor on the output for computing
            CrossEntropy
        m: the margin penalty on the target (ground-truth label)
    """

    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]
