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
import torch.nn as nn
import torch.distributions
import torch.nn.parallel
import torch.optim
import torch.utils.data
import torch.utils.data.distributed
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_poi_fused_avg_pool3d_clone_0(in_out_ptr0, in_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 128
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex // 64
    x1 = xindex // 4 % 4
    x0 = xindex % 4
    x5 = xindex % 64
    x4 = xindex
    tmp0 = -1 + 2 * x3
    tmp1 = tl.full([1], 0, tl.int64)
    tmp2 = tmp0 >= tmp1
    tmp3 = tl.full([1], 4, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tmp2 & tmp4
    tmp6 = x1
    tmp7 = tmp6 >= tmp1
    tmp8 = tmp6 < tmp3
    tmp9 = tmp7 & tmp8
    tmp10 = x0
    tmp11 = tmp10 >= tmp1
    tmp12 = tmp10 < tmp3
    tmp13 = tmp11 & tmp12
    tmp14 = tmp5 & tmp9
    tmp15 = tmp14 & tmp13
    tmp16 = tl.load(in_ptr0 + (-64 + x5 + 128 * x3), tmp15 & xmask, other=0.0)
    tmp17 = 2 * x3
    tmp18 = tmp17 >= tmp1
    tmp19 = tmp17 < tmp3
    tmp20 = tmp18 & tmp19
    tmp21 = tmp20 & tmp9
    tmp22 = tmp21 & tmp13
    tmp23 = tl.load(in_ptr0 + (x5 + 128 * x3), tmp22 & xmask, other=0.0)
    tmp24 = tmp23 + tmp16
    tmp25 = 1 + 2 * x3
    tmp26 = tmp25 >= tmp1
    tmp27 = tmp25 < tmp3
    tmp28 = tmp26 & tmp27
    tmp29 = tmp28 & tmp9
    tmp30 = tmp29 & tmp13
    tmp31 = tl.load(in_ptr0 + (64 + x5 + 128 * x3), tmp30 & xmask, other=0.0)
    tmp32 = tmp31 + tmp24
    tmp33 = x0 * x1 + (4 * (4 <= 1 + x0) + (1 + x0) * (1 + x0 < 4)) * (4 *
        (4 <= 1 + x1) + (1 + x1) * (1 + x1 < 4)) + -1 * x0 * (4 * (4 <= 1 +
        x1) + (1 + x1) * (1 + x1 < 4)) + -1 * x1 * (4 * (4 <= 1 + x0) + (1 +
        x0) * (1 + x0 < 4)) + x0 * x1 * (5 * (5 <= 2 + 2 * x3) + (2 + 2 *
        x3) * (2 + 2 * x3 < 5)) + (4 * (4 <= 1 + x0) + (1 + x0) * (1 + x0 < 4)
        ) * (4 * (4 <= 1 + x1) + (1 + x1) * (1 + x1 < 4)) * (5 * (5 <= 2 + 
        2 * x3) + (2 + 2 * x3) * (2 + 2 * x3 < 5)) + -1 * x0 * (4 * (4 <= 1 +
        x1) + (1 + x1) * (1 + x1 < 4)) * (5 * (5 <= 2 + 2 * x3) + (2 + 2 *
        x3) * (2 + 2 * x3 < 5)) + -1 * x1 * (4 * (4 <= 1 + x0) + (1 + x0) *
        (1 + x0 < 4)) * (5 * (5 <= 2 + 2 * x3) + (2 + 2 * x3) * (2 + 2 * x3 <
        5)) + -2 * x0 * x1 * x3 + -2 * x3 * (4 * (4 <= 1 + x0) + (1 + x0) *
        (1 + x0 < 4)) * (4 * (4 <= 1 + x1) + (1 + x1) * (1 + x1 < 4)
        ) + 2 * x0 * x3 * (4 * (4 <= 1 + x1) + (1 + x1) * (1 + x1 < 4)
        ) + 2 * x1 * x3 * (4 * (4 <= 1 + x0) + (1 + x0) * (1 + x0 < 4))
    tmp34 = tmp32 / tmp33
    tl.store(in_out_ptr0 + x4, tmp34, 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)
        buf0 = empty_strided_cuda((1, 4, 2, 4, 4), (128, 16, 64, 4, 1),
            torch.float32)
        buf1 = reinterpret_tensor(buf0, (1, 2, 4, 4, 4), (128, 64, 16, 4, 1), 0
            )
        del buf0
        get_raw_stream(0)
        triton_poi_fused_avg_pool3d_clone_0[grid(128)](buf1, arg0_1, 128,
            XBLOCK=128, num_warps=4, num_stages=1)
        del arg0_1
    return reinterpret_tensor(buf1, (2, 4, 4, 4), (64, 16, 4, 1), 0),


class TemporalPoolingNew(nn.Module):

    def __init__(self, frames, kernel_size=3, stride=2, mode='avg'):
        """

        Parameters
        ----------
        frames (int): number of input frames
        kernel_size
        stride
        mode
        """
        super().__init__()
        self.frames = frames
        pad_size = (kernel_size - 1) // stride
        if mode == 'avg':
            self.pool = nn.AvgPool3d(kernel_size=(kernel_size, 1, 1),
                stride=(stride, 1, 1), padding=(pad_size, 0, 0))
        elif mode == 'max':
            self.pool = nn.MaxPool3d(kernel_size=(kernel_size, 1, 1),
                stride=(stride, 1, 1), padding=(pad_size, 0, 0))
        else:
            raise ValueError('only support avg or max')

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


Measured latency_ms: 0.015567