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_0(in_ptr0, out_ptr0, out_ptr1,
    out_ptr2, xnumel, rnumel, XBLOCK: tl.constexpr, RBLOCK: tl.constexpr):
    xnumel = 64
    rnumel = 8192
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rbase = tl.arange(0, RBLOCK)[None, :]
    x0 = xindex
    tmp2_mean = tl.zeros([XBLOCK, RBLOCK], tl.float32)
    tmp2_m2 = tl.zeros([XBLOCK, RBLOCK], tl.float32)
    tmp2_weight = tl.zeros([XBLOCK, RBLOCK], tl.float32)
    for roffset in range(0, rnumel, RBLOCK):
        rindex = roffset + rbase
        rmask = rindex < rnumel
        r1 = rindex
        tmp0 = tl.load(in_ptr0 + (r1 + 8192 * x0), rmask & xmask,
            eviction_policy='evict_first', other=0.0)
        tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
        tmp2_mean_next, tmp2_m2_next, tmp2_weight_next = (triton_helpers.
            welford_reduce(tmp1, tmp2_mean, tmp2_m2, tmp2_weight, roffset == 0)
            )
        tmp2_mean = tl.where(rmask & xmask, tmp2_mean_next, tmp2_mean)
        tmp2_m2 = tl.where(rmask & xmask, tmp2_m2_next, tmp2_m2)
        tmp2_weight = tl.where(rmask & xmask, tmp2_weight_next, tmp2_weight)
    tmp2_tmp, tmp3_tmp, tmp4_tmp = triton_helpers.welford(tmp2_mean,
        tmp2_m2, tmp2_weight, 1)
    tmp2 = tmp2_tmp[:, None]
    tmp3 = tmp3_tmp[:, None]
    tmp4 = tmp4_tmp[:, None]
    tl.store(out_ptr0 + x0, tmp2, xmask)
    tl.store(out_ptr1 + x0, tmp3, xmask)
    tl.store(out_ptr2 + x0, tmp4, 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 = 16
    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 = 32768.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_leaky_relu_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 // 32768
    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 = 32768.0
    tmp5 = tmp3 / tmp4
    tmp6 = 1e-05
    tmp7 = tmp5 + tmp6
    tmp8 = libdevice.rsqrt(tmp7)
    tmp9 = tmp2 * tmp8
    tmp10 = 0.0
    tmp11 = tmp9 > tmp10
    tmp12 = 0.02
    tmp13 = tmp9 * tmp12
    tmp14 = tl.where(tmp11, tmp9, tmp13)
    tl.store(out_ptr0 + x2, tmp14, None)


@triton.jit
def triton_red_fused__native_batch_norm_legit_leaky_relu_3(in_ptr0,
    out_ptr0, out_ptr2, out_ptr3, xnumel, rnumel, XBLOCK: tl.constexpr,
    RBLOCK: tl.constexpr):
    xnumel = 32
    rnumel = 4096
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rbase = tl.arange(0, RBLOCK)[None, :]
    x0 = xindex
    tmp2_mean = tl.zeros([XBLOCK, RBLOCK], tl.float32)
    tmp2_m2 = tl.zeros([XBLOCK, RBLOCK], tl.float32)
    tmp2_weight = tl.zeros([XBLOCK, RBLOCK], tl.float32)
    for roffset in range(0, rnumel, RBLOCK):
        rindex = roffset + rbase
        rmask = rindex < rnumel
        r1 = rindex
        tmp0 = tl.load(in_ptr0 + (r1 + 4096 * x0), rmask & xmask,
            eviction_policy='evict_last', other=0.0)
        tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
        tmp2_mean_next, tmp2_m2_next, tmp2_weight_next = (triton_helpers.
            welford_reduce(tmp1, tmp2_mean, tmp2_m2, tmp2_weight, roffset == 0)
            )
        tmp2_mean = tl.where(rmask & xmask, tmp2_mean_next, tmp2_mean)
        tmp2_m2 = tl.where(rmask & xmask, tmp2_m2_next, tmp2_m2)
        tmp2_weight = tl.where(rmask & xmask, tmp2_weight_next, tmp2_weight)
    tmp2_tmp, tmp3_tmp, tmp4_tmp = triton_helpers.welford(tmp2_mean,
        tmp2_m2, tmp2_weight, 1)
    tmp2 = tmp2_tmp[:, None]
    tmp3 = tmp3_tmp[:, None]
    tmp4_tmp[:, None]
    tl.store(out_ptr0 + x0, tmp2, xmask)
    for roffset in range(0, rnumel, RBLOCK):
        rindex = roffset + rbase
        rmask = rindex < rnumel
        r1 = rindex
        tmp5 = tl.load(in_ptr0 + (r1 + 4096 * x0), rmask & xmask,
            eviction_policy='evict_first', other=0.0)
        tmp6 = tmp5 - tmp2
        tmp7 = 4096.0
        tmp8 = tmp3 / tmp7
        tmp9 = 1e-05
        tmp10 = tmp8 + tmp9
        tmp11 = libdevice.rsqrt(tmp10)
        tmp12 = tmp6 * tmp11
        tmp13 = 0.0
        tmp14 = tmp12 > tmp13
        tmp15 = 0.02
        tmp16 = tmp12 * tmp15
        tmp17 = tl.where(tmp14, tmp12, tmp16)
        tl.store(out_ptr2 + (r1 + 4096 * x0), tmp17, rmask & xmask)
    tmp18 = 4096.0
    tmp19 = tmp3 / tmp18
    tmp20 = 1e-05
    tmp21 = tmp19 + tmp20
    tmp22 = libdevice.rsqrt(tmp21)
    tl.store(out_ptr3 + x0, tmp22, xmask)


@triton.jit
def triton_per_fused__native_batch_norm_legit_leaky_relu_4(in_ptr0,
    out_ptr0, out_ptr2, out_ptr3, xnumel, rnumel):
    XBLOCK: tl.constexpr = 1
    RBLOCK: tl.constexpr = 512
    xoffset = tl.program_id(0) * XBLOCK
    xindex = tl.full([1], xoffset, tl.int32)
    tl.full([RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[:]
    tl.full([RBLOCK], True, tl.int1)
    r1 = rindex
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (r1 + 512 * x0), None)
    tmp1 = tl.broadcast_to(tmp0, [RBLOCK])
    tmp3 = tl.broadcast_to(tmp1, [RBLOCK])
    tmp5 = triton_helpers.promote_to_tensor(tl.sum(tmp3, 0))
    tmp6 = tl.full([1], 512, tl.int32)
    tmp7 = tmp6.to(tl.float32)
    tmp8 = tmp5 / tmp7
    tmp9 = tmp1 - tmp8
    tmp10 = tmp9 * tmp9
    tmp11 = tl.broadcast_to(tmp10, [RBLOCK])
    tmp13 = triton_helpers.promote_to_tensor(tl.sum(tmp11, 0))
    tmp14 = tmp0 - tmp8
    tmp15 = 512.0
    tmp16 = tmp13 / tmp15
    tmp17 = 1e-05
    tmp18 = tmp16 + tmp17
    tmp19 = libdevice.rsqrt(tmp18)
    tmp20 = tmp14 * tmp19
    tmp21 = 0.0
    tmp22 = tmp20 > tmp21
    tmp23 = 0.02
    tmp24 = tmp20 * tmp23
    tmp25 = tl.where(tmp22, tmp20, tmp24)
    tl.store(out_ptr2 + (r1 + 512 * x0), tmp25, None)
    tl.store(out_ptr3 + x0, tmp19, None)
    tl.store(out_ptr0 + x0, tmp8, None)


@triton.jit
def triton_per_fused__native_batch_norm_legit_leaky_relu_5(in_ptr0,
    out_ptr0, out_ptr2, out_ptr3, xnumel, rnumel, XBLOCK: tl.constexpr):
    xnumel = 128
    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)
    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
    tmp24 = 0.0
    tmp25 = tmp23 > tmp24
    tmp26 = 0.02
    tmp27 = tmp23 * tmp26
    tmp28 = tl.where(tmp25, tmp23, tmp27)
    tl.store(out_ptr2 + (r1 + 64 * x0), tmp28, xmask)
    tl.store(out_ptr3 + x0, tmp22, xmask)
    tl.store(out_ptr0 + x0, tmp10, xmask)


@triton.jit
def triton_poi_fused_convolution_leaky_relu_leaky_relu_backward_sigmoid_6(
    in_ptr0, in_ptr1, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = 0.0
    tmp4 = tmp2 > tmp3
    tmp5 = 0.02
    tmp6 = tmp2 * tmp5
    tmp7 = tl.where(tmp4, tmp2, tmp6)
    tmp8 = tl.sigmoid(tmp7)
    tmp9 = tmp7 > tmp3
    tl.store(out_ptr0 + x2, tmp8, xmask)
    tl.store(out_ptr1 + x2, tmp9, 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, 1, 4, 4, 4), (64, 64, 16, 4, 1))
    assert_size_stride(primals_2, (4, 1, 64, 64, 64), (262144, 262144, 4096,
        64, 1))
    assert_size_stride(primals_3, (8, 4, 4, 4, 4), (256, 64, 16, 4, 1))
    assert_size_stride(primals_4, (16, 8, 4, 4, 4), (512, 64, 16, 4, 1))
    assert_size_stride(primals_5, (32, 16, 4, 4, 4), (1024, 64, 16, 4, 1))
    assert_size_stride(primals_6, (4, 32, 4, 4, 4), (2048, 64, 16, 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=(2, 
            2, 2), padding=(1, 1, 1), dilation=(1, 1, 1), transposed=False,
            output_padding=(0, 0, 0), groups=1, bias=None)
        assert_size_stride(buf0, (4, 4, 32, 32, 32), (131072, 32768, 1024, 
            32, 1))
        buf1 = empty_strided_cuda((1, 16, 1, 1, 1, 4), (64, 4, 64, 64, 64, 
            1), torch.float32)
        buf2 = empty_strided_cuda((1, 16, 1, 1, 1, 4), (64, 4, 64, 64, 64, 
            1), torch.float32)
        buf3 = empty_strided_cuda((1, 16, 1, 1, 1, 4), (64, 4, 64, 64, 64, 
            1), torch.float32)
        get_raw_stream(0)
        triton_red_fused__native_batch_norm_legit_0[grid(64)](buf0, buf1,
            buf2, buf3, 64, 8192, XBLOCK=1, RBLOCK=2048, num_warps=16,
            num_stages=1)
        buf4 = empty_strided_cuda((1, 16, 1, 1, 1), (16, 1, 16, 16, 16),
            torch.float32)
        buf5 = empty_strided_cuda((1, 16, 1, 1, 1), (16, 1, 16, 16, 16),
            torch.float32)
        buf7 = empty_strided_cuda((1, 16, 1, 1, 1), (16, 1, 16, 16, 16),
            torch.float32)
        triton_per_fused__native_batch_norm_legit_1[grid(16)](buf1, buf2,
            buf3, buf4, buf5, buf7, 16, 4, XBLOCK=1, num_warps=2, num_stages=1)
        del buf1
        buf8 = empty_strided_cuda((4, 4, 32, 32, 32), (131072, 32768, 1024,
            32, 1), torch.float32)
        triton_poi_fused_leaky_relu_2[grid(524288)](buf0, buf4, buf5, buf8,
            524288, XBLOCK=1024, num_warps=4, num_stages=1)
        buf9 = extern_kernels.convolution(buf8, primals_3, stride=(2, 2, 2),
            padding=(1, 1, 1), dilation=(1, 1, 1), transposed=False,
            output_padding=(0, 0, 0), groups=1, bias=None)
        assert_size_stride(buf9, (4, 8, 16, 16, 16), (32768, 4096, 256, 16, 1))
        buf10 = empty_strided_cuda((1, 32, 1, 1, 1), (32, 1, 32, 32, 32),
            torch.float32)
        buf14 = empty_strided_cuda((4, 8, 16, 16, 16), (32768, 4096, 256, 
            16, 1), torch.float32)
        buf13 = empty_strided_cuda((1, 32, 1, 1, 1), (32, 1, 32, 32, 32),
            torch.float32)
        triton_red_fused__native_batch_norm_legit_leaky_relu_3[grid(32)](buf9,
            buf10, buf14, buf13, 32, 4096, XBLOCK=1, RBLOCK=2048, num_warps
            =16, num_stages=1)
        buf15 = extern_kernels.convolution(buf14, primals_4, stride=(2, 2, 
            2), padding=(1, 1, 1), dilation=(1, 1, 1), transposed=False,
            output_padding=(0, 0, 0), groups=1, bias=None)
        assert_size_stride(buf15, (4, 16, 8, 8, 8), (8192, 512, 64, 8, 1))
        buf16 = reinterpret_tensor(buf3, (1, 64, 1, 1, 1), (64, 1, 64, 64, 
            64), 0)
        del buf3
        buf20 = empty_strided_cuda((4, 16, 8, 8, 8), (8192, 512, 64, 8, 1),
            torch.float32)
        buf19 = reinterpret_tensor(buf2, (1, 64, 1, 1, 1), (64, 1, 64, 64, 
            64), 0)
        del buf2
        triton_per_fused__native_batch_norm_legit_leaky_relu_4[grid(64)](buf15,
            buf16, buf20, buf19, 64, 512, num_warps=4, num_stages=1)
        buf21 = extern_kernels.convolution(buf20, primals_5, stride=(2, 2, 
            2), padding=(1, 1, 1), dilation=(1, 1, 1), transposed=False,
            output_padding=(0, 0, 0), groups=1, bias=None)
        assert_size_stride(buf21, (4, 32, 4, 4, 4), (2048, 64, 16, 4, 1))
        buf22 = empty_strided_cuda((1, 128, 1, 1, 1), (128, 1, 128, 128, 
            128), torch.float32)
        buf26 = empty_strided_cuda((4, 32, 4, 4, 4), (2048, 64, 16, 4, 1),
            torch.float32)
        buf25 = empty_strided_cuda((1, 128, 1, 1, 1), (128, 1, 128, 128, 
            128), torch.float32)
        triton_per_fused__native_batch_norm_legit_leaky_relu_5[grid(128)](buf21
            , buf22, buf26, buf25, 128, 64, XBLOCK=32, num_warps=8,
            num_stages=1)
        buf27 = extern_kernels.convolution(buf26, primals_6, stride=(1, 1, 
            1), padding=(0, 0, 0), dilation=(1, 1, 1), transposed=False,
            output_padding=(0, 0, 0), groups=1, bias=None)
        assert_size_stride(buf27, (4, 4, 1, 1, 1), (4, 1, 1, 1, 1))
        buf28 = reinterpret_tensor(buf5, (4, 4), (4, 1), 0)
        del buf5
        buf29 = empty_strided_cuda((4, 4, 1, 1, 1), (4, 1, 1, 1, 1), torch.bool
            )
        triton_poi_fused_convolution_leaky_relu_leaky_relu_backward_sigmoid_6[
            grid(16)](buf27, primals_7, buf28, buf29, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        del buf27
        del primals_7
    return (buf28, primals_1, primals_2, primals_3, primals_4, primals_5,
        primals_6, buf0, reinterpret_tensor(buf7, (16,), (1,), 0), buf8,
        buf9, reinterpret_tensor(buf13, (32,), (1,), 0), buf14, buf15,
        reinterpret_tensor(buf19, (64,), (1,), 0), buf20, buf21,
        reinterpret_tensor(buf25, (128,), (1,), 0), buf26, buf28, buf29,
        reinterpret_tensor(buf22, (1, 128, 1, 1, 1), (128, 1, 1, 1, 1), 0),
        reinterpret_tensor(buf16, (1, 64, 1, 1, 1), (64, 1, 1, 1, 1), 0),
        reinterpret_tensor(buf10, (1, 32, 1, 1, 1), (32, 1, 1, 1, 1), 0),
        reinterpret_tensor(buf4, (1, 16, 1, 1, 1), (16, 1, 1, 1, 1), 0))


class encoderNew(nn.Module):

    def __init__(self, ef_dim, z_dim):
        super(encoderNew, self).__init__()
        self.ef_dim = ef_dim
        self.z_dim = z_dim
        self.conv_1 = nn.Conv3d(1, self.ef_dim, 4, stride=2, padding=1,
            bias=False)
        self.in_1 = nn.InstanceNorm3d(self.ef_dim)
        self.conv_2 = nn.Conv3d(self.ef_dim, self.ef_dim * 2, 4, stride=2,
            padding=1, bias=False)
        self.in_2 = nn.InstanceNorm3d(self.ef_dim * 2)
        self.conv_3 = nn.Conv3d(self.ef_dim * 2, self.ef_dim * 4, 4, stride
            =2, padding=1, bias=False)
        self.in_3 = nn.InstanceNorm3d(self.ef_dim * 4)
        self.conv_4 = nn.Conv3d(self.ef_dim * 4, self.ef_dim * 8, 4, stride
            =2, padding=1, bias=False)
        self.in_4 = nn.InstanceNorm3d(self.ef_dim * 8)
        self.conv_5 = nn.Conv3d(self.ef_dim * 8, self.z_dim, 4, stride=1,
            padding=0, bias=True)
        nn.init.xavier_uniform_(self.conv_1.weight)
        nn.init.xavier_uniform_(self.conv_2.weight)
        nn.init.xavier_uniform_(self.conv_3.weight)
        nn.init.xavier_uniform_(self.conv_4.weight)
        nn.init.xavier_uniform_(self.conv_5.weight)
        nn.init.constant_(self.conv_5.bias, 0)

    def forward(self, input_0):
        primals_1 = self.conv_1.weight
        primals_3 = self.conv_2.weight
        primals_4 = self.conv_3.weight
        primals_5 = self.conv_4.weight
        primals_6 = self.conv_5.weight
        primals_7 = self.conv_5.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.289216