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.triton_helpers import libdevice
import math
from torch import nn
from torch.nn import functional as F
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_per_fused_add_mul_pow_rsqrt_sum_0(in_out_ptr0, in_ptr0, in_ptr1,
    out_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    rnumel = 36
    RBLOCK: tl.constexpr = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rindex = tl.arange(0, RBLOCK)[None, :]
    rmask = rindex < rnumel
    r5 = rindex
    x0 = xindex % 4
    r3 = rindex // 9
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + (r5 + 36 * x0), rmask & xmask, eviction_policy
        ='evict_last', other=0.0)
    tmp3 = tl.load(in_ptr1 + r3, rmask, eviction_policy='evict_last', other=0.0
        )
    tmp1 = 0.16666666666666666
    tmp2 = tmp0 * tmp1
    tmp4 = tmp2 * tmp3
    tmp5 = tmp4 * tmp4
    tmp6 = tl.broadcast_to(tmp5, [XBLOCK, RBLOCK])
    tmp8 = tl.where(rmask & xmask, tmp6, 0)
    tmp9 = tl.sum(tmp8, 1)[:, None]
    tmp10 = 1e-08
    tmp11 = tmp9 + tmp10
    tmp12 = libdevice.rsqrt(tmp11)
    tmp13 = tmp4 * tmp12
    tl.debug_barrier()
    tl.store(in_out_ptr0 + x4, tmp12, xmask)
    tl.store(out_ptr0 + (r5 + 36 * x4), tmp13, rmask & xmask)


