import torch
import torch.nn as nn


class CustomBatchNormAutograd(nn.Module):
    """
    This nn.module implements a custom version of the batch norm operation for MLPs.
    The operations called in self.forward track the history if the input tensors have the
    flag requires_grad set to True.
    """

    def __init__(self, n_neurons, eps=1e-05):
        """
        Initializes CustomBatchNormAutograd object.
    
        Args:
          n_neurons: int specifying the number of neurons
          eps: small float to be added to the variance for stability

        """
        super(CustomBatchNormAutograd, self).__init__()
        self.n_neurons = n_neurons
        self.eps = eps
        self.beta = nn.Parameter(torch.zeros(self.n_neurons))
        self.gamma = nn.Parameter(torch.ones(self.n_neurons))

    def forward(self, input):
        """
        Compute the batch normalization

        Args:
          input: input tensor of shape (n_batch, n_neurons)
        Returns:
          out: batch-normalized tensor

        """
        batch_size = input.shape[0]
        assert input.shape[1
            ] == self.n_neurons, 'Input not in the correct shape'
        mean = 1 / batch_size * torch.sum(input, dim=0)
        var = input.var(dim=0, unbiased=False)
        norm = (input - mean) / torch.sqrt(var + self.eps)
        out = self.gamma * norm + self.beta
        return out


def get_inputs():
    return [torch.rand([4, 4, 4, 4])]


def get_init_inputs():
    return [[], {'n_neurons': 4}]

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.triton_helpers import libdevice
import torch.nn as nn
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_add_div_mul_sqrt_sub_sum_var_0(in_out_ptr0, in_ptr0,
    in_ptr1, in_ptr2, xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 4
    x4 = xindex
    x5 = xindex % 64
    tmp0 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + x4, xmask)
    tmp2 = tl.load(in_ptr1 + x5, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr1 + (64 + x5), xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr1 + (128 + x5), xmask, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr1 + (192 + x5), xmask, eviction_policy='evict_last')
    tmp31 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
    tmp4 = tmp2 + tmp3
    tmp6 = tmp4 + tmp5
    tmp8 = tmp6 + tmp7
    tmp9 = 0.25
    tmp10 = tmp8 * tmp9
    tmp11 = tmp1 - tmp10
    tmp12 = 4.0
    tmp13 = tmp8 / tmp12
    tmp14 = tmp2 - tmp13
    tmp15 = tmp14 * tmp14
    tmp16 = tmp3 - tmp13
    tmp17 = tmp16 * tmp16
    tmp18 = tmp15 + tmp17
    tmp19 = tmp5 - tmp13
    tmp20 = tmp19 * tmp19
    tmp21 = tmp18 + tmp20
    tmp22 = tmp7 - tmp13
    tmp23 = tmp22 * tmp22
    tmp24 = tmp21 + tmp23
    tmp25 = tmp24 / tmp12
    tmp26 = 1e-05
    tmp27 = tmp25 + tmp26
    tmp28 = libdevice.sqrt(tmp27)
    tmp29 = tmp11 / tmp28
    tmp30 = tmp0 * tmp29
    tmp32 = tmp30 + tmp31
    tl.store(in_out_ptr0 + x4, tmp32, xmask)


def call(args):
    primals_1, primals_2, primals_3 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_2, (4,), (1,))
    assert_size_stride(primals_3, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        buf1 = buf0
        del buf0
        get_raw_stream(0)
        triton_poi_fused_add_div_mul_sqrt_sub_sum_var_0[grid(256)](buf1,
            primals_2, primals_1, primals_3, 256, XBLOCK=128, num_warps=4,
            num_stages=1)
        del primals_2
        del primals_3
    return buf1, primals_1


class CustomBatchNormAutogradNew(nn.Module):
    """
    This nn.module implements a custom version of the batch norm operation for MLPs.
    The operations called in self.forward track the history if the input tensors have the
    flag requires_grad set to True.
    """

    def __init__(self, n_neurons, eps=1e-05):
        """
        Initializes CustomBatchNormAutograd object.
    
        Args:
          n_neurons: int specifying the number of neurons
          eps: small float to be added to the variance for stability

        """
        super(CustomBatchNormAutogradNew, self).__init__()
        self.n_neurons = n_neurons
        self.eps = eps
        self.beta = nn.Parameter(torch.zeros(self.n_neurons))
        self.gamma = nn.Parameter(torch.ones(self.n_neurons))

    def forward(self, input_0):
        primals_2 = self.beta
        primals_3 = self.gamma
        primals_1 = input_0
        output = call([primals_1, primals_2, primals_3])
        return output[0]
