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 libdevice, math as tl_math
import abc
import math
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_softplus_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
    ):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + x0, xmask)
    tmp1 = 1.0
    tmp2 = tmp0 * tmp1
    tmp3 = 20.0
    tmp4 = tmp2 > tmp3
    tmp5 = tl_math.exp(tmp2)
    tmp6 = libdevice.log1p(tmp5)
    tmp7 = tmp6 * tmp1
    tmp8 = tl.where(tmp4, tmp0, tmp7)
    tl.store(out_ptr0 + x0, tmp8, xmask)


def call(args):
    primals_1, primals_2, primals_3, primals_4, primals_5 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4), (4, 1))
    assert_size_stride(primals_2, (4,), (1,))
    assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_4, (4, 4), (4, 1))
    assert_size_stride(primals_5, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_2, reinterpret_tensor(primals_3, (64, 
            4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 4), (1, 4), 0
            ), alpha=1, beta=1, out=buf0)
        del primals_1
        del primals_2
        buf1 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_5, reinterpret_tensor(primals_3, (64, 
            4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 4), (1, 4), 0
            ), alpha=1, beta=1, out=buf1)
        del primals_4
        del primals_5
        buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_softplus_0[grid(256)](buf1, buf2, 256, XBLOCK=256,
            num_warps=4, num_stages=1)
    return reinterpret_tensor(buf0, (4, 4, 4, 4), (64, 16, 4, 1), 0
        ), buf2, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0), buf1


class ProbabilisticLayer(torch.nn.Module, metaclass=abc.ABCMeta):
    """Probabilistic layer to be used by the encoder/decoder of a
    Variational AutoEncoder.

    """

    @abc.abstractmethod
    def forward(self, inputs):
        """Compute the parameters of the distribution conditioned on the
        input.

        Args:
            inputs (``torch.Tensor[N,dim]``): Conditional inputs.

        Returns:
            params (object): Parameters of the distribution.

        """
        pass

    @abc.abstractmethod
    def samples_and_llh(self, params, use_mean=False):
        """Samples using the reparameterization trick so that the each
        sample can be backpropagated trough. In addition, returns the
        log-likelihood of the samples given the sampling distribution.

        Args:
            params (object): Parameters of the sampling distribution.
            use_mean (boolean): If true, by pass the sampling and
                just return the mean value of the distribution.

        Returns:
            samples (``torch.Tensor``): Sampled values.
            llh (``torch.Tensor``): Log-likelihood for each sample.
        """
        pass

    @abc.abstractmethod
    def log_likelihood(self, data, params):
        """Log-likelihood of the data.

        Args:
            data (``torch.Tensor[N,dim]``): Data.
            params (object): Parameters of the distribution.

        """
        pass


class NormalDiagonalCovarianceLayerNew(ProbabilisticLayer):
    """Normal distribution with diagonal covariance matrix layer."""

    def __init__(self, dim_in, dim_out, variance_nonlinearity=None):
        super().__init__()
        self.mean = torch.nn.Linear(dim_in, dim_out)
        self.logvar = torch.nn.Linear(dim_in, dim_out)
        if variance_nonlinearity is None:
            variance_nonlinearity = torch.nn.Softplus()
        self.variance_nonlinearity = variance_nonlinearity

    def samples_and_llh(self, params, use_mean=False):
        means, variances = params
        if use_mean:
            samples = means
        else:
            dtype, device = means.dtype, means.device
            noise = torch.randn(*means.shape, dtype=dtype, device=device)
            std_dev = variances.sqrt()
            samples = means + std_dev * noise
        llhs = self.log_likelihood(samples, params)
        return samples, llhs

    def log_likelihood(self, data, params):
        means, variances = params
        dim = means.shape[-1]
        delta = torch.sum((data - means).pow(2) / variances, dim=-1)
        return -0.5 * (variances.log().sum(dim=-1) + delta + dim * math.log
            (2 * math.pi))

    def forward(self, input_0):
        primals_1 = self.mean.weight
        primals_2 = self.mean.bias
        primals_4 = self.logvar.weight
        primals_5 = self.logvar.bias
        primals_3 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4, primals_5])
        return output[0], output[1]


Measured latency_ms: 0.014866