@triton.jit
def triton_poi_fused_add_leaky_relu_mul_1(in_ptr0, in_ptr1, out_ptr0,
    out_ptr1, xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex
    x1 = xindex // 16 % 4
    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.2
    tmp6 = tmp2 * tmp5
    tmp7 = tl.where(tmp4, tmp2, tmp6)
    tmp8 = 1.4142135623730951
    tmp9 = tmp7 * tmp8
    tl.store(out_ptr0 + x3, tmp4, xmask)
    tl.store(out_ptr1 + x3, tmp9, xmask)


@triton.jit
def triton_poi_fused_add_div_leaky_relu_mul_2(in_ptr0, in_ptr1, in_ptr2,
    out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex
    x1 = xindex // 16 % 4
    tmp0 = tl.load(in_ptr0 + x3, xmask)
    tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
    tmp10 = tl.load(in_ptr2 + x3, xmask)
    tmp2 = tmp0 + tmp1
    tmp3 = 0.0
    tmp4 = tmp2 > tmp3
    tmp5 = 0.2
    tmp6 = tmp2 * tmp5
    tmp7 = tl.where(tmp4, tmp2, tmp6)
    tmp8 = 1.4142135623730951
    tmp9 = tmp7 * tmp8
    tmp11 = tmp9 + tmp10
    tmp12 = 0.7071067811865475
    tmp13 = tmp11 * tmp12
    tl.store(out_ptr0 + x3, tmp4, xmask)
    tl.store(out_ptr1 + x3, tmp13, xmask)


def call(args):
    (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
        primals_7) = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_2, (1, 4, 4, 3, 3), (144, 36, 9, 3, 1))
    assert_size_stride(primals_3, (1, 1, 4, 1, 1), (4, 4, 1, 1, 1))
    assert_size_stride(primals_4, (4,), (1,))
    assert_size_stride(primals_5, (1, 4, 4, 3, 3), (144, 36, 9, 3, 1))
    assert_size_stride(primals_6, (1, 1, 4, 1, 1), (4, 4, 1, 1, 1))
    assert_size_stride(primals_7, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf1 = buf0
        del buf0
        buf2 = empty_strided_cuda((4, 4, 4, 3, 3), (144, 36, 9, 3, 1),
            torch.float32)
        get_raw_stream(0)
        triton_per_fused_add_mul_pow_rsqrt_sum_0[grid(16)](buf1, primals_2,
            primals_3, buf2, 16, 36, XBLOCK=8, num_warps=4, num_stages=1)
        buf3 = extern_kernels.convolution(reinterpret_tensor(primals_1, (1,
            16, 4, 4), (256, 16, 4, 1), 0), reinterpret_tensor(buf2, (16, 4,
            3, 3), (36, 9, 3, 1), 0), stride=(1, 1), padding=(1, 1),
            dilation=(1, 1), transposed=False, output_padding=(0, 0),
            groups=4, bias=None)
        assert_size_stride(buf3, (1, 16, 4, 4), (256, 16, 4, 1))
        buf4 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool)
        buf8 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused_add_leaky_relu_mul_1[grid(256)](buf3, primals_4,
            buf4, buf8, 256, XBLOCK=256, num_warps=4, num_stages=1)
        del primals_4
        buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf6 = buf5
        del buf5
        buf7 = empty_strided_cuda((4, 4, 4, 3, 3), (144, 36, 9, 3, 1),
            torch.float32)
        triton_per_fused_add_mul_pow_rsqrt_sum_0[grid(16)](buf6, primals_5,
            primals_6, buf7, 16, 36, XBLOCK=8, num_warps=4, num_stages=1)
        buf9 = extern_kernels.convolution(reinterpret_tensor(buf8, (1, 16, 
            4, 4), (0, 16, 4, 1), 0), reinterpret_tensor(buf7, (16, 4, 3, 3
            ), (36, 9, 3, 1), 0), stride=(1, 1), padding=(1, 1), dilation=(
            1, 1), transposed=False, output_padding=(0, 0), groups=4, bias=None
            )
        assert_size_stride(buf9, (1, 16, 4, 4), (256, 16, 4, 1))
        buf10 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool)
        buf11 = reinterpret_tensor(buf3, (4, 4, 4, 4), (64, 16, 4, 1), 0)
        del buf3
        triton_poi_fused_add_div_leaky_relu_mul_2[grid(256)](buf9,
            primals_7, primals_1, buf10, buf11, 256, XBLOCK=256, num_warps=
            4, num_stages=1)
        del buf9
        del primals_7
    return (buf11, primals_2, primals_3, primals_5, primals_6, buf1,
        reinterpret_tensor(buf2, (16, 4, 3, 3), (36, 9, 3, 1), 0),
        reinterpret_tensor(primals_1, (1, 16, 4, 4), (256, 16, 4, 1), 0),
        buf4, buf6, reinterpret_tensor(buf7, (16, 4, 3, 3), (36, 9, 3, 1), 
        0), reinterpret_tensor(buf8, (1, 16, 4, 4), (256, 16, 4, 1), 0), buf10)


def make_kernel(k):
    k = torch.tensor(k, dtype=torch.float32)
    if k.ndim == 1:
        k = k[None, :] * k[:, None]
    k /= k.sum()
    return k


def upfirdn2d_native(input, kernel, up_x, up_y, down_x, down_y, pad_x0,
    pad_x1, pad_y0, pad_y1):
    _, channel, in_h, in_w = input.shape
    input = input.reshape(-1, in_h, in_w, 1)
    _, in_h, in_w, minor = input.shape
    kernel_h, kernel_w = kernel.shape
    out = input.view(-1, in_h, 1, in_w, 1, minor)
    out = F.pad(out, [0, 0, 0, up_x - 1, 0, 0, 0, up_y - 1])
    out = out.view(-1, in_h * up_y, in_w * up_x, minor)
    out = F.pad(out, [0, 0, max(pad_x0, 0), max(pad_x1, 0), max(pad_y0, 0),
        max(pad_y1, 0)])
    out = out[:, max(-pad_y0, 0):out.shape[1] - max(-pad_y1, 0), max(-
        pad_x0, 0):out.shape[2] - max(-pad_x1, 0), :]
    out = out.permute(0, 3, 1, 2)
    out = out.reshape([-1, 1, in_h * up_y + pad_y0 + pad_y1, in_w * up_x +
        pad_x0 + pad_x1])
    w = torch.flip(kernel, [0, 1]).view(1, 1, kernel_h, kernel_w)
    out = F.conv2d(out, w)
    out = out.reshape(-1, minor, in_h * up_y + pad_y0 + pad_y1 - kernel_h +
        1, in_w * up_x + pad_x0 + pad_x1 - kernel_w + 1)
    out = out.permute(0, 2, 3, 1)
    out = out[:, ::down_y, ::down_x, :]
    out_h = (in_h * up_y + pad_y0 + pad_y1 - kernel_h) // down_y + 1
    out_w = (in_w * up_x + pad_x0 + pad_x1 - kernel_w) // down_x + 1
    return out.view(-1, channel, out_h, out_w)


