# AOT ID: ['0_forward']
from ctypes import c_void_p, c_long, c_int
import torch
import math
import random
import os
import tempfile
from math import inf, nan
from torch._inductor.hooks import run_intermediate_hooks
from torch._inductor.utils import maybe_profile
from torch._inductor.codegen.memory_planning import _align as align
from torch import device, empty_strided
from torch._inductor.async_compile import AsyncCompile
from torch._inductor.select_algorithm import extern_kernels
from torch._inductor.codegen.multi_kernel import MultiKernelCall
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid, split_scan_grid, grid_combo_kernels, start_graph, end_graph
from torch._C import _cuda_getCurrentRawStream as get_raw_stream

aten = torch.ops.aten
inductor_ops = torch.ops.inductor
_quantized = torch.ops._quantized
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cpu = torch._C._dynamo.guards._empty_strided_cpu
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
empty_strided_xpu = torch._C._dynamo.guards._empty_strided_xpu
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
alloc_from_pool = torch.ops.inductor._alloc_from_pool
async_compile = AsyncCompile()


# kernel path: runs/run_shard_7/inductor_cache/ho/choh64wmlh7hsuuqsanwxxdyhyh7ew67epm3l5imkc6atn5yxoef.py
# Topologically Sorted Source Nodes: [output], Original ATen: [aten.cat]
# Source node to ATen node mapping:
#   output => cat_4
# Graph fragment:
#   %cat_4 : [num_users=1] = call_function[target=torch.ops.aten.cat.default](args = ([%cat, %cat_1, %cat_2, %cat_3], 1), kwargs = {})
triton_poi_fused_cat_0 = async_compile.triton('triton_poi_fused_cat_0', '''
import triton
import triton.language as tl
from triton.compiler.compiler import AttrsDescriptor

from torch._inductor.runtime import triton_helpers, triton_heuristics
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
from torch._inductor.runtime.hints import AutotuneHint, ReductionHint, TileHint, instance_descriptor, DeviceProperties

@triton_heuristics.pointwise(
    size_hints=[16], 
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '*fp32', 3: '*fp32', 4: '*fp32', 5: 'i32'}, 'device': DeviceProperties(type='cuda', index=0, cc=80, major=8, regs_per_multiprocessor=65536, max_threads_per_multi_processor=2048, multi_processor_count=108), 'constants': {}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2, 3, 4, 5), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused_cat_0', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 4, 'num_reduction': 0, 'backend_hash': 'A9C866B4A14FD3277824029365D703C2427B2E685E54EC9B3EF4ADC8D1EEAC1D', 'are_deterministic_algorithms_enabled': False, 'assert_indirect_indexing': True, 'autotune_local_cache': True, 'autotune_pointwise': True, 'autotune_remote_cache': None, 'force_disable_caches': False, 'dynamic_scale_rblock': True, 'max_autotune': False, 'max_autotune_pointwise': False, 'min_split_scan_rblock': 256, 'spill_threshold': 16, 'store_cubin': False},
    min_elem_per_thread=0
)
@triton.jit
def triton_poi_fused_cat_0(in_ptr0, in_ptr1, in_ptr2, in_ptr3, 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 % 4
    x1 = (xindex // 4)
    x2 = xindex
    tmp9 = tl.load(in_ptr0 + (0))
    tmp10 = tl.broadcast_to(tmp9, [XBLOCK])
    tmp16 = tl.load(in_ptr1 + (0))
    tmp17 = tl.broadcast_to(tmp16, [XBLOCK])
    tmp26 = tl.load(in_ptr2 + (0))
    tmp27 = tl.broadcast_to(tmp26, [XBLOCK])
    tmp35 = tl.load(in_ptr3 + (0))
    tmp36 = tl.broadcast_to(tmp35, [XBLOCK])
    tmp0 = x0
    tmp1 = tl.full([1], 0, tl.int64)
    tmp2 = tmp0 >= tmp1
    tmp3 = tl.full([1], 1, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = x1
    tmp6 = tmp5 >= tmp1
    tmp7 = tmp5 < tmp3
    tmp8 = tmp7 & tmp4
    tmp11 = tmp5 >= tmp3
    tmp12 = tl.full([1], 2, tl.int64)
    tmp13 = tmp5 < tmp12
    tmp14 = tmp11 & tmp13
    tmp15 = tmp14 & tmp4
    tmp18 = -tmp17
    tmp19 = tl.full(tmp18.shape, 0.0, tmp18.dtype)
    tmp20 = tl.where(tmp15, tmp18, tmp19)
    tmp21 = tmp5 >= tmp12
    tmp22 = tl.full([1], 3, tl.int64)
    tmp23 = tmp5 < tmp22
    tmp24 = tmp21 & tmp23
    tmp25 = tmp24 & tmp4
    tmp28 = -tmp27
    tmp29 = tl.full(tmp28.shape, 0.0, tmp28.dtype)
    tmp30 = tl.where(tmp25, tmp28, tmp29)
    tmp31 = tmp5 >= tmp22
    tmp32 = tl.full([1], 4, tl.int64)
    tmp33 = tmp5 < tmp32
    tmp34 = tmp31 & tmp4
    tmp37 = -tmp36
    tmp38 = tl.full(tmp37.shape, 0.0, tmp37.dtype)
    tmp39 = tl.where(tmp34, tmp37, tmp38)
    tmp40 = tl.where(tmp24, tmp30, tmp39)
    tmp41 = tl.where(tmp14, tmp20, tmp40)
    tmp42 = tl.where(tmp7, tmp10, tmp41)
    tmp43 = tl.full(tmp42.shape, 0.0, tmp42.dtype)
    tmp44 = tl.where(tmp4, tmp42, tmp43)
    tmp45 = tmp0 >= tmp3
    tmp46 = tmp0 < tmp12
    tmp47 = tmp45 & tmp46
    tmp48 = tmp7 & tmp47
    tmp49 = tmp14 & tmp47
    tmp50 = tmp24 & tmp47
    tmp51 = tl.where(tmp50, tmp37, tmp38)
    tmp52 = tmp31 & tmp47
    tmp53 = tl.where(tmp24, tmp51, tmp27)
    tmp54 = tl.where(tmp14, tmp10, tmp53)
    tmp55 = tl.where(tmp7, tmp17, tmp54)
    tmp56 = tl.full(tmp55.shape, 0.0, tmp55.dtype)
    tmp57 = tl.where(tmp47, tmp55, tmp56)
    tmp58 = tmp0 >= tmp12
    tmp59 = tmp0 < tmp22
    tmp60 = tmp58 & tmp59
    tmp61 = tmp7 & tmp60
    tmp62 = tmp14 & tmp60
    tmp63 = tmp24 & tmp60
    tmp64 = tmp31 & tmp60
    tmp65 = tl.where(tmp64, tmp18, tmp19)
    tmp66 = tl.where(tmp24, tmp10, tmp65)
    tmp67 = tl.where(tmp14, tmp36, tmp66)
    tmp68 = tl.where(tmp7, tmp27, tmp67)
    tmp69 = tl.full(tmp68.shape, 0.0, tmp68.dtype)
    tmp70 = tl.where(tmp60, tmp68, tmp69)
    tmp71 = tmp0 >= tmp22
    tmp72 = tmp0 < tmp32
    tmp73 = tmp7 & tmp71
    tmp74 = tmp14 & tmp71
    tmp75 = tl.where(tmp74, tmp28, tmp29)
    tmp76 = tmp24 & tmp71
    tmp77 = tmp31 & tmp71
    tmp78 = tl.where(tmp24, tmp17, tmp10)
    tmp79 = tl.where(tmp14, tmp75, tmp78)
    tmp80 = tl.where(tmp7, tmp36, tmp79)
    tmp81 = tl.full(tmp80.shape, 0.0, tmp80.dtype)
    tmp82 = tl.where(tmp71, tmp80, tmp81)
    tmp83 = tl.where(tmp60, tmp70, tmp82)
    tmp84 = tl.where(tmp47, tmp57, tmp83)
    tmp85 = tl.where(tmp4, tmp44, tmp84)
    tl.store(out_ptr0 + (x2), tmp85, xmask)
''', device_str='cuda')


