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 typing import Callable
from typing import Tuple
import torch.utils.data
from typing import Union
import torch.nn
import torch.cuda
import torch.backends.cudnn
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_std_sub_0(in_ptr0, in_ptr1, in_ptr2,
    out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x1 = xindex // 4
    x0 = xindex % 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp28 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp30 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
    tmp3 = tmp1 + tmp2
    tmp5 = tmp3 + tmp4
    tmp7 = tmp5 + tmp6
    tmp8 = 4.0
    tmp9 = tmp7 / tmp8
    tmp10 = tmp0 - tmp9
    tmp11 = tmp1 - tmp9
    tmp12 = tmp11 * tmp11
    tmp13 = tmp2 - tmp9
    tmp14 = tmp13 * tmp13
    tmp15 = tmp12 + tmp14
    tmp16 = tmp4 - tmp9
    tmp17 = tmp16 * tmp16
    tmp18 = tmp15 + tmp17
    tmp19 = tmp6 - tmp9
    tmp20 = tmp19 * tmp19
    tmp21 = tmp18 + tmp20
    tmp22 = 3.0
    tmp23 = tmp21 / tmp22
    tmp24 = libdevice.sqrt(tmp23)
    tmp25 = 1e-05
    tmp26 = tmp24 + tmp25
    tmp27 = tmp10 / tmp26
    tmp29 = tmp27 * tmp28
    tmp31 = tmp29 + tmp30
    tl.store(out_ptr0 + x2, tmp31, 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)
        get_raw_stream(0)
        triton_poi_fused_add_div_mean_mul_std_sub_0[grid(256)](primals_1,
            primals_2, primals_3, buf0, 256, XBLOCK=256, num_warps=4,
            num_stages=1)
        del primals_2
        del primals_3
    return buf0, primals_1


def batch_elementwise(input: 'torch.Tensor', param: 'torch.Tensor', op:
    'Callable[[torch.Tensor, torch.Tensor], torch.Tensor]', input_batch_dim:
    'int'=0, pndim: 'int'=1) ->torch.Tensor:
    """
    Do elementwise operation in groups.

    :param input: input, any shape, [..., Ci, Cj, ...]
    :param param: the parameter, shape [N, Ci, Cj....], in which case B % N == 0, or [Ci, Cj....]
    :param input_batch_dim: which dimension is the batch in the input
    :param op: the operation to perform
    :param pndim: number of parameter dimensions without batch
    :return: input with the op performed, the same shape as input
    """
    if param.ndim == pndim + 1:
        param = param.squeeze(0)
    if param.ndim == pndim:
        return op(input, param)
    assert param.ndim == pndim + 1
    assert input.shape[input_batch_dim] % param.shape[0] == 0
    input_r = input.view(*input.shape[:input_batch_dim], param.shape[0], -1,
        *input.shape[input_batch_dim + 1:])
    param_r = param.view(*([1] * input_batch_dim), param.shape[0], *([1] *
        (input_r.ndim - input_batch_dim - param.ndim)), *param.shape[1:])
    return op(input_r, param_r).view_as(input)


def batch_bias_add(*args, **kwargs) ->torch.Tensor:
    """
    Batch add bias to the inputs.

    For more details, see batch_elementwise
    """
    return batch_elementwise(*args, op=lambda a, b: a + b, **kwargs)


def batch_const_mul(*args, **kwargs) ->torch.Tensor:
    """
    Batch multiplies bias to the inputs.

    For more details, see batch_elementwise
    """
    return batch_elementwise(*args, op=lambda a, b: a * b, **kwargs)


class MaskedModule(torch.nn.Module):
    pass


class LayerNormNew(MaskedModule):

    def __init__(self, normalized_shape: 'Union[int, Tuple[int]]', eps=1e-05):
        super().__init__()
        if isinstance(normalized_shape, int):
            normalized_shape = normalized_shape,
        self.gamma = torch.nn.Parameter(torch.ones(*normalized_shape))
        self.beta = torch.nn.Parameter(torch.zeros(*normalized_shape))
        self.eps = eps

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


Measured latency_ms: 0.015577