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 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_0(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
    constexpr, XBLOCK: tl.constexpr):
    ynumel = 64
    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]
    tl.full([XBLOCK, YBLOCK], True, tl.int1)
    x2 = xindex
    y3 = yindex
    y0 = yindex % 16
    y1 = yindex // 16
    tmp0 = tl.load(in_ptr0 + (x2 + 4096 * y3), ymask, eviction_policy=
        'evict_last')
    tl.store(out_ptr0 + (y0 + 16 * x2 + 65536 * y1), tmp0, ymask)


@triton.jit
def triton_poi_fused_convolution_sigmoid_1(in_out_ptr0, in_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    tl.full([XBLOCK], True, tl.int1)
    x2 = xindex
    x0 = xindex % 4
    tmp0 = tl.load(in_out_ptr0 + x2, None)
    tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = tl.sigmoid(tmp2)
    tl.store(in_out_ptr0 + x2, tmp3, None)


@triton.jit
def triton_poi_fused_view_2(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
    constexpr, XBLOCK: tl.constexpr):
    ynumel = 16
    xnumel = 16
    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
    x1 = xindex
    y0 = yindex
    tmp0 = tl.load(in_ptr0 + (x1 + 16 * y0), xmask & ymask)
    tl.store(out_ptr0 + (y0 + 16 * x1), tmp0, xmask & ymask)


@triton.jit
def triton_poi_fused_view_3(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    tl.full([XBLOCK], True, tl.int1)
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (64 * (x0 % 4096) + 262144 * (x0 // 262144) + 
        x0 // 4096 % 64), None, eviction_policy='evict_last')
    tl.store(out_ptr0 + x0, tmp0, None)


@triton.jit
def triton_poi_fused_view_4(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
    constexpr, XBLOCK: tl.constexpr):
    xnumel = 25
    yoffset = (tl.program_id(1) + tl.program_id(2) * tl.num_programs(1)
        ) * YBLOCK
    yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
    tl.full([XBLOCK, YBLOCK], True, tl.int1)
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    x1 = xindex
    y0 = yindex
    tmp0 = tl.load(in_ptr0 + (y0 + 65536 * x1), xmask, eviction_policy=
        'evict_last')
    tl.store(out_ptr0 + (x1 + 25 * y0), tmp0, xmask)


def call(args):
    primals_1, primals_2, primals_3, primals_4, primals_5 = args
    args.clear()
    assert_size_stride(primals_1, (4, 16, 1, 1), (16, 1, 1, 1))
    assert_size_stride(primals_2, (4,), (1,))
    assert_size_stride(primals_3, (4, 16, 64, 64), (65536, 4096, 64, 1))
    assert_size_stride(primals_4, (64, 1, 1, 1), (1, 1, 1, 1))
    assert_size_stride(primals_5, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 16, 64, 64), (65536, 1, 1024, 16),
            torch.float32)
        get_raw_stream(0)
        triton_poi_fused_0[grid(64, 4096)](primals_3, buf0, 64, 4096,
            XBLOCK=32, YBLOCK=32, num_warps=4, num_stages=1)
        del primals_3
        buf1 = extern_kernels.convolution(buf0, primals_1, stride=(1, 1),
            padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf1, (4, 4, 64, 64), (16384, 1, 256, 4))
        buf2 = buf1
        del buf1
        triton_poi_fused_convolution_sigmoid_1[grid(65536)](buf2, primals_2,
            65536, XBLOCK=256, num_warps=4, num_stages=1)
        del primals_2
        buf3 = extern_kernels.convolution(buf2, primals_4, stride=(1, 1),
            padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=4, bias=None)
        assert_size_stride(buf3, (4, 64, 64, 64), (262144, 1, 4096, 64))
        buf4 = empty_strided_cuda((1, 16, 4, 4), (256, 1, 64, 16), torch.
            float32)
        triton_poi_fused_view_2[grid(16, 16)](primals_5, buf4, 16, 16,
            XBLOCK=16, YBLOCK=16, num_warps=4, num_stages=1)
        del primals_5
        buf5 = empty_strided_cuda((65536, 1, 4, 4), (16, 4, 4, 1), torch.
            float32)
        triton_poi_fused_view_3[grid(1048576)](buf3, buf5, 1048576, XBLOCK=
            1024, num_warps=4, num_stages=1)
        del buf3
        buf6 = extern_kernels.convolution(buf4, buf5, stride=(1, 1),
            padding=(2, 2), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=16, bias=None)
        assert_size_stride(buf6, (1, 65536, 5, 5), (1638400, 1, 327680, 65536))
        buf7 = empty_strided_cuda((16384, 4, 5, 5), (100, 25, 5, 1), torch.
            float32)
        triton_poi_fused_view_4[grid(65536, 25)](buf6, buf7, 65536, 25,
            XBLOCK=32, YBLOCK=32, num_warps=4, num_stages=1)
        del buf6
    return buf7, primals_1, buf0, primals_4, buf2, buf4, buf5


class WeightNet_DWNew(nn.Module):
    """ Here we show a grouping manner when we apply
    WeightNet to a depthwise convolution. The grouped
    fc layer directly generates the convolutional kernel,
    has fewer parameters while achieving comparable results.
    This layer has M/G*inp inputs, inp groups and inp*ksize*ksize outputs.

    Args:
        inp (int): Number of input channels
        oup (int): Number of output channels
        ksize (int): Size of the convolving kernel
        stride (int): Stride of the convolution
    """

    def __init__(self, inp, ksize, stride):
        super().__init__()
        self.M = 2
        self.G = 2
        self.pad = ksize // 2
        inp_gap = max(16, inp // 16)
        self.inp = inp
        self.ksize = ksize
        self.stride = stride
        self.wn_fc1 = nn.Conv2d(inp_gap, self.M // self.G * inp, 1, 1, 0,
            groups=1, bias=True)
        self.sigmoid = nn.Sigmoid()
        self.wn_fc2 = nn.Conv2d(self.M // self.G * inp, inp * ksize * ksize,
            1, 1, 0, groups=inp, bias=False)

    def forward(self, input_0, input_1):
        primals_1 = self.wn_fc1.weight
        primals_2 = self.wn_fc1.bias
        primals_4 = self.wn_fc2.weight
        primals_5 = input_0
        primals_3 = input_1
        output = call([primals_1, primals_2, primals_3, primals_4, primals_5])
        return output[0]


Measured latency_ms: 0.076941