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 torch.nn as 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_zero_0(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
    tmp0 = 0.0
    tl.store(out_ptr0 + x0, tmp0, xmask)


@triton.jit
def triton_poi_fused_add_mul_sigmoid_sigmoid_backward_tanh_1(in_ptr0,
    in_ptr1, in_ptr2, out_ptr0, out_ptr1, out_ptr2, out_ptr3, out_ptr4,
    out_ptr5, 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 + 16 * x1), xmask)
    tmp1 = tl.load(in_ptr1 + (4 + x0), xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr2 + (4 + x0 + 16 * x1), xmask)
    tmp6 = tl.load(in_ptr0 + (12 + x0 + 16 * x1), xmask)
    tmp7 = tl.load(in_ptr1 + (12 + x0), xmask, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr2 + (12 + x0 + 16 * x1), xmask)
    tmp12 = tl.load(in_ptr0 + (x0 + 16 * x1), xmask)
    tmp13 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp15 = tl.load(in_ptr2 + (x0 + 16 * x1), xmask)
    tmp25 = tl.load(in_ptr0 + (8 + x0 + 16 * x1), xmask)
    tmp26 = tl.load(in_ptr1 + (8 + x0), xmask, eviction_policy='evict_last')
    tmp28 = tl.load(in_ptr2 + (8 + x0 + 16 * x1), xmask)
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 + tmp3
    tmp5 = tl.sigmoid(tmp4)
    tmp8 = tmp6 + tmp7
    tmp10 = tmp8 + tmp9
    tmp11 = libdevice.tanh(tmp10)
    tmp14 = tmp12 + tmp13
    tmp16 = tmp14 + tmp15
    tmp17 = tl.sigmoid(tmp16)
    tmp18 = 0.0
    tmp19 = tmp17 * tmp18
    tmp20 = tmp5 * tmp11
    tmp21 = tmp19 + tmp20
    tmp22 = 1.0
    tmp23 = tmp22 - tmp17
    tmp24 = tmp17 * tmp23
    tmp27 = tmp25 + tmp26
    tmp29 = tmp27 + tmp28
    tmp30 = tl.sigmoid(tmp29)
    tmp31 = libdevice.tanh(tmp21)
    tmp32 = tmp30 * tmp31
    tl.store(out_ptr0 + x2, tmp5, xmask)
    tl.store(out_ptr1 + x2, tmp11, xmask)
    tl.store(out_ptr2 + x2, tmp21, xmask)
    tl.store(out_ptr3 + x2, tmp24, xmask)
    tl.store(out_ptr4 + x2, tmp30, xmask)
    tl.store(out_ptr5 + x2, tmp32, xmask)


