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
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 = 256
    rnumel = 8192
    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 // 32 % 2
    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
        rmask = rindex < rnumel
        r3 = rindex
        tmp0 = tl.load(in_out_ptr0 + (r3 + 8192 * x4), rmask & 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(rmask & xmask, tmp4_mean_next, tmp4_mean)
        tmp4_m2 = tl.where(rmask & xmask, tmp4_m2_next, tmp4_m2)
        tmp4_weight = tl.where(rmask & xmask, tmp4_weight_next, tmp4_weight)
        tl.store(in_out_ptr0 + (r3 + 8192 * x4), tmp2, rmask & 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_poi_fused_repeat_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 8
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + x0 % 2, xmask)
    tl.store(out_ptr0 + x0, tmp0, xmask)


@triton.jit
def triton_per_fused__native_batch_norm_legit_2(in_ptr0, in_ptr1, in_ptr2,
    out_ptr0, out_ptr1, out_ptr2, xnumel, rnumel, XBLOCK: tl.constexpr):
    xnumel = 8
    RBLOCK: tl.constexpr = 32
    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 + 32 * x0), xmask, other=0.0)
    tmp1 = tl.load(in_ptr1 + (r1 + 32 * x0), xmask, other=0.0)
    tmp2 = tl.load(in_ptr2 + (r1 + 32 * 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_relu_3(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4,
    out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    tl.full([XBLOCK], True, tl.int1)
    x3 = xindex
    x4 = xindex // 262144
    x1 = xindex // 262144 % 2
    tmp0 = tl.load(in_ptr0 + x3, None)
    tmp1 = tl.load(in_ptr1 + x4, None, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr2 + x4, None, eviction_policy='evict_last')
    tmp10 = tl.load(in_ptr3 + x4, None, eviction_policy='evict_last')
    tmp12 = tl.load(in_ptr4 + 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
    tmp11 = tmp9 * tmp10
    tmp13 = tmp11 + tmp12
    tmp14 = tl.full([1], 0, tl.int32)
    tmp15 = triton_helpers.maximum(tmp14, tmp13)
    tl.store(out_ptr0 + x3, tmp15, None)


@triton.jit
def triton_poi_fused__to_copy_add_arange_mul_4(out_ptr0, xnumel, XBLOCK: tl
    .constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = x0
    tmp1 = tmp0.to(tl.float32)
    tmp2 = 0.5
    tmp3 = tmp1 * tmp2
    tmp4 = tmp3.to(tl.int32)
    tl.store(out_ptr0 + x0, tmp4, xmask)


@triton.jit
def triton_red_fused__unsafe_index_avg_pool3d_mean_5(in_ptr0, in_ptr1,
    out_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr, RBLOCK: tl.constexpr):
    xnumel = 256
    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 % 32
    x1 = xindex // 32
    x5 = xindex
    _tmp31 = tl.full([XBLOCK, RBLOCK], 0, tl.float32)
    for roffset in range(0, rnumel, RBLOCK):
        rindex = roffset + rbase
        rmask = rindex < rnumel
        r4 = rindex // 4096
        r3 = rindex // 64 % 64
        r2 = rindex % 64
        tmp0 = tl.load(in_ptr0 + (r4 + 2 * x0), rmask & xmask,
            eviction_policy='evict_last', other=0.0)
        tmp5 = tl.load(in_ptr0 + r3, rmask, eviction_policy='evict_last',
            other=0.0)
        tmp9 = tl.load(in_ptr0 + r2, rmask, eviction_policy='evict_last',
            other=0.0)
        tmp1 = tl.full([XBLOCK, RBLOCK], 32, tl.int32)
        tmp2 = tmp0 + tmp1
        tmp3 = tmp0 < 0
        tmp4 = tl.where(tmp3, tmp2, tmp0)
        tmp6 = tmp5 + tmp1
        tmp7 = tmp5 < 0
        tmp8 = tl.where(tmp7, tmp6, tmp5)
        tmp10 = tmp9 + tmp1
        tmp11 = tmp9 < 0
        tmp12 = tl.where(tmp11, tmp10, tmp9)
        tmp13 = tl.load(in_ptr1 + (2 * tmp12 + 128 * tmp8 + 8192 * tmp4 + 
            262144 * x1), rmask & xmask, eviction_policy='evict_last')
        tmp14 = tl.load(in_ptr1 + (1 + 2 * tmp12 + 128 * tmp8 + 8192 * tmp4 +
            262144 * x1), rmask & xmask, eviction_policy='evict_last')
        tmp15 = tmp14 + tmp13
        tmp16 = tl.load(in_ptr1 + (64 + 2 * tmp12 + 128 * tmp8 + 8192 *
            tmp4 + 262144 * x1), rmask & xmask, eviction_policy='evict_last')
        tmp17 = tmp16 + tmp15
        tmp18 = tl.load(in_ptr1 + (65 + 2 * tmp12 + 128 * tmp8 + 8192 *
            tmp4 + 262144 * x1), rmask & xmask, eviction_policy='evict_last')
        tmp19 = tmp18 + tmp17
        tmp20 = tl.load(in_ptr1 + (4096 + 2 * tmp12 + 128 * tmp8 + 8192 *
            tmp4 + 262144 * x1), rmask & xmask, eviction_policy='evict_last')
        tmp21 = tmp20 + tmp19
        tmp22 = tl.load(in_ptr1 + (4097 + 2 * tmp12 + 128 * tmp8 + 8192 *
            tmp4 + 262144 * x1), rmask & xmask, eviction_policy='evict_last')
        tmp23 = tmp22 + tmp21
        tmp24 = tl.load(in_ptr1 + (4160 + 2 * tmp12 + 128 * tmp8 + 8192 *
            tmp4 + 262144 * x1), rmask & xmask, eviction_policy='evict_last')
        tmp25 = tmp24 + tmp23
        tmp26 = tl.load(in_ptr1 + (4161 + 2 * tmp12 + 128 * tmp8 + 8192 *
            tmp4 + 262144 * x1), rmask & xmask, eviction_policy='evict_last')
        tmp27 = tmp26 + tmp25
        tmp28 = 0.125
        tmp29 = tmp27 * tmp28
        tmp30 = tl.broadcast_to(tmp29, [XBLOCK, RBLOCK])
        tmp32 = _tmp31 + tmp30
        _tmp31 = tl.where(rmask & xmask, tmp32, _tmp31)
    tmp31 = tl.sum(_tmp31, 1)[:, None]
    tl.store(out_ptr1 + x5, tmp31, xmask)


@triton.jit
def triton_per_fused_mean_6(in_out_ptr0, in_ptr0, xnumel, rnumel, XBLOCK:
    tl.constexpr):
    xnumel = 8
    RBLOCK: tl.constexpr = 32
    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 + 32 * x0), xmask, other=0.0)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = tl.where(xmask, tmp1, 0)
    tmp4 = tl.sum(tmp3, 1)[:, None]
    tmp5 = 262144.0
    tmp6 = tmp4 / tmp5
    tl.debug_barrier()
    tl.store(in_out_ptr0 + x0, tmp6, xmask)


def call(args):
    primals_1, primals_2, primals_3, primals_4, primals_5 = args
    args.clear()
    assert_size_stride(primals_1, (2, 2, 1, 1, 1), (2, 1, 1, 1, 1))
    assert_size_stride(primals_2, (2,), (1,))
    assert_size_stride(primals_3, (4, 2, 64, 64, 64), (524288, 262144, 4096,
        64, 1))
    assert_size_stride(primals_4, (2,), (1,))
    assert_size_stride(primals_5, (2,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = extern_kernels.convolution(primals_3, primals_1, 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(buf0, (4, 2, 64, 64, 64), (524288, 262144, 4096,
            64, 1))
        buf1 = buf0
        del buf0
        buf3 = empty_strided_cuda((1, 8, 1, 1, 1, 32), (256, 32, 256, 256, 
            256, 1), torch.float32)
        buf4 = empty_strided_cuda((1, 8, 1, 1, 1, 32), (256, 32, 256, 256, 
            256, 1), torch.float32)
        buf5 = empty_strided_cuda((1, 8, 1, 1, 1, 32), (256, 32, 256, 256, 
            256, 1), torch.float32)
        get_raw_stream(0)
        triton_red_fused__native_batch_norm_legit_convolution_0[grid(256)](buf1
            , primals_2, buf3, buf4, buf5, 256, 8192, XBLOCK=1, RBLOCK=2048,
            num_warps=16, num_stages=1)
        del primals_2
        buf2 = empty_strided_cuda((8,), (1,), torch.float32)
        triton_poi_fused_repeat_1[grid(8)](primals_4, buf2, 8, XBLOCK=8,
            num_warps=1, num_stages=1)
        del primals_4
        buf6 = empty_strided_cuda((1, 8, 1, 1, 1), (8, 1, 8, 8, 8), torch.
            float32)
        buf7 = empty_strided_cuda((1, 8, 1, 1, 1), (8, 1, 8, 8, 8), torch.
            float32)
        buf9 = empty_strided_cuda((1, 8, 1, 1, 1), (8, 1, 8, 8, 8), torch.
            float32)
        triton_per_fused__native_batch_norm_legit_2[grid(8)](buf3, buf4,
            buf5, buf6, buf7, buf9, 8, 32, XBLOCK=8, num_warps=2, num_stages=1)
        del buf3
        del buf4
        buf10 = empty_strided_cuda((4, 2, 64, 64, 64), (524288, 262144, 
            4096, 64, 1), torch.float32)
        triton_poi_fused_relu_3[grid(2097152)](buf1, buf6, buf7, buf2,
            primals_5, buf10, 2097152, XBLOCK=512, num_warps=8, num_stages=1)
        del primals_5
        buf11 = empty_strided_cuda((64,), (1,), torch.int64)
        triton_poi_fused__to_copy_add_arange_mul_4[grid(64)](buf11, 64,
            XBLOCK=64, num_warps=1, num_stages=1)
        buf13 = reinterpret_tensor(buf5, (4, 2, 1, 1, 1, 32), (64, 32, 256,
            256, 256, 1), 0)
        del buf5
        triton_red_fused__unsafe_index_avg_pool3d_mean_5[grid(256)](buf11,
            buf10, buf13, 256, 8192, XBLOCK=1, RBLOCK=1024, num_warps=16,
            num_stages=1)
        buf14 = reinterpret_tensor(buf7, (4, 2, 1, 1, 1), (2, 1, 8, 8, 8), 0)
        del buf7
        buf15 = reinterpret_tensor(buf14, (4, 2, 1, 1, 1), (2, 1, 1, 1, 1), 0)
        del buf14
        triton_per_fused_mean_6[grid(8)](buf15, buf13, 8, 32, XBLOCK=8,
            num_warps=2, num_stages=1)
        del buf13
    return buf15, primals_1, primals_3, buf1, buf2, reinterpret_tensor(buf9,
        (8,), (1,), 0), buf10, buf11, reinterpret_tensor(buf6, (1, 8, 1, 1,
        1), (8, 1, 1, 1, 1), 0)


class RefModel3dNew(torch.nn.Module):
    """The 3D reference model."""

    def __init__(self):
        super().__init__()
        self.l1 = torch.nn.Conv3d(2, 2, 1, bias=True)
        self.l2 = torch.nn.InstanceNorm3d(2, affine=True)
        self.l3 = torch.nn.ReLU()
        self.l4 = torch.nn.Dropout3d(0.2)
        self.l5 = torch.nn.AvgPool3d(2)
        self.l7 = torch.nn.AdaptiveAvgPool3d(1)

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


Measured latency_ms: 0.16007