def upfirdn2d(input, kernel, up=1, down=1, pad=(0, 0)):
    out = upfirdn2d_native(input, kernel, up, up, down, down, pad[0], pad[1
        ], pad[0], pad[1])
    return out


def fused_leaky_relu(input, bias, negative_slope=0.2, scale=2 ** 0.5):
    rest_dim = [1] * (input.ndim - bias.ndim - 1)
    if input.ndim == 3:
        return F.leaky_relu(input + bias.view(1, *rest_dim, bias.shape[0]),
            negative_slope=negative_slope) * scale
    else:
        return F.leaky_relu(input + bias.view(1, bias.shape[0], *rest_dim),
            negative_slope=negative_slope) * scale


class Blur(nn.Module):

    def __init__(self, kernel, pad, upsample_factor=1):
        super().__init__()
        kernel = make_kernel(kernel)
        if upsample_factor > 1:
            kernel = kernel * upsample_factor ** 2
        self.register_buffer('kernel', kernel)
        self.pad = pad

    def forward(self, input):
        out = upfirdn2d(input, self.kernel, pad=self.pad)
        return out


class EqualLinear(nn.Module):

    def __init__(self, in_dim, out_dim, bias=True, bias_init=0, lr_mul=1,
        activation=None):
        super().__init__()
        self.weight = nn.Parameter(torch.randn(out_dim, in_dim).div_(lr_mul))
        if bias:
            self.bias = nn.Parameter(torch.zeros(out_dim).fill_(bias_init))
        else:
            self.bias = None
        self.activation = activation
        self.scale = 1 / math.sqrt(in_dim) * lr_mul
        self.lr_mul = lr_mul

    def forward(self, input):
        bias = self.bias * self.lr_mul if self.bias is not None else None
        if self.activation:
            out = F.linear(input, self.weight * self.scale)
            out = fused_leaky_relu(out, bias)
        else:
            out = F.linear(input, self.weight * self.scale, bias=bias)
        return out

    def __repr__(self):
        return (
            f'{self.__class__.__name__}({self.weight.shape[1]}, {self.weight.shape[0]})'
            )


