Advancing Regular Language Reasoning in Linear Recurrent Neural Networks
In recent studies, linear recurrent neural networks (LRNNs) have achieved
Transformer-level performance in natural language and long-range modeling,
while offering rapid parallel training and constant inference cost. With the
resurgence of interest in LRNNs, we study whether they can learn the hidden
rules in training sequences, such as the grammatical structures of regular
language. We theoretically analyze some existing LRNNs and discover their
limitations in modeling regular language. Motivated by this analysis, we
propose a new LRNN equipped with a block-diagonal and input-dependent
transition matrix. Experiments suggest that the proposed model is the only LRNN
capable of performing length extrapolation on regular language tasks such as
Sum, Even Pair, and Modular Arithmetic. The code is released at
\url{https://github.com/tinghanf/RegluarLRNN}.
Ting-Han Fan, Ta-Chung Chi, Alexander I. Rudnicky
# Advancing Regular Language Reasoning in

###### Abstract

In recent studies, linear recurrent neural networks (LRNNs) have achieved Transformer-level performance in natural language modeling and long-range modeling while offering rapid parallel training and constant inference costs. With the resurged interest in LRNNs, we study whether they can learn the hidden rules in training sequences, such as the grammatical structures of regular language. We theoretically analyze some existing LRNNs and discover their limitations on regular language. Motivated by the analysis, we propose a new LRNN equipped with a block-diagonal and input-dependent transition matrix. Experiments suggest that the proposed model is the only LRNN that can perform length extrapolation on regular language tasks such as Sum, Even Pair, and Modular Arithmetic.

## 1 Introduction

There is a recent surge of using linear recurrent neural networks (RNNs) [1, 13, 14] as alternatives to the de-facto Transformer architecture [15, 16] that is ingrained in the field of natural language processing. LRNNs depart from the inter-timestep non-linearity design principle of classic RNNs [1, 12, 13] while at the same time: 1. achieve Transformer-level performance on the task of natural language modeling [12, 13] and even better performance on synthetic long range modeling tasks [13, 14, 15, 16, 17, 18]. 2. have the added benefits of fast parallelizable training [15] and constant inference cost.

In spite of the remarkable empirical performance on natural language, there has been no research on LRNNs' ability to model regular language. Regular language is a type of language that strictly follows certain rules like grammar.1 This is very different from natural language as human language is full of ambiguities. The successful modeling of a regular language is important since it implies a model's ability to learn the underlying rules of the data. For example, if the training data are arithmetic operations such as \(1+2\times 3\), the model should learn the rules of \(a+b\), \(a\times b\), and that \(\times\) has a higher priority than \(+\). Learning unambiguous rules behind the data is a critical step toward sequence modeling with regulated output.

Footnote 1: Formally speaking, the rules are defined/recognized by the underlying finite state machine.

In this paper, we aim to determine if existing LRNNs are competent to learn the correct grammar of regular language by testing their language transduction capability under the extrapolation setting. Concretely, a model is trained only to predict the desired outputs on a set of short sequences of length \(L_{tr}\). It then needs to predict the correct outputs for longer testing sequences of length \(L_{ex}\gg L_{tr}\).

We theoretically show that some of the recently proposed LRNNs lack the expressiveness to encode certain arithmetic operations used in the tasks of regular language. In light of this observation, we propose a new LRNN equipped with a block-diagonal and input-dependent transition matrix. The two modifications of our proposed LRNN allow the model to learn and follow the grammar of the regular language. Experiments show that the proposed model is the only LRNN architecture that can extrapolate on regular language tasks such as Sum, Even Pair, and Modular Arithmetic.

## 2 Limitations of Prior Work

In this section, we show that most LRNNs are unable to represent arithmetic operations, posing a serious issue under the extrapolation setting where
the model has to learn the underlying language to combat the length distributional shift.

### Linear RNN

In this paper, we consider a general family of LRNNs as follows.

\[\begin{split} x_{k}&=A_{k}x_{k-1}+Bu_{k}\\ y_{k}&=h(x_{k}).\end{split} \tag{1}\]

\(A_{k}\) is a matrix that defines the recurrence relation. \(A_{k}\) may or may not depend on the input \(u_{k}\). When it is input-independent, \(A_{k}\) is reduced to \(A\); otherwise, \(A_{k}=g(u_{k})\) for some function \(g\). The first line encodes a linear recurrence on the state \(x_{k}\). The second line is an output \(y_{k}\) that depends on \(x_{k}\). To control the model's expressiveness, the function \(h\) may or may not be a linear operation. Since the existing LRNNs differ in their linear recurrence relations (Eq. (2), (3), and (4)), we mainly focus on the linear recurrence of each model in this paper.

### Input-independent LRNN

To begin with, state-space models (in discrete-time format) follow the standard LRNN recurrence.

\[x_{k}=Ax_{k-1}+Bu_{k} \tag{2}\]

Eq. (2) encapsulates the recurrence relation of the S4 models Gu et al. (2022); Gupta et al. (2022), S5 model Smith et al. (2023), and Linear Recurrent Unit Orvieto et al. (2023). For example, \(A\) is in the family of HiPPO matrix Gu et al. (2023) in S4 or a complex diagonal matrix in Linear Recurrent Unit. We show in Proposition 1 that such input-independent \(A\) matrix cannot represent subtraction.

**Proposition 1**.: _An input-independent LRNN is inconsistent in representing subtraction._

Proof.: Denote \(u_{0}\), \(u_{-}\), and \(u_{1}\) as the input vector w.r.t. input character 0, -, and 1. Denote \(z\) as the initial state vector. The sequences "0-1" and "1-0" are represented as

\[\begin{split} x_{0-1}&=A^{3}z+A^{2}u_{0}+Au_{-}+u_{ 1},\text{\quad for "0-1"}\\ x_{1-0}&=A^{3}z+A^{2}u_{1}+Au_{-}+u_{0},\text{\quad for "1-0"} \end{split}\]

Because \(0-1\neq 1-0\), by forcing \(x_{0-1}\neq x_{1-0}\), we have

\[A^{2}u_{0}+Au_{-}+u_{1}\neq A^{2}u_{1}+Au_{-}+u_{0}.\]

On the other hand, let \(x_{0-}=A^{2}z+Au_{0}+u_{-}\) be the vector representation for "0-". The sequences "0-0-1" and "0-1-0" are represented as

\[\begin{split} x_{0-0-1}&=A^{3}x_{0-}+A^{2}u_{0}+Au_ {-}+u_{1}\\ x_{0-1-0}&=A^{3}x_{0-}+A^{2}u_{1}+Au_{-}+u_{0}. \end{split}\]

Notice \(x_{0-0-1}\) is for "0-0-1" while \(x_{0-1-0}\) for "0-1-0". Enforcing \(x_{0-0-1}=x_{0-1-0}\), we have

\[A^{2}u_{0}+Au_{-}+u_{1}=A^{2}u_{1}+Au_{-}+u_{0},\]

which is a contradiction. 

### Input-dependent Diagonal LRNN

To reduce the computational complexity of LRNNs, there has been interest in applying diagonal linear recurrence Gupta et al. (2022); Smith et al. (2023); Orvieto et al. (2023). In particular, prior work adopts input-independent diagonal recurrence and is unable to represent subtraction as shown in SS2.2. As a result, one may wonder if generalizing the model to diagonal input-dependent RNNs can solve the problem, as defined in Eq. (3).

\[x_{k}=\text{diag}(v_{k})x_{k-1}+Bu_{k}, \tag{3}\]

where \(v_{k}=f(u_{k})\) is a vector that depends on \(u_{k}\). To answer this question, we show that an input-dependent diagonal LRNN cannot represent subtraction in proposition 2.

**Proposition 2**.: _An input-dependent diagonal linear RNN is inconsistent in representing subtraction._

The proof is essentially a generalization of Proposition 1 and is deferred to Appendix A.1.

### Implication

Note the failure in representation implies the extrapolation error is large, but the model may still perform well if the testing length is no greater than the training length. We will evaluate this in SS4.

## 3 Proposed Method

### Motivation from Liquid-S4

The recently proposed liquid-S4 Hasani et al. (2023) can be seen as a generalization of Eq. (2) and (3), as its transition matrix is composed of an input-independent block matrix and an input-dependent diagonal matrix with the following recurrence.

\[\begin{split} x_{k}&=Ax_{k-1}+(Bu_{k})\odot x_{k- 1}+Bu_{k}\\ &=(A+\text{diag}(Bu_{k}))x_{k-1}+Bu_{k},\end{split} \tag{4}\]
where \(\odot\) denotes the Hadamard product and \(\text{diag}(w)\) constructs a diagonal matrix from \(w\).

Although our experiments in SS4.3 show that Liquid-S4 cannot extrapolate on regular language tasks, to our best knowledge, it is still the first to use input-dependent block matrices \(A+\text{diag}(Bu_{k})\). In SS3.2, we will introduce a construction of input-dependent block matrices that can potentially model regular language tasks with sufficient numerical stability.

### Block-diagonal Input-dependent LRNN

As shown in SS2, in the world of LRNNs, neither the input-independent recurrence nor input-dependent diagonal recurrence can accurately represent arithmetic operations. To balance between representation ability and computational efficiency, we propose an input-dependent block-diagonal LRNN as

\[x_{k}=A_{k}x_{k-1}+Bu_{k}, \tag{5}\]

where \(A_{k}=g(u_{k})\) is a block diagonal matrix that depends on \(u_{k}\) but not on the previous timesteps. \(g\) is an arbitrary function with the output being the size of \(A_{k}\).

Eq. (5) is numerically unstable because the product \(\prod_{i=1}^{k}A_{i}\) could produce large numbers. The solution is to impose additional constraints on the blocks of \(A_{k}\) in Eq (5):

\[\begin{split}& A_{k}=\text{diag}\left(A_{k}^{(1)},...,A_{k}^{(h)} \right)\in\mathbb{R}^{bh\timesbh}\\ & A_{k}^{(i)}=\begin{bmatrix}v_{k}^{(i,1)}&\ldots&v_{k}^{(i,b)} \end{bmatrix}\in\mathbb{R}^{b\times b}\\ &\|v_{k}^{(i,j)}\|_{p}\leq 1,&i\in[1,...,h],&j\in[1,...,b],\end{split} \tag{6}\]

where \(\|\cdot\|_{p}\) denotes the vector p-norm and \(v_{k}^{(i,j)}\) is a column vector that depends on \(u_{k}\). For any vector \(v\), we can derive another vector \(v^{\prime}\) to satisfy p-norm constraint through \(v^{\prime}=v/\max(1,\|v\|_{p})\). Because \(\|v\|_{p}\geq\|v\|_{q}\) if \(p\leq q\), a smaller \(p\) imposes a stronger constraint on the columns of \(A_{k}^{(i)}\). In other words, we can stabilize Eq. (5) by selecting a sufficiently small \(p\).

Take \(p=1\) as an example. Every block \(A_{k}^{(i)}\) is a matrix that none of its column norms is greater than 1 in \(\|\cdot\|_{1}\). This implies \(A_{k+1}^{(i)}A_{k}^{(i)}\) is the same kind of matrix. Specifically, let \(v^{(1)},...,v^{(b)}\) be the columns of \(A_{k+1}^{(i)}A_{k}^{(i)}\). We have

\[\begin{split}&\left[\|v^{(1)}\|_{1}\quad\ldots\quad\|v^{(b)}\|_ {1}\right]=\mathbb{1}^{\top}\left|A_{k+1}^{(i)}A_{k}^{(i)}\right|\\ &\leq\mathbb{1}^{\top}\left|A_{k+1}^{(i)}\right|\left|A_{k}^{(i) }\right|\leq\mathbb{1}^{\top}\left|A_{k}^{(i)}\right|\leq\mathbb{1}^{\top}. \end{split} \tag{7}\]

Note that \(\mathbb{1}\) is a column vector of all ones. \(|\cdot|\) and \(\leq\) are element-wise absolute value and inequality operations. The last two inequalities follow from that \(A_{k+1}^{(i)}\) and \(A_{k}^{(i)}\)'s column norms are no greater than 1 in \(\|\cdot\|_{1}\).

Eq. (7) demonstrates that \(p=1\) can stabilize the proposed block-diagonal recurrence, Eq. (5). However, a small \(p\) restricts a model's expressiveness. In SS4.3, we will show that \(p=1.2\) is small enough to yield good empirical performance.

## 4 Experiments

### Regular Language Tasks

Regular language is the type of formal language recognized by a Finite State Automata (FSA) (Chomsky, 1956). An FSA is described by a 5-tuple \((Q,\Sigma,\delta,q_{0},F)\). \(Q\) is a finite non-empty set of states. \(\Sigma\) is a finite non-empty set of symbols. \(q_{0}\in Q\) is an initial state. \(\delta:Q\times\Sigma\to Q\) is a transition function; \(F\subseteq Q\) is a set of final states. As noted in Deletang et al. (2023), language transduction can be more useful than language recognition in practice. Language transduction maps from one string to another while language recognition classifies whether a string obeys a rule.

In this work, we follow the regular language transduction tasks in Deletang et al. (2023). We are particularly interested in **Sum(5)**, **EvenPair(5)**, **ModArith(5)**. For **Sum(M)**, the input is a string \(\{s_{i}\}_{i=0}^{n-1}\) of numbers in \([0,...,M-1]\). The output is the sum of them under modulo M: \(\sum_{i=0}^{n-1}s_{i}\) mod \(M\). For example, when \(M=5\), the input "0324" corresponds to the output "4" because \(0+3+2+4\) mod \(5=4\). Notably, **Sum(2)** is the famous PARITY problem that evaluates whether there is an odd number of 1's in a bit string. Thus, **Sum(M)** is a generalization of PARITY and shares the same characteristic: If one error occurs during the summation, the output will be wrong.

**EvenPair(M)**, the input is a string \(\{s_{i}\}_{i=0}^{n-1}\) of numbers in \([0,...,M-1]\). The output is 1 if \(s_{n-1}=s_{0}\) and 0 otherwise. For example, when \(M=5\), the input "0320" corresponds to the output "1" because the first entry equals the last entry. Since **EvenPair(M)** only cares about the first and last entries, the model should learn to remember the first entry and forget the entries \(i\in[1,..,n-2]\).

In **ModArith(M)**, the input is a string \(\{s_{i}\}_{i=0}^{n-1}\) of odd length (i.e., \(n\) is odd). The even entries (\(i\in[0,2,...]\)) are numbers in \([0,...,M-1]\); The odd entries (\(i\in[1,3,...]\)) are symbols in \(\{+,-,\times\}\). The
output is the answer of the mathematical expression under modulo M. For example, when \(M=5\), the input "1+2-3\(\times\)4" corresponds to the output "1" because \(1+2-3\times 4\) mod \(5=-9\) mod \(5=1\). **ModArith(M)** is much more complicated than **Sum(M)** and **EvenPair(M)** because the model should learn to prioritize multiplication over addition and subtraction.

### Length Extrapolation

In our pilot experiments, we discovered that all models can achieve near-perfect same-length testing accuracy; i.e., testing with \(L_{\text{ex}}=L_{\text{tr}}\). We hypothesize that this is because a huge enough model (e.g., enlarging the embedding dimension) can memorize all training sequences. Memorizing the training sequences does not mean the model can do well during testing, especially when the testing sequences are longer than the training ones. To evaluate whether a model learns the underlying rules of the language without simply memorizing the training data, we first train it on sequences of length \(L_{\text{tr}}\) generated by an FSA; It is then evaluated on sequences of length \(L_{\text{ex}}>L_{\text{tr}}\) generated by the same FSA.

Table 1 summarizes the extrapolation setting. We mostly follow the requirements in Deletang et al. (2023), where the training and extrapolation lengths are 40 and 500. The lengths for **ModArith(5)** are 39 and 499 because this task requires odd-length inputs.

### Experimental Results

We implemented LRNNs such as S4 (Gu et al., 2022), S4D (Gupta et al., 2022), and Liquid-S4 (Hasani et al., 2023) using their released codebases as baseline methods. For the proposed method, we set \(p=1.2\) in Eq. (6) and train the block-diagonal input-dependent LRNN with (b, h) = (8, 8). Because **ModArith** is more complicated than **Sum** and **EvenPair**, **ModArith** uses 3 layers while the others take 1 layer. Each layer is a full pass of LRNN as Eq. (1).

To accelerate the computational speed, all LRNNs in Table 2 are implemented with the parallel scan algorithm (Martin and Cundy, 2018). The computational cost of our model is \(O(b^{2}h\log(T))\) where \(b,h\) and \(T\) are block size, number of blocks, and sequence length, respectively. Because the embedding dimension is held fixed as \(bh\), the complexity scales linearly w.r.t the block size.

Table 2 compares the extrapolation ability of our model with other LRNN baselines on regular language tasks. As we can see, the proposed model, Eq. (5) and (6), is the only LRNN that can extrapolate on regular language.

S4 and S4D cannot extrapolate on **ModArith**. This is expected as Prop. 1 shows that S4 and S4D cannot represent subtraction due to their input-independent transition matrices. As for Liquid-S4, although it uses input-dependent block matrices (discussed in SS3.1), it still cannot extrapolate on regular language. We believe this can be explained by its low expressiveness (Eq. (4)) compared to the proposed model (Eq. (5) and (6)). Overall, we can see that the combination of input dependency and sufficient expressiveness plays an important role in terms of regular language modeling.

## 5 Conclusion

In this work, we explore linear RNNs in the realm of regular language modeling. We discover that existing LRNN models cannot represent subtraction and in turn propose a new LRNN equipped with a block-diagonal and input-dependent transition matrix. Our experiments confirm the proposed model's ability to model regular language tasks like Sum, Even Pair, and Modular Arithmetic under the challenging length extrapolation setting.
