# 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_7/inductor_cache/um/cum65j23qchrjf5dndblqgbw6zomhgwfj2obfidtgy7b5j3zwklm.py
# Topologically Sorted Source Nodes: [mixture_weights], Original ATen: [aten._softmax]
# Source node to ATen node mapping:
#   mixture_weights => amax, exp, sub
# Graph fragment:
#   %amax : [num_users=1] = call_function[target=torch.ops.aten.amax.default](args = (%arg1_1, [-1], True), kwargs = {})
#   %sub : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%arg1_1, %amax), kwargs = {})
#   %exp : [num_users=2] = call_function[target=torch.ops.aten.exp.default](args = (%sub,), kwargs = {})
triton_poi_fused__softmax_0 = async_compile.triton('triton_poi_fused__softmax_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=[16], 
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '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), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused__softmax_0', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 5, '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__softmax_0(in_ptr0, out_ptr0, xnumel, XBLOCK : tl.constexpr):
    xnumel = 16
    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_ptr0 + (4*x1), xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (1 + (4*x1)), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (2 + (4*x1)), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (3 + (4*x1)), 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 + (x2), tmp9, xmask)
''', device_str='cuda')


# kernel path: runs/run_shard_7/inductor_cache/gf/cgftkgna6f5ymcweuriktu5f2pj7nbxnhujfdm6jhofpyxwgvp35.py
# Topologically Sorted Source Nodes: [sub, sigma_x, truediv, x, sub_1, sigma_y, truediv_1, y, add, rho_xy, mul_2, sub_2, sub_3, mul, mul_1, xy, mul_3, arg, neg, pow_3, sub_5, mul_4, truediv_3, pdf, mul_5, mul_6, pow_4, sub_6, sqrt, norm, pdfs, mixture_weights, mul_8], Original ATen: [aten.sub, aten.exp, aten.div, aten.pow, aten.add, aten.tanh, aten.mul, aten.neg, aten.rsub, aten.sqrt, aten._softmax]
# Source node to ATen node mapping:
#   add => add
#   arg => sub_5
#   mixture_weights => div, sum_1
#   mul => mul
#   mul_1 => mul_1
#   mul_2 => mul_2
#   mul_3 => mul_3
#   mul_4 => mul_4
#   mul_5 => mul_5
#   mul_6 => mul_6
#   mul_8 => mul_8
#   neg => neg
#   norm => mul_7
#   pdf => exp_3
#   pdfs => div_5
#   pow_3 => pow_3
#   pow_4 => pow_4
#   rho_xy => tanh
#   sigma_x => exp_1
#   sigma_y => exp_2
#   sqrt => sqrt
#   sub => sub_1
#   sub_1 => sub_2
#   sub_2 => sub_3
#   sub_3 => sub_4
#   sub_5 => sub_6
#   sub_6 => sub_7
#   truediv => div_1
#   truediv_1 => div_2
#   truediv_3 => div_4
#   x => pow_1
#   xy => div_3
#   y => pow_2
# Graph fragment:
#   %sub_1 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%unsqueeze, %select_3), kwargs = {})
#   %exp_1 : [num_users=3] = call_function[target=torch.ops.aten.exp.default](args = (%select_5,), kwargs = {})
#   %div_1 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%sub_1, %exp_1), kwargs = {})
#   %pow_1 : [num_users=1] = call_function[target=torch.ops.aten.pow.Tensor_Scalar](args = (%div_1, 2), kwargs = {})
#   %sub_2 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%unsqueeze_1, %select_4), kwargs = {})
#   %exp_2 : [num_users=3] = call_function[target=torch.ops.aten.exp.default](args = (%select_6,), kwargs = {})
#   %div_2 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%sub_2, %exp_2), kwargs = {})
#   %pow_2 : [num_users=1] = call_function[target=torch.ops.aten.pow.Tensor_Scalar](args = (%div_2, 2), kwargs = {})
#   %add : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%pow_1, %pow_2), kwargs = {})
#   %tanh : [num_users=3] = call_function[target=torch.ops.aten.tanh.default](args = (%select_7,), kwargs = {})
#   %mul_2 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%tanh, 2.0), kwargs = {})
#   %sub_3 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%unsqueeze, %select_3), kwargs = {})
#   %sub_4 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%unsqueeze_1, %select_4), kwargs = {})
#   %mul : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%sub_3, %sub_4), kwargs = {})
#   %mul_1 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%exp_1, %exp_2), kwargs = {})
#   %div_3 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%mul, %mul_1), kwargs = {})
#   %mul_3 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%mul_2, %div_3), kwargs = {})
#   %sub_5 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%add, %mul_3), kwargs = {})
#   %neg : [num_users=1] = call_function[target=torch.ops.aten.neg.default](args = (%sub_5,), kwargs = {})
#   %pow_3 : [num_users=1] = call_function[target=torch.ops.aten.pow.Tensor_Scalar](args = (%tanh, 2), kwargs = {})
#   %sub_6 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (1.0, %pow_3), kwargs = {})
#   %mul_4 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%sub_6, 2), kwargs = {})
#   %div_4 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%neg, %mul_4), kwargs = {})
#   %exp_3 : [num_users=1] = call_function[target=torch.ops.aten.exp.default](args = (%div_4,), kwargs = {})
#   %mul_5 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%exp_1, 6.283185307179586), kwargs = {})
#   %mul_6 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%mul_5, %exp_2), kwargs = {})
#   %pow_4 : [num_users=1] = call_function[target=torch.ops.aten.pow.Tensor_Scalar](args = (%tanh, 2), kwargs = {})
#   %sub_7 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (1.0, %pow_4), kwargs = {})
#   %sqrt : [num_users=1] = call_function[target=torch.ops.aten.sqrt.default](args = (%sub_7,), kwargs = {})
#   %mul_7 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%mul_6, %sqrt), kwargs = {})
#   %div_5 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%exp_3, %mul_7), kwargs = {})
#   %sum_1 : [num_users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%exp, [-1], True), kwargs = {})
#   %div : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%exp, %sum_1), kwargs = {})
#   %mul_8 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%div_5, %div), kwargs = {})
triton_poi_fused__softmax_add_div_exp_mul_neg_pow_rsub_sqrt_sub_tanh_1 = async_compile.triton('triton_poi_fused__softmax_add_div_exp_mul_neg_pow_rsub_sqrt_sub_tanh_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=[16], 
    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__softmax_add_div_exp_mul_neg_pow_rsub_sqrt_sub_tanh_1', 'mutated_arg_names': ['in_out_ptr0'], 'no_x_dim': False, 'num_load': 12, '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__softmax_add_div_exp_mul_neg_pow_rsub_sqrt_sub_tanh_1(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, xnumel, XBLOCK : tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = (xindex // 4)
    x0 = xindex % 4
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + (4*x1), xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + (5*x0), xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr1 + (2 + (5*x0)), xmask, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr0 + (1 + (4*x1)), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr1 + (1 + (5*x0)), xmask, eviction_policy='evict_last')
    tmp10 = tl.load(in_ptr1 + (3 + (5*x0)), xmask, eviction_policy='evict_last')
    tmp15 = tl.load(in_ptr1 + (4 + (5*x0)), xmask, eviction_policy='evict_last')
    tmp37 = tl.load(in_ptr2 + (x2), xmask)
    tmp38 = tl.load(in_ptr2 + (4*x1), xmask, eviction_policy='evict_last')
    tmp39 = tl.load(in_ptr2 + (1 + (4*x1)), xmask, eviction_policy='evict_last')
    tmp41 = tl.load(in_ptr2 + (2 + (4*x1)), xmask, eviction_policy='evict_last')
    tmp43 = tl.load(in_ptr2 + (3 + (4*x1)), xmask, eviction_policy='evict_last')
    tmp2 = tmp0 - tmp1
    tmp4 = tl_math.exp(tmp3)
    tmp5 = tmp2 / tmp4
    tmp6 = tmp5 * tmp5
    tmp9 = tmp7 - tmp8
    tmp11 = tl_math.exp(tmp10)
    tmp12 = tmp9 / tmp11
    tmp13 = tmp12 * tmp12
    tmp14 = tmp6 + tmp13
    tmp16 = libdevice.tanh(tmp15)
    tmp17 = 2.0
    tmp18 = tmp16 * tmp17
    tmp19 = tmp2 * tmp9
    tmp20 = tmp4 * tmp11
    tmp21 = tmp19 / tmp20
    tmp22 = tmp18 * tmp21
    tmp23 = tmp14 - tmp22
    tmp24 = -tmp23
    tmp25 = tmp16 * tmp16
    tmp26 = 1.0
    tmp27 = tmp26 - tmp25
    tmp28 = tmp27 * tmp17
    tmp29 = tmp24 / tmp28
    tmp30 = tl_math.exp(tmp29)
    tmp31 = 6.283185307179586
    tmp32 = tmp4 * tmp31
    tmp33 = tmp32 * tmp11
    tmp34 = libdevice.sqrt(tmp27)
    tmp35 = tmp33 * tmp34
    tmp36 = tmp30 / tmp35
    tmp40 = tmp38 + tmp39
    tmp42 = tmp40 + tmp41
    tmp44 = tmp42 + tmp43
    tmp45 = tmp37 / tmp44
    tmp46 = tmp36 * tmp45
    tl.store(in_out_ptr0 + (x2), tmp46, xmask)
''', device_str='cuda')


