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 import triton_helpers
from torch._inductor.runtime.triton_helpers import math as tl_math
import numpy as np
from torch import 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_abs_add_mul_relu_sign_0(in_out_ptr0, 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
    x1 = xindex % 4
    tmp0 = tl.load(in_out_ptr0 + x0, xmask)
    tmp1 = tl.load(in_ptr0 + x0, xmask)
    tmp11 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = tl.full([1], 0, tl.int32)
    tmp4 = tmp3 < tmp2
    tmp5 = tmp4.to(tl.int8)
    tmp6 = tmp2 < tmp3
    tmp7 = tmp6.to(tl.int8)
    tmp8 = tmp5 - tmp7
    tmp9 = tmp8.to(tmp2.dtype)
    tmp10 = tl_math.abs(tmp2)
    tmp12 = tmp10 + tmp11
    tmp13 = triton_helpers.maximum(tmp3, tmp12)
    tmp14 = tmp9 * tmp13
    tl.store(in_out_ptr0 + x0, tmp2, xmask)
    tl.store(out_ptr0 + x0, tmp14, xmask)


def call(args):
    primals_1, primals_2, primals_3, primals_4, primals_5 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4), (4, 1))
    assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_3, (4, 4), (4, 1))
    assert_size_stride(primals_4, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_5, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_2, (64, 4), (4, 1), 0),
            reinterpret_tensor(primals_1, (4, 4), (1, 4), 0), out=buf0)
        del primals_1
        buf1 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_4, (64, 4), (4, 1), 0),
            reinterpret_tensor(primals_3, (4, 4), (1, 4), 0), out=buf1)
        del primals_3
        buf2 = reinterpret_tensor(buf0, (4, 4, 4, 4), (64, 16, 4, 1), 0)
        del buf0
        buf3 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_abs_add_mul_relu_sign_0[grid(256)](buf2, buf1,
            primals_5, buf3, 256, XBLOCK=128, num_warps=4, num_stages=1)
        del buf1
    return buf3, primals_5, reinterpret_tensor(primals_2, (64, 4), (4, 1), 0
        ), reinterpret_tensor(primals_4, (64, 4), (4, 1), 0), buf2


def create_diag_(A, diag):
    """ This code comes is extracted from https://github.com/Lezcano/expRNN, we just repeat it as it is needed by our experiment"""
    n = A.size(0)
    diag_z = torch.zeros(n - 1)
    diag_z[::2] = diag
    A_init = torch.diag(diag_z, diagonal=1)
    A_init = A_init - A_init.T
    with torch.no_grad():
        A.copy_(A_init)
        return A


def henaff_init_(A):
    """ This code comes is extracted from https://github.com/Lezcano/expRNN, we just repeat it as it is needed by our experiment"""
    size = A.size(0) // 2
    diag = A.new(size).uniform_(-np.pi, np.pi)
    A_init = create_diag_(A, diag)
    I = torch.eye(A_init.size(0))
    return torch.mm(torch.inverse(I + A_init), I - A_init)


class modrelu(nn.Module):
    """ This code comes is extracted from https://github.com/Lezcano/expRNN, we just repeat it as it is needed by our experiment"""

    def __init__(self, features):
        super(modrelu, self).__init__()
        self.features = features
        self.b = nn.Parameter(torch.Tensor(self.features))
        self.reset_parameters()

    def reset_parameters(self):
        self.b.data.uniform_(-0.01, 0.01)

    def forward(self, inputs):
        norm = torch.abs(inputs)
        biased_norm = norm + self.b
        magnitude = nn.functional.relu(biased_norm)
        phase = torch.sign(inputs)
        return phase * magnitude


class FlexibleRNNNew(nn.Module):
    """ This code comes is extracted (and slightly modified) from https://github.com/Lezcano/expRNN, we just repeat it as it is needed by our experiment"""

    def __init__(self, input_size, hidden_size):
        super(FlexibleRNNNew, self).__init__()
        self.input_size = input_size
        self.hidden_size = hidden_size
        self.recurrent_kernel = nn.Linear(in_features=self.hidden_size,
            out_features=self.hidden_size, bias=False)
        self.input_kernel = nn.Linear(in_features=self.input_size,
            out_features=self.hidden_size, bias=False)
        self.nonlinearity = modrelu(hidden_size)
        self.reset_parameters()

    def reset_parameters(self):
        nn.init.kaiming_normal_(self.input_kernel.weight.data, nonlinearity
            ='relu')
        nn.init.zeros_(self.recurrent_kernel.weight.data)
        self.recurrent_kernel.weight.data = henaff_init_(self.
            recurrent_kernel.weight.data)

    def forward(self, input_0, input_1):
        primals_1 = self.recurrent_kernel.weight
        primals_3 = self.input_kernel.weight
        primals_5 = self.nonlinearity.b
        primals_2 = input_0
        primals_4 = input_1
        output = call([primals_1, primals_2, primals_3, primals_4, primals_5])
        return output[0], output[1]


Measured latency_ms: 0.032594