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
from torch.nn import Module
from torch.nn import Conv2d
from torch.nn import InstanceNorm2d
from torch.nn.init import kaiming_normal_
from torch.nn.init import xavier_normal_
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__native_batch_norm_legit_relu_repeat_0(in_ptr0,
    in_ptr1, in_ptr2, out_ptr0, out_ptr1, out_ptr3, out_ptr4, 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)
    x0 = xindex
    r1 = rindex
    x2 = xindex % 4
    tmp0 = tl.load(in_ptr0 + x0 % 4, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + (r1 + 16 * x0), xmask, other=0.0)
    tmp26 = tl.load(in_ptr2 + x2, xmask, eviction_policy='evict_last')
    tmp2 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK])
    tl.where(xmask, tmp2, 0)
    tmp5 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
    tmp7 = tl.where(xmask, tmp5, 0)
    tmp8 = tl.sum(tmp7, 1)[:, None]
    tmp9 = tl.full([XBLOCK, 1], 16, tl.int32)
    tmp10 = tmp9.to(tl.float32)
    tmp11 = tmp8 / tmp10
    tmp12 = tmp2 - tmp11
    tmp13 = tmp12 * tmp12
    tmp14 = tl.broadcast_to(tmp13, [XBLOCK, RBLOCK])
    tmp16 = tl.where(xmask, tmp14, 0)
    tmp17 = tl.sum(tmp16, 1)[:, None]
    tmp18 = tmp1 - tmp11
    tmp19 = 16.0
    tmp20 = tmp17 / tmp19
    tmp21 = 1e-05
    tmp22 = tmp20 + tmp21
    tmp23 = libdevice.rsqrt(tmp22)
    tmp24 = tmp18 * tmp23
    tmp25 = tmp24 * tmp0
    tmp27 = tmp25 + tmp26
    tmp28 = tl.full([1, 1], 0, tl.int32)
    tmp29 = triton_helpers.maximum(tmp28, tmp27)
    tl.store(out_ptr0 + x0, tmp0, xmask)
    tl.store(out_ptr3 + (r1 + 16 * x0), tmp29, xmask)
    tl.store(out_ptr4 + x0, tmp23, xmask)
    tl.store(out_ptr1 + x0, tmp11, xmask)


@triton.jit
def triton_per_fused__native_batch_norm_legit_add_repeat_1(in_ptr0, in_ptr1,
    in_ptr2, in_ptr3, out_ptr0, out_ptr1, out_ptr3, out_ptr4, 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)
    x0 = xindex
    r1 = rindex
    x2 = xindex % 4
    tmp0 = tl.load(in_ptr0 + x0 % 4, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + (r1 + 16 * x0), xmask, other=0.0)
    tmp18 = tl.load(in_ptr2 + (r1 + 16 * x0), xmask, other=0.0)
    tmp27 = tl.load(in_ptr3 + x2, xmask, eviction_policy='evict_last')
    tmp2 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK])
    tl.where(xmask, tmp2, 0)
    tmp5 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
    tmp7 = tl.where(xmask, tmp5, 0)
    tmp8 = tl.sum(tmp7, 1)[:, None]
    tmp9 = tl.full([XBLOCK, 1], 16, tl.int32)
    tmp10 = tmp9.to(tl.float32)
    tmp11 = tmp8 / tmp10
    tmp12 = tmp2 - tmp11
    tmp13 = tmp12 * tmp12
    tmp14 = tl.broadcast_to(tmp13, [XBLOCK, RBLOCK])
    tmp16 = tl.where(xmask, tmp14, 0)
    tmp17 = tl.sum(tmp16, 1)[:, None]
    tmp19 = tmp1 - tmp11
    tmp20 = 16.0
    tmp21 = tmp17 / tmp20
    tmp22 = 1e-05
    tmp23 = tmp21 + tmp22
    tmp24 = libdevice.rsqrt(tmp23)
    tmp25 = tmp19 * tmp24
    tmp26 = tmp25 * tmp0
    tmp28 = tmp26 + tmp27
    tmp29 = tmp18 + tmp28
    tl.store(out_ptr0 + x0, tmp0, xmask)
    tl.store(out_ptr3 + (r1 + 16 * x0), tmp29, xmask)
    tl.store(out_ptr4 + x0, tmp24, xmask)
    tl.store(out_ptr1 + x0, tmp11, xmask)


