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
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_per_fused_convolution_div_mean_relu_std_sub_0(in_out_ptr0,
    in_out_ptr1, in_out_ptr2, in_ptr0, out_ptr0, xnumel, rnumel, XBLOCK: tl
    .constexpr):
    xnumel = 16
    RBLOCK: tl.constexpr = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r2 = rindex
    x3 = xindex
    x0 = xindex % 4
    tmp0 = tl.load(in_out_ptr0 + (r2 + 16 * x3), xmask, other=0.0)
    tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
    tmp5 = tl.where(xmask, tmp3, 0)
    tmp6 = tl.sum(tmp5, 1)[:, None]
    tmp8 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK])
    tmp10 = tl.where(xmask, tmp8, 0)
    tmp11 = tl.sum(tmp10, 1)[:, None]
    tmp12 = tl.full([XBLOCK, 1], 16, tl.int32)
    tmp13 = tmp12.to(tl.float32)
    tmp14 = tmp11 / tmp13
    tmp15 = tmp3 - tmp14
    tmp16 = tmp15 * tmp15
    tmp17 = tl.broadcast_to(tmp16, [XBLOCK, RBLOCK])
    tmp19 = tl.where(xmask, tmp17, 0)
    tmp20 = tl.sum(tmp19, 1)[:, None]
    tmp21 = 16.0
    tmp22 = tmp6 / tmp21
    tmp23 = 15.0
    tmp24 = tmp20 / tmp23
    tmp25 = libdevice.sqrt(tmp24)
    tmp26 = tmp2 - tmp22
    tmp27 = tmp26 / tmp25
    tmp28 = tl.full([1, 1], 0, tl.int32)
    tmp29 = triton_helpers.maximum(tmp28, tmp27)
    tl.store(in_out_ptr0 + (r2 + 16 * x3), tmp2, xmask)
    tl.debug_barrier()
    tl.store(in_out_ptr1 + x3, tmp22, xmask)
    tl.debug_barrier()
    tl.store(in_out_ptr2 + x3, tmp25, xmask)
    tl.store(out_ptr0 + (r2 + 16 * x3), tmp29, xmask)


@triton.jit
def triton_per_fused_add_convolution_div_mean_relu_std_sub_threshold_backward_1(
    in_out_ptr0, in_out_ptr1, in_out_ptr2, in_ptr0, in_ptr1, out_ptr0,
    out_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    RBLOCK: tl.constexpr = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r2 = rindex
    x3 = xindex
    x0 = xindex % 4
    tmp0 = tl.load(in_out_ptr0 + (r2 + 16 * x3), xmask, other=0.0)
    tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp28 = tl.load(in_ptr1 + (r2 + 16 * x3), xmask, other=0.0)
    tmp2 = tmp0 + tmp1
    tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
    tmp5 = tl.where(xmask, tmp3, 0)
    tmp6 = tl.sum(tmp5, 1)[:, None]
    tmp8 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK])
    tmp10 = tl.where(xmask, tmp8, 0)
    tmp11 = tl.sum(tmp10, 1)[:, None]
    tmp12 = tl.full([XBLOCK, 1], 16, tl.int32)
    tmp13 = tmp12.to(tl.float32)
    tmp14 = tmp11 / tmp13
    tmp15 = tmp3 - tmp14
    tmp16 = tmp15 * tmp15
    tmp17 = tl.broadcast_to(tmp16, [XBLOCK, RBLOCK])
    tmp19 = tl.where(xmask, tmp17, 0)
    tmp20 = tl.sum(tmp19, 1)[:, None]
    tmp21 = 16.0
    tmp22 = tmp6 / tmp21
    tmp23 = 15.0
    tmp24 = tmp20 / tmp23
    tmp25 = libdevice.sqrt(tmp24)
    tmp26 = tmp2 - tmp22
    tmp27 = tmp26 / tmp25
    tmp29 = tmp27 + tmp28
    tmp30 = tl.full([1, 1], 0, tl.int32)
    tmp31 = triton_helpers.maximum(tmp30, tmp29)
    tmp32 = 0.0
    tmp33 = tmp31 <= tmp32
    tl.store(in_out_ptr0 + (r2 + 16 * x3), tmp2, xmask)
    tl.debug_barrier()
    tl.store(in_out_ptr1 + x3, tmp22, xmask)
    tl.debug_barrier()
    tl.store(in_out_ptr2 + x3, tmp25, xmask)
    tl.store(out_ptr0 + (r2 + 16 * x3), tmp31, xmask)
    tl.store(out_ptr1 + (r2 + 16 * x3), tmp33, xmask)


