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
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_mul_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 4
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + x0, xmask)
    tmp1 = 0.7071067811865476
    tmp2 = tmp0 * tmp1
    tl.store(out_ptr0 + x0, tmp2, xmask)


@triton.jit
def triton_poi_fused_mul_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 32
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + x0, xmask)
    tmp1 = 0.7071067811865476
    tmp2 = tmp0 * tmp1
    tl.store(out_ptr0 + x0, tmp2, xmask)


@triton.jit
def triton_per_fused__native_batch_norm_legit_add_leaky_relu_mul_repeat_2(
    in_out_ptr0, in_out_ptr1, in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0,
    out_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    RBLOCK: tl.constexpr = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r2 = rindex
    x0 = xindex % 4
    x3 = xindex
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + (r2 + 16 * x0), xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp1 = tl.load(in_ptr1 + r2, None, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr2 + x3 % 4, xmask, eviction_policy='evict_last')
    tmp26 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
    tmp31 = tl.load(in_ptr3 + (x0 + 8 * x1), xmask, eviction_policy=
        'evict_last')
    tmp33 = tl.load(in_ptr3 + (4 + x0 + 8 * x1), xmask, eviction_policy=
        'evict_last')
    tmp3 = tmp1 * tmp2
    tmp4 = tmp0 + tmp3
    tmp5 = tl.broadcast_to(tmp4, [XBLOCK, RBLOCK])
    tl.where(xmask, tmp5, 0)
    tmp8 = tl.broadcast_to(tmp5, [XBLOCK, RBLOCK])
    tmp10 = tl.where(xmask, tmp8, 0)
    tmp11 = tl.sum(tmp10, 1)[:, None]
    tmp12 = tl.full([XBLOCK, 1], 16, tl.int32)
    tmp13 = tmp12.to(tl.float32)
    tmp14 = tmp11 / tmp13
    tmp15 = tmp5 - tmp14
    tmp16 = tmp15 * tmp15
    tmp17 = tl.broadcast_to(tmp16, [XBLOCK, RBLOCK])
    tmp19 = tl.where(xmask, tmp17, 0)
    tmp20 = tl.sum(tmp19, 1)[:, None]
    tmp21 = 16.0
    tmp22 = tmp20 / tmp21
    tmp23 = 1e-05
    tmp24 = tmp22 + tmp23
    tmp25 = libdevice.rsqrt(tmp24)
    tmp27 = tmp1 * tmp26
    tmp28 = tmp0 + tmp27
    tmp29 = tmp28 - tmp14
    tmp30 = tmp29 * tmp25
    tmp32 = tmp30 * tmp31
    tmp34 = tmp32 + tmp33
    tmp35 = 0.0
    tmp36 = tmp34 > tmp35
    tmp37 = 0.2
    tmp38 = tmp34 * tmp37
    tmp39 = tl.where(tmp36, tmp34, tmp38)
    tl.store(out_ptr0 + (r2 + 16 * x3), tmp0, xmask)
    tl.debug_barrier()
    tl.store(in_out_ptr0 + x3, tmp25, xmask)
    tl.store(in_out_ptr1 + (r2 + 16 * x3), tmp39, xmask)
    tl.store(out_ptr1 + x3, tmp14, xmask)


@triton.jit
def triton_poi_fused_mul_3(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 144
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + x0, xmask)
    tmp1 = 0.23570226039551584
    tmp2 = tmp0 * tmp1
    tl.store(out_ptr0 + x0, tmp2, xmask)


@triton.jit
def triton_per_fused__native_batch_norm_legit_add_convolution_leaky_relu_mul_4(
    in_out_ptr0, in_out_ptr1, in_out_ptr2, in_ptr0, in_ptr1, in_ptr2,
    in_ptr3, out_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    RBLOCK: tl.constexpr = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r2 = rindex
    x3 = xindex
    x0 = xindex % 4
    x1 = xindex // 4
    tmp0 = tl.load(in_out_ptr0 + (r2 + 16 * x3), xmask, other=0.0)
    tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr1 + r2, None, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr2 + x3 % 4, xmask, eviction_policy='evict_last')
    tmp28 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
    tmp33 = tl.load(in_ptr3 + (x0 + 8 * x1), xmask, eviction_policy=
        'evict_last')
    tmp35 = tl.load(in_ptr3 + (4 + x0 + 8 * x1), xmask, eviction_policy=
        'evict_last')
    tmp2 = tmp0 + tmp1
    tmp5 = tmp3 * tmp4
    tmp6 = tmp2 + tmp5
    tmp7 = tl.broadcast_to(tmp6, [XBLOCK, RBLOCK])
    tl.where(xmask, tmp7, 0)
    tmp10 = tl.broadcast_to(tmp7, [XBLOCK, RBLOCK])
    tmp12 = tl.where(xmask, tmp10, 0)
    tmp13 = tl.sum(tmp12, 1)[:, None]
    tmp14 = tl.full([XBLOCK, 1], 16, tl.int32)
    tmp15 = tmp14.to(tl.float32)
    tmp16 = tmp13 / tmp15
    tmp17 = tmp7 - tmp16
    tmp18 = tmp17 * tmp17
    tmp19 = tl.broadcast_to(tmp18, [XBLOCK, RBLOCK])
    tmp21 = tl.where(xmask, tmp19, 0)
    tmp22 = tl.sum(tmp21, 1)[:, None]
    tmp23 = 16.0
    tmp24 = tmp22 / tmp23
    tmp25 = 1e-05
    tmp26 = tmp24 + tmp25
    tmp27 = libdevice.rsqrt(tmp26)
    tmp29 = tmp3 * tmp28
    tmp30 = tmp2 + tmp29
    tmp31 = tmp30 - tmp16
    tmp32 = tmp31 * tmp27
    tmp34 = tmp32 * tmp33
    tmp36 = tmp34 + tmp35
    tmp37 = 0.0
    tmp38 = tmp36 > tmp37
    tmp39 = 0.2
    tmp40 = tmp36 * tmp39
    tmp41 = tl.where(tmp38, tmp36, tmp40)
    tl.store(in_out_ptr0 + (r2 + 16 * x3), tmp2, xmask)
    tl.debug_barrier()
    tl.store(in_out_ptr1 + x3, tmp27, xmask)
    tl.store(in_out_ptr2 + (r2 + 16 * x3), tmp41, xmask)
    tl.store(out_ptr0 + x3, tmp16, xmask)


def call(args):
    (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
        primals_7, primals_8, primals_9, primals_10, primals_11) = args
    args.clear()
    assert_size_stride(primals_1, (1, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_2, (4, 4), (4, 1))
    assert_size_stride(primals_3, (1, 4, 1, 1), (4, 1, 1, 1))
    assert_size_stride(primals_4, (8, 4), (4, 1))
    assert_size_stride(primals_5, (8,), (1,))
    assert_size_stride(primals_6, (4, 4), (4, 1))
    assert_size_stride(primals_7, (4, 4, 3, 3), (36, 9, 3, 1))
    assert_size_stride(primals_8, (4,), (1,))
    assert_size_stride(primals_9, (1, 4, 1, 1), (4, 1, 1, 1))
    assert_size_stride(primals_10, (8, 4), (4, 1))
    assert_size_stride(primals_11, (8,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf1 = empty_strided_cuda((1, 4, 1, 1), (4, 1, 1, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_mul_0[grid(4)](primals_3, buf1, 4, XBLOCK=4,
            num_warps=1, num_stages=1)
        del primals_3
        buf2 = empty_strided_cuda((8, 4), (4, 1), torch.float32)
        triton_poi_fused_mul_1[grid(32)](primals_4, buf2, 32, XBLOCK=32,
            num_warps=1, num_stages=1)
        del primals_4
        buf3 = empty_strided_cuda((4, 8), (8, 1), torch.float32)
        extern_kernels.addmm(primals_5, primals_6, reinterpret_tensor(buf2,
            (4, 8), (1, 4), 0), alpha=1, beta=1, out=buf3)
        del primals_5
        buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        buf4 = empty_strided_cuda((1, 16, 1, 1), (16, 1, 1, 1), torch.float32)
        buf5 = empty_strided_cuda((1, 16, 1, 1), (16, 1, 16, 16), torch.float32
            )
        buf7 = reinterpret_tensor(buf5, (1, 16, 1, 1), (16, 1, 1, 1), 0)
        del buf5
        buf8 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        buf9 = buf8
        del buf8
        triton_per_fused__native_batch_norm_legit_add_leaky_relu_mul_repeat_2[
            grid(16)](buf7, buf9, primals_1, primals_2, buf1, buf3, buf0,
            buf4, 16, 16, XBLOCK=8, num_warps=2, num_stages=1)
        del primals_1
        buf10 = empty_strided_cuda((4, 4, 3, 3), (36, 9, 3, 1), torch.float32)
        triton_poi_fused_mul_3[grid(144)](primals_7, buf10, 144, XBLOCK=256,
            num_warps=4, num_stages=1)
        del primals_7
        buf11 = extern_kernels.convolution(buf9, buf10, stride=(1, 1),
            padding=(1, 1), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf11, (4, 4, 4, 4), (64, 16, 4, 1))
        buf13 = empty_strided_cuda((1, 4, 1, 1), (4, 1, 1, 1), torch.float32)
        triton_poi_fused_mul_0[grid(4)](primals_9, buf13, 4, XBLOCK=4,
            num_warps=1, num_stages=1)
        del primals_9
        buf14 = empty_strided_cuda((8, 4), (4, 1), torch.float32)
        triton_poi_fused_mul_1[grid(32)](primals_10, buf14, 32, XBLOCK=32,
            num_warps=1, num_stages=1)
        del primals_10
        buf15 = empty_strided_cuda((4, 8), (8, 1), torch.float32)
        extern_kernels.addmm(primals_11, primals_6, reinterpret_tensor(
            buf14, (4, 8), (1, 4), 0), alpha=1, beta=1, out=buf15)
        del primals_11
        buf12 = buf11
        del buf11
        buf16 = empty_strided_cuda((1, 16, 1, 1), (16, 1, 1, 1), torch.float32)
        buf17 = empty_strided_cuda((1, 16, 1, 1), (16, 1, 16, 16), torch.
            float32)
        buf19 = reinterpret_tensor(buf17, (1, 16, 1, 1), (16, 1, 1, 1), 0)
        del buf17
        buf20 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        buf21 = buf20
        del buf20
        triton_per_fused__native_batch_norm_legit_add_convolution_leaky_relu_mul_4[
            grid(16)](buf12, buf19, buf21, primals_8, primals_2, buf13,
            buf15, buf16, 16, 16, XBLOCK=1, num_warps=2, num_stages=1)
        del primals_8
    return (buf21, buf1, buf2, buf10, buf13, buf14, primals_2, primals_6,
        buf0, buf1, reinterpret_tensor(buf3, (4, 4, 1, 1), (8, 1, 1, 1), 0),
        reinterpret_tensor(buf3, (4, 4, 1, 1), (8, 1, 1, 1), 4), buf4, buf7,
        buf9, buf10, buf12, buf13, reinterpret_tensor(buf15, (4, 4, 1, 1),
        (8, 1, 1, 1), 0), reinterpret_tensor(buf15, (4, 4, 1, 1), (8, 1, 1,
        1), 4), buf16, buf19)


def quick_scale(module, name='weight'):
    ScaleW.apply(module, name)
    return module


class ScaleW:
    """
    Constructor: name - name of attribute to be scaled
    """

    def __init__(self, name):
        self.name = name

    def scale(self, module):
        weight = getattr(module, self.name + '_orig')
        fan_in = weight.data.size(1) * weight.data[0][0].numel()
        return weight * math.sqrt(2 / fan_in)

    @staticmethod
    def apply(module, name):
        """
        Apply runtime scaling to specific module
        """
        hook = ScaleW(name)
        weight = getattr(module, name)
        module.register_parameter(name + '_orig', nn.Parameter(weight.data))
        del module._parameters[name]
        module.register_forward_pre_hook(hook)

    def __call__(self, module, whatever):
        weight = self.scale(module)
        setattr(module, self.name, weight)


class SLinear(nn.Module):

    def __init__(self, dim_in, dim_out):
        super().__init__()
        linear = nn.Linear(dim_in, dim_out)
        linear.weight.data.normal_()
        linear.bias.data.zero_()
        self.linear = quick_scale(linear)

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


class SConv2d(nn.Module):

    def __init__(self, *args, **kwargs):
        super().__init__()
        conv = nn.Conv2d(*args, **kwargs)
        conv.weight.data.normal_()
        conv.bias.data.zero_()
        self.conv = quick_scale(conv)

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


class FC_A(nn.Module):
    """
    Learned affine transform A, this module is used to transform
    midiate vector w into a style vector
    """

    def __init__(self, dim_latent, n_channel):
        super().__init__()
        self.transform = SLinear(dim_latent, n_channel * 2)
        self.transform.linear.bias.data[:n_channel] = 1
        self.transform.linear.bias.data[n_channel:] = 0

    def forward(self, w):
        style = self.transform(w).unsqueeze(2).unsqueeze(3)
        return style


class AdaIn(nn.Module):
    """
    adaptive instance normalization
    """

    def __init__(self, n_channel):
        super().__init__()
        self.norm = nn.InstanceNorm2d(n_channel)

    def forward(self, image, style):
        factor, bias = style.chunk(2, 1)
        result = self.norm(image)
        result = result * factor + bias
        return result


class Scale_B(nn.Module):
    """
    Learned per-channel scale factor, used to scale the noise
    """

    def __init__(self, n_channel):
        super().__init__()
        self.weight = nn.Parameter(torch.zeros((1, n_channel, 1, 1)))

    def forward(self, noise):
        result = noise * self.weight
        return result


class Early_StyleConv_BlockNew(nn.Module):
    """
    This is the very first block of generator that get the constant value as input
    """

    def __init__(self, n_channel, dim_latent, dim_input):
        super().__init__()
        self.constant = nn.Parameter(torch.randn(1, n_channel, dim_input,
            dim_input))
        self.style1 = FC_A(dim_latent, n_channel)
        self.style2 = FC_A(dim_latent, n_channel)
        self.noise1 = quick_scale(Scale_B(n_channel))
        self.noise2 = quick_scale(Scale_B(n_channel))
        self.adain = AdaIn(n_channel)
        self.lrelu = nn.LeakyReLU(0.2)
        self.conv = SConv2d(n_channel, n_channel, 3, padding=1)

    def forward(self, input_0, input_1):
        primals_1 = self.constant
        primals_5 = self.style1.transform.linear.bias
        primals_4 = self.style1.transform.linear.weight_orig
        primals_11 = self.style2.transform.linear.bias
        primals_10 = self.style2.transform.linear.weight_orig
        primals_3 = self.noise1.weight_orig
        primals_9 = self.noise2.weight_orig
        primals_8 = self.conv.conv.bias
        primals_7 = self.conv.conv.weight_orig
        primals_2 = input_0
        primals_6 = input_1
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6, primals_7, primals_8, primals_9,
            primals_10, primals_11])
        return output[0]


Measured latency_ms: 0.033554