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 import triton_helpers
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
import math
import numpy as np
import torch.nn as nn
import torch.nn.functional as F
import torch.utils.data
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_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 = tmp0 * tmp0
    tl.store(out_ptr0 + x0, tmp1, xmask)


@triton.jit
def triton_poi_fused_exp_mul_1(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK:
    tl.constexpr):
    xnumel = 200
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + 0)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK])
    tmp3 = tl.load(in_ptr1 + x0, xmask)
    tmp2 = tl_math.exp(tmp1)
    tmp4 = tmp2 * tmp3
    tmp5 = tmp4 * tmp3
    tl.store(out_ptr0 + x0, tmp5, xmask)


@triton.jit
def triton_poi_fused_add_mul_relu_sqrt_2(in_out_ptr0, in_ptr0, in_ptr1,
    out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 3200
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 50
    tmp0 = tl.load(in_out_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr1 + x2, xmask)
    tmp2 = tmp0 + tmp1
    tmp4 = 1e-16
    tmp5 = tmp3 + tmp4
    tmp6 = libdevice.sqrt(tmp5)
    tmp7 = 0.0
    tmp8 = tmp6 * tmp7
    tmp9 = tmp2 + tmp8
    tmp10 = tl.full([1], 0, tl.int32)
    tmp11 = triton_helpers.maximum(tmp10, tmp9)
    tmp12 = tmp11 * tmp11
    tl.store(in_out_ptr0 + x2, tmp11, xmask)
    tl.store(out_ptr0 + x2, tmp12, xmask)


@triton.jit
def triton_poi_fused_exp_mul_t_3(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK:
    tl.constexpr):
    xnumel = 400
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + 0)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK])
    tmp3 = tl.load(in_ptr1 + x0, xmask)
    tmp2 = tl_math.exp(tmp1)
    tmp4 = tmp2 * tmp3
    tmp5 = tmp4 * tmp3
    tl.store(out_ptr0 + x0, tmp5, xmask)


@triton.jit
def triton_poi_fused_add_mul_sqrt_4(in_out_ptr0, in_ptr0, in_ptr1, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 512
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 8
    tmp0 = tl.load(in_out_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr1 + x2, xmask)
    tmp2 = tmp0 + tmp1
    tmp4 = 1e-16
    tmp5 = tmp3 + tmp4
    tmp6 = libdevice.sqrt(tmp5)
    tmp7 = 0.0
    tmp8 = tmp6 * tmp7
    tmp9 = tmp2 + tmp8
    tl.store(in_out_ptr0 + x2, tmp9, xmask)


@triton.jit
def triton_poi_fused_add_div_exp_log1p_mul_neg_sum_5(in_ptr0, in_ptr1,
    out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xoffset = tl.program_id(0) * XBLOCK
    xoffset + tl.arange(0, XBLOCK)[:]
    tl.full([XBLOCK], True, tl.int1)
    tmp0 = tl.load(in_ptr0 + 0)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK])
    tmp13 = tl.load(in_ptr1 + 0)
    tmp14 = tl.broadcast_to(tmp13, [XBLOCK])
    tmp2 = -tmp1
    tmp3 = tl_math.exp(tmp2)
    tmp4 = libdevice.log1p(tmp3)
    tmp5 = 0.5
    tmp6 = tmp4 * tmp5
    tmp7 = 200.0
    tmp8 = tmp6 * tmp7
    tmp9 = 1.0
    tmp10 = tmp8 * tmp9
    tmp11 = 0.0
    tmp12 = tmp10 + tmp11
    tmp15 = -tmp14
    tmp16 = tl_math.exp(tmp15)
    tmp17 = libdevice.log1p(tmp16)
    tmp18 = tmp17 * tmp5
    tmp19 = 400.0
    tmp20 = tmp18 * tmp19
    tmp21 = tmp20 * tmp9
    tmp22 = tmp12 + tmp21
    tl.store(out_ptr0 + tl.full([XBLOCK], 0, tl.int32), tmp22, None)


