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_max_pool2d_with_indices_0(in_ptr0, out_ptr0, ynumel,
    xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr):
    ynumel = 16
    xnumel = 9
    yoffset = tl.program_id(1) * YBLOCK
    yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
    ymask = yindex < ynumel
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    x2 = xindex % 3
    x3 = xindex // 3
    y4 = yindex
    x5 = xindex
    y0 = yindex % 4
    y1 = yindex // 4
    tmp0 = tl.load(in_ptr0 + (x2 + 4 * x3 + 16 * y4), xmask & ymask,
        eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr0 + (1 + x2 + 4 * x3 + 16 * y4), xmask & ymask,
        eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (4 + x2 + 4 * x3 + 16 * y4), xmask & ymask,
        eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr0 + (5 + x2 + 4 * x3 + 16 * y4), xmask & ymask,
        eviction_policy='evict_last')
    tmp2 = triton_helpers.maximum(tmp1, tmp0)
    tmp4 = triton_helpers.maximum(tmp3, tmp2)
    tmp6 = triton_helpers.maximum(tmp5, tmp4)
    tl.store(out_ptr0 + (y0 + 4 * x5 + 36 * y1), tmp6, xmask & ymask)


@triton.jit
def triton_poi_fused_convolution_max_pool2d_with_indices_1(in_ptr0,
    out_ptr0, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr):
    ynumel = 16
    xnumel = 4
    yoffset = tl.program_id(1) * YBLOCK
    yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
    ymask = yindex < ynumel
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    x2 = xindex
    y0 = yindex % 4
    y1 = yindex // 4
    y3 = yindex
    tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 16 * y1), xmask & ymask,
        eviction_policy='evict_last')
    tl.store(out_ptr0 + (x2 + 4 * y3), tmp0, xmask & ymask)


def call(args):
    arg0_1, arg1_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg1_1, (4, 1, 3, 3), (9, 9, 3, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4, 3, 3), (36, 1, 12, 4), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_max_pool2d_with_indices_0[grid(16, 9)](arg0_1,
            buf0, 16, 9, XBLOCK=16, YBLOCK=16, num_warps=4, num_stages=1)
        del arg0_1
        buf1 = extern_kernels.convolution(buf0, arg1_1, stride=(2, 2),
            padding=(1, 1), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=4, bias=None)
        assert_size_stride(buf1, (4, 4, 2, 2), (16, 1, 8, 4))
        del arg1_1
        del buf0
        buf2 = empty_strided_cuda((4, 4, 2, 2), (16, 4, 2, 1), torch.float32)
        triton_poi_fused_convolution_max_pool2d_with_indices_1[grid(16, 4)](
            buf1, buf2, 16, 4, XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1)
        del buf1
    return buf2,


class BlurPool2dNew(nn.Sequential):
    """Blur Pooling Layer (MaxPool2d replacement)
    See: https://richzhang.github.io/antialiased-cnns/
    Paper: https://arxiv.org/abs/1904.11486
    """
    __constants__ = ['in_features']
    _blur_kernel = torch.tensor([[1 / 16, 2 / 16, 1 / 16], [2 / 16, 4 / 16,
        2 / 16], [1 / 16, 2 / 16, 1 / 16]])

    def __init__(self, in_features):
        """
        Args:
            in_features (int): The number of channels in the input
        """
        super().__init__()
        self.in_features = in_features
        self.add_module('maxpool', nn.MaxPool2d(2, stride=1))
        blurpool = nn.Conv2d(in_features, in_features, kernel_size=3,
            padding=1, stride=2, bias=False, groups=in_features)
        blurpool.weight = torch.nn.Parameter(self._blur_kernel.repeat(
            in_features, 1, 1, 1), requires_grad=False)
        self.add_module('blurpool', blurpool)

    def extra_repr(self):
        return 'in_features={}'.format(self.in_features)

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


Measured latency_ms: 0.036615