class ModulatedConv2d(nn.Module):

    def __init__(self, in_channel, out_channel, kernel_size, style_dim,
        use_style=True, demodulate=True, upsample=False, downsample=False,
        blur_kernel=[1, 3, 3, 1]):
        super().__init__()
        self.eps = 1e-08
        self.kernel_size = kernel_size
        self.in_channel = in_channel
        self.out_channel = out_channel
        self.upsample = upsample
        self.downsample = downsample
        self.use_style = use_style
        if upsample:
            factor = 2
            p = len(blur_kernel) - factor - (kernel_size - 1)
            pad0 = (p + 1) // 2 + factor - 1
            pad1 = p // 2 + 1
            self.blur = Blur(blur_kernel, pad=(pad0, pad1), upsample_factor
                =factor)
        if downsample:
            factor = 2
            p = len(blur_kernel) - factor + (kernel_size - 1)
            pad0 = (p + 1) // 2
            pad1 = p // 2
            self.blur = Blur(blur_kernel, pad=(pad0, pad1))
        fan_in = in_channel * kernel_size ** 2
        self.scale = 1 / math.sqrt(fan_in)
        self.padding = kernel_size // 2
        self.weight = nn.Parameter(torch.randn(1, out_channel, in_channel,
            kernel_size, kernel_size))
        if use_style:
            self.modulation = EqualLinear(style_dim, in_channel, bias_init=1)
        else:
            self.modulation = nn.Parameter(torch.Tensor(1, 1, in_channel, 1,
                1).fill_(1))
        self.demodulate = demodulate

    def __repr__(self):
        return (
            f'{self.__class__.__name__}({self.in_channel}, {self.out_channel}, {self.kernel_size}, upsample={self.upsample}, downsample={self.downsample})'
            )

    def forward(self, input, style):
        batch, in_channel, height, width = input.shape
        if self.use_style:
            style = self.modulation(style).view(batch, 1, in_channel, 1, 1)
            weight = self.scale * self.weight * style
        else:
            weight = self.scale * self.weight.expand(batch, -1, -1, -1, -1
                ) * self.modulation
        if self.demodulate:
            demod = torch.rsqrt(weight.pow(2).sum([2, 3, 4]) + 1e-08)
            weight = weight * demod.view(batch, self.out_channel, 1, 1, 1)
        weight = weight.view(batch * self.out_channel, in_channel, self.
            kernel_size, self.kernel_size)
        if self.upsample:
            input = input.view(1, batch * in_channel, height, width)
            weight = weight.view(batch, self.out_channel, in_channel, self.
                kernel_size, self.kernel_size)
            weight = weight.transpose(1, 2).reshape(batch * in_channel,
                self.out_channel, self.kernel_size, self.kernel_size)
            out = F.conv_transpose2d(input, weight, padding=0, stride=2,
                groups=batch)
            _, _, height, width = out.shape
            out = out.view(batch, self.out_channel, height, width)
            out = self.blur(out)
        elif self.downsample:
            input = self.blur(input)
            _, _, height, width = input.shape
            input = input.view(1, batch * in_channel, height, width)
            out = F.conv2d(input, weight, padding=0, stride=2, groups=batch)
            _, _, height, width = out.shape
            out = out.view(batch, self.out_channel, height, width)
        else:
            input = input.view(1, batch * in_channel, height, width)
            out = F.conv2d(input, weight, padding=self.padding, groups=batch)
            _, _, height, width = out.shape
            out = out.view(batch, self.out_channel, height, width)
        return out


class FusedLeakyReLU(nn.Module):

    def __init__(self, channel, negative_slope=0.2, scale=2 ** 0.5):
        super().__init__()
        self.bias = nn.Parameter(torch.zeros(channel))
        self.negative_slope = negative_slope
        self.scale = scale

    def forward(self, input):
        return fused_leaky_relu(input, self.bias, self.negative_slope, self
            .scale)


class StyledConv(nn.Module):

    def __init__(self, in_channel, out_channel, kernel_size, style_dim,
        use_style=True, upsample=False, downsample=False, blur_kernel=[1, 3,
        3, 1], demodulate=True):
        super().__init__()
        self.use_style = use_style
        self.conv = ModulatedConv2d(in_channel, out_channel, kernel_size,
            style_dim, use_style=use_style, upsample=upsample, downsample=
            downsample, blur_kernel=blur_kernel, demodulate=demodulate)
        self.activate = FusedLeakyReLU(out_channel)

    def forward(self, input, style=None, noise=None):
        out = self.conv(input, style)
        out = self.activate(out)
        return out


class InResBlockNew(nn.Module):

    def __init__(self, in_channel, blur_kernel=[1, 3, 3, 1]):
        super().__init__()
        self.conv1 = StyledConv(in_channel, in_channel, 3, None,
            blur_kernel=blur_kernel, demodulate=True, use_style=False)
        self.conv2 = StyledConv(in_channel, in_channel, 3, None,
            blur_kernel=blur_kernel, demodulate=True, use_style=False)

    def forward(self, input_0):
        primals_2 = self.conv1.conv.weight
        primals_3 = self.conv1.conv.modulation
        primals_4 = self.conv1.activate.bias
        primals_5 = self.conv2.conv.weight
        primals_6 = self.conv2.conv.modulation
        primals_7 = self.conv2.activate.bias
        primals_1 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6, primals_7])
        return output[0]


Measured latency_ms: 0.087865