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 torch.nn as nn
from torch.nn import Parameter
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__unsafe_index_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.
    constexpr):
    xnumel = 1024
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 8 % 8
    x0 = xindex % 8
    x2 = xindex // 64
    x4 = xindex
    tmp0 = x1
    tmp1 = tmp0.to(tl.float32)
    tmp2 = 0.5
    tmp3 = tmp1 * tmp2
    tmp4 = tmp3.to(tl.int32)
    tmp5 = x0
    tmp6 = tmp5.to(tl.float32)
    tmp7 = tmp6 * tmp2
    tmp8 = tmp7.to(tl.int32)
    tmp9 = tl.load(in_ptr0 + (tmp8 + 4 * tmp4 + 16 * x2), xmask,
        eviction_policy='evict_last')
    tl.store(out_ptr0 + x4, tmp9, xmask)


@triton.jit
def triton_per_fused_add_div_linalg_vector_norm_1(in_ptr0, out_ptr1, xnumel,
    rnumel, XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 64
    xoffset = tl.program_id(0) * XBLOCK
    xoffset + tl.arange(0, XBLOCK)[:, None]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r0 = rindex
    tmp0 = tl.load(in_ptr0 + r0, None)
    tmp1 = tmp0 * tmp0
    tmp2 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK])
    tmp4 = tl.sum(tmp2, 1)[:, None]
    tmp5 = libdevice.sqrt(tmp4)
    tmp6 = 1e-12
    tmp7 = tmp5 + tmp6
    tmp8 = tmp0 / tmp7
    tl.store(out_ptr1 + tl.broadcast_to(r0, [XBLOCK, RBLOCK]), tmp8, None)


@triton.jit
def triton_per_fused_add_div_linalg_vector_norm_mul_sum_2(in_ptr0, out_ptr1,
    out_ptr2, xnumel, rnumel, XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 4
    xoffset = tl.program_id(0) * XBLOCK
    xoffset + tl.arange(0, XBLOCK)[:, None]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r0 = rindex
    tmp0 = tl.load(in_ptr0 + r0, None)
    tmp1 = tmp0 * tmp0
    tmp2 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK])
    tmp4 = tl.sum(tmp2, 1)[:, None]
    tmp5 = libdevice.sqrt(tmp4)
    tmp6 = 1e-12
    tmp7 = tmp5 + tmp6
    tmp8 = tmp0 / tmp7
    tmp9 = tmp8 * tmp0
    tmp10 = tl.broadcast_to(tmp9, [XBLOCK, RBLOCK])
    tmp12 = tl.sum(tmp10, 1)[:, None]
    tl.store(out_ptr2 + tl.broadcast_to(r0, [XBLOCK, RBLOCK]), tmp8, None)
    tl.store(out_ptr1 + tl.full([XBLOCK, 1], 0, tl.int32), tmp12, None)


@triton.jit
def triton_poi_fused_div_3(in_ptr0, in_ptr1, 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_ptr0 + x0, xmask)
    tmp1 = tl.load(in_ptr1 + 0)
    tmp2 = tl.broadcast_to(tmp1, [XBLOCK])
    tmp3 = tmp0 / tmp2
    tl.store(out_ptr0 + x0, tmp3, xmask)


