import torch
from torch import nn
from functools import partial


def get_initializer(name, activation):
    if activation in ['id', 'identity', 'linear', 'modrelu']:
        nonlinearity = 'linear'
    elif activation in ['relu', 'tanh', 'sigmoid']:
        nonlinearity = activation
    else:
        assert False, f'get_initializer: activation {activation} not supported'
    if name == 'uniform':
        initializer = partial(torch.nn.init.kaiming_uniform_, nonlinearity=
            nonlinearity)
    elif name == 'normal':
        initializer = partial(torch.nn.init.kaiming_normal_, nonlinearity=
            nonlinearity)
    elif name == 'xavier':
        initializer = torch.nn.init.xavier_normal_
    elif name == 'zero':
        initializer = partial(torch.nn.init.constant_, val=0)
    elif name == 'one':
        initializer = partial(torch.nn.init.constant_, val=1)
    else:
        assert False, f'get_initializer: initializer type {name} not supported'
    return initializer


def Linear_(input_size, output_size, bias, init='normal', zero_bias_init=
    False, **kwargs):
    """ Returns a nn.Linear module with initialization options """
    l = nn.Linear(input_size, output_size, bias=bias, **kwargs)
    get_initializer(init, 'linear')(l.weight)
    if bias and zero_bias_init:
        nn.init.zeros_(l.bias)
    return l


def get_activation(activation, size):
    if activation == 'id':
        return nn.Identity()
    elif activation == 'tanh':
        return torch.tanh
    elif activation == 'relu':
        return torch.relu
    elif activation == 'sigmoid':
        return torch.sigmoid
    elif activation == 'modrelu':
        return Modrelu(size)
    else:
        raise NotImplementedError("hidden activation '{}' is not implemented"
            .format(activation))


class Gate(nn.Module):
    """ Implements gating mechanisms.

    Mechanisms:
    N  - No gate
    G  - Standard sigmoid gate
    """

    def __init__(self, size, preact_ctor, preact_args, mechanism='N'):
        super().__init__()
        self.size = size
        self.mechanism = mechanism
        if self.mechanism == 'N':
            pass
        elif self.mechanism == 'G':
            self.W_g = preact_ctor(*preact_args)
        else:
            assert False, f'Gating type {self.mechanism} is not supported.'

    def forward(self, *inputs):
        if self.mechanism == 'N':
            return 1.0
        if self.mechanism == 'G':
            g_preact = self.W_g(*inputs)
            g = torch.sigmoid(g_preact)
        return g


class modrelu(nn.Module):

    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 CellBase(nn.Module):
    """ Abstract class for our recurrent cell interface.

    Passes input through
    """
    registry = {}

    def __init_subclass__(cls, **kwargs):
        super().__init_subclass__(**kwargs)
        if hasattr(cls, 'name') and cls.name is not None:
            cls.registry[cls.name] = cls
    name = 'id'
    valid_keys = []

    def default_initializers(self):
        return {}

    def default_architecture(self):
        return {}

    def __init__(self, input_size, hidden_size, initializers=None,
        architecture=None):
        super().__init__()
        self.input_size = input_size
        self.hidden_size = hidden_size
        self.architecture = self.default_architecture()
        self.initializers = self.default_initializers()
        if initializers is not None:
            self.initializers.update(initializers)
            None
        if architecture is not None:
            self.architecture.update(architecture)
        assert set(self.initializers.keys()).issubset(self.valid_keys)
        assert set(self.architecture.keys()).issubset(self.valid_keys)
        self.reset_parameters()

    def reset_parameters(self):
        pass

    def forward(self, input, hidden):
        return input, input

    def default_state(self, input, batch_size=None):
        return input.new_zeros(input.size(0) if batch_size is None else
            batch_size, self.hidden_size, requires_grad=False)

    def output(self, h):
        return h

    def state_size(self):
        return self.hidden_size

    def output_size(self):
        return self.hidden_size

    def initial_state(self, trainable=False):
        """ Return initial state of the RNN
        This should not need to see the input as it should be batch size agnostic and automatically broadcasted

        # TODO Currently not used
        """
        if trainable:
            self.initial_state = torch.zeros(self.hidden_size,
                requires_grad=True)
        else:
            return torch.zeros(self.hidden_size, requires_grad=True)


class Modrelu(modrelu):

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