async_compile.wait(globals())
del async_compile

def call(args):
    primals_1, primals_2, primals_3, primals_4, primals_5, primals_6 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4), (4, 1))
    assert_size_stride(primals_2, (1, 1), (1, 1))
    assert_size_stride(primals_3, (1, 1), (1, 1))
    assert_size_stride(primals_4, (1, 1), (1, 1))
    assert_size_stride(primals_5, (1, 1), (1, 1))
    assert_size_stride(primals_6, (4, ), (1, ))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        # Topologically Sorted Source Nodes: [output], Original ATen: [aten.cat]
        stream0 = get_raw_stream(0)
        triton_poi_fused_cat_0.run(primals_2, primals_3, primals_4, primals_5, buf0, 16, grid=grid(16), stream=stream0)
        del primals_2
        del primals_3
        del primals_4
        del primals_5
        buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        # Topologically Sorted Source Nodes: [output], Original ATen: [aten.addmm]
        extern_kernels.addmm(primals_6, primals_1, buf0, alpha=1, beta=1, out=buf1)
        del buf0
        del primals_6
    return (buf1, primals_1, )


def benchmark_compiled_module(times=10, repeat=10):
    from torch._dynamo.testing import rand_strided
    from torch._inductor.utils import print_performance
    primals_1 = rand_strided((4, 4), (4, 1), device='cuda:0', dtype=torch.float32)
    primals_2 = rand_strided((1, 1), (1, 1), device='cuda:0', dtype=torch.float32)
    primals_3 = rand_strided((1, 1), (1, 1), device='cuda:0', dtype=torch.float32)
    primals_4 = rand_strided((1, 1), (1, 1), device='cuda:0', dtype=torch.float32)
    primals_5 = rand_strided((1, 1), (1, 1), device='cuda:0', dtype=torch.float32)
    primals_6 = rand_strided((4, ), (1, ), device='cuda:0', dtype=torch.float32)
    fn = lambda: call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6])
    return print_performance(fn, times=times, repeat=repeat)


if __name__ == "__main__":
    from torch._inductor.wrapper_benchmark import compiled_module_main
    compiled_module_main('None', benchmark_compiled_module)

from torch.nn import Module
import torch
import numpy as np
from numpy.random import RandomState
from torch.nn.parameter import Parameter


