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.triton_helpers import libdevice
import torch.nn.functional as F
import torch.nn as nn
import torch.nn.functional
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_leaky_relu_leaky_relu_backward_0(
    in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, out_ptr0, out_ptr2, out_ptr3,
    xnumel, rnumel, XBLOCK: tl.constexpr):
    xnumel = 4
    RBLOCK: tl.constexpr = 64
    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)
    r1 = rindex
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (r1 + 64 * x0), xmask, other=0.0)
    tmp24 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp26 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tl.where(xmask, tmp1, 0)
    tmp4 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK])
    tmp6 = tl.where(xmask, tmp4, 0)
    tmp7 = tl.sum(tmp6, 1)[:, None]
    tmp8 = tl.full([XBLOCK, 1], 64, tl.int32)
    tmp9 = tmp8.to(tl.float32)
    tmp10 = tmp7 / tmp9
    tmp11 = tmp1 - tmp10
    tmp12 = tmp11 * tmp11
    tmp13 = tl.broadcast_to(tmp12, [XBLOCK, RBLOCK])
    tmp15 = tl.where(xmask, tmp13, 0)
    tmp16 = tl.sum(tmp15, 1)[:, None]
    tmp17 = tmp0 - tmp10
    tmp18 = 64.0
    tmp19 = tmp16 / tmp18
    tmp20 = 1e-05
    tmp21 = tmp19 + tmp20
    tmp22 = libdevice.rsqrt(tmp21)
    tmp23 = tmp17 * tmp22
    tmp25 = tmp23 * tmp24
    tmp27 = tmp25 + tmp26
    tmp28 = 0.0
    tmp29 = tmp27 > tmp28
    tmp30 = 0.01
    tmp31 = tmp27 * tmp30
    tmp32 = tl.where(tmp29, tmp27, tmp31)
    tmp33 = tmp32 > tmp28
    tl.store(in_out_ptr0 + (r1 + 64 * x0), tmp32, xmask)
    tl.store(out_ptr2 + (r1 + 64 * x0), tmp33, xmask)
    tl.store(out_ptr3 + x0, tmp22, xmask)
    tl.store(out_ptr0 + x0, tmp10, xmask)