class MinimalRNNCell(CellBase):
    name = 'mrnn'
    valid_keys = ['hx', 'bias']

    def default_initializers(self):
        return {'hx': 'xavier'}

    def default_architecture(self):
        return {'bias': True}

    def __init__(self, input_size, hidden_size, hidden_activation='tanh',
        orthogonal=False, ortho_args=None, zero_bias_init=False, **kwargs):
        self.hidden_activation = hidden_activation
        self.zero_bias_init = zero_bias_init
        super().__init__(input_size, hidden_size, **kwargs)

    def reset_parameters(self):
        self.W_hx = Linear_(self.input_size, self.hidden_size, bias=self.
            architecture['bias'], zero_bias_init=self.zero_bias_init)
        get_initializer(self.initializers['hx'], self.hidden_activation)(self
            .W_hx.weight)
        self.hidden_activation_fn = get_activation(self.hidden_activation,
            self.hidden_size)
        preact_ctor = Linear_
        preact_args = [self.input_size + self.hidden_size, self.hidden_size,
            self.architecture['bias']]
        self.W_g = Gate(self.hidden_size, preact_ctor, preact_args,
            mechanism='G')

    def forward(self, input, h):
        hidden_preact = self.W_hx(input)
        hidden = self.hidden_activation_fn(hidden_preact)
        hx = torch.cat((input, h), dim=-1)
        g = self.W_g(hx)
        h = (1.0 - g) * h + g * hidden
        return h, h


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