def call(args):
    (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
        primals_7) = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 3, 3), (36, 9, 3, 1))
    assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_3, (4,), (1,))
    assert_size_stride(primals_4, (4,), (1,))
    assert_size_stride(primals_5, (4, 4, 3, 3), (36, 9, 3, 1))
    assert_size_stride(primals_6, (4,), (1,))
    assert_size_stride(primals_7, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = extern_kernels.convolution(primals_2, 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 = empty_strided_cuda((16,), (1,), torch.float32)
        buf2 = empty_strided_cuda((1, 16, 1, 1), (16, 1, 16, 16), torch.float32
            )
        buf6 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        buf5 = empty_strided_cuda((1, 16, 1, 1), (16, 1, 16, 16), torch.float32
            )
        get_raw_stream(0)
        triton_per_fused__native_batch_norm_legit_relu_repeat_0[grid(16)](
            primals_3, buf0, primals_4, buf1, buf2, buf6, buf5, 16, 16,
            XBLOCK=8, num_warps=2, num_stages=1)
        del primals_3
        del primals_4
        buf7 = extern_kernels.convolution(buf6, primals_5, stride=(1, 1),
            padding=(1, 1), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf7, (4, 4, 4, 4), (64, 16, 4, 1))
        buf8 = empty_strided_cuda((16,), (1,), torch.float32)
        buf9 = empty_strided_cuda((1, 16, 1, 1), (16, 1, 16, 16), torch.float32
            )
        buf13 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        buf12 = empty_strided_cuda((1, 16, 1, 1), (16, 1, 16, 16), torch.
            float32)
        triton_per_fused__native_batch_norm_legit_add_repeat_1[grid(16)](
            primals_6, buf7, primals_2, primals_7, buf8, buf9, buf13, buf12,
            16, 16, XBLOCK=1, num_warps=2, num_stages=1)
        del primals_6
        del primals_7
    return (buf13, primals_1, primals_2, primals_5, buf0, buf1,
        reinterpret_tensor(buf5, (16,), (1,), 0), buf6, buf7, buf8,
        reinterpret_tensor(buf12, (16,), (1,), 0), reinterpret_tensor(buf9,
        (1, 16, 1, 1), (16, 1, 1, 1), 0), reinterpret_tensor(buf2, (1, 16, 
        1, 1), (16, 1, 1, 1), 0))


def create_init_function(method: 'str'='none'):

    def init(module: 'Module'):
        if method == 'none':
            return module
        elif method == 'he':
            kaiming_normal_(module.weight)
            return module
        elif method == 'xavier':
            xavier_normal_(module.weight)
            return module
        else:
            raise ('Invalid initialization method %s' % method)
    return init


def Conv3(in_channels: 'int', out_channels: 'int', initialization_method='he'
    ) ->Module:
    init = create_init_function(initialization_method)
    return init(Conv2d(in_channels, out_channels, kernel_size=3, stride=1,
        padding=1, bias=False))


class ResNetBlockNew(Module):

    def __init__(self, num_channels: 'int', initialization_method: 'str'='he'):
        super().__init__()
        self.conv1 = Conv3(num_channels, num_channels, initialization_method)
        self.norm1 = InstanceNorm2d(num_features=num_channels, affine=True)
        self.conv2 = Conv3(num_channels, num_channels, initialization_method)
        self.norm2 = InstanceNorm2d(num_features=num_channels, affine=True)

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


Measured latency_ms: 0.062415