def call(args):
    primals_1, primals_2, primals_3, primals_4, primals_5 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 3, 3), (36, 9, 3, 1))
    assert_size_stride(primals_2, (4,), (1,))
    assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_4, (4, 4, 3, 3), (36, 9, 3, 1))
    assert_size_stride(primals_5, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = extern_kernels.convolution(primals_3, primals_1, stride=(1, 
            1), padding=(1, 1), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf0, (4, 4, 4, 4), (64, 16, 4, 1))
        buf1 = buf0
        del buf0
        buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf3 = buf2
        del buf2
        buf7 = buf5
        del buf5
        buf8 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_per_fused_convolution_div_mean_relu_std_sub_0[grid(16)](buf1,
            buf3, buf7, primals_2, buf8, 16, 16, XBLOCK=8, num_warps=2,
            num_stages=1)
        del primals_2
        buf9 = extern_kernels.convolution(buf8, primals_4, stride=(1, 1),
            padding=(1, 1), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf9, (4, 4, 4, 4), (64, 16, 4, 1))
        buf10 = buf9
        del buf9
        buf11 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf14 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf12 = buf11
        del buf11
        buf16 = buf14
        del buf14
        buf17 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        buf18 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool)
        triton_per_fused_add_convolution_div_mean_relu_std_sub_threshold_backward_1[
            grid(16)](buf10, buf12, buf16, primals_5, primals_3, buf17,
            buf18, 16, 16, XBLOCK=1, num_warps=2, num_stages=1)
        del primals_5
    return (buf8, buf17, primals_1, primals_3, primals_4, buf1,
        reinterpret_tensor(buf3, (4, 4, 1, 1), (4, 1, 1, 1), 0), buf7, buf8,
        buf10, reinterpret_tensor(buf12, (4, 4, 1, 1), (4, 1, 1, 1), 0),
        buf16, buf18)


class RemoveChannelMeanStd(torch.nn.Module):

    def forward(self, x):
        x2 = x.view(x.size(0), x.size(1), -1)
        mean = x2.mean(dim=2).view(x.size(0), x.size(1), 1, 1)
        std = x2.std(dim=2).view(x.size(0), x.size(1), 1, 1)
        return (x - mean) / std


class BlockNew(nn.Module):

    def __init__(self, in_planes, planes, stride=1, groups=False):
        super(BlockNew, self).__init__()
        self.bn1 = RemoveChannelMeanStd()
        if not groups:
            self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=3, stride
                =stride, padding=1, bias=True)
            self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=1,
                padding=1, bias=True)
        else:
            self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=3, stride
                =stride, padding=1, bias=True, groups=min(in_planes, planes))
            self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=1,
                padding=1, bias=True, groups=planes)
        self.bn2 = RemoveChannelMeanStd()
        if stride != 1 or in_planes != planes:
            self.shortcut = nn.Sequential(nn.Conv2d(in_planes, planes,
                kernel_size=1, stride=stride, bias=True),
                RemoveChannelMeanStd())

    def forward(self, input_0):
        primals_1 = self.conv1.weight
        primals_2 = self.conv1.bias
        primals_4 = self.conv2.weight
        primals_5 = self.conv2.bias
        primals_3 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4, primals_5])
        return output[0], output[1]


Measured latency_ms: 0.015066