def get_init_inputs():
    return [[], {'input_size': 4, 'hidden_size': 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._inductor.runtime.triton_helpers import libdevice
from torch import nn
from functools import partial
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_cat_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.
    constexpr):
    xnumel = 512
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 8
    x1 = xindex // 8
    x2 = xindex
    tmp0 = x0
    tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 4, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + (4 * x1 + x0), tmp4 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp6 = tmp0 >= tmp3
    tl.full([1], 8, tl.int64)
    tmp9 = tl.load(in_ptr1 + (4 * x1 + (-4 + x0)), tmp6 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp10 = tl.where(tmp4, tmp5, tmp9)
    tl.store(out_ptr0 + x2, tmp10, xmask)


@triton.jit
def triton_poi_fused_add_mul_rsub_sigmoid_tanh_1(in_ptr0, in_ptr1, in_ptr2,
    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)
    tmp4 = tl.load(in_ptr1 + x0, xmask)
    tmp6 = tl.load(in_ptr2 + x0, xmask)
    tmp1 = tl.sigmoid(tmp0)
    tmp2 = 1.0
    tmp3 = tmp2 - tmp1
    tmp5 = tmp3 * tmp4
    tmp7 = libdevice.tanh(tmp6)
    tmp8 = tmp1 * tmp7
    tmp9 = tmp5 + tmp8
    tl.store(out_ptr0 + x0, tmp9, 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, (4,), (1,))
    assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_4, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_5, (4, 8), (8, 1))
    assert_size_stride(primals_6, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_2, reinterpret_tensor(primals_3, (64, 
            4), (4, 1), 0), reinterpret_tensor(primals_1, (4, 4), (1, 4), 0
            ), alpha=1, beta=1, out=buf0)
        del primals_1
        del primals_2
        buf1 = empty_strided_cuda((4, 4, 4, 8), (128, 32, 8, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_cat_0[grid(512)](primals_3, primals_4, buf1, 512,
            XBLOCK=256, num_warps=4, num_stages=1)
        buf2 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_6, reinterpret_tensor(buf1, (64, 8), (
            8, 1), 0), reinterpret_tensor(primals_5, (8, 4), (1, 8), 0),
            alpha=1, beta=1, out=buf2)
        del primals_5
        del primals_6
        buf3 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused_add_mul_rsub_sigmoid_tanh_1[grid(256)](buf2,
            primals_4, buf0, buf3, 256, XBLOCK=128, num_warps=4, num_stages=1)
    return buf3, primals_4, reinterpret_tensor(primals_3, (64, 4), (4, 1), 0
        ), buf0, reinterpret_tensor(buf1, (64, 8), (8, 1), 0), buf2


def get_initializer(name, activation):
    if activation in ['id', 'identity', 'linear', 'modrelu']:
        nonlinearity = 'linear'
    elif activation in ['relu', 'tanh', 'sigmoid']:
        nonlinearity = activation
    else:
        assert False, f'get_initializer: activation {activation} not supported'
    if name == 'uniform':
        initializer = partial(torch.nn.init.kaiming_uniform_, nonlinearity=
            nonlinearity)
    elif name == 'normal':
        initializer = partial(torch.nn.init.kaiming_normal_, nonlinearity=
            nonlinearity)
    elif name == 'xavier':
        initializer = torch.nn.init.xavier_normal_
    elif name == 'zero':
        initializer = partial(torch.nn.init.constant_, val=0)
    elif name == 'one':
        initializer = partial(torch.nn.init.constant_, val=1)
    else:
        assert False, f'get_initializer: initializer type {name} not supported'
    return initializer


def Linear_(input_size, output_size, bias, init='normal', zero_bias_init=
    False, **kwargs):
    """ Returns a nn.Linear module with initialization options """
    l = nn.Linear(input_size, output_size, bias=bias, **kwargs)
    get_initializer(init, 'linear')(l.weight)
    if bias and zero_bias_init:
        nn.init.zeros_(l.bias)
    return l


def get_activation(activation, size):
    if activation == 'id':
        return nn.Identity()
    elif activation == 'tanh':
        return torch.tanh
    elif activation == 'relu':
        return torch.relu
    elif activation == 'sigmoid':
        return torch.sigmoid
    elif activation == 'modrelu':
        return Modrelu(size)
    else:
        raise NotImplementedError("hidden activation '{}' is not implemented"
            .format(activation))


class Gate(nn.Module):
    """ Implements gating mechanisms.

    Mechanisms:
    N  - No gate
    G  - Standard sigmoid gate
    """

    def __init__(self, size, preact_ctor, preact_args, mechanism='N'):
        super().__init__()
        self.size = size
        self.mechanism = mechanism
        if self.mechanism == 'N':
            pass
        elif self.mechanism == 'G':
            self.W_g = preact_ctor(*preact_args)
        else:
            assert False, f'Gating type {self.mechanism} is not supported.'

    def forward(self, *inputs):
        if self.mechanism == 'N':
            return 1.0
        if self.mechanism == 'G':
            g_preact = self.W_g(*inputs)
            g = torch.sigmoid(g_preact)
        return g


class modrelu(nn.Module):

    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 CellBase(nn.Module):
    """ Abstract class for our recurrent cell interface.

    Passes input through
    """
    registry = {}

    def __init_subclass__(cls, **kwargs):
        super().__init_subclass__(**kwargs)
        if hasattr(cls, 'name') and cls.name is not None:
            cls.registry[cls.name] = cls
    name = 'id'
    valid_keys = []

    def default_initializers(self):
        return {}

    def default_architecture(self):
        return {}

    def __init__(self, input_size, hidden_size, initializers=None,
        architecture=None):
        super().__init__()
        self.input_size = input_size
        self.hidden_size = hidden_size
        self.architecture = self.default_architecture()
        self.initializers = self.default_initializers()
        if initializers is not None:
            self.initializers.update(initializers)
            None
        if architecture is not None:
            self.architecture.update(architecture)
        assert set(self.initializers.keys()).issubset(self.valid_keys)
        assert set(self.architecture.keys()).issubset(self.valid_keys)
        self.reset_parameters()

    def reset_parameters(self):
        pass

    def forward(self, input, hidden):
        return input, input

    def default_state(self, input, batch_size=None):
        return input.new_zeros(input.size(0) if batch_size is None else
            batch_size, self.hidden_size, requires_grad=False)

    def output(self, h):
        return h

    def state_size(self):
        return self.hidden_size

    def output_size(self):
        return self.hidden_size

    def initial_state(self, trainable=False):
        """ Return initial state of the RNN
        This should not need to see the input as it should be batch size agnostic and automatically broadcasted

        # TODO Currently not used
        """
        if trainable:
            self.initial_state = torch.zeros(self.hidden_size,
                requires_grad=True)
        else:
            return torch.zeros(self.hidden_size, requires_grad=True)


class Modrelu(modrelu):

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


class MinimalRNNCellNew(CellBase):
    name = 'mrnn'
    valid_keys = ['hx', 'bias']

    def default_initializers(self):
        return {'hx': 'xavier'}

    def default_architecture(self):
        return {'bias': True}

    def __init__(self, input_size, hidden_size, hidden_activation='tanh',
        orthogonal=False, ortho_args=None, zero_bias_init=False, **kwargs):
        self.hidden_activation = hidden_activation
        self.zero_bias_init = zero_bias_init
        super().__init__(input_size, hidden_size, **kwargs)

    def reset_parameters(self):
        self.W_hx = Linear_(self.input_size, self.hidden_size, bias=self.
            architecture['bias'], zero_bias_init=self.zero_bias_init)
        get_initializer(self.initializers['hx'], self.hidden_activation)(self
            .W_hx.weight)
        self.hidden_activation_fn = get_activation(self.hidden_activation,
            self.hidden_size)
        preact_ctor = Linear_
        preact_args = [self.input_size + self.hidden_size, self.hidden_size,
            self.architecture['bias']]
        self.W_g = Gate(self.hidden_size, preact_ctor, preact_args,
            mechanism='G')

    def forward(self, input_0, input_1):
        primals_1 = self.W_hx.weight
        primals_2 = self.W_hx.bias
        primals_5 = self.W_g.W_g.weight
        primals_6 = self.W_g.W_g.bias
        primals_3 = input_0
        primals_4 = input_1
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6])
        return output[0], output[1]