# kernel path: runs/run_shard_7/inductor_cache/zo/czoofsklsepvakticzs2madlnyiefc3vzpufz622fhzso3ktudkd.py
# Topologically Sorted Source Nodes: [sum_1, add_1, log, position_loss, ne, valid_stroke, mul_9, sum_2, sum_3, position_loss_1, pen_state, view_1, pen_loss, add_2], Original ATen: [aten.sum, aten.add, aten.log, aten.neg, aten.ne, aten._to_copy, aten.mul, aten.div, aten.argmax, aten.view, aten.nll_loss_forward, aten._log_softmax]
# Source node to ATen node mapping:
#   add_1 => add_1
#   add_2 => add_2
#   log => log
#   mul_9 => mul_9
#   ne => ne
#   pen_loss => amax_1, convert_element_type_1, div_7, exp_4, full_default_1, gather, log_1, ne_2, ne_3, neg_2, sub_8, sub_9, sum_5, sum_6, sum_7, where_1
#   pen_state => argmax
#   position_loss => neg_1
#   position_loss_1 => div_6
#   sum_1 => sum_2
#   sum_2 => sum_3
#   sum_3 => sum_4
#   valid_stroke => convert_element_type
#   view_1 => view_1
# Graph fragment:
#   %sum_2 : [num_users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%mul_8, [-1]), kwargs = {})
#   %add_1 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%sum_2, 1e-05), kwargs = {})
#   %log : [num_users=1] = call_function[target=torch.ops.aten.log.default](args = (%add_1,), kwargs = {})
#   %neg_1 : [num_users=1] = call_function[target=torch.ops.aten.neg.default](args = (%log,), kwargs = {})
#   %ne : [num_users=1] = call_function[target=torch.ops.aten.ne.Scalar](args = (%select_2, 1.0), kwargs = {})
#   %convert_element_type : [num_users=2] = call_function[target=torch.ops.prims.convert_element_type.default](args = (%ne, torch.float32), kwargs = {})
#   %mul_9 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%neg_1, %convert_element_type), kwargs = {})
#   %sum_3 : [num_users=1] = call_function[target=torch.ops.aten.sum.default](args = (%mul_9,), kwargs = {})
#   %sum_4 : [num_users=1] = call_function[target=torch.ops.aten.sum.default](args = (%convert_element_type,), kwargs = {})
#   %div_6 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%sum_3, %sum_4), kwargs = {})
#   %argmax : [num_users=1] = call_function[target=torch.ops.aten.argmax.default](args = (%slice_1, -1), kwargs = {})
#   %view_1 : [num_users=4] = call_function[target=torch.ops.aten.reshape.default](args = (%argmax, [-1]), kwargs = {})
#   %ne_2 : [num_users=1] = call_function[target=torch.ops.aten.ne.Scalar](args = (%view_1, -100), kwargs = {})
#   %amax_1 : [num_users=1] = call_function[target=torch.ops.aten.amax.default](args = (%view, [1], True), kwargs = {})
#   %sub_8 : [num_users=2] = call_function[target=torch.ops.aten.sub.Tensor](args = (%view, %amax_1), kwargs = {})
#   %exp_4 : [num_users=1] = call_function[target=torch.ops.aten.exp.default](args = (%sub_8,), kwargs = {})
#   %sum_5 : [num_users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%exp_4, [1], True), kwargs = {})
#   %log_1 : [num_users=1] = call_function[target=torch.ops.aten.log.default](args = (%sum_5,), kwargs = {})
#   %sub_9 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%sub_8, %log_1), kwargs = {})
#   %gather : [num_users=1] = call_function[target=torch.ops.aten.gather.default](args = (%sub_9, 1, %unsqueeze_2), kwargs = {})
#   %neg_2 : [num_users=1] = call_function[target=torch.ops.aten.neg.default](args = (%squeeze,), kwargs = {})
#   %full_default_1 : [num_users=1] = call_function[target=torch.ops.aten.full.default](args = ([], 0.0), kwargs = {dtype: torch.float32, layout: torch.strided, device: cuda:0, pin_memory: False})
#   %where_1 : [num_users=1] = call_function[target=torch.ops.aten.where.self](args = (%ne_2, %neg_2, %full_default_1), kwargs = {})
#   %sum_7 : [num_users=1] = call_function[target=torch.ops.aten.sum.default](args = (%where_1,), kwargs = {})
#   %ne_3 : [num_users=1] = call_function[target=torch.ops.aten.ne.Scalar](args = (%view_1, -100), kwargs = {})
#   %sum_6 : [num_users=1] = call_function[target=torch.ops.aten.sum.default](args = (%ne_3,), kwargs = {})
#   %convert_element_type_1 : [num_users=1] = call_function[target=torch.ops.prims.convert_element_type.default](args = (%sum_6, torch.float32), kwargs = {})
#   %div_7 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%sum_7, %convert_element_type_1), kwargs = {})
#   %add_2 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%div_6, %div_7), kwargs = {})
triton_per_fused__log_softmax__to_copy_add_argmax_div_log_mul_ne_neg_nll_loss_forward_sum_view_2 = async_compile.triton('triton_per_fused__log_softmax__to_copy_add_argmax_div_log_mul_ne_neg_nll_loss_forward_sum_view_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, 4],
    reduction_hint=ReductionHint.DEFAULT,
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '*fp32', 3: '*fp32', 4: 'i32', 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': {4: 1}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2, 3), equal_to_1=(4,))]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_per_fused__log_softmax__to_copy_add_argmax_div_log_mul_ne_neg_nll_loss_forward_sum_view_2', 'mutated_arg_names': ['in_out_ptr0'], 'no_x_dim': False, 'num_load': 9, 'num_reduction': 4, '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__log_softmax__to_copy_add_argmax_div_log_mul_ne_neg_nll_loss_forward_sum_view_2(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, xnumel, rnumel, XBLOCK : tl.constexpr):
    xnumel = 1
    rnumel = 4
    RBLOCK: tl.constexpr = 4
    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 + (2 + (4*r0)), None, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr0 + (3 + (4*r0)), None, eviction_policy='evict_last')
    tmp26 = tl.load(in_ptr1 + (3*r0), None, eviction_policy='evict_last')
    tmp27 = tl.load(in_ptr1 + (1 + (3*r0)), None, eviction_policy='evict_last')
    tmp29 = tl.load(in_ptr1 + (2 + (3*r0)), None, eviction_policy='evict_last')
    tmp42 = tl.load(in_ptr2 + (4*r0), None, eviction_policy='evict_last')
    tmp43 = tl.load(in_ptr2 + (1 + (4*r0)), None, eviction_policy='evict_last')
    tmp45 = tl.load(in_ptr2 + (2 + (4*r0)), None, eviction_policy='evict_last')
    tmp47 = tl.load(in_ptr2 + (3 + (4*r0)), None, eviction_policy='evict_last')
    tmp2 = tmp0 > tmp1
    tmp3 = tmp0 == tmp1
    tmp4 = tmp0 != tmp0
    tmp5 = tmp1 != tmp1
    tmp6 = tmp4 > tmp5
    tmp7 = tmp2 | tmp6
    tmp8 = tmp4 & tmp5
    tmp9 = tmp3 | tmp8
    tmp10 = tl.full([1, 1], 0, tl.int64)
    tmp11 = tl.full([1, 1], 1, tl.int64)
    tmp12 = tmp10 < tmp11
    tmp13 = tmp9 & tmp12
    tmp14 = tmp7 | tmp13
    tmp15 = tl.where(tmp14, tmp0, tmp1)
    tmp16 = tl.where(tmp14, tmp10, tmp11)
    tmp17 = tl.full([1, 1], -100, tl.int64)
    tmp18 = tmp16 != tmp17
    tmp19 = tl.where(tmp18, tmp16, tmp10)
    tmp20 = tl.full([XBLOCK, RBLOCK], 3, tl.int32)
    tmp21 = tmp19 + tmp20
    tmp22 = tmp19 < 0
    tmp23 = tl.where(tmp22, tmp21, tmp19)
    tl.device_assert((0 <= tmp23) & (tmp23 < 3), "index out of bounds: 0 <= tmp23 < 3")
    tmp25 = tl.load(in_ptr1 + (tmp23 + (3*r0)), None, eviction_policy='evict_last')
    tmp28 = triton_helpers.maximum(tmp26, tmp27)
    tmp30 = triton_helpers.maximum(tmp28, tmp29)
    tmp31 = tmp25 - tmp30
    tmp32 = tmp26 - tmp30
    tmp33 = tl_math.exp(tmp32)
    tmp34 = tmp27 - tmp30
    tmp35 = tl_math.exp(tmp34)
    tmp36 = tmp33 + tmp35
    tmp37 = tmp29 - tmp30
    tmp38 = tl_math.exp(tmp37)
    tmp39 = tmp36 + tmp38
    tmp40 = tl_math.log(tmp39)
    tmp41 = tmp31 - tmp40
    tmp44 = tmp42 + tmp43
    tmp46 = tmp44 + tmp45
    tmp48 = tmp46 + tmp47
    tmp49 = 1e-05
    tmp50 = tmp48 + tmp49
    tmp51 = tl_math.log(tmp50)
    tmp52 = -tmp51
    tmp53 = 1.0
    tmp54 = tmp1 != tmp53
    tmp55 = tmp54.to(tl.float32)
    tmp56 = tmp52 * tmp55
    tmp57 = tl.broadcast_to(tmp56, [XBLOCK, RBLOCK])
    tmp59 = tl.sum(tmp57, 1)[:, None]
    tmp60 = tl.broadcast_to(tmp55, [XBLOCK, RBLOCK])
    tmp62 = tl.sum(tmp60, 1)[:, None]
    tmp63 = -tmp41
    tmp64 = 0.0
    tmp65 = tl.where(tmp18, tmp63, tmp64)
    tmp66 = tl.broadcast_to(tmp65, [XBLOCK, RBLOCK])
    tmp68 = tl.sum(tmp66, 1)[:, None]
    tmp69 = tmp18.to(tl.int64)
    tmp70 = tl.broadcast_to(tmp69, [XBLOCK, RBLOCK])
    tmp72 = tl.sum(tmp70, 1)[:, None]
    tmp73 = tmp59 / tmp62
    tmp74 = tmp72.to(tl.float32)
    tmp75 = tmp68 / tmp74
    tmp76 = tmp73 + tmp75
    tl.debug_barrier()
    tl.store(in_out_ptr0 + (tl.full([XBLOCK, 1], 0, tl.int32)), tmp76, None)