def quaternion_init(in_features, out_features, rng, kernel_size=None,
    criterion='glorot'):
    if kernel_size is not None:
        receptive_field = np.prod(kernel_size)
        fan_in = in_features * receptive_field
        fan_out = out_features * receptive_field
    else:
        fan_in = in_features
        fan_out = out_features
    if criterion == 'glorot':
        s = 1.0 / np.sqrt(2 * (fan_in + fan_out))
    elif criterion == 'he':
        s = 1.0 / np.sqrt(2 * fan_in)
    else:
        raise ValueError('Invalid criterion: ' + criterion)
    rng = RandomState(123)
    if kernel_size is None:
        kernel_shape = in_features, out_features
    elif type(kernel_size) is int:
        kernel_shape = (out_features, in_features) + tuple((kernel_size,))
    else:
        kernel_shape = (out_features, in_features) + (*kernel_size,)
    number_of_weights = np.prod(kernel_shape)
    v_i = np.random.normal(0.0, s, number_of_weights)
    v_j = np.random.normal(0.0, s, number_of_weights)
    v_k = np.random.normal(0.0, s, number_of_weights)
    for i in range(0, number_of_weights):
        norm = np.sqrt(v_i[i] ** 2 + v_j[i] ** 2 + v_k[i] ** 2) + 0.0001
        v_i[i] /= norm
        v_j[i] /= norm
        v_k[i] /= norm
    v_i = v_i.reshape(kernel_shape)
    v_j = v_j.reshape(kernel_shape)
    v_k = v_k.reshape(kernel_shape)
    modulus = rng.uniform(low=-s, high=s, size=kernel_shape)
    phase = rng.uniform(low=-np.pi, high=np.pi, size=kernel_shape)
    weight_r = modulus * np.cos(phase)
    weight_i = modulus * v_i * np.sin(phase)
    weight_j = modulus * v_j * np.sin(phase)
    weight_k = modulus * v_k * np.sin(phase)
    return weight_r, weight_i, weight_j, weight_k


def unitary_init(in_features, out_features, rng, kernel_size=None,
    criterion='he'):
    if kernel_size is not None:
        receptive_field = np.prod(kernel_size)
        fan_in = in_features * receptive_field
        fan_out = out_features * receptive_field
    else:
        fan_in = in_features
        fan_out = out_features
    if criterion == 'glorot':
        s = 1.0 / np.sqrt(2 * (fan_in + fan_out))
    elif criterion == 'he':
        s = 1.0 / np.sqrt(2 * fan_in)
    else:
        raise ValueError('Invalid criterion: ' + criterion)
    if kernel_size is None:
        kernel_shape = in_features, out_features
    elif type(kernel_size) is int:
        kernel_shape = (out_features, in_features) + tuple((kernel_size,))
    else:
        kernel_shape = (out_features, in_features) + (*kernel_size,)
    number_of_weights = np.prod(kernel_shape)
    v_r = np.random.normal(0.0, s, number_of_weights)
    v_i = np.random.normal(0.0, s, number_of_weights)
    v_j = np.random.normal(0.0, s, number_of_weights)
    v_k = np.random.normal(0.0, s, number_of_weights)
    for i in range(0, number_of_weights):
        norm = np.sqrt(v_r[i] ** 2 + v_i[i] ** 2 + v_j[i] ** 2 + v_k[i] ** 2
            ) + 0.0001
        v_r[i] /= norm
        v_i[i] /= norm
        v_j[i] /= norm
        v_k[i] /= norm
    v_r = v_r.reshape(kernel_shape)
    v_i = v_i.reshape(kernel_shape)
    v_j = v_j.reshape(kernel_shape)
    v_k = v_k.reshape(kernel_shape)
    return v_r, v_i, v_j, v_k


def affect_init(r_weight, i_weight, j_weight, k_weight, init_func, rng,
    init_criterion):
    if r_weight.size() != i_weight.size() or r_weight.size() != j_weight.size(
        ) or r_weight.size() != k_weight.size():
        raise ValueError(
            'The real and imaginary weights should have the same size. Found:'
             + ' r:' + str(r_weight.size()) + ' i:' + str(i_weight.size()) +
            ' j:' + str(j_weight.size()) + ' k:' + str(k_weight.size()))
    elif r_weight.dim() != 2:
        raise Exception(
            'affect_init accepts only matrices. Found dimension = ' + str(
            r_weight.dim()))
    kernel_size = None
    r, i, j, k = init_func(r_weight.size(0), r_weight.size(1), rng,
        kernel_size, init_criterion)
    r, i, j, k = torch.from_numpy(r), torch.from_numpy(i), torch.from_numpy(j
        ), torch.from_numpy(k)
    r_weight.data = r.type_as(r_weight.data)
    i_weight.data = i.type_as(i_weight.data)
    j_weight.data = j.type_as(j_weight.data)
    k_weight.data = k.type_as(k_weight.data)


def check_input(input):
    if input.dim() not in {2, 3}:
        raise RuntimeError(
            'quaternion linear accepts only input of dimension 2 or 3. input.dim = '
             + str(input.dim()))
    nb_hidden = input.size()[-1]
    if nb_hidden % 4 != 0:
        raise RuntimeError(
            'Quaternion Tensors must be divisible by 4. input.size()[1] = ' +
            str(nb_hidden))