@triton.jit
def triton_poi_fused_add_mul_sigmoid_tanh_2(in_ptr0, in_ptr1, in_ptr2,
    in_ptr3, out_ptr0, out_ptr1, out_ptr2, out_ptr3, out_ptr4, out_ptr5,
    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 + (x0 + 16 * x1), xmask)
    tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr2 + (x0 + 16 * x1), xmask)
    tmp6 = tl.load(in_ptr0 + (4 + x0 + 16 * x1), xmask)
    tmp7 = tl.load(in_ptr1 + (4 + x0), xmask, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr2 + (4 + x0 + 16 * x1), xmask)
    tmp12 = tl.load(in_ptr0 + (12 + x0 + 16 * x1), xmask)
    tmp13 = tl.load(in_ptr1 + (12 + x0), xmask, eviction_policy='evict_last')
    tmp15 = tl.load(in_ptr2 + (12 + x0 + 16 * x1), xmask)
    tmp18 = tl.load(in_ptr3 + x2, xmask)
    tmp22 = tl.load(in_ptr0 + (8 + x0 + 16 * x1), xmask)
    tmp23 = tl.load(in_ptr1 + (8 + x0), xmask, eviction_policy='evict_last')
    tmp25 = tl.load(in_ptr2 + (8 + x0 + 16 * x1), 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 = tmp14 + tmp15
    tmp17 = libdevice.tanh(tmp16)
    tmp19 = tmp5 * tmp18
    tmp20 = tmp11 * tmp17
    tmp21 = tmp19 + tmp20
    tmp24 = tmp22 + tmp23
    tmp26 = tmp24 + tmp25
    tmp27 = tl.sigmoid(tmp26)
    tmp28 = libdevice.tanh(tmp21)
    tmp29 = tmp27 * tmp28
    tl.store(out_ptr0 + x2, tmp5, xmask)
    tl.store(out_ptr1 + x2, tmp11, xmask)
    tl.store(out_ptr2 + x2, tmp17, xmask)
    tl.store(out_ptr3 + x2, tmp21, xmask)
    tl.store(out_ptr4 + x2, tmp27, xmask)
    tl.store(out_ptr5 + x2, tmp29, xmask)


@triton.jit
def triton_poi_fused_add_mul_sigmoid_tanh_3(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 + (x0 + 16 * x1), xmask)
    tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr2 + (x0 + 16 * x1), xmask)
    tmp6 = tl.load(in_ptr0 + (4 + x0 + 16 * x1), xmask)
    tmp7 = tl.load(in_ptr1 + (4 + x0), xmask, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr2 + (4 + x0 + 16 * x1), xmask)
    tmp12 = tl.load(in_ptr0 + (12 + x0 + 16 * x1), xmask)
    tmp13 = tl.load(in_ptr1 + (12 + x0), xmask, eviction_policy='evict_last')
    tmp15 = tl.load(in_ptr2 + (12 + x0 + 16 * x1), xmask)
    tmp18 = 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 = tmp14 + tmp15
    tmp17 = libdevice.tanh(tmp16)
    tmp19 = tmp5 * tmp18
    tmp20 = tmp11 * tmp17
    tmp21 = tmp19 + tmp20
    tmp22 = libdevice.tanh(tmp21)
    tl.store(out_ptr0 + x2, tmp5, xmask)
    tl.store(out_ptr1 + x2, tmp11, xmask)
    tl.store(out_ptr2 + x2, tmp17, xmask)
    tl.store(out_ptr3 + x2, tmp22, xmask)


