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
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor


@triton.jit
def triton_poi_fused_add_mean_mul_sqrt_sub_0(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 + 4 * x0, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr0 + (64 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp3 = tl.load(in_ptr0 + (128 + 4 * x0), xmask, eviction_policy=
        'evict_last')
    tmp5 = tl.load(in_ptr0 + (192 + 4 * x0), xmask, eviction_policy=
        'evict_last')
    tmp24 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp25 = tl.load(in_ptr0 + (65 + 4 * x0), xmask, eviction_policy=
        'evict_last')
    tmp27 = tl.load(in_ptr0 + (129 + 4 * x0), xmask, eviction_policy=
        'evict_last')
    tmp29 = tl.load(in_ptr0 + (193 + 4 * x0), xmask, eviction_policy=
        'evict_last')
    tmp47 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp48 = tl.load(in_ptr0 + (66 + 4 * x0), xmask, eviction_policy=
        'evict_last')
    tmp50 = tl.load(in_ptr0 + (130 + 4 * x0), xmask, eviction_policy=
        'evict_last')
    tmp52 = tl.load(in_ptr0 + (194 + 4 * x0), xmask, eviction_policy=
        'evict_last')
    tmp70 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp71 = tl.load(in_ptr0 + (67 + 4 * x0), xmask, eviction_policy=
        'evict_last')
    tmp73 = tl.load(in_ptr0 + (131 + 4 * x0), xmask, eviction_policy=
        'evict_last')
    tmp75 = tl.load(in_ptr0 + (195 + 4 * x0), xmask, eviction_policy=
        'evict_last')
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 + tmp3
    tmp6 = tmp4 + tmp5
    tmp7 = 4.0
    tmp8 = tmp6 / tmp7
    tmp9 = tmp0 - tmp8
    tmp10 = tmp9 * tmp9
    tmp11 = tmp1 - tmp8
    tmp12 = tmp11 * tmp11
    tmp13 = tmp10 + tmp12
    tmp14 = tmp3 - tmp8
    tmp15 = tmp14 * tmp14
    tmp16 = tmp13 + tmp15
    tmp17 = tmp5 - tmp8
    tmp18 = tmp17 * tmp17
    tmp19 = tmp16 + tmp18
    tmp20 = tmp19 / tmp7
    tmp21 = 1e-08
    tmp22 = tmp20 + tmp21
    tmp23 = libdevice.sqrt(tmp22)
    tmp26 = tmp24 + tmp25
    tmp28 = tmp26 + tmp27
    tmp30 = tmp28 + tmp29
    tmp31 = tmp30 / tmp7
    tmp32 = tmp24 - tmp31
    tmp33 = tmp32 * tmp32
    tmp34 = tmp25 - tmp31
    tmp35 = tmp34 * tmp34
    tmp36 = tmp33 + tmp35
    tmp37 = tmp27 - tmp31
    tmp38 = tmp37 * tmp37
    tmp39 = tmp36 + tmp38
    tmp40 = tmp29 - tmp31
    tmp41 = tmp40 * tmp40
    tmp42 = tmp39 + tmp41
    tmp43 = tmp42 / tmp7
    tmp44 = tmp43 + tmp21
    tmp45 = libdevice.sqrt(tmp44)
    tmp46 = tmp23 + tmp45
    tmp49 = tmp47 + tmp48
    tmp51 = tmp49 + tmp50
    tmp53 = tmp51 + tmp52
    tmp54 = tmp53 / tmp7
    tmp55 = tmp47 - tmp54
    tmp56 = tmp55 * tmp55
    tmp57 = tmp48 - tmp54
    tmp58 = tmp57 * tmp57
    tmp59 = tmp56 + tmp58
    tmp60 = tmp50 - tmp54
    tmp61 = tmp60 * tmp60
    tmp62 = tmp59 + tmp61
    tmp63 = tmp52 - tmp54
    tmp64 = tmp63 * tmp63
    tmp65 = tmp62 + tmp64
    tmp66 = tmp65 / tmp7
    tmp67 = tmp66 + tmp21
    tmp68 = libdevice.sqrt(tmp67)
    tmp69 = tmp46 + tmp68
    tmp72 = tmp70 + tmp71
    tmp74 = tmp72 + tmp73
    tmp76 = tmp74 + tmp75
    tmp77 = tmp76 / tmp7
    tmp78 = tmp70 - tmp77
    tmp79 = tmp78 * tmp78
    tmp80 = tmp71 - tmp77
    tmp81 = tmp80 * tmp80
    tmp82 = tmp79 + tmp81
    tmp83 = tmp73 - tmp77
    tmp84 = tmp83 * tmp83
    tmp85 = tmp82 + tmp84
    tmp86 = tmp75 - tmp77
    tmp87 = tmp86 * tmp86
    tmp88 = tmp85 + tmp87
    tmp89 = tmp88 / tmp7
    tmp90 = tmp89 + tmp21
    tmp91 = libdevice.sqrt(tmp90)
    tmp92 = tmp69 + tmp91
    tmp93 = tmp92 / tmp7
    tl.store(out_ptr0 + x0, tmp93, xmask)


@triton.jit
def triton_per_fused_mean_1(in_ptr0, out_ptr0, xnumel, rnumel, XBLOCK: tl.
    constexpr):
    RBLOCK: tl.constexpr = 4
    xoffset = tl.program_id(0) * XBLOCK
    xoffset + tl.arange(0, XBLOCK)[:, None]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r0 = rindex
    tmp0 = tl.load(in_ptr0 + 4 * r0, None, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr0 + (1 + 4 * r0), None, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (2 + 4 * r0), None, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr0 + (3 + 4 * r0), None, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 + tmp3
    tmp6 = tmp4 + tmp5
    tmp7 = 4.0
    tmp8 = tmp6 / tmp7
    tmp9 = tl.broadcast_to(tmp8, [XBLOCK, RBLOCK])
    tmp11 = tl.sum(tmp9, 1)[:, None]
    tl.store(out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp11, None)


@triton.jit
def triton_poi_fused_cat_2(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
    x2 = xindex
    x0 = xindex % 64
    x1 = xindex // 64
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tl.store(out_ptr0 + (x0 + 80 * x1), tmp0, xmask)


@triton.jit
def triton_poi_fused_mean_repeat_3(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 % 16
    x1 = xindex // 16
    tmp0 = tl.load(in_ptr0 + 0)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK])
    tmp2 = 4.0
    tmp3 = tmp1 / tmp2
    tl.store(out_ptr0 + (x0 + 80 * x1), tmp3, xmask)


def call(args):
    arg0_1, = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((1, 4, 4, 1), (16, 4, 1, 16), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_add_mean_mul_sqrt_sub_0[grid(16)](arg0_1, buf0, 16,
            XBLOCK=16, num_warps=1, num_stages=1)
        buf1 = empty_strided_cuda((1, 1, 1, 1), (1, 1, 1, 1), torch.float32)
        triton_per_fused_mean_1[grid(1)](buf0, buf1, 1, 4, XBLOCK=1,
            num_warps=2, num_stages=1)
        del buf0
        buf4 = empty_strided_cuda((4, 5, 4, 4), (80, 16, 4, 1), torch.float32)
        buf2 = reinterpret_tensor(buf4, (4, 4, 4, 4), (80, 16, 4, 1), 0)
        triton_poi_fused_cat_2[grid(256)](arg0_1, buf2, 256, XBLOCK=128,
            num_warps=4, num_stages=1)
        del arg0_1
        buf3 = reinterpret_tensor(buf4, (4, 1, 4, 4), (80, 16, 4, 1), 64)
        triton_poi_fused_mean_repeat_3[grid(64)](buf1, buf3, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        del buf1
    return buf4,


class MinibatchStdLayerNew(nn.Module):

    def __init__(self, group_size=4):
        super().__init__()
        self.group_size = group_size

    def forward(self, input_0):
        arg0_1 = input_0
        output = call([arg0_1])
        return output[0]


Measured latency_ms: 0.015006