def get_i(input):
    check_input(input)
    nb_hidden = input.size()[-1]
    if input.dim() == 2:
        return input.narrow(1, nb_hidden // 4, nb_hidden // 4)
    if input.dim() == 3:
        return input.narrow(2, nb_hidden // 4, nb_hidden // 4)


def get_j(input):
    check_input(input)
    nb_hidden = input.size()[-1]
    if input.dim() == 2:
        return input.narrow(1, nb_hidden // 2, nb_hidden // 4)
    if input.dim() == 3:
        return input.narrow(2, nb_hidden // 2, nb_hidden // 4)


def get_k(input):
    check_input(input)
    nb_hidden = input.size()[-1]
    if input.dim() == 2:
        return input.narrow(1, nb_hidden - nb_hidden // 4, nb_hidden // 4)
    if input.dim() == 3:
        return input.narrow(2, nb_hidden - nb_hidden // 4, nb_hidden // 4)


def get_r(input):
    check_input(input)
    nb_hidden = input.size()[-1]
    if input.dim() == 2:
        return input.narrow(1, 0, nb_hidden // 4)
    elif input.dim() == 3:
        return input.narrow(2, 0, nb_hidden // 4)


class QuaternionLinearFunction(torch.autograd.Function):

    @staticmethod
    def forward(ctx, input, r_weight, i_weight, j_weight, k_weight, bias=None):
        ctx.save_for_backward(input, r_weight, i_weight, j_weight, k_weight,
            bias)
        check_input(input)
        cat_kernels_4_r = torch.cat((r_weight, -i_weight, -j_weight, -
            k_weight), dim=0)
        cat_kernels_4_i = torch.cat((i_weight, r_weight, -k_weight,
            j_weight), dim=0)
        cat_kernels_4_j = torch.cat((j_weight, k_weight, r_weight, -
            i_weight), dim=0)
        cat_kernels_4_k = torch.cat((k_weight, -j_weight, i_weight,
            r_weight), dim=0)
        cat_kernels_4_quaternion = torch.cat((cat_kernels_4_r,
            cat_kernels_4_i, cat_kernels_4_j, cat_kernels_4_k), dim=1)
        if input.dim() == 2:
            if bias is not None:
                return torch.addmm(bias, input, cat_kernels_4_quaternion)
            else:
                return torch.mm(input, cat_kernels_4_quaternion)
        else:
            output = torch.matmul(input, cat_kernels_4_quaternion)
            if bias is not None:
                return output + bias
            else:
                return output

    @staticmethod
    def backward(ctx, grad_output):
        input, r_weight, i_weight, j_weight, k_weight, _bias = (ctx.
            saved_tensors)
        (grad_input) = (grad_weight_r) = (grad_weight_i) = (grad_weight_j) = (
            grad_weight_k) = (grad_bias) = None
        input_r = torch.cat((r_weight, -i_weight, -j_weight, -k_weight), dim=0)
        input_i = torch.cat((i_weight, r_weight, -k_weight, j_weight), dim=0)
        input_j = torch.cat((j_weight, k_weight, r_weight, -i_weight), dim=0)
        input_k = torch.cat((k_weight, -j_weight, i_weight, r_weight), dim=0)
        cat_kernels_4_quaternion_T = torch.cat((input_r, input_i, input_j,
            input_k), dim=1).permute(1, 0)
        cat_kernels_4_quaternion_T.requires_grad_(False)
        r = get_r(input)
        i = get_i(input)
        j = get_j(input)
        k = get_k(input)
        input_r = torch.cat((r, -i, -j, -k), dim=0)
        input_i = torch.cat((i, r, -k, j), dim=0)
        input_j = torch.cat((j, k, r, -i), dim=0)
        input_k = torch.cat((k, -j, i, r), dim=0)
        input_mat = torch.cat((input_r, input_i, input_j, input_k), dim=1)
        input_mat.requires_grad_(False)
        r = get_r(grad_output)
        i = get_i(grad_output)
        j = get_j(grad_output)
        k = get_k(grad_output)
        input_r = torch.cat((r, i, j, k), dim=1)
        input_i = torch.cat((-i, r, k, -j), dim=1)
        input_j = torch.cat((-j, -k, r, i), dim=1)
        input_k = torch.cat((-k, j, -i, r), dim=1)
        grad_mat = torch.cat((input_r, input_i, input_j, input_k), dim=0)
        if ctx.needs_input_grad[0]:
            grad_input = grad_output.mm(cat_kernels_4_quaternion_T)
        if ctx.needs_input_grad[1]:
            grad_weight = grad_mat.permute(1, 0).mm(input_mat).permute(1, 0)
            unit_size_x = r_weight.size(0)
            unit_size_y = r_weight.size(1)
            grad_weight_r = grad_weight.narrow(0, 0, unit_size_x).narrow(1,
                0, unit_size_y)
            grad_weight_i = grad_weight.narrow(0, 0, unit_size_x).narrow(1,
                unit_size_y, unit_size_y)
            grad_weight_j = grad_weight.narrow(0, 0, unit_size_x).narrow(1,
                unit_size_y * 2, unit_size_y)
            grad_weight_k = grad_weight.narrow(0, 0, unit_size_x).narrow(1,
                unit_size_y * 3, unit_size_y)
        if ctx.needs_input_grad[5]:
            grad_bias = grad_output.sum(0).squeeze(0)
        return (grad_input, grad_weight_r, grad_weight_i, grad_weight_j,
            grad_weight_k, grad_bias)


class QuaternionLinear(Module):
    """Applies a quaternion linear transformation to the incoming data.
    """

    def __init__(self, in_features, out_features, bias=True, init_criterion
        ='glorot', weight_init='quaternion', seed=None):
        super(QuaternionLinear, self).__init__()
        self.in_features = in_features // 4
        self.out_features = out_features // 4
        self.r_weight = Parameter(torch.Tensor(self.in_features, self.
            out_features))
        self.i_weight = Parameter(torch.Tensor(self.in_features, self.
            out_features))
        self.j_weight = Parameter(torch.Tensor(self.in_features, self.
            out_features))
        self.k_weight = Parameter(torch.Tensor(self.in_features, self.
            out_features))
        if bias:
            self.bias = Parameter(torch.Tensor(self.out_features * 4))
        else:
            self.register_parameter('bias', None)
        self.init_criterion = init_criterion
        self.weight_init = weight_init
        self.seed = seed if seed is not None else np.random.randint(0, 1234)
        self.rng = RandomState(self.seed)
        self.reset_parameters()

    def reset_parameters(self):
        winit = {'quaternion': quaternion_init, 'unitary': unitary_init}[self
            .weight_init]
        if self.bias is not None:
            self.bias.data.fill_(0)
        affect_init(self.r_weight, self.i_weight, self.j_weight, self.
            k_weight, winit, self.rng, self.init_criterion)

    def forward(self, input):
        if input.dim() == 3:
            T, N, C = input.size()
            input = input.contiguous().view(T * N, C)
            output = QuaternionLinearFunction.apply(input, self.r_weight,
                self.i_weight, self.j_weight, self.k_weight, self.bias)
            output = output.view(T, N, output.size(1))
        elif input.dim() == 2:
            output = QuaternionLinearFunction.apply(input, self.r_weight,
                self.i_weight, self.j_weight, self.k_weight, self.bias)
        else:
            raise NotImplementedError
        return output

    def __repr__(self):
        return self.__class__.__name__ + '(' + 'in_features=' + str(self.
            in_features) + ', out_features=' + str(self.out_features
            ) + ', bias=' + str(self.bias is not None
            ) + ', init_criterion=' + str(self.init_criterion
            ) + ', weight_init=' + str(self.weight_init) + ', seed=' + str(self
            .seed) + ')'


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


def get_init_inputs():
    return [[], {'in_features': 4, 'out_features': 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
from torch.nn import Module
import numpy as np
from numpy.random import RandomState
from torch.nn.parameter import Parameter
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_cat_0(in_ptr0, in_ptr1, in_ptr2, in_ptr3, 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 % 4
    x1 = xindex // 4
    x2 = xindex
    tmp9 = tl.load(in_ptr0 + 0)
    tmp10 = tl.broadcast_to(tmp9, [XBLOCK])
    tmp16 = tl.load(in_ptr1 + 0)
    tmp17 = tl.broadcast_to(tmp16, [XBLOCK])
    tmp26 = tl.load(in_ptr2 + 0)
    tmp27 = tl.broadcast_to(tmp26, [XBLOCK])
    tmp35 = tl.load(in_ptr3 + 0)
    tmp36 = tl.broadcast_to(tmp35, [XBLOCK])
    tmp0 = x0
    tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 1, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = x1
    tmp7 = tmp5 < tmp3
    tmp7 & tmp4
    tmp11 = tmp5 >= tmp3
    tmp12 = tl.full([1], 2, tl.int64)
    tmp13 = tmp5 < tmp12
    tmp14 = tmp11 & tmp13
    tmp15 = tmp14 & tmp4
    tmp18 = -tmp17
    tmp19 = tl.full(tmp18.shape, 0.0, tmp18.dtype)
    tmp20 = tl.where(tmp15, tmp18, tmp19)
    tmp21 = tmp5 >= tmp12
    tmp22 = tl.full([1], 3, tl.int64)
    tmp23 = tmp5 < tmp22
    tmp24 = tmp21 & tmp23
    tmp25 = tmp24 & tmp4
    tmp28 = -tmp27
    tmp29 = tl.full(tmp28.shape, 0.0, tmp28.dtype)
    tmp30 = tl.where(tmp25, tmp28, tmp29)
    tmp31 = tmp5 >= tmp22
    tl.full([1], 4, tl.int64)
    tmp34 = tmp31 & tmp4
    tmp37 = -tmp36
    tmp38 = tl.full(tmp37.shape, 0.0, tmp37.dtype)
    tmp39 = tl.where(tmp34, tmp37, tmp38)
    tmp40 = tl.where(tmp24, tmp30, tmp39)
    tmp41 = tl.where(tmp14, tmp20, tmp40)
    tmp42 = tl.where(tmp7, tmp10, tmp41)
    tmp43 = tl.full(tmp42.shape, 0.0, tmp42.dtype)
    tmp44 = tl.where(tmp4, tmp42, tmp43)
    tmp45 = tmp0 >= tmp3
    tmp46 = tmp0 < tmp12
    tmp47 = tmp45 & tmp46
    tmp7 & tmp47
    tmp14 & tmp47
    tmp50 = tmp24 & tmp47
    tmp51 = tl.where(tmp50, tmp37, tmp38)
    tmp31 & tmp47
    tmp53 = tl.where(tmp24, tmp51, tmp27)
    tmp54 = tl.where(tmp14, tmp10, tmp53)
    tmp55 = tl.where(tmp7, tmp17, tmp54)
    tmp56 = tl.full(tmp55.shape, 0.0, tmp55.dtype)
    tmp57 = tl.where(tmp47, tmp55, tmp56)
    tmp58 = tmp0 >= tmp12
    tmp59 = tmp0 < tmp22
    tmp60 = tmp58 & tmp59
    tmp7 & tmp60
    tmp14 & tmp60
    tmp24 & tmp60
    tmp64 = tmp31 & tmp60
    tmp65 = tl.where(tmp64, tmp18, tmp19)
    tmp66 = tl.where(tmp24, tmp10, tmp65)
    tmp67 = tl.where(tmp14, tmp36, tmp66)
    tmp68 = tl.where(tmp7, tmp27, tmp67)
    tmp69 = tl.full(tmp68.shape, 0.0, tmp68.dtype)
    tmp70 = tl.where(tmp60, tmp68, tmp69)
    tmp71 = tmp0 >= tmp22
    tmp7 & tmp71
    tmp74 = tmp14 & tmp71
    tmp75 = tl.where(tmp74, tmp28, tmp29)
    tmp24 & tmp71
    tmp31 & tmp71
    tmp78 = tl.where(tmp24, tmp17, tmp10)
    tmp79 = tl.where(tmp14, tmp75, tmp78)
    tmp80 = tl.where(tmp7, tmp36, tmp79)
    tmp81 = tl.full(tmp80.shape, 0.0, tmp80.dtype)
    tmp82 = tl.where(tmp71, tmp80, tmp81)
    tmp83 = tl.where(tmp60, tmp70, tmp82)
    tmp84 = tl.where(tmp47, tmp57, tmp83)
    tmp85 = tl.where(tmp4, tmp44, tmp84)
    tl.store(out_ptr0 + x2, tmp85, xmask)


def call(args):
    primals_1, primals_2, primals_3, primals_4, primals_5, primals_6 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4), (4, 1))
    assert_size_stride(primals_2, (1, 1), (1, 1))
    assert_size_stride(primals_3, (1, 1), (1, 1))
    assert_size_stride(primals_4, (1, 1), (1, 1))
    assert_size_stride(primals_5, (1, 1), (1, 1))
    assert_size_stride(primals_6, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_cat_0[grid(16)](primals_2, primals_3, primals_4,
            primals_5, buf0, 16, XBLOCK=16, num_warps=1, num_stages=1)
        del primals_2
        del primals_3
        del primals_4
        del primals_5
        buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_6, primals_1, buf0, alpha=1, beta=1,
            out=buf1)
        del buf0
        del primals_6
    return buf1, primals_1


def quaternion_init(in_features, out_features, rng, kernel_size=None,
    criterion='glorot'):
    if kernel_size is not None:
        receptive_field = np.prod(kernel_size)
        fan_in = in_features * receptive_field
        fan_out = out_features * receptive_field
    else:
        fan_in = in_features
        fan_out = out_features
    if criterion == 'glorot':
        s = 1.0 / np.sqrt(2 * (fan_in + fan_out))
    elif criterion == 'he':
        s = 1.0 / np.sqrt(2 * fan_in)
    else:
        raise ValueError('Invalid criterion: ' + criterion)
    rng = RandomState(123)
    if kernel_size is None:
        kernel_shape = in_features, out_features
    elif type(kernel_size) is int:
        kernel_shape = (out_features, in_features) + tuple((kernel_size,))
    else:
        kernel_shape = (out_features, in_features) + (*kernel_size,)
    number_of_weights = np.prod(kernel_shape)
    v_i = np.random.normal(0.0, s, number_of_weights)
    v_j = np.random.normal(0.0, s, number_of_weights)
    v_k = np.random.normal(0.0, s, number_of_weights)
    for i in range(0, number_of_weights):
        norm = np.sqrt(v_i[i] ** 2 + v_j[i] ** 2 + v_k[i] ** 2) + 0.0001
        v_i[i] /= norm
        v_j[i] /= norm
        v_k[i] /= norm
    v_i = v_i.reshape(kernel_shape)
    v_j = v_j.reshape(kernel_shape)
    v_k = v_k.reshape(kernel_shape)
    modulus = rng.uniform(low=-s, high=s, size=kernel_shape)
    phase = rng.uniform(low=-np.pi, high=np.pi, size=kernel_shape)
    weight_r = modulus * np.cos(phase)
    weight_i = modulus * v_i * np.sin(phase)
    weight_j = modulus * v_j * np.sin(phase)
    weight_k = modulus * v_k * np.sin(phase)
    return weight_r, weight_i, weight_j, weight_k


def unitary_init(in_features, out_features, rng, kernel_size=None,
    criterion='he'):
    if kernel_size is not None:
        receptive_field = np.prod(kernel_size)
        fan_in = in_features * receptive_field
        fan_out = out_features * receptive_field
    else:
        fan_in = in_features
        fan_out = out_features
    if criterion == 'glorot':
        s = 1.0 / np.sqrt(2 * (fan_in + fan_out))
    elif criterion == 'he':
        s = 1.0 / np.sqrt(2 * fan_in)
    else:
        raise ValueError('Invalid criterion: ' + criterion)
    if kernel_size is None:
        kernel_shape = in_features, out_features
    elif type(kernel_size) is int:
        kernel_shape = (out_features, in_features) + tuple((kernel_size,))
    else:
        kernel_shape = (out_features, in_features) + (*kernel_size,)
    number_of_weights = np.prod(kernel_shape)
    v_r = np.random.normal(0.0, s, number_of_weights)
    v_i = np.random.normal(0.0, s, number_of_weights)
    v_j = np.random.normal(0.0, s, number_of_weights)
    v_k = np.random.normal(0.0, s, number_of_weights)
    for i in range(0, number_of_weights):
        norm = np.sqrt(v_r[i] ** 2 + v_i[i] ** 2 + v_j[i] ** 2 + v_k[i] ** 2
            ) + 0.0001
        v_r[i] /= norm
        v_i[i] /= norm
        v_j[i] /= norm
        v_k[i] /= norm
    v_r = v_r.reshape(kernel_shape)
    v_i = v_i.reshape(kernel_shape)
    v_j = v_j.reshape(kernel_shape)
    v_k = v_k.reshape(kernel_shape)
    return v_r, v_i, v_j, v_k


def affect_init(r_weight, i_weight, j_weight, k_weight, init_func, rng,
    init_criterion):
    if r_weight.size() != i_weight.size() or r_weight.size() != j_weight.size(
        ) or r_weight.size() != k_weight.size():
        raise ValueError(
            'The real and imaginary weights should have the same size. Found:'
             + ' r:' + str(r_weight.size()) + ' i:' + str(i_weight.size()) +
            ' j:' + str(j_weight.size()) + ' k:' + str(k_weight.size()))
    elif r_weight.dim() != 2:
        raise Exception(
            'affect_init accepts only matrices. Found dimension = ' + str(
            r_weight.dim()))
    kernel_size = None
    r, i, j, k = init_func(r_weight.size(0), r_weight.size(1), rng,
        kernel_size, init_criterion)
    r, i, j, k = torch.from_numpy(r), torch.from_numpy(i), torch.from_numpy(j
        ), torch.from_numpy(k)
    r_weight.data = r.type_as(r_weight.data)
    i_weight.data = i.type_as(i_weight.data)
    j_weight.data = j.type_as(j_weight.data)
    k_weight.data = k.type_as(k_weight.data)


def check_input(input):
    if input.dim() not in {2, 3}:
        raise RuntimeError(
            'quaternion linear accepts only input of dimension 2 or 3. input.dim = '
             + str(input.dim()))
    nb_hidden = input.size()[-1]
    if nb_hidden % 4 != 0:
        raise RuntimeError(
            'Quaternion Tensors must be divisible by 4. input.size()[1] = ' +
            str(nb_hidden))


def get_i(input):
    check_input(input)
    nb_hidden = input.size()[-1]
    if input.dim() == 2:
        return input.narrow(1, nb_hidden // 4, nb_hidden // 4)
    if input.dim() == 3:
        return input.narrow(2, nb_hidden // 4, nb_hidden // 4)


def get_j(input):
    check_input(input)
    nb_hidden = input.size()[-1]
    if input.dim() == 2:
        return input.narrow(1, nb_hidden // 2, nb_hidden // 4)
    if input.dim() == 3:
        return input.narrow(2, nb_hidden // 2, nb_hidden // 4)


def get_k(input):
    check_input(input)
    nb_hidden = input.size()[-1]
    if input.dim() == 2:
        return input.narrow(1, nb_hidden - nb_hidden // 4, nb_hidden // 4)
    if input.dim() == 3:
        return input.narrow(2, nb_hidden - nb_hidden // 4, nb_hidden // 4)


def get_r(input):
    check_input(input)
    nb_hidden = input.size()[-1]
    if input.dim() == 2:
        return input.narrow(1, 0, nb_hidden // 4)
    elif input.dim() == 3:
        return input.narrow(2, 0, nb_hidden // 4)


class QuaternionLinearFunction(torch.autograd.Function):

    @staticmethod
    def forward(ctx, input, r_weight, i_weight, j_weight, k_weight, bias=None):
        ctx.save_for_backward(input, r_weight, i_weight, j_weight, k_weight,
            bias)
        check_input(input)
        cat_kernels_4_r = torch.cat((r_weight, -i_weight, -j_weight, -
            k_weight), dim=0)
        cat_kernels_4_i = torch.cat((i_weight, r_weight, -k_weight,
            j_weight), dim=0)
        cat_kernels_4_j = torch.cat((j_weight, k_weight, r_weight, -
            i_weight), dim=0)
        cat_kernels_4_k = torch.cat((k_weight, -j_weight, i_weight,
            r_weight), dim=0)
        cat_kernels_4_quaternion = torch.cat((cat_kernels_4_r,
            cat_kernels_4_i, cat_kernels_4_j, cat_kernels_4_k), dim=1)
        if input.dim() == 2:
            if bias is not None:
                return torch.addmm(bias, input, cat_kernels_4_quaternion)
            else:
                return torch.mm(input, cat_kernels_4_quaternion)
        else:
            output = torch.matmul(input, cat_kernels_4_quaternion)
            if bias is not None:
                return output + bias
            else:
                return output

    @staticmethod
    def backward(ctx, grad_output):
        input, r_weight, i_weight, j_weight, k_weight, _bias = (ctx.
            saved_tensors)
        (grad_input) = (grad_weight_r) = (grad_weight_i) = (grad_weight_j) = (
            grad_weight_k) = (grad_bias) = None
        input_r = torch.cat((r_weight, -i_weight, -j_weight, -k_weight), dim=0)
        input_i = torch.cat((i_weight, r_weight, -k_weight, j_weight), dim=0)
        input_j = torch.cat((j_weight, k_weight, r_weight, -i_weight), dim=0)
        input_k = torch.cat((k_weight, -j_weight, i_weight, r_weight), dim=0)
        cat_kernels_4_quaternion_T = torch.cat((input_r, input_i, input_j,
            input_k), dim=1).permute(1, 0)
        cat_kernels_4_quaternion_T.requires_grad_(False)
        r = get_r(input)
        i = get_i(input)
        j = get_j(input)
        k = get_k(input)
        input_r = torch.cat((r, -i, -j, -k), dim=0)
        input_i = torch.cat((i, r, -k, j), dim=0)
        input_j = torch.cat((j, k, r, -i), dim=0)
        input_k = torch.cat((k, -j, i, r), dim=0)
        input_mat = torch.cat((input_r, input_i, input_j, input_k), dim=1)
        input_mat.requires_grad_(False)
        r = get_r(grad_output)
        i = get_i(grad_output)
        j = get_j(grad_output)
        k = get_k(grad_output)
        input_r = torch.cat((r, i, j, k), dim=1)
        input_i = torch.cat((-i, r, k, -j), dim=1)
        input_j = torch.cat((-j, -k, r, i), dim=1)
        input_k = torch.cat((-k, j, -i, r), dim=1)
        grad_mat = torch.cat((input_r, input_i, input_j, input_k), dim=0)
        if ctx.needs_input_grad[0]:
            grad_input = grad_output.mm(cat_kernels_4_quaternion_T)
        if ctx.needs_input_grad[1]:
            grad_weight = grad_mat.permute(1, 0).mm(input_mat).permute(1, 0)
            unit_size_x = r_weight.size(0)
            unit_size_y = r_weight.size(1)
            grad_weight_r = grad_weight.narrow(0, 0, unit_size_x).narrow(1,
                0, unit_size_y)
            grad_weight_i = grad_weight.narrow(0, 0, unit_size_x).narrow(1,
                unit_size_y, unit_size_y)
            grad_weight_j = grad_weight.narrow(0, 0, unit_size_x).narrow(1,
                unit_size_y * 2, unit_size_y)
            grad_weight_k = grad_weight.narrow(0, 0, unit_size_x).narrow(1,
                unit_size_y * 3, unit_size_y)
        if ctx.needs_input_grad[5]:
            grad_bias = grad_output.sum(0).squeeze(0)
        return (grad_input, grad_weight_r, grad_weight_i, grad_weight_j,
            grad_weight_k, grad_bias)


class QuaternionLinearNew(Module):
    """Applies a quaternion linear transformation to the incoming data.
    """

    def __init__(self, in_features, out_features, bias=True, init_criterion
        ='glorot', weight_init='quaternion', seed=None):
        super(QuaternionLinearNew, self).__init__()
        self.in_features = in_features // 4
        self.out_features = out_features // 4
        self.r_weight = Parameter(torch.Tensor(self.in_features, self.
            out_features))
        self.i_weight = Parameter(torch.Tensor(self.in_features, self.
            out_features))
        self.j_weight = Parameter(torch.Tensor(self.in_features, self.
            out_features))
        self.k_weight = Parameter(torch.Tensor(self.in_features, self.
            out_features))
        if bias:
            self.bias = Parameter(torch.Tensor(self.out_features * 4))
        else:
            self.register_parameter('bias', None)
        self.init_criterion = init_criterion
        self.weight_init = weight_init
        self.seed = seed if seed is not None else np.random.randint(0, 1234)
        self.rng = RandomState(self.seed)
        self.reset_parameters()

    def reset_parameters(self):
        winit = {'quaternion': quaternion_init, 'unitary': unitary_init}[self
            .weight_init]
        if self.bias is not None:
            self.bias.data.fill_(0)
        affect_init(self.r_weight, self.i_weight, self.j_weight, self.
            k_weight, winit, self.rng, self.init_criterion)

    def __repr__(self):
        return self.__class__.__name__ + '(' + 'in_features=' + str(self.
            in_features) + ', out_features=' + str(self.out_features
            ) + ', bias=' + str(self.bias is not None
            ) + ', init_criterion=' + str(self.init_criterion
            ) + ', weight_init=' + str(self.weight_init) + ', seed=' + str(self
            .seed) + ')'

    def forward(self, input_0):
        primals_2 = self.r_weight
        primals_3 = self.i_weight
        primals_4 = self.j_weight
        primals_5 = self.k_weight
        primals_6 = self.bias
        primals_1 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6])
        return output[0]