@triton.jit
def triton_poi_fused_sigmoid_4(in_ptr0, in_ptr1, in_ptr2, 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
    tmp0 = tl.load(in_ptr0 + (8 + x0 + 16 * x1), xmask)
    tmp1 = tl.load(in_ptr1 + (8 + x0), xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr2 + (8 + x0 + 16 * x1), xmask)
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 + tmp3
    tmp5 = tl.sigmoid(tmp4)
    tl.store(out_ptr0 + x2, tmp5, xmask)


@triton.jit
def triton_poi_fused_stack_5(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4,
    in_ptr5, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 4
    x0 = xindex % 4
    x2 = xindex
    tmp0 = x1
    tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 4, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + (x0 + 4 * x1), tmp4 & xmask, other=0.0)
    tmp6 = tmp0 >= tmp3
    tmp7 = tl.full([1], 8, tl.int64)
    tmp8 = tmp0 < tmp7
    tmp9 = tmp6 & tmp8
    tmp10 = tl.load(in_ptr1 + (x0 + 4 * (-4 + x1)), tmp9 & xmask, other=0.0)
    tmp11 = tmp0 >= tmp7
    tmp12 = tl.full([1], 12, tl.int64)
    tmp13 = tmp0 < tmp12
    tmp14 = tmp11 & tmp13
    tmp15 = tl.load(in_ptr2 + (x0 + 4 * (-8 + x1)), tmp14 & xmask, other=0.0)
    tmp16 = tmp0 >= tmp12
    tl.full([1], 16, tl.int64)
    tmp19 = tl.load(in_ptr3 + (x0 + 4 * (-12 + x1)), tmp16 & xmask, other=0.0)
    tmp20 = tl.load(in_ptr2 + (x0 + 4 * (-12 + x1)), tmp16 & xmask, other=0.0)
    tmp21 = tmp19 * tmp20
    tmp22 = tl.load(in_ptr4 + (x0 + 4 * (-12 + x1)), tmp16 & xmask, other=0.0)
    tmp23 = tl.load(in_ptr5 + (x0 + 4 * (-12 + x1)), tmp16 & xmask, other=0.0)
    tmp24 = tmp22 * tmp23
    tmp25 = tmp21 + tmp24
    tmp26 = tl.full(tmp25.shape, 0.0, tmp25.dtype)
    tmp27 = tl.where(tmp16, tmp25, tmp26)
    tmp28 = tl.where(tmp14, tmp15, tmp27)
    tmp29 = tl.where(tmp9, tmp10, tmp28)
    tmp30 = tl.where(tmp4, tmp5, tmp29)
    tl.store(out_ptr0 + x2, tmp30, xmask)


@triton.jit
def triton_poi_fused_stack_6(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4,
    out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 4
    x0 = xindex % 4
    x2 = xindex
    tmp0 = x1
    tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 4, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + (x0 + 4 * x1), tmp4 & xmask, other=0.0)
    tmp6 = tmp0 >= tmp3
    tmp7 = tl.full([1], 8, tl.int64)
    tmp8 = tmp0 < tmp7
    tmp9 = tmp6 & tmp8
    tmp10 = tl.load(in_ptr1 + (x0 + 4 * (-4 + x1)), tmp9 & xmask, other=0.0)
    tmp11 = tmp0 >= tmp7
    tmp12 = tl.full([1], 12, tl.int64)
    tmp13 = tmp0 < tmp12
    tmp14 = tmp11 & tmp13
    tmp15 = tl.load(in_ptr2 + (x0 + 4 * (-8 + x1)), tmp14 & xmask, other=0.0)
    tmp16 = tmp0 >= tmp12
    tl.full([1], 16, tl.int64)
    tmp19 = tl.load(in_ptr3 + (x0 + 4 * (-12 + x1)), tmp16 & xmask, other=0.0)
    tmp20 = tl.load(in_ptr4 + (x0 + 4 * (-12 + x1)), tmp16 & xmask, other=0.0)
    tmp21 = tmp19 * tmp20
    tmp22 = tl.full(tmp21.shape, 0.0, tmp21.dtype)
    tmp23 = tl.where(tmp16, tmp21, tmp22)
    tmp24 = tl.where(tmp14, tmp15, tmp23)
    tmp25 = tl.where(tmp9, tmp10, tmp24)
    tmp26 = tl.where(tmp4, tmp5, tmp25)
    tl.store(out_ptr0 + x2, tmp26, xmask)


def call(args):
    primals_1, primals_2, primals_3, primals_4 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1))
    assert_size_stride(primals_2, (16,), (1,))
    assert_size_stride(primals_3, (4, 16), (16, 1))
    assert_size_stride(primals_4, (4, 16), (16, 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_zero_0[grid(16)](buf0, 16, XBLOCK=16, num_warps=1,
            num_stages=1)
        buf1 = empty_strided_cuda((4, 16), (16, 1), torch.float32)
        extern_kernels.mm(buf0, primals_3, out=buf1)
        buf2 = empty_strided_cuda((4, 16), (16, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_1, (4, 4), (16, 1), 0),
            primals_4, out=buf2)
        buf3 = 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)
        buf33 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf6 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf7 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_poi_fused_add_mul_sigmoid_sigmoid_backward_tanh_1[grid(16)](buf1
            , primals_2, buf2, buf3, buf4, buf5, buf33, buf6, buf7, 16,
            XBLOCK=16, num_warps=1, num_stages=1)
        buf8 = buf2
        del buf2
        extern_kernels.mm(buf7, primals_3, out=buf8)
        buf9 = buf1
        del buf1
        extern_kernels.mm(reinterpret_tensor(primals_1, (4, 4), (16, 1), 4),
            primals_4, out=buf9)
        buf10 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf11 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf12 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf13 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf14 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf15 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_poi_fused_add_mul_sigmoid_tanh_2[grid(16)](buf8, primals_2,
            buf9, buf5, buf10, buf11, buf12, buf13, buf14, buf15, 16,
            XBLOCK=16, num_warps=1, num_stages=1)
        buf16 = buf9
        del buf9
        extern_kernels.mm(buf15, primals_3, out=buf16)
        buf17 = buf8
        del buf8
        extern_kernels.mm(reinterpret_tensor(primals_1, (4, 4), (16, 1), 8),
            primals_4, out=buf17)
        buf18 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf19 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf20 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf21 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf22 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf23 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_poi_fused_add_mul_sigmoid_tanh_2[grid(16)](buf16, primals_2,
            buf17, buf13, buf18, buf19, buf20, buf21, buf22, buf23, 16,
            XBLOCK=16, num_warps=1, num_stages=1)
        buf24 = buf17
        del buf17
        extern_kernels.mm(buf23, primals_3, out=buf24)
        buf25 = buf16
        del buf16
        extern_kernels.mm(reinterpret_tensor(primals_1, (4, 4), (16, 1), 12
            ), primals_4, out=buf25)
        del primals_4
        buf26 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf27 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf28 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf30 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_poi_fused_add_mul_sigmoid_tanh_3[grid(16)](buf24, primals_2,
            buf25, buf21, buf26, buf27, buf28, buf30, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        buf29 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_poi_fused_sigmoid_4[grid(16)](buf24, primals_2, buf25, buf29,
            16, XBLOCK=16, num_warps=1, num_stages=1)
        del primals_2
        buf31 = reinterpret_tensor(buf25, (16, 4), (4, 1), 0)
        del buf25
        triton_poi_fused_stack_5[grid(64)](buf5, buf13, buf21, buf26, buf27,
            buf28, buf31, 64, XBLOCK=64, num_warps=1, num_stages=1)
        buf32 = reinterpret_tensor(buf24, (16, 4), (4, 1), 0)
        del buf24
        triton_poi_fused_stack_6[grid(64)](buf7, buf15, buf23, buf29, buf30,
            buf32, 64, XBLOCK=64, num_warps=1, num_stages=1)
    return (reinterpret_tensor(buf32, (4, 4, 4), (4, 16, 1), 0),
        reinterpret_tensor(buf31, (4, 4, 4), (4, 16, 1), 0), buf0, buf3,
        buf4, buf5, buf6, buf10, buf11, buf12, buf13, buf14, buf18, buf19,
        buf20, buf21, buf22, buf26, buf27, buf28, buf29, buf30,
        reinterpret_tensor(primals_1, (4, 4), (1, 16), 12),
        reinterpret_tensor(primals_3, (16, 4), (1, 16), 0),
        reinterpret_tensor(buf23, (4, 4), (1, 4), 0), reinterpret_tensor(
        primals_1, (4, 4), (1, 16), 8), reinterpret_tensor(buf15, (4, 4), (
        1, 4), 0), reinterpret_tensor(primals_1, (4, 4), (1, 16), 4),
        reinterpret_tensor(buf7, (4, 4), (1, 4), 0), buf33,
        reinterpret_tensor(primals_1, (4, 4), (1, 16), 0))


class LSTMNew(nn.Module):
    """Implementation of the standard LSTM.

    TODO: Include ref and LaTeX equations

    Parameters
    ----------
    input_size : int
        Number of input features
    hidden_size : int
        Number of hidden/memory cells.
    batch_first : bool, optional
        If True, expects the batch inputs to be of shape [batch, seq, features] otherwise, the
        shape has to be [seq, batch, features], by default True.
    initial_forget_bias : int, optional
        Value of the initial forget gate bias, by default 0
    """

    def __init__(self, input_size: 'int', hidden_size: 'int', batch_first:
        'bool'=True, initial_forget_bias: 'int'=0):
        super(LSTMNew, self).__init__()
        self.input_size = input_size
        self.hidden_size = hidden_size
        self.batch_first = batch_first
        self.initial_forget_bias = initial_forget_bias
        self.weight_ih = nn.Parameter(torch.FloatTensor(input_size, 4 *
            hidden_size))
        self.weight_hh = nn.Parameter(torch.FloatTensor(hidden_size, 4 *
            hidden_size))
        self.bias = nn.Parameter(torch.FloatTensor(4 * hidden_size))
        self.reset_parameters()

    def reset_parameters(self):
        """Initialize all learnable parameters of the LSTM"""
        nn.init.orthogonal_(self.weight_ih.data)
        weight_hh_data = torch.eye(self.hidden_size)
        weight_hh_data = weight_hh_data.repeat(1, 4)
        self.weight_hh.data = weight_hh_data
        nn.init.constant_(self.bias.data, val=0)
        if self.initial_forget_bias != 0:
            self.bias.data[:self.hidden_size] = self.initial_forget_bias

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


Measured latency_ms: 0.016193