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
import torch.nn.parallel
import torch.utils.data
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_per_fused_linalg_vector_norm_mv_0(in_ptr0, in_ptr1, out_ptr0,
    out_ptr1, 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 = tl.load(in_ptr1 + 0)
    tmp2 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK])
    tmp4 = tl.load(in_ptr0 + (4 + r0), None)
    tmp5 = tl.load(in_ptr1 + 1)
    tmp6 = tl.broadcast_to(tmp5, [XBLOCK, RBLOCK])
    tmp9 = tl.load(in_ptr0 + (8 + r0), None)
    tmp10 = tl.load(in_ptr1 + 2)
    tmp11 = tl.broadcast_to(tmp10, [XBLOCK, RBLOCK])
    tmp14 = tl.load(in_ptr0 + (12 + r0), None)
    tmp15 = tl.load(in_ptr1 + 3)
    tmp16 = tl.broadcast_to(tmp15, [XBLOCK, RBLOCK])
    tmp3 = tmp0 * tmp2
    tmp7 = tmp4 * tmp6
    tmp8 = tmp3 + tmp7
    tmp12 = tmp9 * tmp11
    tmp13 = tmp8 + tmp12
    tmp17 = tmp14 * tmp16
    tmp18 = tmp13 + tmp17
    tmp19 = tmp18 * tmp18
    tmp20 = tl.broadcast_to(tmp19, [XBLOCK, RBLOCK])
    tmp22 = tl.sum(tmp20, 1)[:, None]
    tl.store(out_ptr0 + tl.broadcast_to(r0, [XBLOCK, RBLOCK]), tmp18, None)
    tl.store(out_ptr1 + tl.full([XBLOCK, 1], 0, tl.int32), tmp22, None)


@triton.jit
def triton_per_fused_add_div_dot_linalg_vector_norm_mv_1(in_out_ptr0,
    in_ptr0, in_ptr1, in_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 + 4 * r0, None, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + 0)
    tmp2 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK])
    tmp3 = tl.load(in_ptr2 + 0)
    tmp4 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK])
    tmp10 = tl.load(in_ptr0 + (1 + 4 * r0), None, eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr1 + 1)
    tmp12 = tl.broadcast_to(tmp11, [XBLOCK, RBLOCK])
    tmp16 = tl.load(in_ptr0 + (2 + 4 * r0), None, eviction_policy='evict_last')
    tmp17 = tl.load(in_ptr1 + 2)
    tmp18 = tl.broadcast_to(tmp17, [XBLOCK, RBLOCK])
    tmp22 = tl.load(in_ptr0 + (3 + 4 * r0), None, eviction_policy='evict_last')
    tmp23 = tl.load(in_ptr1 + 3)
    tmp24 = tl.broadcast_to(tmp23, [XBLOCK, RBLOCK])
    tmp5 = libdevice.sqrt(tmp4)
    tmp6 = 0.0001
    tmp7 = tmp5 + tmp6
    tmp8 = tmp2 / tmp7
    tmp9 = tmp0 * tmp8
    tmp13 = tmp12 / tmp7
    tmp14 = tmp10 * tmp13
    tmp15 = tmp9 + tmp14
    tmp19 = tmp18 / tmp7
    tmp20 = tmp16 * tmp19
    tmp21 = tmp15 + tmp20
    tmp25 = tmp24 / tmp7
    tmp26 = tmp22 * tmp25
    tmp27 = tmp21 + tmp26
    tmp28 = tmp27 * tmp27
    tmp29 = tl.broadcast_to(tmp28, [XBLOCK, RBLOCK])
    tmp31 = tl.sum(tmp29, 1)[:, None]
    tmp32 = libdevice.sqrt(tmp31)
    tmp33 = tmp32 + tmp6
    tmp34 = tmp27 / tmp33
    tmp35 = tmp34 * tmp27
    tmp36 = tl.broadcast_to(tmp35, [XBLOCK, RBLOCK])
    tmp38 = tl.sum(tmp36, 1)[:, None]
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp38, None)


@triton.jit
def triton_poi_fused_div_2(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.
    constexpr):
    xnumel = 16
    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__native_batch_norm_legit_no_training_add_mul_3(in_ptr0,
    in_ptr1, in_ptr2, in_ptr3, in_ptr4, out_ptr0, xnumel, XBLOCK: tl.constexpr
    ):
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    tl.full([XBLOCK], True, tl.int1)
    x3 = xindex // 16
    x4 = xindex
    x1 = xindex // 16 % 4
    tmp0 = tl.load(in_ptr0 + x3, None, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr1 + x4, None)
    tmp4 = tl.load(in_ptr2 + x1, None, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr3 + x1, None, eviction_policy='evict_last')
    tmp15 = tl.load(in_ptr4 + x3, None, eviction_policy='evict_last')
    tmp1 = 1.0
    tmp2 = tmp0 + tmp1
    tmp5 = tmp3 - tmp4
    tmp7 = 0.0001
    tmp8 = tmp6 + tmp7
    tmp9 = libdevice.sqrt(tmp8)
    tmp10 = tl.full([1], 1, tl.int32)
    tmp11 = tmp10 / tmp9
    tmp12 = tmp11 * tmp1
    tmp13 = tmp5 * tmp12
    tmp14 = tmp2 * tmp13
    tmp16 = tmp14 + tmp15
    tl.store(out_ptr0 + x4, tmp16, None)


