import torch
import torch.nn as nn
import torch.nn.functional as F


class cnn_4layer_LeakyRelu(nn.Module):

    def __init__(self, in_ch, in_dim, width=2, linear_size=256, alpha=0.1):
        super(cnn_4layer_LeakyRelu, self).__init__()
        self.conv1 = nn.Conv2d(in_ch, 4 * width, 4, stride=2, padding=1)
        self.conv2 = nn.Conv2d(4 * width, 8 * width, 4, stride=2, padding=1)
        self.fc1 = nn.Linear(8 * width * (in_dim // 4) * (in_dim // 4),
            linear_size)
        self.fc2 = nn.Linear(linear_size, 10)
        self.alpha = alpha

    def forward(self, x):
        x = F.leaky_relu(self.conv1(x), self.alpha)
        x = F.leaky_relu(self.conv2(x), self.alpha)
        x = x.view(x.size(0), -1)
        x = F.leaky_relu(self.fc1(x), self.alpha)
        x = self.fc2(x)
        return x


def get_inputs():
    return [torch.rand([4, 4, 4, 4])]


def get_init_inputs():
    return [[], {'in_ch': 4, 'in_dim': 4}]

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
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor


@triton.jit
def triton_poi_fused_convolution_leaky_relu_0(in_ptr0, in_ptr1, out_ptr0,
    out_ptr1, xnumel, XBLOCK: tl.constexpr):
    xnumel = 128
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex
    x1 = xindex // 4 % 8
    tmp0 = tl.load(in_ptr0 + x3, xmask)
    tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = 0.0
    tmp4 = tmp2 > tmp3
    tmp5 = 0.1
    tmp6 = tmp2 * tmp5
    tmp7 = tl.where(tmp4, tmp2, tmp6)
    tl.store(out_ptr0 + x3, tmp4, xmask)
    tl.store(out_ptr1 + x3, tmp7, xmask)


@triton.jit
def triton_poi_fused_convolution_leaky_relu_1(in_ptr0, in_ptr1, out_ptr0,
    out_ptr1, xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 16
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = 0.0
    tmp4 = tmp2 > tmp3
    tmp5 = 0.1
    tmp6 = tmp2 * tmp5
    tmp7 = tl.where(tmp4, tmp2, tmp6)
    tl.store(out_ptr0 + x2, tmp4, xmask)
    tl.store(out_ptr1 + x2, tmp7, xmask)


@triton.jit
def triton_poi_fused_leaky_relu_2(in_ptr0, in_ptr1, out_ptr0, out_ptr1,
    xnumel, XBLOCK: tl.constexpr):
    xnumel = 1024
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 256
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = 0.0
    tmp4 = tmp2 > tmp3
    tmp5 = 0.1
    tmp6 = tmp2 * tmp5
    tmp7 = tl.where(tmp4, tmp2, tmp6)
    tl.store(out_ptr0 + x2, tmp4, xmask)
    tl.store(out_ptr1 + x2, tmp7, xmask)


def call(args):
    (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
        primals_7, primals_8, primals_9) = args
    args.clear()
    assert_size_stride(primals_1, (8, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_2, (8,), (1,))
    assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_4, (16, 8, 4, 4), (128, 16, 4, 1))
    assert_size_stride(primals_5, (16,), (1,))
    assert_size_stride(primals_6, (256, 16), (16, 1))
    assert_size_stride(primals_7, (256,), (1,))
    assert_size_stride(primals_8, (10, 256), (256, 1))
    assert_size_stride(primals_9, (10,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = extern_kernels.convolution(primals_3, primals_1, stride=(2, 
            2), padding=(1, 1), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf0, (4, 8, 2, 2), (32, 4, 2, 1))
        buf1 = empty_strided_cuda((4, 8, 2, 2), (32, 4, 2, 1), torch.bool)
        buf2 = empty_strided_cuda((4, 8, 2, 2), (32, 4, 2, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_convolution_leaky_relu_0[grid(128)](buf0,
            primals_2, buf1, buf2, 128, XBLOCK=128, num_warps=4, num_stages=1)
        del buf0
        del primals_2
        buf3 = extern_kernels.convolution(buf2, primals_4, stride=(2, 2),
            padding=(1, 1), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf3, (4, 16, 1, 1), (16, 1, 1, 1))
        buf4 = empty_strided_cuda((4, 16, 1, 1), (16, 1, 1, 1), torch.bool)
        buf5 = empty_strided_cuda((4, 16, 1, 1), (16, 1, 64, 64), torch.float32
            )
        triton_poi_fused_convolution_leaky_relu_1[grid(64)](buf3, primals_5,
            buf4, buf5, 64, XBLOCK=64, num_warps=1, num_stages=1)
        del buf3
        del primals_5
        buf6 = empty_strided_cuda((4, 256), (256, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf5, (4, 16), (16, 1), 0),
            reinterpret_tensor(primals_6, (16, 256), (1, 16), 0), out=buf6)
        buf7 = empty_strided_cuda((4, 256), (256, 1), torch.bool)
        buf8 = empty_strided_cuda((4, 256), (256, 1), torch.float32)
        triton_poi_fused_leaky_relu_2[grid(1024)](buf6, primals_7, buf7,
            buf8, 1024, XBLOCK=128, num_warps=4, num_stages=1)
        del buf6
        del primals_7
        buf9 = empty_strided_cuda((4, 10), (10, 1), torch.float32)
        extern_kernels.addmm(primals_9, buf8, reinterpret_tensor(primals_8,
            (256, 10), (1, 256), 0), alpha=1, beta=1, out=buf9)
        del primals_9
    return (buf9, primals_1, primals_3, primals_4, buf1, buf2, buf4,
        reinterpret_tensor(buf5, (4, 16), (16, 1), 0), buf7, buf8,
        primals_8, primals_6)


class cnn_4layer_LeakyReluNew(nn.Module):

    def __init__(self, in_ch, in_dim, width=2, linear_size=256, alpha=0.1):
        super(cnn_4layer_LeakyReluNew, self).__init__()
        self.conv1 = nn.Conv2d(in_ch, 4 * width, 4, stride=2, padding=1)
        self.conv2 = nn.Conv2d(4 * width, 8 * width, 4, stride=2, padding=1)
        self.fc1 = nn.Linear(8 * width * (in_dim // 4) * (in_dim // 4),
            linear_size)
        self.fc2 = nn.Linear(linear_size, 10)
        self.alpha = alpha

    def forward(self, input_0):
        primals_1 = self.conv1.weight
        primals_2 = self.conv1.bias
        primals_4 = self.conv2.weight
        primals_5 = self.conv2.bias
        primals_6 = self.fc1.weight
        primals_7 = self.fc1.bias
        primals_8 = self.fc2.weight
        primals_9 = self.fc2.bias
        primals_3 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6, primals_7, primals_8, primals_9])
        return output[0]
