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]


Measured latency_ms: 0.160386