def call(args):
    (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
        primals_7) = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_2, (50, 4), (4, 1))
    assert_size_stride(primals_3, (1, 50), (50, 1))
    assert_size_stride(primals_4, (1, 1), (1, 1))
    assert_size_stride(primals_5, (8, 50), (50, 1))
    assert_size_stride(primals_6, (1, 8), (8, 1))
    assert_size_stride(primals_7, (1, 1), (1, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((64, 50), (50, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_1, (64, 4), (4, 1), 0),
            reinterpret_tensor(primals_2, (4, 50), (1, 4), 0), out=buf0)
        buf1 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_mul_0[grid(256)](primals_1, buf1, 256, XBLOCK=128,
            num_warps=4, num_stages=1)
        buf2 = empty_strided_cuda((50, 4), (4, 1), torch.float32)
        triton_poi_fused_exp_mul_1[grid(200)](primals_4, primals_2, buf2, 
            200, XBLOCK=128, num_warps=4, num_stages=1)
        buf3 = empty_strided_cuda((64, 50), (50, 1), torch.float32)
        extern_kernels.mm(buf1, reinterpret_tensor(buf2, (4, 50), (1, 4), 0
            ), out=buf3)
        del buf2
        buf4 = buf0
        del buf0
        buf6 = empty_strided_cuda((64, 50), (50, 1), torch.float32)
        triton_poi_fused_add_mul_relu_sqrt_2[grid(3200)](buf4, primals_3,
            buf3, buf6, 3200, XBLOCK=256, num_warps=4, num_stages=1)
        del primals_3
        buf5 = empty_strided_cuda((64, 8), (8, 1), torch.float32)
        extern_kernels.mm(buf4, reinterpret_tensor(primals_5, (50, 8), (1, 
            50), 0), out=buf5)
        buf7 = empty_strided_cuda((50, 8), (1, 50), torch.float32)
        triton_poi_fused_exp_mul_t_3[grid(400)](primals_7, primals_5, buf7,
            400, XBLOCK=128, num_warps=4, num_stages=1)
        buf8 = empty_strided_cuda((64, 8), (8, 1), torch.float32)
        extern_kernels.mm(buf6, buf7, out=buf8)
        buf9 = buf5
        del buf5
        triton_poi_fused_add_mul_sqrt_4[grid(512)](buf9, primals_6, buf8, 
            512, XBLOCK=256, num_warps=4, num_stages=1)
        del primals_6
        buf10 = empty_strided_cuda((), (), torch.float32)
        triton_poi_fused_add_div_exp_log1p_mul_neg_sum_5[grid(1)](primals_4,
            primals_7, buf10, 1, XBLOCK=1, num_warps=1, num_stages=1)
    return (buf9, buf10, primals_2, primals_4, primals_5, primals_7,
        reinterpret_tensor(primals_1, (64, 4), (4, 1), 0), buf1, buf3, buf4,
        buf6, buf8, reinterpret_tensor(buf7, (8, 50), (50, 1), 0))


def calculate_kl(log_alpha):
    return 0.5 * torch.sum(torch.log1p(torch.exp(-log_alpha)))


class VdLinear(nn.Module):
    """
    variational dropout

    """

    def __init__(self, n_in, n_out, alpha_shape=(1, 1), bias=True):
        super(VdLinear, self).__init__()
        self.n_in = n_in
        self.n_out = n_out
        self.alpha_shape = alpha_shape
        self.bias = bias
        self.W = nn.Parameter(torch.Tensor(self.n_out, self.n_in))
        self.log_alpha = nn.Parameter(torch.Tensor(*self.alpha_shape))
        if bias:
            self.bias = nn.Parameter(torch.Tensor(1, self.n_out))
        else:
            self.register_parameter('bias', None)
        self.reset_parameters()
        self.kl_value = calculate_kl

    def reset_parameters(self):
        stdv = 1.0 / math.sqrt(self.W.size(1))
        self.W.data.uniform_(-stdv, stdv)
        self.log_alpha.data.fill_(-5.0)
        if self.bias is not None:
            self.bias.data.zero_()

    def forward(self, X, sample=False):
        mean = F.linear(X, self.W)
        if self.bias is not None:
            mean = mean + self.bias
        sigma = torch.exp(self.log_alpha) * self.W * self.W
        std = torch.sqrt(1e-16 + F.linear(X * X, sigma))
        if self.training or sample:
            epsilon = std.data.new(std.size()).normal_()
        else:
            epsilon = 0.0
        out = mean + std * epsilon
        kl = self.kl_loss()
        return out, kl

    def kl_loss(self):
        return self.W.nelement() * self.kl_value(self.log_alpha
            ) / self.log_alpha.nelement()


class vd_linear_1L_heteroNew(nn.Module):
    """1 hidden layer Variational Dropout Network"""

    def __init__(self, input_dim, output_dim, alpha_shape=(1, 1), bias=True,
        n_hid=50):
        super(vd_linear_1L_heteroNew, self).__init__()
        self.input_dim = input_dim
        self.output_dim = output_dim
        self.alpha_shape = alpha_shape
        self.bias = bias
        self.bfc1 = VdLinear(input_dim, n_hid, self.alpha_shape, self.bias)
        self.bfc2 = VdLinear(n_hid, 2 * output_dim, self.alpha_shape, self.bias
            )
        self.act = nn.ReLU(inplace=True)

    def sample_predict(self, x, Nsamples):
        """Used for estimating the data's likelihood by approximately marginalising the weights with MC"""
        predictions = x.data.new(Nsamples, x.shape[0], self.output_dim)
        tkl_vec = np.zeros(Nsamples)
        for i in range(Nsamples):
            y, tkl = self.forward(x, sample=True)
            predictions[i] = y
            tkl_vec[i] = tkl
        return predictions, tkl_vec

    def forward(self, input_0):
        primals_2 = self.bfc1.W
        primals_4 = self.bfc1.log_alpha
        primals_3 = self.bfc1.bias
        primals_5 = self.bfc2.W
        primals_7 = self.bfc2.log_alpha
        primals_6 = self.bfc2.bias
        primals_1 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6, primals_7])
        return output[0], output[1]


Measured latency_ms: 0.029636