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._inductor.runtime.triton_helpers import math as tl_math
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_add_exp_log_max_mul_sub_sum_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)
    x0 = xindex
    r1 = rindex
    tmp0 = tl.load(in_ptr0 + 16 * x0, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr0 + (1 + 16 * x0), xmask, eviction_policy='evict_last'
        )
    tmp3 = tl.load(in_ptr0 + (2 + 16 * x0), xmask, eviction_policy='evict_last'
        )
    tmp5 = tl.load(in_ptr0 + (3 + 16 * x0), xmask, eviction_policy='evict_last'
        )
    tmp7 = tl.load(in_ptr0 + (4 + 16 * x0), xmask, eviction_policy='evict_last'
        )
    tmp8 = tl.load(in_ptr0 + (5 + 16 * x0), xmask, eviction_policy='evict_last'
        )
    tmp10 = tl.load(in_ptr0 + (6 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp12 = tl.load(in_ptr0 + (7 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp15 = tl.load(in_ptr0 + (8 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp16 = tl.load(in_ptr0 + (9 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp18 = tl.load(in_ptr0 + (10 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp20 = tl.load(in_ptr0 + (11 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp23 = tl.load(in_ptr0 + (12 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp24 = tl.load(in_ptr0 + (13 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp26 = tl.load(in_ptr0 + (14 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp28 = tl.load(in_ptr0 + (15 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp31 = tl.load(in_ptr0 + (r1 + 16 * x0), xmask, other=0.0)
    tmp2 = triton_helpers.maximum(tmp0, tmp1)
    tmp4 = triton_helpers.maximum(tmp2, tmp3)
    tmp6 = triton_helpers.maximum(tmp4, tmp5)
    tmp9 = triton_helpers.maximum(tmp7, tmp8)
    tmp11 = triton_helpers.maximum(tmp9, tmp10)
    tmp13 = triton_helpers.maximum(tmp11, tmp12)
    tmp14 = triton_helpers.maximum(tmp6, tmp13)
    tmp17 = triton_helpers.maximum(tmp15, tmp16)
    tmp19 = triton_helpers.maximum(tmp17, tmp18)
    tmp21 = triton_helpers.maximum(tmp19, tmp20)
    tmp22 = triton_helpers.maximum(tmp14, tmp21)
    tmp25 = triton_helpers.maximum(tmp23, tmp24)
    tmp27 = triton_helpers.maximum(tmp25, tmp26)
    tmp29 = triton_helpers.maximum(tmp27, tmp28)
    tmp30 = triton_helpers.maximum(tmp22, tmp29)
    tmp32 = tmp31 - tmp30
    tmp33 = 4.0
    tmp34 = tmp32 * tmp33
    tmp35 = tl_math.exp(tmp34)
    tmp36 = tl.broadcast_to(tmp35, [XBLOCK, RBLOCK])
    tmp38 = tl.where(xmask, tmp36, 0)
    tmp39 = tl.sum(tmp38, 1)[:, None]
    tmp40 = 0.0625
    tmp41 = tmp39 * tmp40
    tmp42 = tl_math.log(tmp41)
    tmp43 = 0.25
    tmp44 = tmp42 * tmp43
    tmp45 = tmp30 + tmp44
    tl.debug_barrier()
    tl.store(in_out_ptr0 + x0, tmp45, 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)
        buf1 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 16, 16), torch.float32)
        buf2 = reinterpret_tensor(buf1, (4, 4, 1, 1), (4, 1, 1, 1), 0)
        del buf1
        get_raw_stream(0)
        triton_per_fused_add_exp_log_max_mul_sub_sum_0[grid(16)](buf2,
            arg0_1, 16, 16, XBLOCK=1, num_warps=2, num_stages=1)
        del arg0_1
    return buf2,


class LogSumExpPoolNew(nn.Module):

    def __init__(self, gamma):
        super(LogSumExpPoolNew, self).__init__()
        self.gamma = gamma

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


Measured latency_ms: 0.015328