import torch
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
import torch.nn as nn
from torch.nn.modules.utils import _pair
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_div_index_put_lift_fresh_relu_sum_0(in_ptr0, out_ptr3,
    xnumel, rnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    rnumel = 9
    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, :]
    rmask = rindex < rnumel
    r3 = rindex
    x0 = xindex % 2
    x1 = xindex // 2 % 2
    x2 = xindex // 4
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 4 * x1 + 4 * (r3 // 3) + 16 * x2 + r3 % 
        3), rmask & xmask, other=0.0)
    tmp1 = tl.full([1, 1], 0, tl.int32)
    tmp2 = triton_helpers.maximum(tmp1, tmp0)
    tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
    tmp5 = tl.where(rmask & xmask, tmp3, 0)
    tmp6 = tl.sum(tmp5, 1)[:, None]
    tmp7 = tmp2 / tmp6
    tmp8 = tmp7 != tmp7
    tmp9 = 1.0
    tmp10 = tl.where(tmp8, tmp9, tmp7)
    tmp11 = tl.broadcast_to(tmp10, [XBLOCK, RBLOCK])
    tmp13 = tl.where(rmask & xmask, tmp11, 0)
    tmp14 = tl.sum(tmp13, 1)[:, None]
    tmp15 = tmp10 / tmp14
    tl.store(out_ptr3 + (r3 + 9 * x4), tmp15, rmask & xmask)


@triton.jit
def triton_poi_fused_gather_sum_1(in_ptr0, in_ptr1, out_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex
    x0 = xindex % 2
    x1 = xindex // 2 % 2
    x2 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x3, xmask)
    tmp1 = tl.full([XBLOCK], 9, tl.int32)
    tmp2 = tmp0 + tmp1
    tmp3 = tmp0 < 0
    tmp4 = tl.where(tmp3, tmp2, tmp0)
    tl.device_assert((0 <= tmp4) & (tmp4 < 9) | ~xmask,
        'index out of bounds: 0 <= tmp4 < 9')
    tmp6 = tl.load(in_ptr1 + (x0 + 4 * x1 + 4 * (tmp4 // 3 % 3) + 16 * x2 +
        tmp4 % 3), xmask)
    tl.store(out_ptr0 + x3, tmp6, xmask)


def call(args):
    arg0_1, = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf3 = empty_strided_cuda((4, 4, 2, 2, 9), (144, 36, 18, 9, 1),
            torch.float32)
        get_raw_stream(0)
        triton_per_fused_div_index_put_lift_fresh_relu_sum_0[grid(64)](arg0_1,
            buf3, 64, 9, XBLOCK=1, num_warps=2, num_stages=1)
        buf4 = torch.ops.aten.multinomial.default(reinterpret_tensor(buf3,
            (64, 9), (9, 1), 0), 1, True)
        del buf3
        buf5 = buf4
        del buf4
        buf6 = empty_strided_cuda((4, 4, 2, 2), (16, 4, 2, 1), torch.float32)
        triton_poi_fused_gather_sum_1[grid(64)](buf5, arg0_1, buf6, 64,
            XBLOCK=64, num_warps=1, num_stages=1)
        del arg0_1
        del buf5
    return buf6,


class StochasticPool2DNew(nn.Module):
    """
    Args: 
        kernel_size : size of the pooling kernel
        stride      : pool stride
    
    Note: valid padding is implemented
    """

    def __init__(self, kernel_size=3, stride=1, training=True):
        super(StochasticPool2DNew, self).__init__()
        self.k = _pair(kernel_size)
        self.stride = _pair(stride)
        self.training = training

    def forward(self, input_0):
        arg0_1 = input_0
        output = call([arg0_1])
        return output[0]


Measured latency_ms: 0.082628