import torch
from torch import device
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 numpy as np
import torch.nn as nn
import torch.utils.data
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda


@triton.jit
def triton_per_fused_add_exp_mul_pow_sub_sum_0(in_out_ptr0, in_ptr0,
    in_ptr1, in_ptr2, out_ptr0, 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 + r0, None)
    tmp1 = tl.load(in_ptr1 + r0, None)
    tmp3 = tl.load(in_ptr2 + r0, None)
    tmp2 = tl_math.exp(tmp1)
    tmp4 = tmp2 * tmp3
    tmp5 = tmp0 + tmp4
    tmp6 = tmp3 * tmp3
    tmp7 = 0.5
    tmp8 = tmp6 * tmp7
    tmp9 = tmp1 + tmp8
    tmp10 = tl.broadcast_to(tmp9, [XBLOCK, RBLOCK])
    tmp12 = tl.sum(tmp10, 1)[:, None]
    tmp13 = -3.6757541328186907
    tmp14 = tmp13 - tmp12
    tl.store(out_ptr0 + tl.broadcast_to(r0, [XBLOCK, RBLOCK]), tmp5, None)
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp14, None)


def call(args):
    primals_1, primals_2 = args
    args.clear()
    assert_size_stride(primals_1, (1, 4), (4, 1))
    assert_size_stride(primals_2, (1, 4), (4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = torch.ops.aten.randn.default([1, 4], device=device(type=
            'cuda', index=0), pin_memory=False)
        buf1 = buf0
        del buf0
        buf2 = empty_strided_cuda((1, 4), (4, 1), torch.float32)
        buf3 = empty_strided_cuda((1,), (1,), torch.float32)
        buf4 = buf3
        del buf3
        get_raw_stream(0)
        triton_per_fused_add_exp_mul_pow_sub_sum_0[grid(1)](buf4, primals_1,
            primals_2, buf1, buf2, 1, 4, XBLOCK=1, num_warps=2, num_stages=1)
        del primals_1
    return buf2, buf4, primals_2, buf1


class BaseDistribution(nn.Module):
    """
    Base distribution of a flow-based model
    Parameters do not depend of target variable (as is the case for a VAE encoder)
    """

    def __init__(self):
        super().__init__()

    def forward(self, num_samples=1):
        """
        Samples from base distribution and calculates log probability
        :param num_samples: Number of samples to draw from the distriubtion
        :return: Samples drawn from the distribution, log probability
        """
        raise NotImplementedError

    def log_prob(self, z):
        """
        Calculate log probability of batch of samples
        :param z: Batch of random variables to determine log probability for
        :return: log probability for each batch element
        """
        raise NotImplementedError


class DiagGaussianNew(BaseDistribution):
    """
    Multivariate Gaussian distribution with diagonal covariance matrix
    """

    def __init__(self, d):
        """
        Constructor
        :param d: Dimension of Gaussian distribution
        """
        super().__init__()
        self.d = d
        self.loc = nn.Parameter(torch.zeros(1, self.d))
        self.log_scale = nn.Parameter(torch.zeros(1, self.d))

    def log_prob(self, z):
        log_p = -0.5 * self.d * np.log(2 * np.pi) - torch.sum(self.
            log_scale + 0.5 * torch.pow((z - self.loc) / torch.exp(self.
            log_scale), 2), 1)
        return log_p

    def forward(self):
        primals_1 = self.loc
        primals_2 = self.log_scale
        output = call([primals_1, primals_2])
        return output[0], output[1]


Measured latency_ms: 0.025815