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.nn import Module
import math
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_poi_fused_add_mul_sigmoid_sigmoid_backward_0(in_ptr0, in_ptr1,
    in_ptr2, in_ptr3, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 4
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr1 + (x0 + 12 * x1), xmask)
    tmp2 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr3 + (x0 + 8 * x1), xmask)
    tmp3 = tmp1 + tmp2
    tmp5 = tmp3 + tmp4
    tmp6 = tl.sigmoid(tmp5)
    tmp7 = tmp0 * tmp6
    tmp8 = 1.0
    tmp9 = tmp8 - tmp6
    tmp10 = tmp6 * tmp9
    tl.store(out_ptr0 + x2, tmp7, xmask)
    tl.store(out_ptr1 + x2, tmp10, xmask)


@triton.jit
def triton_poi_fused_add_mul_relu_rsub_sigmoid_1(in_out_ptr0, in_ptr0,
    in_ptr1, in_ptr2, in_ptr3, out_ptr0, out_ptr1, 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 + 8 * x1), xmask)
    tmp6 = tl.load(in_ptr0 + (8 + x0 + 12 * x1), xmask)
    tmp7 = tl.load(in_ptr1 + (8 + x0), xmask, eviction_policy='evict_last')
    tmp9 = tl.load(in_out_ptr0 + x2, xmask)
    tmp16 = tl.load(in_ptr3 + x2, xmask)
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 + tmp3
    tmp5 = tl.sigmoid(tmp4)
    tmp8 = tmp6 + tmp7
    tmp10 = tmp8 + tmp9
    tmp11 = tl.full([1], 0, tl.int32)
    tmp12 = triton_helpers.maximum(tmp11, tmp10)
    tmp13 = 1.0
    tmp14 = tmp13 - tmp5
    tmp15 = tmp14 * tmp12
    tmp17 = tmp5 * tmp16
    tmp18 = tmp15 + tmp17
    tl.store(out_ptr0 + x2, tmp5, xmask)
    tl.store(in_out_ptr0 + x2, tmp12, xmask)
    tl.store(out_ptr1 + x2, tmp18, xmask)


def call(args):
    primals_1, primals_2, primals_3, primals_4, primals_5, primals_6 = args
    args.clear()
    assert_size_stride(primals_1, (12, 4), (4, 1))
    assert_size_stride(primals_2, (12,), (1,))
    assert_size_stride(primals_3, (4, 4), (4, 1))
    assert_size_stride(primals_4, (8, 4), (4, 1))
    assert_size_stride(primals_5, (4, 4), (4, 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_3, reinterpret_tensor(primals_1, (4, 12),
            (1, 4), 0), out=buf0)
        del primals_1
        buf1 = empty_strided_cuda((4, 8), (8, 1), torch.float32)
        extern_kernels.mm(primals_5, reinterpret_tensor(primals_4, (4, 8),
            (1, 4), 0), out=buf1)
        del primals_4
        buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf7 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_add_mul_sigmoid_sigmoid_backward_0[grid(16)](primals_5
            , buf0, primals_2, buf1, buf3, buf7, 16, XBLOCK=16, num_warps=1,
            num_stages=1)
        buf4 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.mm(buf3, reinterpret_tensor(primals_6, (4, 4), (1, 4
            ), 0), out=buf4)
        buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf5 = buf4
        del buf4
        buf6 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_poi_fused_add_mul_relu_rsub_sigmoid_1[grid(16)](buf5, buf0,
            primals_2, buf1, primals_5, buf2, buf6, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        del buf0
        del buf1
        del primals_2
    return buf6, primals_3, primals_5, buf2, buf3, buf5, primals_6, buf7


def clip_grad(v, min, max):
    v_tmp = v.expand_as(v)
    v_tmp.register_hook(lambda g: g.clamp(min, max))
    return v_tmp


class RNNCellBase(Module):

    def __repr__(self):
        s = '{name}({input_size}, {hidden_size}'
        if 'bias' in self.__dict__ and self.bias is not True:
            s += ', bias={bias}'
        if 'nonlinearity' in self.__dict__ and self.nonlinearity != 'tanh':
            s += ', nonlinearity={nonlinearity}'
        s += ')'
        return s.format(name=self.__class__.__name__, **self.__dict__)


class GRUCellNew(RNNCellBase):

    def __init__(self, input_size, hidden_size, bias=True, grad_clip=None):
        super(GRUCellNew, self).__init__()
        self.input_size = input_size
        self.hidden_size = hidden_size
        self.grad_clip = grad_clip
        self.weight_ih = Parameter(torch.Tensor(3 * hidden_size, input_size))
        self.weight_hh_rz = Parameter(torch.Tensor(2 * hidden_size,
            hidden_size))
        self.weight_hh = Parameter(torch.Tensor(hidden_size, hidden_size))
        if bias:
            self.bias = Parameter(torch.Tensor(3 * hidden_size))
        else:
            self.register_parameter('bias', None)
        self.reset_parameters()

    def reset_parameters(self):
        stdv = 1.0 / math.sqrt(self.hidden_size)
        for weight in self.parameters():
            weight.data.uniform_(-stdv, stdv)

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


Measured latency_ms: 0.094571