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
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda


@triton.jit
def triton_poi_fused__adaptive_avg_pool2d_0(in_ptr0, out_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 320
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 4 % 5
    x0 = xindex % 4
    x2 = xindex // 20
    x4 = xindex
    tmp0 = 4 * x1 // 5
    tmp1 = (8 + 4 * x1) // 5
    tmp2 = tmp0 < tmp1
    tmp3 = x0
    tmp4 = 1 + x0
    tmp5 = tmp3 < tmp4
    tmp6 = tmp2 & tmp5
    tmp7 = tl.load(in_ptr0 + (x0 + 4 * (4 * x1 // 5) + 16 * x2), tmp6 &
        xmask, other=0.0)
    tmp8 = tmp4 < tmp4
    tmp9 = tmp2 & tmp8
    tmp10 = tl.load(in_ptr0 + (1 + x0 + 4 * (4 * x1 // 5) + 16 * x2), tmp9 &
        xmask, other=0.0)
    tmp11 = tmp10 + tmp7
    tmp12 = 1 + 4 * x1 // 5
    tmp13 = tmp12 < tmp1
    tmp14 = tmp13 & tmp5
    tmp15 = tl.load(in_ptr0 + (4 + x0 + 4 * (4 * x1 // 5) + 16 * x2), tmp14 &
        xmask, other=0.0)
    tmp16 = tmp15 + tmp11
    tmp17 = tmp13 & tmp8
    tmp18 = tl.load(in_ptr0 + (5 + x0 + 4 * (4 * x1 // 5) + 16 * x2), tmp17 &
        xmask, other=0.0)
    tmp19 = tmp18 + tmp16
    tmp20 = 1.0
    tmp21 = tl.full(tmp20.shape, 0.0, tmp20.dtype)
    tmp22 = tl.where(tmp6, tmp20, tmp21)
    tmp23 = tl.where(tmp9, tmp20, tmp21)
    tmp24 = tmp23 + tmp22
    tmp25 = tl.where(tmp14, tmp20, tmp21)
    tmp26 = tmp25 + tmp24
    tmp27 = tl.where(tmp17, tmp20, tmp21)
    tmp28 = tmp27 + tmp26
    tmp29 = tmp19 / tmp28
    tl.store(out_ptr0 + x4, tmp29, 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((4, 4, 5, 4), (80, 20, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused__adaptive_avg_pool2d_0[grid(320)](arg0_1, buf0, 
            320, XBLOCK=256, num_warps=4, num_stages=1)
        del arg0_1
    return buf0,


class AdaptiveAveragePoolingNew(nn.Module):
    """Adaptive Pooling neck.

    Args:

        dim (int): Dimensions of each sample channel, can be one of {1, 2, 3}.
            Default: 2
        output_size (int | tuple):  output size,
            If dim equals to 1: output_size is a single integer.
            Else, if output_size is a tuple of integers with the length of dim.
            Default: (5, 4)
    """

    def __init__(self, dim=2, output_size=(5, 4)):
        super(AdaptiveAveragePoolingNew, self).__init__()
        assert dim in [1, 2, 3
            ], f'AdaptiveAveragePooling dim only support {1, 2, 3}, get {dim} instead.'
        if dim == 1:
            assert isinstance(output_size, int)
            self.aap = nn.AdaptiveAvgPool1d(output_size)
        elif dim == 2:
            assert isinstance(output_size, tuple)
            assert len(output_size) == 2
            self.aap = nn.AdaptiveAvgPool2d(output_size)
        else:
            assert isinstance(output_size, tuple)
            assert len(output_size) == 3
            self.aap = nn.AdaptiveAvgPool3d(output_size)
        self.output_size = output_size

    def init_weights(self):
        pass

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


Measured latency_ms: 0.014368