# 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/ze/czetl2lnlymajgf44kwgzndhmj7dfbh53efotptquu26u7kszbuy.py
# Topologically Sorted Source Nodes: [sub, mul, sub_1, mul_1, sub_2, mul_2], Original ATen: [aten.sub, aten.mul]
# Source node to ATen node mapping:
#   mul => mul_1
#   mul_1 => mul_3
#   mul_2 => mul_5
#   sub => sub
#   sub_1 => sub_1
#   sub_2 => sub_2
# Graph fragment:
#   %sub : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%expand, %expand_1), kwargs = {})
#   %mul_1 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%unsqueeze_4, -0.5), kwargs = {})
#   %sub_1 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%expand_8, %expand_9), kwargs = {})
#   %mul_3 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%unsqueeze_11, -0.5), kwargs = {})
#   %sub_2 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%expand_16, %expand_17), kwargs = {})
#   %mul_5 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%unsqueeze_18, -0.5), kwargs = {})
triton_poi_fused_mul_sub_0 = async_compile.triton('triton_poi_fused_mul_sub_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=[1024], 
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '*fp32', 3: '*fp32', 4: '*fp32', 5: '*fp32', 6: '*fp32', 7: '*fp32', 8: '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, 6, 7, 8), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused_mul_sub_0', '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_mul_sub_0(in_ptr0, in_ptr1, out_ptr0, out_ptr1, out_ptr2, out_ptr3, out_ptr4, out_ptr5, xnumel, XBLOCK : tl.constexpr):
    xnumel = 1024
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 4
    x4 = (xindex // 16)
    x3 = (xindex // 64)
    x5 = xindex % 16
    x6 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + (4*x4)), xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr0 + (x5 + (16*x3)), xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr1 + (x0 + (4*x4)), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr1 + (x5 + (16*x3)), xmask, eviction_policy='evict_last')
    tmp2 = tmp0 - tmp1
    tmp3 = -0.5
    tmp4 = tmp2 * tmp3
    tmp7 = tmp5 - tmp6
    tmp8 = tmp7 * tmp3
    tmp9 = tmp0 - tmp6
    tmp10 = tmp9 * tmp3
    tl.store(out_ptr0 + (x6), tmp4, xmask)
    tl.store(out_ptr1 + (x6), tmp2, xmask)
    tl.store(out_ptr2 + (x6), tmp8, xmask)
    tl.store(out_ptr3 + (x6), tmp7, xmask)
    tl.store(out_ptr4 + (x6), tmp10, xmask)
    tl.store(out_ptr5 + (x6), tmp9, xmask)
''', device_str='cuda')


# kernel path: runs/run_shard_4/inductor_cache/dc/cdccm75rscnek67vl76op7chvgrztmrcqbicbtpdhlpzrniyw6ne.py
# Topologically Sorted Source Nodes: [sigma_inv, sigma_inv_1, sigma_inv_2], Original ATen: [aten.diag_embed]
# Source node to ATen node mapping:
#   sigma_inv => full_default, where
#   sigma_inv_1 => full_default_1, where_1
#   sigma_inv_2 => full_default_2, where_2
# Graph fragment:
#   %full_default : [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 : [num_users=1] = call_function[target=torch.ops.aten.where.self](args = (%view, %permute, %full_default), 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 = (%view_7, %permute_2, %full_default_1), kwargs = {})
#   %full_default_2 : [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_2 : [num_users=1] = call_function[target=torch.ops.aten.where.self](args = (%view_14, %permute_4, %full_default_2), kwargs = {})
triton_poi_fused_diag_embed_1 = async_compile.triton('triton_poi_fused_diag_embed_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=[4096], 
    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_diag_embed_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_diag_embed_1(in_ptr0, in_ptr1, out_ptr0, out_ptr1, out_ptr2, xnumel, XBLOCK : tl.constexpr):
    xnumel = 4096
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = tl.full([XBLOCK], True, tl.int1)
    x0 = xindex % 4
    x1 = (xindex // 4) % 4
    x6 = (xindex // 64)
    x2 = (xindex // 16) % 4
    x4 = (xindex // 256)
    x7 = xindex
    tmp3 = tl.load(in_ptr0 + (x0 + (4*x6)), None, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (x0 + (4*x2) + (16*x4)), None, eviction_policy='evict_last')
    tmp12 = tl.load(in_ptr1 + (x0 + (4*x6)), None, eviction_policy='evict_last')
    tmp13 = tl.load(in_ptr1 + (x0 + (4*x2) + (16*x4)), None, eviction_policy='evict_last')
    tmp0 = x0
    tmp1 = x1
    tmp2 = tmp0 == tmp1
    tmp5 = tmp3 + tmp4
    tmp6 = tl.full([1], 1, tl.int32)
    tmp7 = tmp6 / tmp5
    tmp8 = 1.0
    tmp9 = tmp7 * tmp8
    tmp10 = 0.0
    tmp11 = tl.where(tmp2, tmp9, tmp10)
    tmp14 = tmp12 + tmp13
    tmp15 = tmp6 / tmp14
    tmp16 = tmp15 * tmp8
    tmp17 = tl.where(tmp2, tmp16, tmp10)
    tmp18 = tmp3 + tmp13
    tmp19 = tmp6 / tmp18
    tmp20 = tmp19 * tmp8
    tmp21 = tl.where(tmp2, tmp20, tmp10)
    tl.store(out_ptr0 + (x7), tmp11, None)
    tl.store(out_ptr1 + (x7), tmp17, None)
    tl.store(out_ptr2 + (x7), tmp21, None)
''', device_str='cuda')


