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
from torch.nn import Module
import torch.utils
import torch.utils.data
from torch.nn.parameter import Parameter
from torch.nn.modules import Module
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_mean_mul_pow_sqrt_sub_0(in_ptr0, in_ptr1,
    in_ptr2, in_ptr3, in_ptr4, out_ptr0, out_ptr1, out_ptr2, out_ptr3,
    xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 64
    x2 = xindex
    x3 = xindex % 4
    tmp0 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr0 + (64 + x0), xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (128 + x0), xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr0 + (192 + x0), xmask, eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr1 + x2, xmask)
    tmp24 = tl.load(in_ptr2 + x2, xmask)
    tmp27 = tl.load(in_ptr0 + x2, xmask)
    tmp35 = tl.load(in_ptr3 + x3, xmask, eviction_policy='evict_last')
    tmp37 = tl.load(in_ptr4 + x3, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 + tmp3
    tmp6 = tmp4 + tmp5
    tmp7 = 4.0
    tmp8 = tmp6 / tmp7
    tmp9 = 0.2
    tmp10 = tmp8 * tmp9
    tmp12 = 0.8
    tmp13 = tmp11 * tmp12
    tmp14 = tmp10 + tmp13
    tmp15 = tmp0 * tmp0
    tmp16 = tmp1 * tmp1
    tmp17 = tmp15 + tmp16
    tmp18 = tmp3 * tmp3
    tmp19 = tmp17 + tmp18
    tmp20 = tmp5 * tmp5
    tmp21 = tmp19 + tmp20
    tmp22 = tmp21 / tmp7
    tmp23 = tmp22 * tmp9
    tmp25 = tmp24 * tmp12
    tmp26 = tmp23 + tmp25
    tmp28 = tmp27 - tmp14
    tmp29 = 1e-05
    tmp30 = tmp26 + tmp29
    tmp31 = tmp14 * tmp14
    tmp32 = tmp30 - tmp31
    tmp33 = libdevice.sqrt(tmp32)
    tmp34 = tmp28 / tmp33
    tmp36 = tmp34 * tmp35
    tmp38 = tmp36 + tmp37
    tl.store(out_ptr0 + x2, tmp14, xmask)
    tl.store(out_ptr1 + x2, tmp26, xmask)
    tl.store(out_ptr2 + x2, tmp34, xmask)
    tl.store(out_ptr3 + x2, tmp38, 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, 4), (64, 16, 4, 1))
    assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_4, (1, 4), (4, 1))
    assert_size_stride(primals_5, (1, 4), (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 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        buf3 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_add_div_mean_mul_pow_sqrt_sub_0[grid(256)](primals_1,
            primals_2, primals_3, primals_4, primals_5, buf0, buf1, buf2,
            buf3, 256, XBLOCK=128, num_warps=4, num_stages=1)
        del primals_1
        del primals_2
        del primals_3
        del primals_4
        del primals_5
    return buf3, buf0, buf1, buf2


class VirtualBatchNormNNNew(Module):
    """
    Module for Virtual Batch Normalization.
    Implementation borrowed and modified from Rafael_Valle's code + help of SimonW from this discussion thread:
    https://discuss.pytorch.org/t/parameter-grad-of-conv-weight-is-none-after-virtual-batch-normalization/9036
    """

    def __init__(self, num_features: 'int', eps: 'float'=1e-05):
        super().__init__()
        self.num_features = num_features
        self.eps = eps
        self.ref_mean = self.register_parameter('ref_mean', None)
        self.ref_mean_sq = self.register_parameter('ref_mean_sq', None)
        gamma = torch.normal(mean=torch.ones(1, num_features), std=0.02)
        self.gamma = Parameter(gamma.float())
        self.beta = Parameter(torch.FloatTensor(1, num_features).fill_(0))

    def get_stats(self, x):
        """
        Calculates mean and mean square for given batch x.
        Args:
            x: tensor containing batch of activations
        Returns:
            mean: mean tensor over features
            mean_sq: squared mean tensor over features
        """
        mean = x.mean(0, keepdim=True)
        mean_sq = (x ** 2).mean(0, keepdim=True)
        return mean, mean_sq

    def _normalize(self, x, mean, mean_sq):
        """
        Normalize tensor x given the statistics.
        Args:
            x: input tensor
            mean: mean over features. it has size [1:num_features:]
            mean_sq: squared means over features.
        Result:
            x: normalized batch tensor
        """
        assert mean_sq is not None
        assert mean is not None
        if mean.size(1) != self.num_features:
            raise Exception(
                'Mean size not equal to number of featuers : given {}, expected {}'
                .format(mean.size(1), self.num_features))
        if mean_sq.size(1) != self.num_features:
            raise Exception(
                'Squared mean tensor size not equal to number of features : given {}, expected {}'
                .format(mean_sq.size(1), self.num_features))
        std = torch.sqrt(self.eps + mean_sq - mean ** 2)
        x = x - mean
        x = x / std
        x = x * self.gamma
        x = x + self.beta
        return x

    def __repr__(self):
        return '{name}(num_features={num_features}, eps={eps}'.format(name=
            self.__class__.__name__, **self.__dict__)

    def forward(self, input_0, input_1, input_2):
        primals_4 = self.gamma
        primals_5 = self.beta
        primals_1 = input_0
        primals_2 = input_1
        primals_3 = input_2
        output = call([primals_1, primals_2, primals_3, primals_4, primals_5])
        return output[0], output[1], output[2]


Measured latency_ms: 0.058946