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_red_fused__native_batch_norm_legit_convolution_0(in_out_ptr0,
    in_ptr0, out_ptr0, out_ptr1, out_ptr2, xnumel, rnumel, XBLOCK: tl.
    constexpr, RBLOCK: tl.constexpr):
    xnumel = 512
    rnumel = 65536
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rbase = tl.arange(0, RBLOCK)[None, :]
    x4 = xindex
    x1 = xindex // 4 % 32
    tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
    tmp4_mean = tl.zeros([XBLOCK, RBLOCK], tl.float32)
    tmp4_m2 = tl.zeros([XBLOCK, RBLOCK], tl.float32)
    tmp4_weight = tl.zeros([XBLOCK, RBLOCK], tl.float32)
    for roffset in range(0, rnumel, RBLOCK):
        rindex = roffset + rbase
        tl.full([XBLOCK, RBLOCK], True, tl.int1)
        r3 = rindex
        tmp0 = tl.load(in_out_ptr0 + (r3 + 65536 * x4), xmask,
            eviction_policy='evict_first', other=0.0)
        tmp2 = tmp0 + tmp1
        tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
        tmp4_mean_next, tmp4_m2_next, tmp4_weight_next = (triton_helpers.
            welford_reduce(tmp3, tmp4_mean, tmp4_m2, tmp4_weight, roffset == 0)
            )
        tmp4_mean = tl.where(xmask, tmp4_mean_next, tmp4_mean)
        tmp4_m2 = tl.where(xmask, tmp4_m2_next, tmp4_m2)
        tmp4_weight = tl.where(xmask, tmp4_weight_next, tmp4_weight)
        tl.store(in_out_ptr0 + (r3 + 65536 * x4), tmp2, xmask)
    tmp4_tmp, tmp5_tmp, tmp6_tmp = triton_helpers.welford(tmp4_mean,
        tmp4_m2, tmp4_weight, 1)
    tmp4 = tmp4_tmp[:, None]
    tmp5 = tmp5_tmp[:, None]
    tmp6 = tmp6_tmp[:, None]
    tl.store(out_ptr0 + x4, tmp4, xmask)
    tl.store(out_ptr1 + x4, tmp5, xmask)
    tl.store(out_ptr2 + x4, tmp6, xmask)


@triton.jit
def triton_per_fused__native_batch_norm_legit_1(in_ptr0, in_ptr1, in_ptr2,
    out_ptr0, out_ptr1, out_ptr2, xnumel, rnumel, XBLOCK: tl.constexpr):
    xnumel = 128
    RBLOCK: tl.constexpr = 4
    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 + 4 * x0), xmask, other=0.0)
    tmp1 = tl.load(in_ptr1 + (r1 + 4 * x0), xmask, other=0.0)
    tmp2 = tl.load(in_ptr2 + (r1 + 4 * x0), xmask, other=0.0)
    tmp3 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp4 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK])
    tmp5 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
    tmp7 = tl.where(xmask, tmp3, 0)
    tmp8 = tl.where(xmask, tmp4, 0)
    tmp9 = tl.where(xmask, tmp5, 0)
    tmp10, tmp11, tmp12 = triton_helpers.welford(tmp7, tmp8, tmp9, 1)
    tmp13 = tmp10[:, None]
    tmp14 = tmp11[:, None]
    tmp12[:, None]
    tmp16 = 262144.0
    tmp17 = tmp14 / tmp16
    tmp18 = 1e-05
    tmp19 = tmp17 + tmp18
    tmp20 = libdevice.rsqrt(tmp19)
    tl.store(out_ptr2 + x0, tmp20, xmask)
    tl.store(out_ptr0 + x0, tmp13, xmask)
    tl.store(out_ptr1 + x0, tmp14, xmask)