# kernel path: runs/run_shard_4/inductor_cache/nk/cnk3jqn6qx3e35kewqnmgkp63i5s2tlqppjouxpbe6xzm2eri6lj.py
# Topologically Sorted Source Nodes: [mul_3, mul_4, add_3, mul_5, mul_6, L2, L2_1], Original ATen: [aten.mul, aten.add, aten.sub, aten.sum]
# Source node to ATen node mapping:
#   L2 => sub_3
#   L2_1 => sum_1
#   add_3 => add_3
#   mul_3 => mul_6
#   mul_4 => mul_7
#   mul_5 => mul_8
#   mul_6 => mul_9
# Graph fragment:
#   %mul_6 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%view_23, %select_1), kwargs = {})
#   %mul_7 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%view_26, %select_3), kwargs = {})
#   %add_3 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%mul_6, %mul_7), kwargs = {})
#   %mul_8 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%view_29, 2), kwargs = {})
#   %mul_9 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%mul_8, %select_5), kwargs = {})
#   %sub_3 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%add_3, %mul_9), kwargs = {})
#   %sum_1 : [num_users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%sub_3, [-2, -1]), kwargs = {})
triton_per_fused_add_mul_sub_sum_2 = async_compile.triton('triton_per_fused_add_mul_sub_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=[64, 16],
    reduction_hint=ReductionHint.INNER,
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '*fp32', 3: '*fp32', 4: '*fp32', 5: '*fp32', 6: '*fp32', 7: 'i32', 8: '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, 6, 7, 8), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_per_fused_add_mul_sub_sum_2', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 6, '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_add_mul_sub_sum_2(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, out_ptr0, xnumel, rnumel, XBLOCK : tl.constexpr):
    xnumel = 64
    rnumel = 16
    RBLOCK: tl.constexpr = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rindex = tl.arange(0, RBLOCK)[None, :]
    roffset = 0
    rmask = tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r2 = rindex
    x3 = xindex
    x0 = xindex % 16
    tmp0 = tl.load(in_ptr0 + (r2 + (16*x3)), xmask, other=0.0)
    tmp1 = tl.load(in_ptr1 + (r2 + (16*x0)), xmask, eviction_policy='evict_last', other=0.0)
    tmp4 = tl.load(in_ptr2 + (r2 + (16*x3)), xmask, other=0.0)
    tmp5 = tl.load(in_ptr3 + (r2 + (16*x0)), xmask, eviction_policy='evict_last', other=0.0)
    tmp9 = tl.load(in_ptr4 + (r2 + (16*x3)), xmask, other=0.0)
    tmp12 = tl.load(in_ptr5 + (r2 + (16*x0)), xmask, eviction_policy='evict_last', other=0.0)
    tmp2 = tl_math.exp(tmp1)
    tmp3 = tmp0 * tmp2
    tmp6 = tl_math.exp(tmp5)
    tmp7 = tmp4 * tmp6
    tmp8 = tmp3 + tmp7
    tmp10 = 2.0
    tmp11 = tmp9 * tmp10
    tmp13 = tl_math.exp(tmp12)
    tmp14 = tmp11 * tmp13
    tmp15 = tmp8 - tmp14
    tmp16 = tl.broadcast_to(tmp15, [XBLOCK, RBLOCK])
    tmp18 = tl.where(xmask, tmp16, 0)
    tmp19 = tl.sum(tmp18, 1)[:, None]
    tl.store(out_ptr0 + (x3), tmp19, xmask)
''', device_str='cuda')


# kernel path: runs/run_shard_4/inductor_cache/j3/cj3rjanwhrokncxqzelpshcwemj6ytvab74b32utvu3rezmkjl52.py
# Topologically Sorted Source Nodes: [L2_2], Original ATen: [aten.mean]
# Source node to ATen node mapping:
#   L2_2 => mean
# Graph fragment:
#   %mean : [num_users=1] = call_function[target=torch.ops.aten.mean.default](args = (%sum_1,), kwargs = {})
triton_per_fused_mean_3 = async_compile.triton('triton_per_fused_mean_3', '''
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_mean_3', 'mutated_arg_names': ['in_out_ptr0'], 'no_x_dim': False, 'num_load': 1, '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_mean_3(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 + (r0), None)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = tl.sum(tmp1, 1)[:, None]
    tmp4 = 64.0
    tmp5 = tmp3 / tmp4
    tl.debug_barrier()
    tl.store(in_out_ptr0 + (tl.full([XBLOCK, 1], 0, tl.int32)), tmp5, None)
''', device_str='cuda')


async_compile.wait(globals())
del async_compile

def call(args):
    arg0_1, arg1_1, arg2_1, arg3_1, arg4_1, arg5_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))
    assert_size_stride(arg2_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg3_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg4_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg5_1, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((64, 4, 4), (16, 4, 1), torch.float32)
        # Topologically Sorted Source Nodes: [w1_w1_cross], Original ATen: [aten.bmm]
        extern_kernels.bmm(reinterpret_tensor(arg4_1, (64, 4, 1), (4, 1, 1), 0), reinterpret_tensor(arg4_1, (64, 1, 4), (4, 4, 1), 0), out=buf0)
        buf1 = empty_strided_cuda((4, 4, 4, 4, 1, 4), (256, 64, 16, 4, 4, 1), torch.float32)
        buf4 = empty_strided_cuda((4, 4, 4, 4, 4), (256, 64, 16, 4, 1), torch.float32)
        buf7 = empty_strided_cuda((4, 4, 4, 4, 1, 4), (256, 64, 16, 4, 4, 1), torch.float32)
        buf10 = empty_strided_cuda((4, 4, 4, 4, 4), (256, 64, 16, 4, 1), torch.float32)
        buf13 = empty_strided_cuda((4, 4, 4, 4, 1, 4), (256, 64, 16, 4, 4, 1), torch.float32)
        buf16 = empty_strided_cuda((4, 4, 4, 4, 4), (256, 64, 16, 4, 1), torch.float32)
        # Topologically Sorted Source Nodes: [sub, mul, sub_1, mul_1, sub_2, mul_2], Original ATen: [aten.sub, aten.mul]
        stream0 = get_raw_stream(0)
        triton_poi_fused_mul_sub_0.run(arg0_1, arg2_1, buf1, buf4, buf7, buf10, buf13, buf16, 1024, grid=grid(1024), stream=stream0)
        del arg0_1
        del arg2_1
        buf2 = empty_strided_cuda((4, 4, 4, 4, 4, 4), (1024, 256, 64, 16, 4, 1), torch.float32)
        buf8 = empty_strided_cuda((4, 4, 4, 4, 4, 4), (1024, 256, 64, 16, 4, 1), torch.float32)
        buf14 = empty_strided_cuda((4, 4, 4, 4, 4, 4), (1024, 256, 64, 16, 4, 1), torch.float32)
        # Topologically Sorted Source Nodes: [sigma_inv, sigma_inv_1, sigma_inv_2], Original ATen: [aten.diag_embed]
        triton_poi_fused_diag_embed_1.run(arg1_1, arg3_1, buf2, buf8, buf14, 4096, grid=grid(4096), stream=stream0)
        del arg1_1
        del arg3_1
        buf3 = empty_strided_cuda((256, 1, 4), (4, 4, 1), torch.float32)
        # Topologically Sorted Source Nodes: [matmul], Original ATen: [aten.bmm]
        extern_kernels.bmm(reinterpret_tensor(buf1, (256, 1, 4), (4, 0, 1), 0), reinterpret_tensor(buf2, (256, 4, 4), (16, 4, 1), 0), out=buf3)
        del buf1
        del buf2
        buf5 = empty_strided_cuda((256, 1, 1), (1, 1, 1), torch.float32)
        # Topologically Sorted Source Nodes: [matmul_1], Original ATen: [aten.bmm]
        extern_kernels.bmm(buf3, reinterpret_tensor(buf4, (256, 4, 1), (4, 1, 0), 0), out=buf5)
        buf6 = reinterpret_tensor(buf4, (64, 4, 4), (16, 4, 1), 0); del buf4  # reuse
        # Topologically Sorted Source Nodes: [w2_w2_cross], Original ATen: [aten.bmm]
        extern_kernels.bmm(reinterpret_tensor(arg5_1, (64, 4, 1), (4, 1, 1), 0), reinterpret_tensor(arg5_1, (64, 1, 4), (4, 4, 1), 0), out=buf6)
        buf9 = buf3; del buf3  # reuse
        # Topologically Sorted Source Nodes: [matmul_2], Original ATen: [aten.bmm]
        extern_kernels.bmm(reinterpret_tensor(buf7, (256, 1, 4), (4, 0, 1), 0), reinterpret_tensor(buf8, (256, 4, 4), (16, 4, 1), 0), out=buf9)
        del buf7
        del buf8
        buf11 = empty_strided_cuda((256, 1, 1), (1, 1, 1), torch.float32)
        # Topologically Sorted Source Nodes: [matmul_3], Original ATen: [aten.bmm]
        extern_kernels.bmm(buf9, reinterpret_tensor(buf10, (256, 4, 1), (4, 1, 0), 0), out=buf11)
        buf12 = reinterpret_tensor(buf9, (64, 4, 4), (16, 4, 1), 0); del buf9  # reuse
        # Topologically Sorted Source Nodes: [w1_w2_cross], Original ATen: [aten.bmm]
        extern_kernels.bmm(reinterpret_tensor(arg4_1, (64, 4, 1), (4, 1, 1), 0), reinterpret_tensor(arg5_1, (64, 1, 4), (4, 4, 1), 0), out=buf12)
        del arg4_1
        del arg5_1
        buf15 = reinterpret_tensor(buf10, (256, 1, 4), (4, 4, 1), 0); del buf10  # reuse
        # Topologically Sorted Source Nodes: [matmul_4], Original ATen: [aten.bmm]
        extern_kernels.bmm(reinterpret_tensor(buf13, (256, 1, 4), (4, 0, 1), 0), reinterpret_tensor(buf14, (256, 4, 4), (16, 4, 1), 0), out=buf15)
        del buf13
        del buf14
        buf17 = empty_strided_cuda((256, 1, 1), (1, 1, 1), torch.float32)
        # Topologically Sorted Source Nodes: [matmul_5], Original ATen: [aten.bmm]
        extern_kernels.bmm(buf15, reinterpret_tensor(buf16, (256, 4, 1), (4, 1, 0), 0), out=buf17)
        del buf15
        del buf16
        buf18 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        # Topologically Sorted Source Nodes: [mul_3, mul_4, add_3, mul_5, mul_6, L2, L2_1], Original ATen: [aten.mul, aten.add, aten.sub, aten.sum]
        triton_per_fused_add_mul_sub_sum_2.run(buf0, buf5, buf6, buf11, buf12, buf17, buf18, 64, 16, grid=grid(64), stream=stream0)
        del buf0
        del buf11
        del buf12
        del buf17
        del buf5
        del buf6
        buf19 = empty_strided_cuda((), (), torch.float32)
        buf20 = buf19; del buf19  # reuse
        # Topologically Sorted Source Nodes: [L2_2], Original ATen: [aten.mean]
        triton_per_fused_mean_3.run(buf20, buf18, 1, 64, grid=grid(1), stream=stream0)
        del buf18
    return (buf20, )


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)
    arg2_1 = rand_strided((4, 4, 4, 4), (64, 16, 4, 1), device='cuda:0', dtype=torch.float32)
    arg3_1 = rand_strided((4, 4, 4, 4), (64, 16, 4, 1), device='cuda:0', dtype=torch.float32)
    arg4_1 = rand_strided((4, 4, 4, 4), (64, 16, 4, 1), device='cuda:0', dtype=torch.float32)
    arg5_1 = rand_strided((4, 4, 4, 4), (64, 16, 4, 1), device='cuda:0', dtype=torch.float32)
    fn = lambda: call([arg0_1, arg1_1, arg2_1, arg3_1, arg4_1, arg5_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
import torch.nn as nn


def gaussian_prob_density(x, mu, sigma, normalized=False):
    if normalized:
        k = mu.shape[-1]
        scaler = (2 * math.pi) ** (-k / 2)
        sigma_det = torch.prod(sigma, dim=-1) ** -0.5
    bias = (x - mu).unsqueeze(-2)
    sigma_inv = torch.diag_embed(1 / sigma)
    exp = torch.exp(-0.5 * bias @ sigma_inv @ bias.transpose(-1, -2))
    if normalized:
        return (scaler * sigma_det * exp)[..., 0, 0]
    else:
        return exp[..., 0, 0]


class EuclideanGMM(nn.Module):

    def __init__(self, reduction='mean'):
        super().__init__()
        self.reduction = reduction

    def forward(self, w1, mu1, sigma1, w2, mu2, sigma2):
        """Compute Euclidean distance between two mixtures of Gaussian

        Parameters
        ----------
        w1/w2 : torch.Tensor
            (*, M, N) where * is one or more batch dimensions. The weight for the GMM
        mu1/mu2 : torch.Tensor
            (*, M, N) where * is one or more batch dimensions. The mean vectors for the GMM
        sigma1/sigma2: torch.Tensor
            (*, M, N) where * is one or more batch dimensions. The diagonal covariance matrix for the GMM
        
        Returns
        ----------
        torch.Tensor
        """
        density_1 = EuclideanGMM.gaussian_cross_product(mu1, sigma1, mu1,
            sigma1)
        density_2 = EuclideanGMM.gaussian_cross_product(mu2, sigma2, mu2,
            sigma2)
        density_x = EuclideanGMM.gaussian_cross_product(mu1, sigma1, mu2,
            sigma2)
        w1_w1_cross = w1.unsqueeze(-1) @ w1.unsqueeze(-2)
        w2_w2_cross = w2.unsqueeze(-1) @ w2.unsqueeze(-2)
        w1_w2_cross = w1.unsqueeze(-1) @ w2.unsqueeze(-2)
        L2 = (w1_w1_cross * density_1 + w2_w2_cross * density_2 - 2 *
            w1_w2_cross * density_x)
        L2 = torch.sum(L2, dim=[-2, -1])
        if self.reduction == 'sum':
            L2 = torch.sum(L2)
        elif self.reduction == 'mean':
            L2 = torch.mean(L2)
        return L2

    @staticmethod
    def gaussian_cross_product(mu1, sigma1, mu2, sigma2):
        n_gauss_1 = mu1.shape[-2]
        n_gauss_2 = mu2.shape[-2]
        batch_size = mu1.shape[:-2]
        mu1 = mu1.unsqueeze(-2).expand(*batch_size, -1, n_gauss_2, -1)
        mu2 = mu2.unsqueeze(-3).expand(*batch_size, n_gauss_1, -1, -1)
        sigma1 = sigma1.unsqueeze(-2).expand(*batch_size, -1, n_gauss_2, -1)
        sigma2 = sigma2.unsqueeze(-3).expand(*batch_size, n_gauss_1, -1, -1)
        return gaussian_prob_density(mu1, mu2, sigma1 + sigma2)


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


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

import torch
from torch._inductor.select_algorithm import extern_kernels
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.triton_helpers import 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
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor


@triton.jit
def triton_poi_fused_mul_sub_0(in_ptr0, in_ptr1, out_ptr0, out_ptr1,
    out_ptr2, out_ptr3, out_ptr4, out_ptr5, xnumel, XBLOCK: tl.constexpr):
    xnumel = 1024
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 4
    x4 = xindex // 16
    x3 = xindex // 64
    x5 = xindex % 16
    x6 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 4 * x4), xmask, eviction_policy='evict_last'
        )
    tmp1 = tl.load(in_ptr0 + (x5 + 16 * x3), xmask, eviction_policy=
        'evict_last')
    tmp5 = tl.load(in_ptr1 + (x0 + 4 * x4), xmask, eviction_policy='evict_last'
        )
    tmp6 = tl.load(in_ptr1 + (x5 + 16 * x3), xmask, eviction_policy=
        'evict_last')
    tmp2 = tmp0 - tmp1
    tmp3 = -0.5
    tmp4 = tmp2 * tmp3
    tmp7 = tmp5 - tmp6
    tmp8 = tmp7 * tmp3
    tmp9 = tmp0 - tmp6
    tmp10 = tmp9 * tmp3
    tl.store(out_ptr0 + x6, tmp4, xmask)
    tl.store(out_ptr1 + x6, tmp2, xmask)
    tl.store(out_ptr2 + x6, tmp8, xmask)
    tl.store(out_ptr3 + x6, tmp7, xmask)
    tl.store(out_ptr4 + x6, tmp10, xmask)
    tl.store(out_ptr5 + x6, tmp9, xmask)


@triton.jit
def triton_poi_fused_diag_embed_1(in_ptr0, in_ptr1, out_ptr0, out_ptr1,
    out_ptr2, xnumel, XBLOCK: tl.constexpr):
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    tl.full([XBLOCK], True, tl.int1)
    x0 = xindex % 4
    x1 = xindex // 4 % 4
    x6 = xindex // 64
    x2 = xindex // 16 % 4
    x4 = xindex // 256
    x7 = xindex
    tmp3 = tl.load(in_ptr0 + (x0 + 4 * x6), None, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (x0 + 4 * x2 + 16 * x4), None, eviction_policy
        ='evict_last')
    tmp12 = tl.load(in_ptr1 + (x0 + 4 * x6), None, eviction_policy='evict_last'
        )
    tmp13 = tl.load(in_ptr1 + (x0 + 4 * x2 + 16 * x4), None,
        eviction_policy='evict_last')
    tmp0 = x0
    tmp1 = x1
    tmp2 = tmp0 == tmp1
    tmp5 = tmp3 + tmp4
    tmp6 = tl.full([1], 1, tl.int32)
    tmp7 = tmp6 / tmp5
    tmp8 = 1.0
    tmp9 = tmp7 * tmp8
    tmp10 = 0.0
    tmp11 = tl.where(tmp2, tmp9, tmp10)
    tmp14 = tmp12 + tmp13
    tmp15 = tmp6 / tmp14
    tmp16 = tmp15 * tmp8
    tmp17 = tl.where(tmp2, tmp16, tmp10)
    tmp18 = tmp3 + tmp13
    tmp19 = tmp6 / tmp18
    tmp20 = tmp19 * tmp8
    tmp21 = tl.where(tmp2, tmp20, tmp10)
    tl.store(out_ptr0 + x7, tmp11, None)
    tl.store(out_ptr1 + x7, tmp17, None)
    tl.store(out_ptr2 + x7, tmp21, None)


@triton.jit
def triton_per_fused_add_mul_sub_sum_2(in_ptr0, in_ptr1, in_ptr2, in_ptr3,
    in_ptr4, in_ptr5, out_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    RBLOCK: tl.constexpr = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r2 = rindex
    x3 = xindex
    x0 = xindex % 16
    tmp0 = tl.load(in_ptr0 + (r2 + 16 * x3), xmask, other=0.0)
    tmp1 = tl.load(in_ptr1 + (r2 + 16 * x0), xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp4 = tl.load(in_ptr2 + (r2 + 16 * x3), xmask, other=0.0)
    tmp5 = tl.load(in_ptr3 + (r2 + 16 * x0), xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp9 = tl.load(in_ptr4 + (r2 + 16 * x3), xmask, other=0.0)
    tmp12 = tl.load(in_ptr5 + (r2 + 16 * x0), xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp2 = tl_math.exp(tmp1)
    tmp3 = tmp0 * tmp2
    tmp6 = tl_math.exp(tmp5)
    tmp7 = tmp4 * tmp6
    tmp8 = tmp3 + tmp7
    tmp10 = 2.0
    tmp11 = tmp9 * tmp10
    tmp13 = tl_math.exp(tmp12)
    tmp14 = tmp11 * tmp13
    tmp15 = tmp8 - tmp14
    tmp16 = tl.broadcast_to(tmp15, [XBLOCK, RBLOCK])
    tmp18 = tl.where(xmask, tmp16, 0)
    tmp19 = tl.sum(tmp18, 1)[:, None]
    tl.store(out_ptr0 + x3, tmp19, xmask)


@triton.jit
def triton_per_fused_mean_3(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 + r0, None)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = tl.sum(tmp1, 1)[:, None]
    tmp4 = 64.0
    tmp5 = tmp3 / tmp4
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp5, None)


def call(args):
    arg0_1, arg1_1, arg2_1, arg3_1, arg4_1, arg5_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))
    assert_size_stride(arg2_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg3_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg4_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg5_1, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((64, 4, 4), (16, 4, 1), torch.float32)
        extern_kernels.bmm(reinterpret_tensor(arg4_1, (64, 4, 1), (4, 1, 1),
            0), reinterpret_tensor(arg4_1, (64, 1, 4), (4, 4, 1), 0), out=buf0)
        buf1 = empty_strided_cuda((4, 4, 4, 4, 1, 4), (256, 64, 16, 4, 4, 1
            ), torch.float32)
        buf4 = empty_strided_cuda((4, 4, 4, 4, 4), (256, 64, 16, 4, 1),
            torch.float32)
        buf7 = empty_strided_cuda((4, 4, 4, 4, 1, 4), (256, 64, 16, 4, 4, 1
            ), torch.float32)
        buf10 = empty_strided_cuda((4, 4, 4, 4, 4), (256, 64, 16, 4, 1),
            torch.float32)
        buf13 = empty_strided_cuda((4, 4, 4, 4, 1, 4), (256, 64, 16, 4, 4, 
            1), torch.float32)
        buf16 = empty_strided_cuda((4, 4, 4, 4, 4), (256, 64, 16, 4, 1),
            torch.float32)
        get_raw_stream(0)
        triton_poi_fused_mul_sub_0[grid(1024)](arg0_1, arg2_1, buf1, buf4,
            buf7, buf10, buf13, buf16, 1024, XBLOCK=256, num_warps=4,
            num_stages=1)
        del arg0_1
        del arg2_1
        buf2 = empty_strided_cuda((4, 4, 4, 4, 4, 4), (1024, 256, 64, 16, 4,
            1), torch.float32)
        buf8 = empty_strided_cuda((4, 4, 4, 4, 4, 4), (1024, 256, 64, 16, 4,
            1), torch.float32)
        buf14 = empty_strided_cuda((4, 4, 4, 4, 4, 4), (1024, 256, 64, 16, 
            4, 1), torch.float32)
        triton_poi_fused_diag_embed_1[grid(4096)](arg1_1, arg3_1, buf2,
            buf8, buf14, 4096, XBLOCK=128, num_warps=4, num_stages=1)
        del arg1_1
        del arg3_1
        buf3 = empty_strided_cuda((256, 1, 4), (4, 4, 1), torch.float32)
        extern_kernels.bmm(reinterpret_tensor(buf1, (256, 1, 4), (4, 0, 1),
            0), reinterpret_tensor(buf2, (256, 4, 4), (16, 4, 1), 0), out=buf3)
        del buf1
        del buf2
        buf5 = empty_strided_cuda((256, 1, 1), (1, 1, 1), torch.float32)
        extern_kernels.bmm(buf3, reinterpret_tensor(buf4, (256, 4, 1), (4, 
            1, 0), 0), out=buf5)
        buf6 = reinterpret_tensor(buf4, (64, 4, 4), (16, 4, 1), 0)
        del buf4
        extern_kernels.bmm(reinterpret_tensor(arg5_1, (64, 4, 1), (4, 1, 1),
            0), reinterpret_tensor(arg5_1, (64, 1, 4), (4, 4, 1), 0), out=buf6)
        buf9 = buf3
        del buf3
        extern_kernels.bmm(reinterpret_tensor(buf7, (256, 1, 4), (4, 0, 1),
            0), reinterpret_tensor(buf8, (256, 4, 4), (16, 4, 1), 0), out=buf9)
        del buf7
        del buf8
        buf11 = empty_strided_cuda((256, 1, 1), (1, 1, 1), torch.float32)
        extern_kernels.bmm(buf9, reinterpret_tensor(buf10, (256, 4, 1), (4,
            1, 0), 0), out=buf11)
        buf12 = reinterpret_tensor(buf9, (64, 4, 4), (16, 4, 1), 0)
        del buf9
        extern_kernels.bmm(reinterpret_tensor(arg4_1, (64, 4, 1), (4, 1, 1),
            0), reinterpret_tensor(arg5_1, (64, 1, 4), (4, 4, 1), 0), out=buf12
            )
        del arg4_1
        del arg5_1
        buf15 = reinterpret_tensor(buf10, (256, 1, 4), (4, 4, 1), 0)
        del buf10
        extern_kernels.bmm(reinterpret_tensor(buf13, (256, 1, 4), (4, 0, 1),
            0), reinterpret_tensor(buf14, (256, 4, 4), (16, 4, 1), 0), out=
            buf15)
        del buf13
        del buf14
        buf17 = empty_strided_cuda((256, 1, 1), (1, 1, 1), torch.float32)
        extern_kernels.bmm(buf15, reinterpret_tensor(buf16, (256, 4, 1), (4,
            1, 0), 0), out=buf17)
        del buf15
        del buf16
        buf18 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_per_fused_add_mul_sub_sum_2[grid(64)](buf0, buf5, buf6,
            buf11, buf12, buf17, buf18, 64, 16, XBLOCK=1, num_warps=2,
            num_stages=1)
        del buf0
        del buf11
        del buf12
        del buf17
        del buf5
        del buf6
        buf19 = empty_strided_cuda((), (), torch.float32)
        buf20 = buf19
        del buf19
        triton_per_fused_mean_3[grid(1)](buf20, buf18, 1, 64, XBLOCK=1,
            num_warps=2, num_stages=1)
        del buf18
    return buf20,


def gaussian_prob_density(x, mu, sigma, normalized=False):
    if normalized:
        k = mu.shape[-1]
        scaler = (2 * math.pi) ** (-k / 2)
        sigma_det = torch.prod(sigma, dim=-1) ** -0.5
    bias = (x - mu).unsqueeze(-2)
    sigma_inv = torch.diag_embed(1 / sigma)
    exp = torch.exp(-0.5 * bias @ sigma_inv @ bias.transpose(-1, -2))
    if normalized:
        return (scaler * sigma_det * exp)[..., 0, 0]
    else:
        return exp[..., 0, 0]


class EuclideanGMMNew(nn.Module):

    def __init__(self, reduction='mean'):
        super().__init__()
        self.reduction = reduction

    @staticmethod
    def gaussian_cross_product(mu1, sigma1, mu2, sigma2):
        n_gauss_1 = mu1.shape[-2]
        n_gauss_2 = mu2.shape[-2]
        batch_size = mu1.shape[:-2]
        mu1 = mu1.unsqueeze(-2).expand(*batch_size, -1, n_gauss_2, -1)
        mu2 = mu2.unsqueeze(-3).expand(*batch_size, n_gauss_1, -1, -1)
        sigma1 = sigma1.unsqueeze(-2).expand(*batch_size, -1, n_gauss_2, -1)
        sigma2 = sigma2.unsqueeze(-3).expand(*batch_size, n_gauss_1, -1, -1)
        return gaussian_prob_density(mu1, mu2, sigma1 + sigma2)

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