@triton.jit
def triton_per_fused__native_batch_norm_legit_add_1(in_out_ptr0,
    in_out_ptr1, in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0, xnumel,
    rnumel, XBLOCK: tl.constexpr):
    xnumel = 4
    RBLOCK: tl.constexpr = 64
    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)
    r1 = rindex
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (r1 + 64 * x0), xmask, other=0.0)
    tmp24 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp26 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
    tmp33 = tl.load(in_ptr3 + (r1 + 64 * x0), xmask, other=0.0)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tl.where(xmask, tmp1, 0)
    tmp4 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK])
    tmp6 = tl.where(xmask, tmp4, 0)
    tmp7 = tl.sum(tmp6, 1)[:, None]
    tmp8 = tl.full([XBLOCK, 1], 64, tl.int32)
    tmp9 = tmp8.to(tl.float32)
    tmp10 = tmp7 / tmp9
    tmp11 = tmp1 - tmp10
    tmp12 = tmp11 * tmp11
    tmp13 = tl.broadcast_to(tmp12, [XBLOCK, RBLOCK])
    tmp15 = tl.where(xmask, tmp13, 0)
    tmp16 = tl.sum(tmp15, 1)[:, None]
    tmp17 = 64.0
    tmp18 = tmp16 / tmp17
    tmp19 = 1e-05
    tmp20 = tmp18 + tmp19
    tmp21 = libdevice.rsqrt(tmp20)
    tmp22 = tmp0 - tmp10
    tmp23 = tmp22 * tmp21
    tmp25 = tmp23 * tmp24
    tmp27 = tmp25 + tmp26
    tmp28 = 0.0
    tmp29 = tmp27 > tmp28
    tmp30 = 0.01
    tmp31 = tmp27 * tmp30
    tmp32 = tl.where(tmp29, tmp27, tmp31)
    tmp34 = tmp32 + tmp33
    tl.debug_barrier()
    tl.store(in_out_ptr0 + x0, tmp21, xmask)
    tl.store(in_out_ptr1 + (r1 + 64 * x0), tmp34, xmask)
    tl.store(out_ptr0 + x0, tmp10, 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, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_2, (4, 4, 3, 3, 3), (108, 27, 9, 3, 1))
    assert_size_stride(primals_3, (4,), (1,))
    assert_size_stride(primals_4, (4,), (1,))
    assert_size_stride(primals_5, (4, 4, 3, 3, 3), (108, 27, 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(reinterpret_tensor(primals_1, (1,
            4, 4, 4, 4), (256, 64, 16, 4, 1), 0), primals_2, stride=(1, 1, 
            1), padding=(1, 1, 1), dilation=(1, 1, 1), transposed=False,
            output_padding=(0, 0, 0), groups=1, bias=None)
        assert_size_stride(buf0, (1, 4, 4, 4, 4), (256, 64, 16, 4, 1))
        buf1 = empty_strided_cuda((1, 4, 1, 1, 1), (4, 1, 4, 4, 4), torch.
            float32)
        buf5 = empty_strided_cuda((1, 4, 4, 4, 4), (256, 64, 16, 4, 1),
            torch.float32)
        buf6 = reinterpret_tensor(buf5, (4, 4, 4, 4), (64, 16, 4, 1), 0)
        del buf5
        buf14 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool)
        buf4 = empty_strided_cuda((1, 4, 1, 1, 1), (4, 1, 4, 4, 4), torch.
            float32)
        get_raw_stream(0)
        triton_per_fused__native_batch_norm_legit_leaky_relu_leaky_relu_backward_0[
            grid(4)](buf6, buf0, primals_3, primals_4, buf1, buf14, buf4, 4,
            64, XBLOCK=1, num_warps=2, num_stages=1)
        del primals_4
        buf7 = extern_kernels.convolution(reinterpret_tensor(buf6, (1, 4, 4,
            4, 4), (0, 64, 16, 4, 1), 0), primals_5, stride=(1, 1, 1),
            padding=(1, 1, 1), dilation=(1, 1, 1), transposed=False,
            output_padding=(0, 0, 0), groups=1, bias=None)
        assert_size_stride(buf7, (1, 4, 4, 4, 4), (256, 64, 16, 4, 1))
        buf8 = empty_strided_cuda((1, 4, 1, 1, 1), (4, 1, 1, 1, 1), torch.
            float32)
        buf9 = empty_strided_cuda((1, 4, 1, 1, 1), (4, 1, 4, 4, 4), torch.
            float32)
        buf11 = reinterpret_tensor(buf9, (1, 4, 1, 1, 1), (4, 1, 1, 1, 1), 0)
        del buf9
        buf12 = empty_strided_cuda((1, 4, 4, 4, 4), (256, 64, 16, 4, 1),
            torch.float32)
        buf13 = reinterpret_tensor(buf12, (4, 4, 4, 4), (64, 16, 4, 1), 0)
        del buf12
        triton_per_fused__native_batch_norm_legit_add_1[grid(4)](buf11,
            buf13, buf7, primals_6, primals_7, primals_1, buf8, 4, 64,
            XBLOCK=1, num_warps=2, num_stages=1)
    return (buf13, primals_2, primals_3, primals_5, primals_6, primals_7,
        reinterpret_tensor(primals_1, (1, 4, 4, 4, 4), (256, 64, 16, 4, 1),
        0), buf0, reinterpret_tensor(buf4, (4,), (1,), 0),
        reinterpret_tensor(buf6, (1, 4, 4, 4, 4), (256, 64, 16, 4, 1), 0),
        buf7, buf8, buf11, buf14, reinterpret_tensor(buf1, (1, 4, 1, 1, 1),
        (4, 1, 1, 1, 1), 0))


def Activation_layer(activation_cfg, inplace=True):
    out = None
    if activation_cfg == 'ReLU':
        out = nn.ReLU(inplace=inplace)
    else:
        out = nn.LeakyReLU(negative_slope=0.01, inplace=inplace)
    return out


def Norm_layer(norm_cfg, inplanes):
    out = None
    if norm_cfg == 'BN':
        out = nn.BatchNorm3d(inplanes)
    elif norm_cfg == 'SyncBN':
        out = nn.SyncBatchNorm(inplanes)
    elif norm_cfg == 'GN':
        out = nn.GroupNorm(16, inplanes)
    else:
        out = nn.InstanceNorm3d(inplanes, affine=True)
    return out


def conv3x3x3(in_planes, out_planes, kernel_size, stride=(1, 1, 1), padding
    =(0, 0, 0), dilation=(1, 1, 1), groups=1, bias=False, weight_std=False):
    """3x3x3 convolution with padding"""
    if weight_std:
        return Conv3d_wd(in_planes, out_planes, kernel_size=kernel_size,
            stride=stride, padding=padding, dilation=dilation, groups=
            groups, bias=bias)
    else:
        return nn.Conv3d(in_planes, out_planes, kernel_size=kernel_size,
            stride=stride, padding=padding, dilation=dilation, groups=
            groups, bias=bias)


class Conv3d_wd(nn.Conv3d):

    def __init__(self, in_channels, out_channels, kernel_size, stride=(1, 1,
        1), padding=(0, 0, 0), dilation=(1, 1, 1), groups=1, bias=False):
        super(Conv3d_wd, self).__init__(in_channels, out_channels,
            kernel_size, stride, padding, dilation, groups, bias)

    def forward(self, x):
        weight = self.weight
        weight_mean = weight.mean(dim=1, keepdim=True).mean(dim=2, keepdim=True
            ).mean(dim=3, keepdim=True).mean(dim=4, keepdim=True)
        weight = weight - weight_mean
        std = torch.sqrt(torch.var(weight.view(weight.size(0), -1), dim=1) +
            1e-12).view(-1, 1, 1, 1, 1)
        weight = weight / std.expand_as(weight)
        return F.conv3d(x, weight, self.bias, self.stride, self.padding,
            self.dilation, self.groups)


class Conv3dBlock(nn.Module):

    def __init__(self, in_channels, out_channels, norm_cfg, activation_cfg,
        kernel_size, stride=(1, 1, 1), padding=(0, 0, 0), dilation=(1, 1, 1
        ), bias=False, weight_std=False):
        super(Conv3dBlock, self).__init__()
        self.conv = conv3x3x3(in_channels, out_channels, kernel_size=
            kernel_size, stride=stride, padding=padding, dilation=dilation,
            bias=bias, weight_std=weight_std)
        self.norm = Norm_layer(norm_cfg, out_channels)
        self.nonlin = Activation_layer(activation_cfg, inplace=True)

    def forward(self, x):
        x = self.conv(x)
        x = self.norm(x)
        x = self.nonlin(x)
        return x


class ResBlockNew(nn.Module):

    def __init__(self, inplanes, planes, norm_cfg, activation_cfg,
        weight_std=False):
        super(ResBlockNew, self).__init__()
        self.resconv1 = Conv3dBlock(inplanes, planes, norm_cfg,
            activation_cfg, kernel_size=3, stride=1, padding=1, bias=False,
            weight_std=weight_std)
        self.resconv2 = Conv3dBlock(planes, planes, norm_cfg,
            activation_cfg, kernel_size=3, stride=1, padding=1, bias=False,
            weight_std=weight_std)

    def forward(self, input_0):
        primals_2 = self.resconv1.conv.weight
        primals_3 = self.resconv1.norm.weight
        primals_4 = self.resconv1.norm.bias
        primals_5 = self.resconv2.conv.weight
        primals_6 = self.resconv2.norm.weight
        primals_7 = self.resconv2.norm.bias
        primals_1 = 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.031078