@triton.jit
def triton_poi_fused_elu_2(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    tl.full([XBLOCK], True, tl.int1)
    x2 = xindex
    x1 = xindex // 262144
    tmp0 = tl.load(in_ptr0 + x2, None)
    tmp1 = tl.load(in_ptr1 + x1, None, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr2 + x1, None, eviction_policy='evict_last')
    tmp2 = tmp0 - tmp1
    tmp4 = 262144.0
    tmp5 = tmp3 / tmp4
    tmp6 = 1e-05
    tmp7 = tmp5 + tmp6
    tmp8 = libdevice.rsqrt(tmp7)
    tmp9 = tmp2 * tmp8
    tmp10 = 0.0
    tmp11 = tmp9 > tmp10
    tmp12 = 1.0
    tmp13 = tmp9 * tmp12
    tmp14 = libdevice.expm1(tmp13)
    tmp15 = tmp14 * tmp12
    tmp16 = tl.where(tmp11, tmp13, tmp15)
    tl.store(out_ptr0 + x2, tmp16, None)


def call(args):
    primals_1, primals_2, primals_3, primals_4, primals_5 = args
    args.clear()
    assert_size_stride(primals_1, (32, 1, 3, 3, 3), (27, 27, 9, 3, 1))
    assert_size_stride(primals_2, (32,), (1,))
    assert_size_stride(primals_3, (4, 1, 64, 64, 64), (262144, 262144, 4096,
        64, 1))
    assert_size_stride(primals_4, (32, 32, 3, 3, 3), (864, 27, 9, 3, 1))
    assert_size_stride(primals_5, (32,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = extern_kernels.convolution(primals_3, primals_1, 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, (4, 32, 64, 64, 64), (8388608, 262144, 
            4096, 64, 1))
        buf1 = buf0
        del buf0
        buf2 = empty_strided_cuda((1, 128, 1, 1, 1, 4), (512, 4, 512, 512, 
            512, 1), torch.float32)
        buf3 = empty_strided_cuda((1, 128, 1, 1, 1, 4), (512, 4, 512, 512, 
            512, 1), torch.float32)
        buf4 = empty_strided_cuda((1, 128, 1, 1, 1, 4), (512, 4, 512, 512, 
            512, 1), torch.float32)
        get_raw_stream(0)
        triton_red_fused__native_batch_norm_legit_convolution_0[grid(512)](buf1
            , primals_2, buf2, buf3, buf4, 512, 65536, XBLOCK=1, RBLOCK=
            2048, num_warps=16, num_stages=1)
        del primals_2
        buf5 = empty_strided_cuda((1, 128, 1, 1, 1), (128, 1, 128, 128, 128
            ), torch.float32)
        buf6 = empty_strided_cuda((1, 128, 1, 1, 1), (128, 1, 128, 128, 128
            ), torch.float32)
        buf8 = empty_strided_cuda((1, 128, 1, 1, 1), (128, 1, 128, 128, 128
            ), torch.float32)
        triton_per_fused__native_batch_norm_legit_1[grid(128)](buf2, buf3,
            buf4, buf5, buf6, buf8, 128, 4, XBLOCK=32, num_warps=2,
            num_stages=1)
        buf9 = empty_strided_cuda((4, 32, 64, 64, 64), (8388608, 262144, 
            4096, 64, 1), torch.float32)
        triton_poi_fused_elu_2[grid(33554432)](buf1, buf5, buf6, buf9, 
            33554432, XBLOCK=1024, num_warps=4, num_stages=1)
        buf10 = extern_kernels.convolution(buf9, primals_4, 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(buf10, (4, 32, 64, 64, 64), (8388608, 262144, 
            4096, 64, 1))
        buf11 = buf10
        del buf10
        buf12 = buf4
        del buf4
        buf13 = buf3
        del buf3
        buf14 = buf2
        del buf2
        triton_red_fused__native_batch_norm_legit_convolution_0[grid(512)](
            buf11, primals_5, buf12, buf13, buf14, 512, 65536, XBLOCK=1,
            RBLOCK=2048, num_warps=16, num_stages=1)
        del primals_5
        buf15 = buf6
        del buf6
        buf16 = empty_strided_cuda((1, 128, 1, 1, 1), (128, 1, 128, 128, 
            128), torch.float32)
        buf18 = empty_strided_cuda((1, 128, 1, 1, 1), (128, 1, 128, 128, 
            128), torch.float32)
        triton_per_fused__native_batch_norm_legit_1[grid(128)](buf12, buf13,
            buf14, buf15, buf16, buf18, 128, 4, XBLOCK=32, num_warps=2,
            num_stages=1)
        del buf12
        del buf13
        del buf14
        buf19 = empty_strided_cuda((4, 32, 64, 64, 64), (8388608, 262144, 
            4096, 64, 1), torch.float32)
        triton_poi_fused_elu_2[grid(33554432)](buf11, buf15, buf16, buf19, 
            33554432, XBLOCK=1024, num_warps=4, num_stages=1)
        del buf16
    return buf19, primals_1, primals_3, primals_4, buf1, reinterpret_tensor(
        buf8, (128,), (1,), 0), buf9, buf11, reinterpret_tensor(buf18, (128
        ,), (1,), 0), buf19, reinterpret_tensor(buf15, (1, 128, 1, 1, 1), (
        128, 1, 1, 1, 1), 0), reinterpret_tensor(buf5, (1, 128, 1, 1, 1), (
        128, 1, 1, 1, 1), 0)


def ELUCons(elu, nchan):
    if elu:
        return nn.ELU(inplace=True)
    else:
        return nn.PReLU(nchan)


class InputTransitionNew(nn.Module):

    def __init__(self, outChans, elu):
        super(InputTransitionNew, self).__init__()
        self.conv1 = nn.Conv3d(1, 32, kernel_size=3, padding=1)
        self.bn1 = nn.InstanceNorm3d(32)
        self.relu1 = ELUCons(elu, 32)
        self.conv2 = nn.Conv3d(32, 32, kernel_size=3, padding=1)
        self.bn2 = nn.InstanceNorm3d(32)
        self.relu2 = ELUCons(elu, 32)

    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]


Measured latency_ms: 0.037652