import torch
import numpy as np
import torch.nn.functional as F
from torch import nn


class GRUCell(nn.Module):

    def __init__(self, input_size, hidden_size, bias=True):
        super(GRUCell, self).__init__()
        self.input_size = input_size
        self.hidden_size = hidden_size
        self.bias = bias
        self.x2h = nn.Linear(input_size, 3 * hidden_size, bias=bias)
        self.h2h = nn.Linear(hidden_size, 3 * hidden_size, bias=bias)
        self.reset_parameters()

    def reset_parameters(self):
        std = 1.0 / np.sqrt(self.hidden_size)
        for w in self.parameters():
            w.data.uniform_(-std, std)

    def forward(self, x, hidden):
        x = x.view(-1, x.size(-1))
        gate_x = self.x2h(x)
        gate_h = self.h2h(hidden)
        gate_x = gate_x.squeeze()
        gate_h = gate_h.squeeze()
        i_r, i_i, i_n = gate_x.chunk(3, 1)
        h_r, h_i, h_n = gate_h.chunk(3, 1)
        resetgate = F.sigmoid(i_r + h_r)
        inputgate = F.sigmoid(i_i + h_i)
        newgate = F.tanh(i_n + resetgate * h_n)
        hy = newgate + inputgate * (hidden - newgate)
        return hy


def get_inputs():
    return [torch.rand([4, 4]), torch.rand([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
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_add_mul_sigmoid_sub_tanh_0(in_ptr0, in_ptr1, in_ptr2,
    in_ptr3, out_ptr0, out_ptr1, out_ptr2, out_ptr3, 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
    tmp0 = tl.load(in_ptr0 + (4 + x0 + 12 * x1), xmask)
    tmp1 = tl.load(in_ptr1 + (4 + x0), xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr2 + (4 + x0 + 12 * x1), xmask)
    tmp6 = tl.load(in_ptr0 + (x0 + 12 * x1), xmask)
    tmp7 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr2 + (x0 + 12 * x1), xmask)
    tmp12 = tl.load(in_ptr0 + (8 + x0 + 12 * x1), xmask)
    tmp13 = tl.load(in_ptr1 + (8 + x0), xmask, eviction_policy='evict_last')
    tmp15 = tl.load(in_ptr2 + (8 + x0 + 12 * x1), xmask)
    tmp19 = tl.load(in_ptr3 + x2, xmask)
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 + tmp3
    tmp5 = tl.sigmoid(tmp4)
    tmp8 = tmp6 + tmp7
    tmp10 = tmp8 + tmp9
    tmp11 = tl.sigmoid(tmp10)
    tmp14 = tmp12 + tmp13
    tmp16 = tmp11 * tmp15
    tmp17 = tmp14 + tmp16
    tmp18 = libdevice.tanh(tmp17)
    tmp20 = tmp19 - tmp18
    tmp21 = tmp5 * tmp20
    tmp22 = tmp18 + tmp21
    tl.store(out_ptr0 + x2, tmp5, xmask)
    tl.store(out_ptr1 + x2, tmp11, xmask)
    tl.store(out_ptr2 + x2, tmp18, xmask)
    tl.store(out_ptr3 + x2, tmp22, 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, (12, 4), (4, 1))
    assert_size_stride(primals_3, (12,), (1,))
    assert_size_stride(primals_4, (12, 4), (4, 1))
    assert_size_stride(primals_5, (12,), (1,))
    assert_size_stride(primals_6, (4, 4), (4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 12), (12, 1), torch.float32)
        extern_kernels.mm(primals_1, reinterpret_tensor(primals_2, (4, 12),
            (1, 4), 0), out=buf0)
        del primals_2
        buf1 = empty_strided_cuda((4, 12), (12, 1), torch.float32)
        extern_kernels.addmm(primals_5, primals_6, reinterpret_tensor(
            primals_4, (4, 12), (1, 4), 0), alpha=1, beta=1, out=buf1)
        del primals_4
        del primals_5
        buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf4 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_add_mul_sigmoid_sub_tanh_0[grid(16)](buf0,
            primals_3, buf1, primals_6, buf3, buf2, buf4, buf5, 16, XBLOCK=
            16, num_warps=1, num_stages=1)
        del buf0
        del primals_3
    return buf5, primals_6, primals_1, reinterpret_tensor(buf1, (4, 4), (12,
        1), 8), buf2, buf3, buf4


class GRUCellNew(nn.Module):

    def __init__(self, input_size, hidden_size, bias=True):
        super(GRUCellNew, self).__init__()
        self.input_size = input_size
        self.hidden_size = hidden_size
        self.bias = bias
        self.x2h = nn.Linear(input_size, 3 * hidden_size, bias=bias)
        self.h2h = nn.Linear(hidden_size, 3 * hidden_size, bias=bias)
        self.reset_parameters()

    def reset_parameters(self):
        std = 1.0 / np.sqrt(self.hidden_size)
        for w in self.parameters():
            w.data.uniform_(-std, std)

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