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


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


@triton.jit
def triton_poi_fused_add_mul_sigmoid_tanh_3(in_ptr0, in_ptr1, in_ptr2,
    in_ptr3, in_ptr4, out_ptr0, out_ptr1, out_ptr2, 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 + 12 * x1), xmask)
    tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr2 + (x0 + 12 * 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_ptr2 + (8 + x0 + 12 * x1), xmask)
    tmp12 = tl.load(in_ptr3 + x2, xmask)
    tmp14 = tl.load(in_ptr4 + x2, xmask)
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 + tmp3
    tmp5 = tl.sigmoid(tmp4)
    tmp8 = tmp6 + tmp7
    tmp10 = tmp8 + tmp9
    tmp11 = libdevice.tanh(tmp10)
    tmp13 = tmp5 * tmp12
    tmp15 = tl.sigmoid(tmp14)
    tmp16 = tmp15 * tmp11
    tmp17 = tmp13 + tmp16
    tmp18 = libdevice.tanh(tmp17)
    tl.store(out_ptr0 + x2, tmp5, xmask)
    tl.store(out_ptr1 + x2, tmp11, xmask)
    tl.store(out_ptr2 + x2, tmp18, 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 + (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)
    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.sigmoid(tmp22)
    tmp24 = tl.load(in_ptr5 + (x0 + 4 * (-12 + x1)), tmp16 & xmask, other=0.0)
    tmp25 = tmp23 * tmp24
    tmp26 = tmp21 + tmp25
    tmp27 = tl.full(tmp26.shape, 0.0, tmp26.dtype)
    tmp28 = tl.where(tmp16, tmp26, tmp27)
    tmp29 = tl.where(tmp14, tmp15, tmp28)
    tmp30 = tl.where(tmp9, tmp10, tmp29)
    tmp31 = tl.where(tmp4, tmp5, tmp30)
    tl.store(out_ptr0 + x2, tmp31, 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, primals_5, primals_6,
        primals_7) = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1))
    assert_size_stride(primals_2, (12,), (1,))
    assert_size_stride(primals_3, (4,), (1,))
    assert_size_stride(primals_4, (4, 4), (4, 1))
    assert_size_stride(primals_5, (4, 4), (4, 1))
    assert_size_stride(primals_6, (4, 12), (12, 1))
    assert_size_stride(primals_7, (4, 12), (12, 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, 4), (4, 1), torch.float32)
        extern_kernels.addmm(reinterpret_tensor(primals_3, (4, 4), (0, 1), 
            0), primals_5, primals_4, alpha=1, beta=1, out=buf1)
        del primals_3
        del primals_4
        buf2 = empty_strided_cuda((4, 12), (12, 1), torch.float32)
        extern_kernels.mm(buf0, primals_6, out=buf2)
        buf3 = empty_strided_cuda((4, 12), (12, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_1, (4, 4), (16, 1), 0),
            primals_7, out=buf3)
        buf4 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf30 = 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)](buf2
            , primals_2, buf3, buf1, buf4, buf5, buf30, buf6, buf7, 16,
            XBLOCK=16, num_warps=1, num_stages=1)
        buf8 = buf3
        del buf3
        extern_kernels.mm(buf7, primals_6, out=buf8)
        buf9 = buf2
        del buf2
        extern_kernels.mm(reinterpret_tensor(primals_1, (4, 4), (16, 1), 4),
            primals_7, 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)
        triton_poi_fused_add_mul_sigmoid_tanh_2[grid(16)](buf8, primals_2,
            buf9, buf5, buf1, buf10, buf11, buf12, buf13, buf14, 16, XBLOCK
            =16, num_warps=1, num_stages=1)
        buf15 = buf9
        del buf9
        extern_kernels.mm(buf14, primals_6, out=buf15)
        buf16 = buf8
        del buf8
        extern_kernels.mm(reinterpret_tensor(primals_1, (4, 4), (16, 1), 8),
            primals_7, out=buf16)
        buf17 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        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)
        triton_poi_fused_add_mul_sigmoid_tanh_2[grid(16)](buf15, primals_2,
            buf16, buf12, buf1, buf17, buf18, buf19, buf20, buf21, 16,
            XBLOCK=16, num_warps=1, num_stages=1)
        buf22 = buf16
        del buf16
        extern_kernels.mm(buf21, primals_6, out=buf22)
        buf23 = buf15
        del buf15
        extern_kernels.mm(reinterpret_tensor(primals_1, (4, 4), (16, 1), 12
            ), primals_7, out=buf23)
        del primals_7
        buf24 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf25 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf27 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_poi_fused_add_mul_sigmoid_tanh_3[grid(16)](buf22, primals_2,
            buf23, buf19, buf1, buf24, buf25, buf27, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        buf26 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_poi_fused_sigmoid_4[grid(16)](buf22, primals_2, buf23, buf26,
            16, XBLOCK=16, num_warps=1, num_stages=1)
        del buf22
        del buf23
        del primals_2
        buf28 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        triton_poi_fused_stack_5[grid(64)](buf5, buf12, buf19, buf24, buf1,
            buf25, buf28, 64, XBLOCK=64, num_warps=1, num_stages=1)
        buf29 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        triton_poi_fused_stack_6[grid(64)](buf7, buf14, buf21, buf26, buf27,
            buf29, 64, XBLOCK=64, num_warps=1, num_stages=1)
    return (reinterpret_tensor(buf29, (4, 4, 4), (4, 16, 1), 0),
        reinterpret_tensor(buf28, (4, 4, 4), (4, 16, 1), 0), buf0, buf1,
        buf4, buf5, buf6, buf10, buf11, buf12, buf13, buf17, buf18, buf19,
        buf20, buf24, buf25, buf26, buf27, reinterpret_tensor(primals_1, (4,
        4), (1, 16), 12), reinterpret_tensor(primals_6, (12, 4), (1, 12), 0
        ), reinterpret_tensor(buf21, (4, 4), (1, 4), 0), reinterpret_tensor
        (primals_1, (4, 4), (1, 16), 8), reinterpret_tensor(buf14, (4, 4),
        (1, 4), 0), reinterpret_tensor(primals_1, (4, 4), (1, 16), 4),
        reinterpret_tensor(buf7, (4, 4), (1, 4), 0), buf30,
        reinterpret_tensor(primals_1, (4, 4), (1, 16), 0),
        reinterpret_tensor(primals_5, (4, 4), (1, 4), 0))


class EALSTMNew(nn.Module):
    """Implementation of the Entity-Aware-LSTM (EA-LSTM)

    TODO: Include paper ref and latex equations

    Parameters
    ----------
    input_size_dyn : int
        Number of dynamic features, which are those, passed to the LSTM at each time step.
    input_size_stat : int
        Number of static features, which are those that are used to modulate the input gate.
    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_dyn: 'int', input_size_stat: 'int',
        hidden_size: 'int', batch_first: 'bool'=True, initial_forget_bias:
        'int'=0):
        super(EALSTMNew, self).__init__()
        self.input_size_dyn = input_size_dyn
        self.input_size_stat = input_size_stat
        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_dyn, 3 *
            hidden_size))
        self.weight_hh = nn.Parameter(torch.FloatTensor(hidden_size, 3 *
            hidden_size))
        self.weight_sh = nn.Parameter(torch.FloatTensor(input_size_stat,
            hidden_size))
        self.bias = nn.Parameter(torch.FloatTensor(3 * hidden_size))
        self.bias_s = nn.Parameter(torch.FloatTensor(hidden_size))
        self.reset_parameters()

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

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


Measured latency_ms: 0.015665