import torch
from torch import device
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 import triton_helpers
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
import math
import torch.nn as nn
from torch.nn import init
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_exp_pow_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
    ):
    xnumel = 64
    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 = tl_math.exp(tmp0)
    tmp2 = tmp1 * tmp1
    tl.store(out_ptr0 + x0, tmp2, xmask)


@triton.jit
def triton_poi_fused_pow_1(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
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + x0, xmask)
    tmp1 = tmp0 * tmp0
    tl.store(out_ptr0 + x0, tmp1, xmask)


@triton.jit
def triton_poi_fused_add_clamp_convolution_mul_sqrt_2(in_out_ptr0,
    in_out_ptr1, in_ptr0, in_ptr1, xnumel, XBLOCK: tl.constexpr):
    xnumel = 36
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x1 = xindex // 9
    tmp0 = tl.load(in_out_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_out_ptr1 + x2, xmask)
    tmp8 = tl.load(in_ptr1 + x2, xmask)
    tmp2 = tmp0 + tmp1
    tmp4 = tmp3 + tmp1
    tmp5 = 1e-16
    tmp6 = triton_helpers.maximum(tmp2, tmp5)
    tmp7 = libdevice.sqrt(tmp6)
    tmp9 = tmp7 * tmp8
    tmp10 = tmp4 + tmp9
    tl.store(in_out_ptr0 + x2, tmp2, xmask)
    tl.store(in_out_ptr1 + x2, tmp10, xmask)


def call(args):
    primals_1, primals_2, primals_3, primals_4 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1))
    assert_size_stride(primals_2, (4,), (1,))
    assert_size_stride(primals_3, (4, 4), (4, 1))
    assert_size_stride(primals_4, (4, 4, 4), (16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = extern_kernels.convolution(reinterpret_tensor(primals_3, (1,
            4, 4), (16, 4, 1), 0), primals_1, stride=(1,), padding=(4,),
            dilation=(1,), transposed=False, output_padding=(0,), groups=1,
            bias=None)
        assert_size_stride(buf0, (1, 4, 9), (36, 9, 1))
        buf1 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_exp_pow_0[grid(64)](primals_4, buf1, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_poi_fused_pow_1[grid(16)](primals_3, buf2, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        buf3 = extern_kernels.convolution(reinterpret_tensor(buf2, (1, 4, 4
            ), (0, 4, 1), 0), buf1, stride=(1,), padding=(4,), dilation=(1,
            ), transposed=False, output_padding=(0,), groups=1, bias=None)
        assert_size_stride(buf3, (1, 4, 9), (36, 9, 1))
        buf5 = torch.ops.aten.randn.default([4, 9], dtype=torch.float32,
            device=device(type='cuda', index=0), pin_memory=False)
        buf6 = buf5
        del buf5
        buf4 = buf3
        del buf3
        buf7 = reinterpret_tensor(buf0, (4, 9), (9, 1), 0)
        del buf0
        triton_poi_fused_add_clamp_convolution_mul_sqrt_2[grid(36)](buf4,
            buf7, primals_2, buf6, 36, XBLOCK=64, num_warps=1, num_stages=1)
        del primals_2
    return buf7, primals_1, primals_4, reinterpret_tensor(primals_3, (1, 4,
        4), (16, 4, 1), 0), buf1, reinterpret_tensor(buf2, (1, 4, 4), (16, 
        4, 1), 0), buf4, buf6


def calculate_kl(mu_p, sig_p, mu_q, sig_q):
    """
    Calculates the Kullback-Leibler divergence between two univariate Gaussians (p and q)

    Args:
        mu_p: mean of the Gaussian p
        sig_p: standard deviation of the Gaussian p
        mu_q: mean of the Gaussian q
        sig_q: standard deviation of the Gaussian q
    """
    kl = 0.5 * (2 * torch.log(sig_p / sig_q) - 1 + (sig_q / sig_p).pow(2) +
        ((mu_p - mu_q) / sig_p).pow(2)).sum()
    return kl


class BayesConv1dNew(nn.Module):
    """
    This class implements a Bayesian 1-dimensional Convolutional layer.
    """

    def __init__(self, in_channels, out_channels, kernel_size, stride,
        padding, dilation, bias=True, log_sigma_prior=-5, mu_prior=-1):
        """
        Initializes BayesConv1d layer. 

        Args:
            in_channels: number of input channels
            out_channels: number of output channels
            kernel_size: size of the convolutional kernel
            stride: stride of the convolution
            dilation: spacing between the kernel points of the convolution
            bias: whether to add bias  
            log_sigma_prior: the initial value of the standard deviation of the distribution
            mu_prior: the initial value of the mean of the distribution
           """
        super(BayesConv1dNew, self).__init__()
        self.in_channels = in_channels
        self.out_channels = out_channels
        self.kernel_size = kernel_size
        self.stride = stride
        self.padding = padding
        self.dilation = dilation
        self.w_mu = nn.Parameter(torch.Tensor(out_channels, in_channels,
            kernel_size))
        self.w_log_sigma = nn.Parameter(torch.Tensor(out_channels,
            in_channels, kernel_size))
        self.mu_prior_init = mu_prior
        self.log_sigma_prior_init = log_sigma_prior
        if bias is True:
            self.bias = nn.Parameter(torch.Tensor(out_channels))
        else:
            self.register_parameter('bias', None)
        self.reset_parameters()

    def reset_parameters(self):
        """
        Resets the parameters of the layer
        """
        init.kaiming_uniform_(self.w_mu, a=math.sqrt(5))
        init.uniform_(self.w_log_sigma, self.log_sigma_prior_init - 0.1,
            self.log_sigma_prior_init)
        if self.bias is not None:
            fan_in, _ = init._calculate_fan_in_and_fan_out(self.w_mu)
            bound = 1 / math.sqrt(fan_in)
            init.uniform_(self.bias, -bound, bound)

    def kl(self):
        """
        Returns the Kullback-Leibler divergence between the prior and the posterior of Bayesian layer.
        """
        return calculate_kl(torch.Tensor([self.mu_prior_init]), torch.exp(
            torch.Tensor([self.log_sigma_prior_init])), self.w_mu, torch.
            exp(self.w_log_sigma))

    def forward(self, input_0):
        primals_1 = self.w_mu
        primals_4 = self.w_log_sigma
        primals_2 = self.bias
        primals_3 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4])
        return output[0]


Measured latency_ms: 0.035724