import torch
import numpy as np
import torch.nn as nn


class BatchNormLayer(nn.Module):
    """Implements batch normalization layer."""

    def __init__(self, channels, gamma=False, beta=True, decay=0.9, epsilon
        =1e-05):
        """Initializes with basic settings.

    Args:
      channels: Number of channels of the input tensor.
      gamma: Whether the scale (weight) of the affine mapping is learnable.
      beta: Whether the center (bias) of the affine mapping is learnable.
      decay: Decay factor for moving average operations in this layer.
      epsilon: A value added to the denominator for numerical stability.
    """
        super().__init__()
        self.bn = nn.BatchNorm2d(num_features=channels, affine=True,
            track_running_stats=True, momentum=1 - decay, eps=epsilon)
        self.bn.weight.requires_grad = gamma
        self.bn.bias.requires_grad = beta

    def forward(self, x):
        return self.bn(x)


class FirstBlock(nn.Module):
    """Implements the first block, which is a convolutional block."""

    def __init__(self, in_channels, out_channels, use_wscale=False,
        wscale_gain=np.sqrt(2.0), use_bn=False, activation_type='lrelu'):
        super().__init__()
        self.conv = nn.Conv2d(in_channels=in_channels, out_channels=
            out_channels, kernel_size=3, stride=1, padding=1, bias=False)
        self.scale = wscale_gain / np.sqrt(in_channels * 3 * 3
            ) if use_wscale else 1.0
        self.bn = BatchNormLayer(channels=out_channels
            ) if use_bn else nn.Identity()
        if activation_type == 'linear':
            self.activate = nn.Identity()
        elif activation_type == 'lrelu':
            self.activate = nn.LeakyReLU(negative_slope=0.2, inplace=True)
        else:
            raise NotImplementedError(
                f'Not implemented activation function: {activation_type}!')

    def forward(self, x):
        return self.activate(self.bn(self.conv(x) * self.scale))


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


def get_init_inputs():
    return [[], {'in_channels': 4, 'out_channels': 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 numpy as np
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_leaky_relu_leaky_relu_backward_mul_0(in_out_ptr0,
    out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_out_ptr0 + x0, xmask)
    tmp1 = 1.0
    tmp2 = tmp0 * tmp1
    tmp3 = 0.0
    tmp4 = tmp2 > tmp3
    tmp5 = 0.2
    tmp6 = tmp2 * tmp5
    tmp7 = tl.where(tmp4, tmp2, tmp6)
    tmp8 = tmp7 > tmp3
    tl.store(in_out_ptr0 + x0, tmp7, xmask)
    tl.store(out_ptr0 + x0, tmp8, xmask)


def call(args):
    primals_1, primals_2 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 3, 3), (36, 9, 3, 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, 4, 4), (64, 16, 4, 1))
        buf1 = buf0
        del buf0
        buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool)
        get_raw_stream(0)
        triton_poi_fused_leaky_relu_leaky_relu_backward_mul_0[grid(256)](buf1,
            buf2, 256, XBLOCK=128, num_warps=4, num_stages=1)
    return buf1, primals_1, primals_2, buf2


class BatchNormLayer(nn.Module):
    """Implements batch normalization layer."""

    def __init__(self, channels, gamma=False, beta=True, decay=0.9, epsilon
        =1e-05):
        """Initializes with basic settings.

    Args:
      channels: Number of channels of the input tensor.
      gamma: Whether the scale (weight) of the affine mapping is learnable.
      beta: Whether the center (bias) of the affine mapping is learnable.
      decay: Decay factor for moving average operations in this layer.
      epsilon: A value added to the denominator for numerical stability.
    """
        super().__init__()
        self.bn = nn.BatchNorm2d(num_features=channels, affine=True,
            track_running_stats=True, momentum=1 - decay, eps=epsilon)
        self.bn.weight.requires_grad = gamma
        self.bn.bias.requires_grad = beta

    def forward(self, x):
        return self.bn(x)


class FirstBlockNew(nn.Module):
    """Implements the first block, which is a convolutional block."""

    def __init__(self, in_channels, out_channels, use_wscale=False,
        wscale_gain=np.sqrt(2.0), use_bn=False, activation_type='lrelu'):
        super().__init__()
        self.conv = nn.Conv2d(in_channels=in_channels, out_channels=
            out_channels, kernel_size=3, stride=1, padding=1, bias=False)
        self.scale = wscale_gain / np.sqrt(in_channels * 3 * 3
            ) if use_wscale else 1.0
        self.bn = BatchNormLayer(channels=out_channels
            ) if use_bn else nn.Identity()
        if activation_type == 'linear':
            self.activate = nn.Identity()
        elif activation_type == 'lrelu':
            self.activate = nn.LeakyReLU(negative_slope=0.2, inplace=True)
        else:
            raise NotImplementedError(
                f'Not implemented activation function: {activation_type}!')

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