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
from torch import 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_per_fused_adaptive_max_pool2d_add_mean_0(in_out_ptr0, in_ptr0,
    xnumel, rnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    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, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r1 = rindex
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (r1 + 16 * x0), xmask, other=0.0)
    tmp5 = tl.load(in_ptr0 + 16 * x0, xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (1 + 16 * x0), xmask, eviction_policy='evict_last'
        )
    tmp8 = tl.load(in_ptr0 + (2 + 16 * x0), xmask, eviction_policy='evict_last'
        )
    tmp10 = tl.load(in_ptr0 + (3 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp12 = tl.load(in_ptr0 + (4 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp14 = tl.load(in_ptr0 + (5 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp16 = tl.load(in_ptr0 + (6 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp18 = tl.load(in_ptr0 + (7 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp20 = tl.load(in_ptr0 + (8 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp22 = tl.load(in_ptr0 + (9 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp24 = tl.load(in_ptr0 + (10 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp26 = tl.load(in_ptr0 + (11 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp28 = tl.load(in_ptr0 + (12 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp30 = tl.load(in_ptr0 + (13 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp32 = tl.load(in_ptr0 + (14 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp34 = tl.load(in_ptr0 + (15 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = tl.where(xmask, tmp1, 0)
    tmp4 = tl.sum(tmp3, 1)[:, None]
    tmp7 = triton_helpers.maximum(tmp6, tmp5)
    tmp9 = triton_helpers.maximum(tmp8, tmp7)
    tmp11 = triton_helpers.maximum(tmp10, tmp9)
    tmp13 = triton_helpers.maximum(tmp12, tmp11)
    tmp15 = triton_helpers.maximum(tmp14, tmp13)
    tmp17 = triton_helpers.maximum(tmp16, tmp15)
    tmp19 = triton_helpers.maximum(tmp18, tmp17)
    tmp21 = triton_helpers.maximum(tmp20, tmp19)
    tmp23 = triton_helpers.maximum(tmp22, tmp21)
    tmp25 = triton_helpers.maximum(tmp24, tmp23)
    tmp27 = triton_helpers.maximum(tmp26, tmp25)
    tmp29 = triton_helpers.maximum(tmp28, tmp27)
    tmp31 = triton_helpers.maximum(tmp30, tmp29)
    tmp33 = triton_helpers.maximum(tmp32, tmp31)
    tmp35 = triton_helpers.maximum(tmp34, tmp33)
    tmp36 = 16.0
    tmp37 = tmp4 / tmp36
    tmp38 = tmp37 + tmp35
    tl.debug_barrier()
    tl.store(in_out_ptr0 + x0, tmp38, 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, 1, 1), (4, 1, 16, 16), torch.float32)
        buf2 = reinterpret_tensor(buf0, (4, 4, 1, 1), (4, 1, 1, 1), 0)
        del buf0
        get_raw_stream(0)
        triton_per_fused_adaptive_max_pool2d_add_mean_0[grid(16)](buf2,
            arg0_1, 16, 16, XBLOCK=8, num_warps=2, num_stages=1)
        del arg0_1
    return buf2,


class FastGlobalAvgPool2d(nn.Module):

    def __init__(self, flatten=False):
        super(FastGlobalAvgPool2d, self).__init__()
        self.flatten = flatten

    def forward(self, x):
        if self.flatten:
            in_size = x.size()
            return x.view((in_size[0], in_size[1], -1)).mean(dim=2)
        else:
            return x.view(x.size(0), x.size(1), -1).mean(-1).view(x.size(0),
                x.size(1), 1, 1)


class AdaptiveAvgMaxPool2dNew(nn.Module):

    def __init__(self):
        super(AdaptiveAvgMaxPool2dNew, self).__init__()
        self.gap = FastGlobalAvgPool2d()
        self.gmp = nn.AdaptiveMaxPool2d(1)

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


Measured latency_ms: 0.01541