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
import torch.nn.functional as F
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_avg_pool2d_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.
    constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + 9 * x0, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr0 + (1 + 9 * x0), xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (3 + 9 * x0), xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr0 + (4 + 9 * x0), xmask, eviction_policy='evict_last')
    tmp2 = tmp1 + tmp0
    tmp4 = tmp3 + tmp2
    tmp6 = tmp5 + tmp4
    tmp7 = 0.25
    tmp8 = tmp6 * tmp7
    tl.store(out_ptr0 + x0, tmp8, 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, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = extern_kernels.convolution(primals_2, primals_1, stride=(1, 
            1), padding=(1, 1), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf0, (4, 4, 3, 3), (36, 9, 3, 1))
        buf1 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 1, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_avg_pool2d_0[grid(16)](buf0, buf1, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
    return buf1, primals_1, primals_2, buf0


def l2normalize(v, esp=1e-08):
    return v / (v.norm() + esp)


def sn_weight(weight, u, height, n_power_iterations):
    weight.requires_grad_(False)
    for _ in range(n_power_iterations):
        v = l2normalize(torch.mv(weight.view(height, -1).t(), u))
        u = l2normalize(torch.mv(weight.view(height, -1), v))
    weight.requires_grad_(True)
    sigma = u.dot(weight.view(height, -1).mv(v))
    return torch.div(weight, sigma), u


def get_nonlinearity(nonlinearity=None):
    if not nonlinearity:
        pass
    elif callable(nonlinearity):
        if nonlinearity == nn.LeakyReLU:
            nonlinearity = nonlinearity(0.02, inplace=True)
    elif hasattr(nn, nonlinearity):
        nonlinearity = getattr(nn, nonlinearity)
        if nonlinearity == 'LeakyReLU':
            nonlinearity = nonlinearity(0.02, inplace=True)
        else:
            nonlinearity = nonlinearity()
    elif hasattr(nn.functional, nonlinearity):
        nonlinearity = getattr(nn.functional, nonlinearity)
    else:
        raise ValueError(nonlinearity)
    return nonlinearity


class SNConv2d(nn.Conv2d):

    def __init__(self, *args, n_power_iterations=1, **kwargs):
        super(SNConv2d, self).__init__(*args, **kwargs)
        self.n_power_iterations = n_power_iterations
        self.height = self.weight.shape[0]
        self.register_buffer('u', l2normalize(self.weight.new_empty(self.
            height).normal_(0, 1)))

    def forward(self, input):
        w_sn, self.u = sn_weight(self.weight, self.u, self.height, self.
            n_power_iterations)
        return F.conv2d(input, w_sn, self.bias, self.stride, self.padding,
            self.dilation, self.groups)


class ConvMeanPoolNew(nn.Module):

    def __init__(self, dim_in, dim_out, f_size, nonlinearity=None, prefix=
        '', spectral_norm=False):
        super(ConvMeanPoolNew, self).__init__()
        Conv2d = SNConv2d if spectral_norm else nn.Conv2d
        models = nn.Sequential()
        nonlinearity = get_nonlinearity(nonlinearity)
        name = 'cmp' + prefix
        models.add_module(name, Conv2d(dim_in, dim_out, f_size, 1, 1, bias=
            False))
        models.add_module(name + '_pool', nn.AvgPool2d(2, count_include_pad
            =False))
        if nonlinearity:
            models.add_module('{}_{}'.format(name, nonlinearity.__class__.
                __name__), nonlinearity)
        self.models = models

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


Measured latency_ms: 0.022193