''', device_str='cuda')


async_compile.wait(globals())
del async_compile

def call(args):
    arg0_1, arg1_1, arg2_1, arg3_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4), (4, 1))
    assert_size_stride(arg1_1, (4, 4), (4, 1))
    assert_size_stride(arg2_1, (4, 5), (5, 1))
    assert_size_stride(arg3_1, (4, 3), (3, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        # Topologically Sorted Source Nodes: [mixture_weights], Original ATen: [aten._softmax]
        stream0 = get_raw_stream(0)
        triton_poi_fused__softmax_0.run(arg1_1, buf1, 16, grid=grid(16), stream=stream0)
        del arg1_1
        buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf2 = buf0; del buf0  # reuse
        # Topologically Sorted Source Nodes: [sub, sigma_x, truediv, x, sub_1, sigma_y, truediv_1, y, add, rho_xy, mul_2, sub_2, sub_3, mul, mul_1, xy, mul_3, arg, neg, pow_3, sub_5, mul_4, truediv_3, pdf, mul_5, mul_6, pow_4, sub_6, sqrt, norm, pdfs, mixture_weights, mul_8], Original ATen: [aten.sub, aten.exp, aten.div, aten.pow, aten.add, aten.tanh, aten.mul, aten.neg, aten.rsub, aten.sqrt, aten._softmax]
        triton_poi_fused__softmax_add_div_exp_mul_neg_pow_rsub_sqrt_sub_tanh_1.run(buf2, arg0_1, arg2_1, buf1, 16, grid=grid(16), stream=stream0)
        del arg2_1
        del buf1
        buf3 = empty_strided_cuda((), (), torch.float32)
        buf8 = buf3; del buf3  # reuse
        # Topologically Sorted Source Nodes: [sum_1, add_1, log, position_loss, ne, valid_stroke, mul_9, sum_2, sum_3, position_loss_1, pen_state, view_1, pen_loss, add_2], Original ATen: [aten.sum, aten.add, aten.log, aten.neg, aten.ne, aten._to_copy, aten.mul, aten.div, aten.argmax, aten.view, aten.nll_loss_forward, aten._log_softmax]
        triton_per_fused__log_softmax__to_copy_add_argmax_div_log_mul_ne_neg_nll_loss_forward_sum_view_2.run(buf8, arg0_1, arg3_1, buf2, 1, 4, grid=grid(1), stream=stream0)
        del arg0_1
        del arg3_1
        del buf2
    return (buf8, )


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, 1), device='cuda:0', dtype=torch.float32)
    arg1_1 = rand_strided((4, 4), (4, 1), device='cuda:0', dtype=torch.float32)
    arg2_1 = rand_strided((4, 5), (5, 1), device='cuda:0', dtype=torch.float32)
    arg3_1 = rand_strided((4, 3), (3, 1), device='cuda:0', dtype=torch.float32)
    fn = lambda: call([arg0_1, arg1_1, arg2_1, arg3_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 torch
import numpy as np
import torch as th


def gaussian_pdfs(dx, dy, params):
    """Returns the pdf at (dx, dy) for each Gaussian in the mixture.
    """
    dx = dx.unsqueeze(-1)
    dy = dy.unsqueeze(-1)
    mu_x = params[..., 0]
    mu_y = params[..., 1]
    sigma_x = params[..., 2].exp()
    sigma_y = params[..., 3].exp()
    rho_xy = th.tanh(params[..., 4])
    x = ((dx - mu_x) / sigma_x).pow(2)
    y = ((dy - mu_y) / sigma_y).pow(2)
    xy = (dx - mu_x) * (dy - mu_y) / (sigma_x * sigma_y)
    arg = x + y - 2.0 * rho_xy * xy
    pdf = th.exp(-arg / (2 * (1.0 - rho_xy.pow(2))))
    norm = 2.0 * np.pi * sigma_x * sigma_y * (1.0 - rho_xy.pow(2)).sqrt()
    return pdf / norm


class GaussianMixtureReconstructionLoss(th.nn.Module):
    """
    Args:
    """

    def __init__(self, eps=1e-05):
        super(GaussianMixtureReconstructionLoss, self).__init__()
        self.eps = eps

    def forward(self, pen_logits, mixture_logits, gaussian_params, targets):
        dx = targets[..., 0]
        dy = targets[..., 1]
        pen_state = targets[..., 2:].argmax(-1)
        valid_stroke = (targets[..., -1] != 1.0).float()
        mixture_weights = th.nn.functional.softmax(mixture_logits, -1)
        pdfs = gaussian_pdfs(dx, dy, gaussian_params)
        position_loss = -th.log(self.eps + (pdfs * mixture_weights).sum(-1))
        position_loss = (position_loss * valid_stroke).sum(
            ) / valid_stroke.sum()
        pen_loss = th.nn.functional.cross_entropy(pen_logits.view(-1, 3),
            pen_state.view(-1))
        return position_loss + pen_loss


def get_inputs():
    return [torch.rand([4, 3]), torch.rand([4, 4]), torch.rand([4, 5]),
        torch.rand([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 numpy as np
import torch as th
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__softmax_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
    ):
    xnumel = 16
    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_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), 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 + x2, tmp9, xmask)


@triton.jit
def triton_poi_fused__softmax_add_div_exp_mul_neg_pow_rsub_sqrt_sub_tanh_1(
    in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, xnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 4
    x0 = xindex % 4
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + 5 * x0, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr1 + (2 + 5 * x0), xmask, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr1 + (1 + 5 * x0), xmask, eviction_policy='evict_last')
    tmp10 = tl.load(in_ptr1 + (3 + 5 * x0), xmask, eviction_policy='evict_last'
        )
    tmp15 = tl.load(in_ptr1 + (4 + 5 * x0), xmask, eviction_policy='evict_last'
        )
    tmp37 = tl.load(in_ptr2 + x2, xmask)
    tmp38 = tl.load(in_ptr2 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp39 = tl.load(in_ptr2 + (1 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp41 = tl.load(in_ptr2 + (2 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp43 = tl.load(in_ptr2 + (3 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp2 = tmp0 - tmp1
    tmp4 = tl_math.exp(tmp3)
    tmp5 = tmp2 / tmp4
    tmp6 = tmp5 * tmp5
    tmp9 = tmp7 - tmp8
    tmp11 = tl_math.exp(tmp10)
    tmp12 = tmp9 / tmp11
    tmp13 = tmp12 * tmp12
    tmp14 = tmp6 + tmp13
    tmp16 = libdevice.tanh(tmp15)
    tmp17 = 2.0
    tmp18 = tmp16 * tmp17
    tmp19 = tmp2 * tmp9
    tmp20 = tmp4 * tmp11
    tmp21 = tmp19 / tmp20
    tmp22 = tmp18 * tmp21
    tmp23 = tmp14 - tmp22
    tmp24 = -tmp23
    tmp25 = tmp16 * tmp16
    tmp26 = 1.0
    tmp27 = tmp26 - tmp25
    tmp28 = tmp27 * tmp17
    tmp29 = tmp24 / tmp28
    tmp30 = tl_math.exp(tmp29)
    tmp31 = 6.283185307179586
    tmp32 = tmp4 * tmp31
    tmp33 = tmp32 * tmp11
    tmp34 = libdevice.sqrt(tmp27)
    tmp35 = tmp33 * tmp34
    tmp36 = tmp30 / tmp35
    tmp40 = tmp38 + tmp39
    tmp42 = tmp40 + tmp41
    tmp44 = tmp42 + tmp43
    tmp45 = tmp37 / tmp44
    tmp46 = tmp36 * tmp45
    tl.store(in_out_ptr0 + x2, tmp46, xmask)


@triton.jit
def triton_per_fused__log_softmax__to_copy_add_argmax_div_log_mul_ne_neg_nll_loss_forward_sum_view_2(
    in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, xnumel, rnumel, XBLOCK: tl.
    constexpr):
    RBLOCK: tl.constexpr = 4
    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 + (2 + 4 * r0), None, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr0 + (3 + 4 * r0), None, eviction_policy='evict_last')
    tmp26 = tl.load(in_ptr1 + 3 * r0, None, eviction_policy='evict_last')
    tmp27 = tl.load(in_ptr1 + (1 + 3 * r0), None, eviction_policy='evict_last')
    tmp29 = tl.load(in_ptr1 + (2 + 3 * r0), None, eviction_policy='evict_last')
    tmp42 = tl.load(in_ptr2 + 4 * r0, None, eviction_policy='evict_last')
    tmp43 = tl.load(in_ptr2 + (1 + 4 * r0), None, eviction_policy='evict_last')
    tmp45 = tl.load(in_ptr2 + (2 + 4 * r0), None, eviction_policy='evict_last')
    tmp47 = tl.load(in_ptr2 + (3 + 4 * r0), None, eviction_policy='evict_last')
    tmp2 = tmp0 > tmp1
    tmp3 = tmp0 == tmp1
    tmp4 = tmp0 != tmp0
    tmp5 = tmp1 != tmp1
    tmp6 = tmp4 > tmp5
    tmp7 = tmp2 | tmp6
    tmp8 = tmp4 & tmp5
    tmp9 = tmp3 | tmp8
    tmp10 = tl.full([1, 1], 0, tl.int64)
    tmp11 = tl.full([1, 1], 1, tl.int64)
    tmp12 = tmp10 < tmp11
    tmp13 = tmp9 & tmp12
    tmp14 = tmp7 | tmp13
    tl.where(tmp14, tmp0, tmp1)
    tmp16 = tl.where(tmp14, tmp10, tmp11)
    tmp17 = tl.full([1, 1], -100, tl.int64)
    tmp18 = tmp16 != tmp17
    tmp19 = tl.where(tmp18, tmp16, tmp10)
    tmp20 = tl.full([XBLOCK, RBLOCK], 3, tl.int32)
    tmp21 = tmp19 + tmp20
    tmp22 = tmp19 < 0
    tmp23 = tl.where(tmp22, tmp21, tmp19)
    tl.device_assert((0 <= tmp23) & (tmp23 < 3),
        'index out of bounds: 0 <= tmp23 < 3')
    tmp25 = tl.load(in_ptr1 + (tmp23 + 3 * r0), None, eviction_policy=
        'evict_last')
    tmp28 = triton_helpers.maximum(tmp26, tmp27)
    tmp30 = triton_helpers.maximum(tmp28, tmp29)
    tmp31 = tmp25 - tmp30
    tmp32 = tmp26 - tmp30
    tmp33 = tl_math.exp(tmp32)
    tmp34 = tmp27 - tmp30
    tmp35 = tl_math.exp(tmp34)
    tmp36 = tmp33 + tmp35
    tmp37 = tmp29 - tmp30
    tmp38 = tl_math.exp(tmp37)
    tmp39 = tmp36 + tmp38
    tmp40 = tl_math.log(tmp39)
    tmp41 = tmp31 - tmp40
    tmp44 = tmp42 + tmp43
    tmp46 = tmp44 + tmp45
    tmp48 = tmp46 + tmp47
    tmp49 = 1e-05
    tmp50 = tmp48 + tmp49
    tmp51 = tl_math.log(tmp50)
    tmp52 = -tmp51
    tmp53 = 1.0
    tmp54 = tmp1 != tmp53
    tmp55 = tmp54.to(tl.float32)
    tmp56 = tmp52 * tmp55
    tmp57 = tl.broadcast_to(tmp56, [XBLOCK, RBLOCK])
    tmp59 = tl.sum(tmp57, 1)[:, None]
    tmp60 = tl.broadcast_to(tmp55, [XBLOCK, RBLOCK])
    tmp62 = tl.sum(tmp60, 1)[:, None]
    tmp63 = -tmp41
    tmp64 = 0.0
    tmp65 = tl.where(tmp18, tmp63, tmp64)
    tmp66 = tl.broadcast_to(tmp65, [XBLOCK, RBLOCK])
    tmp68 = tl.sum(tmp66, 1)[:, None]
    tmp69 = tmp18.to(tl.int64)
    tmp70 = tl.broadcast_to(tmp69, [XBLOCK, RBLOCK])
    tmp72 = tl.sum(tmp70, 1)[:, None]
    tmp73 = tmp59 / tmp62
    tmp74 = tmp72.to(tl.float32)
    tmp75 = tmp68 / tmp74
    tmp76 = tmp73 + tmp75
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp76, None)


def call(args):
    arg0_1, arg1_1, arg2_1, arg3_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4), (4, 1))
    assert_size_stride(arg1_1, (4, 4), (4, 1))
    assert_size_stride(arg2_1, (4, 5), (5, 1))
    assert_size_stride(arg3_1, (4, 3), (3, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused__softmax_0[grid(16)](arg1_1, buf1, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        del arg1_1
        buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf2 = buf0
        del buf0
        triton_poi_fused__softmax_add_div_exp_mul_neg_pow_rsub_sqrt_sub_tanh_1[
            grid(16)](buf2, arg0_1, arg2_1, buf1, 16, XBLOCK=16, num_warps=
            1, num_stages=1)
        del arg2_1
        del buf1
        buf3 = empty_strided_cuda((), (), torch.float32)
        buf8 = buf3
        del buf3
        triton_per_fused__log_softmax__to_copy_add_argmax_div_log_mul_ne_neg_nll_loss_forward_sum_view_2[
            grid(1)](buf8, arg0_1, arg3_1, buf2, 1, 4, XBLOCK=1, num_warps=
            2, num_stages=1)
        del arg0_1
        del arg3_1
        del buf2
    return buf8,


def gaussian_pdfs(dx, dy, params):
    """Returns the pdf at (dx, dy) for each Gaussian in the mixture.
    """
    dx = dx.unsqueeze(-1)
    dy = dy.unsqueeze(-1)
    mu_x = params[..., 0]
    mu_y = params[..., 1]
    sigma_x = params[..., 2].exp()
    sigma_y = params[..., 3].exp()
    rho_xy = th.tanh(params[..., 4])
    x = ((dx - mu_x) / sigma_x).pow(2)
    y = ((dy - mu_y) / sigma_y).pow(2)
    xy = (dx - mu_x) * (dy - mu_y) / (sigma_x * sigma_y)
    arg = x + y - 2.0 * rho_xy * xy
    pdf = th.exp(-arg / (2 * (1.0 - rho_xy.pow(2))))
    norm = 2.0 * np.pi * sigma_x * sigma_y * (1.0 - rho_xy.pow(2)).sqrt()
    return pdf / norm


class GaussianMixtureReconstructionLossNew(th.nn.Module):
    """
    Args:
    """

    def __init__(self, eps=1e-05):
        super(GaussianMixtureReconstructionLossNew, self).__init__()
        self.eps = eps

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