def call(args):
    (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
        primals_7, primals_8) = args
    args.clear()
    assert_size_stride(primals_1, (64, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_2, (4,), (1,))
    assert_size_stride(primals_3, (4,), (1,))
    assert_size_stride(primals_4, (4,), (1,))
    assert_size_stride(primals_5, (4, 4), (4, 1))
    assert_size_stride(primals_6, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_7, (4,), (1,))
    assert_size_stride(primals_8, (4, 4), (4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4,), (1,), torch.float32)
        buf1 = empty_strided_cuda((), (), torch.float32)
        get_raw_stream(0)
        triton_per_fused_linalg_vector_norm_mv_0[grid(1)](primals_5,
            primals_4, buf0, buf1, 1, 4, XBLOCK=1, num_warps=2, num_stages=1)
        buf3 = empty_strided_cuda((), (), torch.float32)
        buf4 = buf3
        del buf3
        triton_per_fused_add_div_dot_linalg_vector_norm_mv_1[grid(1)](buf4,
            primals_5, buf0, buf1, 1, 4, XBLOCK=1, num_warps=2, num_stages=1)
        buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_poi_fused_div_2[grid(16)](primals_5, buf4, buf5, 16, XBLOCK=
            16, num_warps=1, num_stages=1)
        buf6 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_6, (64, 4), (4, 1), 0),
            reinterpret_tensor(buf5, (4, 4), (1, 4), 0), out=buf6)
        buf7 = buf0
        del buf0
        buf8 = buf4
        del buf4
        triton_per_fused_linalg_vector_norm_mv_0[grid(1)](primals_8,
            primals_7, buf7, buf8, 1, 4, XBLOCK=1, num_warps=2, num_stages=1)
        buf10 = buf1
        del buf1
        buf11 = buf10
        del buf10
        triton_per_fused_add_div_dot_linalg_vector_norm_mv_1[grid(1)](buf11,
            primals_8, buf7, buf8, 1, 4, XBLOCK=1, num_warps=2, num_stages=1)
        del buf7
        del buf8
        buf12 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_poi_fused_div_2[grid(16)](primals_8, buf11, buf12, 16,
            XBLOCK=16, num_warps=1, num_stages=1)
        del buf11
        buf13 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_6, (64, 4), (4, 1), 0),
            reinterpret_tensor(buf12, (4, 4), (1, 4), 0), out=buf13)
        buf14 = empty_strided_cuda((64, 4, 4, 4), (64, 16, 4, 1), torch.float32
            )
        triton_poi_fused__native_batch_norm_legit_no_training_add_mul_3[grid
            (4096)](buf6, primals_1, primals_2, primals_3, buf13, buf14, 
            4096, XBLOCK=128, num_warps=4, num_stages=1)
        del buf13
        del buf6
    return (buf14, buf5, buf12, primals_1, primals_2, primals_3, primals_4,
        primals_5, primals_7, primals_8, reinterpret_tensor(primals_6, (64,
        4), (4, 1), 0))


def l2normalize(v, eps=0.0001):
    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]
        _w = w.view(height, -1)
        for _ in range(self.power_iterations):
            v = l2normalize(torch.matmul(_w.t(), u))
            u = l2normalize(torch.matmul(_w, v))
        sigma = u.dot(_w.mv(v))
        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]
        w.view(height, -1).data.shape[1]
        u = Parameter(w.data.new(height).normal_(0, 1), requires_grad=False)
        v = Parameter(w.data.new(height).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 ConditionalBatchNorm2dNew(nn.Module):

    def __init__(self, num_features, num_classes, eps=0.0001, momentum=0.1):
        super().__init__()
        self.num_features = num_features
        self.bn = nn.BatchNorm2d(num_features, affine=False, eps=eps,
            momentum=momentum)
        self.gamma_embed = SpectralNorm(nn.Linear(num_classes, num_features,
            bias=False))
        self.beta_embed = SpectralNorm(nn.Linear(num_classes, num_features,
            bias=False))

    def forward(self, input_0, input_1):
        primals_2 = self.gamma_embed.module.weight_u
        primals_3 = self.gamma_embed.module.weight_v
        primals_5 = self.gamma_embed.module.weight_bar
        primals_4 = self.beta_embed.module.weight_u
        primals_7 = self.beta_embed.module.weight_v
        primals_8 = self.beta_embed.module.weight_bar
        primals_1 = input_0
        primals_6 = input_1
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6, primals_7, primals_8])
        return output[0]


Measured latency_ms: 0.020389