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]


Measured latency_ms: 0.01497