import torch
from torch._inductor.select_algorithm import extern_kernels
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
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_add_avg_pool2d_convolution_max_pool2d_with_indices_mul_rsub_sigmoid_0(
    in_out_ptr0, in_ptr0, in_ptr1, out_ptr0, out_ptr1, out_ptr2, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 4
    tmp0 = tl.load(in_out_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr1 + 16 * x2, xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr1 + (1 + 16 * x2), xmask, eviction_policy='evict_last'
        )
    tmp6 = tl.load(in_ptr1 + (2 + 16 * x2), xmask, eviction_policy='evict_last'
        )
    tmp8 = tl.load(in_ptr1 + (3 + 16 * x2), xmask, eviction_policy='evict_last'
        )
    tmp10 = tl.load(in_ptr1 + (4 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp12 = tl.load(in_ptr1 + (5 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp14 = tl.load(in_ptr1 + (6 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp16 = tl.load(in_ptr1 + (7 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp18 = tl.load(in_ptr1 + (8 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp20 = tl.load(in_ptr1 + (9 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp22 = tl.load(in_ptr1 + (10 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp24 = tl.load(in_ptr1 + (11 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp26 = tl.load(in_ptr1 + (12 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp28 = tl.load(in_ptr1 + (13 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp30 = tl.load(in_ptr1 + (14 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp32 = tl.load(in_ptr1 + (15 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp2 = tmp0 + tmp1
    tmp5 = triton_helpers.maximum(tmp4, tmp3)
    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)
    tmp34 = tmp4 + tmp3
    tmp35 = tmp6 + tmp34
    tmp36 = tmp8 + tmp35
    tmp37 = tmp10 + tmp36
    tmp38 = tmp12 + tmp37
    tmp39 = tmp14 + tmp38
    tmp40 = tmp16 + tmp39
    tmp41 = tmp18 + tmp40
    tmp42 = tmp20 + tmp41
    tmp43 = tmp22 + tmp42
    tmp44 = tmp24 + tmp43
    tmp45 = tmp26 + tmp44
    tmp46 = tmp28 + tmp45
    tmp47 = tmp30 + tmp46
    tmp48 = tmp32 + tmp47
    tmp49 = 0.0625
    tmp50 = tmp48 * tmp49
    tmp51 = tl.sigmoid(tmp2)
    tmp52 = tmp51 * tmp33
    tmp53 = 1.0
    tmp54 = tmp53 - tmp51
    tmp55 = tmp54 * tmp50
    tmp56 = tmp52 + tmp55
    tl.store(in_out_ptr0 + x2, tmp2, xmask)
    tl.store(out_ptr0 + x2, tmp33, xmask)
    tl.store(out_ptr1 + x2, tmp50, xmask)
    tl.store(out_ptr2 + x2, tmp56, xmask)


def call(args):
    primals_1, primals_2, primals_3 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_2, (4,), (1,))
    assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = extern_kernels.convolution(primals_3, primals_1, stride=(4, 
            4), padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf0, (4, 4, 1, 1), (4, 1, 1, 1))
        buf1 = buf0
        del buf0
        buf2 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 1, 1), torch.float32)
        buf3 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 1, 1), torch.float32)
        buf4 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 1, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_add_avg_pool2d_convolution_max_pool2d_with_indices_mul_rsub_sigmoid_0[
            grid(16)](buf1, primals_2, primals_3, buf2, buf3, buf4, 16,
            XBLOCK=16, num_warps=1, num_stages=1)
        del primals_2
    return buf4, primals_1, primals_3, buf1, buf2, buf3


class GatedPoolingNew(nn.Module):
    """
        Gated pooling as defined in https://arxiv.org/abs/1509.08985
        This implementation is the LR variant
    """

    def __init__(self, kernel_size, filter):
        super(GatedPoolingNew, self).__init__()
        self.avgpool = nn.AvgPool2d(kernel_size)
        self.maxpool = nn.MaxPool2d(kernel_size)
        self.transform = nn.Conv2d(filter, filter, kernel_size=kernel_size,
            stride=kernel_size)

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


Measured latency_ms: 0.026962