@triton.jit
def triton_poi_fused_convolution_leaky_relu_mul_sigmoid_4(in_out_ptr0,
    in_out_ptr1, in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 400
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex
    x1 = xindex // 25 % 4
    tmp0 = tl.load(in_out_ptr0 + x3, xmask)
    tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_out_ptr1 + x3, xmask)
    tmp4 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp5 = tmp3 + tmp4
    tmp6 = 0.0
    tmp7 = tmp2 > tmp6
    tmp8 = 0.2
    tmp9 = tmp2 * tmp8
    tmp10 = tl.where(tmp7, tmp2, tmp9)
    tmp11 = tl.sigmoid(tmp5)
    tmp12 = tmp10 * tmp11
    tl.store(in_out_ptr0 + x3, tmp2, xmask)
    tl.store(in_out_ptr1 + x3, tmp5, xmask)
    tl.store(out_ptr0 + x3, tmp12, 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, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_2, (4,), (1,))
    assert_size_stride(primals_3, (64,), (1,))
    assert_size_stride(primals_4, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_5, (4,), (1,))
    assert_size_stride(primals_6, (4,), (1,))
    assert_size_stride(primals_7, (64,), (1,))
    assert_size_stride(primals_8, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_9, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4, 8, 8), (256, 64, 8, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused__unsafe_index_0[grid(1024)](primals_1, buf0, 1024,
            XBLOCK=256, num_warps=4, num_stages=1)
        del primals_1
        buf1 = empty_strided_cuda((64, 1), (1, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_4, (64, 4), (1, 64), 0
            ), reinterpret_tensor(primals_2, (4, 1), (1, 1), 0), out=buf1)
        buf3 = empty_strided_cuda((64,), (1,), torch.float32)
        triton_per_fused_add_div_linalg_vector_norm_1[grid(1)](buf1, buf3, 
            1, 64, XBLOCK=1, num_warps=2, num_stages=1)
        buf4 = empty_strided_cuda((4, 1), (1, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_4, (4, 64), (64, 1), 0
            ), reinterpret_tensor(buf3, (64, 1), (1, 0), 0), out=buf4)
        buf6 = empty_strided_cuda((), (), torch.float32)
        buf20 = empty_strided_cuda((4,), (1,), torch.float32)
        triton_per_fused_add_div_linalg_vector_norm_mul_sum_2[grid(1)](buf4,
            buf6, buf20, 1, 4, XBLOCK=1, num_warps=2, num_stages=1)
        buf7 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused_div_3[grid(256)](primals_4, buf6, buf7, 256,
            XBLOCK=128, num_warps=4, num_stages=1)
        del primals_4
        buf8 = extern_kernels.convolution(buf0, buf7, stride=(1, 1),
            padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf8, (4, 4, 5, 5), (100, 25, 5, 1))
        buf10 = empty_strided_cuda((64, 1), (1, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_8, (64, 4), (1, 64), 0
            ), reinterpret_tensor(primals_6, (4, 1), (1, 1), 0), out=buf10)
        buf12 = empty_strided_cuda((64,), (1,), torch.float32)
        triton_per_fused_add_div_linalg_vector_norm_1[grid(1)](buf10, buf12,
            1, 64, XBLOCK=1, num_warps=2, num_stages=1)
        buf13 = empty_strided_cuda((4, 1), (1, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_8, (4, 64), (64, 1), 0
            ), reinterpret_tensor(buf12, (64, 1), (1, 0), 0), out=buf13)
        buf15 = empty_strided_cuda((), (), torch.float32)
        buf27 = empty_strided_cuda((4,), (1,), torch.float32)
        triton_per_fused_add_div_linalg_vector_norm_mul_sum_2[grid(1)](buf13,
            buf15, buf27, 1, 4, XBLOCK=1, num_warps=2, num_stages=1)
        buf16 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused_div_3[grid(256)](primals_8, buf15, buf16, 256,
            XBLOCK=128, num_warps=4, num_stages=1)
        del primals_8
        buf17 = extern_kernels.convolution(buf0, buf16, stride=(1, 1),
            padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf17, (4, 4, 5, 5), (100, 25, 5, 1))
        buf9 = buf8
        del buf8
        buf18 = buf17
        del buf17
        buf19 = empty_strided_cuda((4, 4, 5, 5), (100, 25, 5, 1), torch.float32
            )
        triton_poi_fused_convolution_leaky_relu_mul_sigmoid_4[grid(400)](buf9,
            buf18, primals_5, primals_9, buf19, 400, XBLOCK=128, num_warps=
            4, num_stages=1)
        del primals_5
        del primals_9
        buf21 = torch.ops.aten.set_.source_Tensor(primals_2, buf20)
        assert_size_stride(buf21, (4,), (1,))
        del buf1
        del primals_2
        buf24 = torch.ops.aten.set_.source_Tensor(primals_3, buf3)
        assert_size_stride(buf24, (64,), (1,))
        del buf4
        del primals_3
        buf28 = torch.ops.aten.set_.source_Tensor(primals_6, buf27)
        assert_size_stride(buf28, (4,), (1,))
        del buf10
        del primals_6
        buf31 = torch.ops.aten.set_.source_Tensor(primals_7, buf12)
        assert_size_stride(buf31, (64,), (1,))
        del buf13
        del primals_7
    return buf19, buf7, buf16, buf0, buf6, buf7, buf9, buf15, buf16, buf18


def l2normalize(v, eps=1e-12):
    return v / (v.norm() + eps)


class SpectralNorm(nn.Module):

    def __init__(self, module, name='weight', power_iterations=1):
        super(SpectralNorm, self).__init__()
        self.module = module
        self.name = name
        self.power_iterations = power_iterations
        if not self._made_params():
            self._make_params()

    def _update_u_v(self):
        u = getattr(self.module, self.name + '_u')
        v = getattr(self.module, self.name + '_v')
        w = getattr(self.module, self.name + '_bar')
        height = w.data.shape[0]
        for _ in range(self.power_iterations):
            v.data = l2normalize(torch.mm(torch.t(w.view(height, -1).data),
                u.data.view(-1, 1)).squeeze(1))
            u.data = l2normalize(torch.mm(w.view(height, -1).data, v.data.
                view(-1, 1)).squeeze(1))
        sigma = u.data * w.view(height, -1).data.mm(v.data.view(-1, 1)
            ).squeeze(1)
        sigma = sigma.sum()
        setattr(self.module, self.name, w / sigma.expand_as(w))

    def _made_params(self):
        try:
            getattr(self.module, self.name + '_u')
            getattr(self.module, self.name + '_v')
            getattr(self.module, self.name + '_bar')
            return True
        except AttributeError:
            return False

    def _make_params(self):
        w = getattr(self.module, self.name)
        height = w.data.shape[0]
        width = w.view(height, -1).data.shape[1]
        u = Parameter(w.data.new(height).normal_(0, 1), requires_grad=False)
        v = Parameter(w.data.new(width).normal_(0, 1), requires_grad=False)
        u.data = l2normalize(u.data)
        v.data = l2normalize(v.data)
        w_bar = Parameter(w.data)
        del self.module._parameters[self.name]
        self.module.register_parameter(self.name + '_u', u)
        self.module.register_parameter(self.name + '_v', v)
        self.module.register_parameter(self.name + '_bar', w_bar)

    def forward(self, *args):
        self._update_u_v()
        return self.module.forward(*args)


class GatedConv2d(nn.Module):

    def __init__(self, in_channels, out_channels, kernel_size, stride=1,
        padding=0, dilation=1, activation='elu', sn=False):
        super(GatedConv2d, self).__init__()
        self.pad = nn.ZeroPad2d(padding)
        if activation == 'relu':
            self.activation = nn.ReLU(inplace=True)
        elif activation == 'lrelu':
            self.activation = nn.LeakyReLU(0.2, inplace=True)
        elif activation == 'elu':
            self.activation = nn.ELU()
        elif activation == 'selu':
            self.activation = nn.SELU(inplace=True)
        elif activation == 'tanh':
            self.activation = nn.Tanh()
        elif activation == 'sigmoid':
            self.activation = nn.Sigmoid()
        elif activation == 'none':
            self.activation = None
        else:
            assert 0, 'Unsupported activation: {}'.format(activation)
        if sn:
            self.conv2d = SpectralNorm(nn.Conv2d(in_channels, out_channels,
                kernel_size, stride, padding=0, dilation=dilation))
            self.mask_conv2d = SpectralNorm(nn.Conv2d(in_channels,
                out_channels, kernel_size, stride, padding=0, dilation=
                dilation))
        else:
            self.conv2d = nn.Conv2d(in_channels, out_channels, kernel_size,
                stride, padding=0, dilation=dilation)
            self.mask_conv2d = nn.Conv2d(in_channels, out_channels,
                kernel_size, stride, padding=0, dilation=dilation)
        self.sigmoid = torch.nn.Sigmoid()

    def forward(self, x):
        x = self.pad(x)
        conv = self.conv2d(x)
        if self.activation:
            conv = self.activation(conv)
        mask = self.mask_conv2d(x)
        gated_mask = self.sigmoid(mask)
        x = conv * gated_mask
        return x


class TransposeGatedConv2dNew(nn.Module):

    def __init__(self, in_channels, out_channels, kernel_size, stride=1,
        padding=0, dilation=1, activation='lrelu', sn=True, scale_factor=2):
        super(TransposeGatedConv2dNew, self).__init__()
        self.scale_factor = scale_factor
        self.gated_conv2d = GatedConv2d(in_channels, out_channels,
            kernel_size, stride, padding, dilation, activation, sn)

    def forward(self, input_0):
        primals_2 = self.gated_conv2d.conv2d.module.bias
        primals_5 = self.gated_conv2d.conv2d.module.weight_u
        primals_3 = self.gated_conv2d.conv2d.module.weight_v
        primals_1 = self.gated_conv2d.conv2d.module.weight_bar
        primals_6 = self.gated_conv2d.mask_conv2d.module.bias
        primals_9 = self.gated_conv2d.mask_conv2d.module.weight_u
        primals_7 = self.gated_conv2d.mask_conv2d.module.weight_v
        primals_4 = self.gated_conv2d.mask_conv2d.module.weight_bar
        primals_8 = 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]


Measured latency_ms: 0.032086