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 string import ascii_lowercase
import torch.optim
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_mul_0(in_out_ptr0,
    in_ptr0, in_ptr1, out_ptr0, out_ptr1, 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
    x2 = xindex % 4
    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')
    tmp38 = tl.load(in_ptr1 + x2, 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
    tmp39 = tl.sigmoid(tmp38)
    tmp40 = tmp37 * tmp39
    tmp41 = 1.0
    tmp42 = tmp41 - tmp39
    tmp43 = tmp35 * tmp42
    tmp44 = tmp40 + tmp43
    tl.store(out_ptr0 + x0, tmp35, xmask)
    tl.debug_barrier()
    tl.store(in_out_ptr0 + x0, tmp37, xmask)
    tl.store(out_ptr1 + x0, tmp44, xmask)


def call(args):
    primals_1, primals_2 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_2, (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)
        buf1 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 16, 16), torch.float32)
        buf2 = buf0
        del buf0
        buf3 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 1, 1), torch.float32)
        get_raw_stream(0)
        triton_per_fused_adaptive_max_pool2d_add_mean_mul_0[grid(16)](buf2,
            primals_1, primals_2, buf1, buf3, 16, 16, XBLOCK=8, num_warps=2,
            num_stages=1)
        del primals_1
    return buf3, primals_2, reinterpret_tensor(buf2, (4, 4, 1, 1), (4, 1, 1,
        1), 0), reinterpret_tensor(buf1, (4, 4, 1, 1), (4, 1, 1, 1), 0)


class CollapseNew(nn.Module):

    def __init__(self, size):
        super(CollapseNew, self).__init__()
        self.weight = nn.Parameter(torch.Tensor(size), requires_grad=True)
        self.weight.data.zero_()
        self.p_avg_l = nn.AdaptiveAvgPool2d(output_size=(1, 1))
        self.p_max_l = nn.AdaptiveMaxPool2d(output_size=(1, 1))

    def collapse(self, inputs):
        p_avg = self.p_avg_l(inputs)
        p_max = self.p_max_l(inputs)
        factor = torch.sigmoid(self.weight)
        eqn = 'ay{0},y->ay{0}'.format(ascii_lowercase[1:3])
        return torch.einsum(eqn, [p_avg, factor]) + torch.einsum(eqn, [
            p_max, torch.sub(1.0, factor)])

    def forward(self, input_0):
        primals_2 = self.weight
        primals_1 = input_0
        output = call([primals_1, primals_2])
        return output[0]


Measured latency_ms: 0.023298