Zoology: Measuring and Improving Recall in Efficient Language Models โ€” part 12/14

Cleaned-text code points [170241, 185795). Bibliography omitted here; source.md includes it.

In total, we only need ๐’ช โก ( c 2 ) \mathcal{O}(c^{2}) -many parameters to perform these multiplications and the linear bias in the first layer is a hyperparameter that is static and unlearned; the time complexity comes from the multiplication of ๐๐Š โŠค {\bf QK^{\top}} in ๐’ช โก ( N 2 โ€‹ c ) {\mathcal{O}}(N^{2}c) , and projections in ๐’ช โก ( N โ€‹ c 2 ) {\mathcal{O}}(Nc^{2}) . Finally, we only need ๐’ช โก ( 1 ) \mathcal{O}(1) layers for this solution. โˆŽ

In the sequel, we develop a parallel algorithm to solve the multiple-query associative recall problem with ๐’ช โก ( N โ€‹ d โ‹… log 2 โก N ) \mathcal{O}(Nd\cdot\log^{2}{N}) work complexity and ๐’ช โก ( d โ‹… log 2 โก N ) \mathcal{O}(d\cdot\log^{2}{N}) time. We then convert the algorithm into a BaseConv model via the route of arithmetic circuits, which then solves the multiple-query associative recall problem with ๐’ช ~ โ€‹ ( 1 ) \tilde{\mathcal{O}}(1) layers and ๐’ช ~ โ€‹ ( N โ€‹ d ) \tilde{\mathcal{O}}(Nd) parameters.

#### H.7.3 Initial Attempt: A Sequential Algorithm

We will first discuss the algorithm that simply uses an associative array to solve the multiple-query associative recall problem. Specifically, we want to use a data structure that allows for logarithmic insertion and membership query. Here, we do not specify a choice but data structures including self-balancing binary search trees which allow for ๐’ช โก ( log โก N โ‹… d ) \mathcal{O}(\log{N}\cdot d) ๐š’๐š—๐šœ๐šŽ๐š›๐š {\tt insert} and ๐š๐š’๐š—๐š {\tt find} operations for d d -bit entries should be sufficient.

###### Proposition H.28 .

Algorithm 9 solves the multiple-query associative recall problem ( Mqar ) \left(\textsc{Mqar}\right) in ๐’ช โก ( d โ€‹ N โ€‹ log โก N ) \mathcal{O}(dN\log{N}) time for an input sequence ๐ฎ โˆˆ { 0 , 1 } N ร— d \bm{u}\in\{0,1\}^{N\times d} .

###### Proof.

For any i โˆˆ { 0 , โ€ฆ , N 3 โˆ’ 1 } i\in\{0,\ldots,\frac{N}{3}-1\} , we know that both insertion and lookup operations take ๐’ช โก ( log โก ( i ) โ‹… d ) \mathcal{O}(\log(i)\cdot d) time. Overall, the runtime of the algorithm is โˆ‘ i = 0 N 3 โˆ’ 1 ๐’ช โก ( log โก ( i ) โ‹… d ) = ๐’ช โก ( log โก ( N ! ) โ‹… d ) = ๐’ช โก ( N โ€‹ log โ€‹ N โ‹… d ) . \sum_{i=0}^{\frac{N}{3}-1}\mathcal{O}(\log(i)\cdot d)=\mathcal{O}(\log(N!)\cdot d)=\mathcal{O}(N\log{N}\cdot d). โˆŽ

#### H.7.4 Algorithm via Parallel Binary Search

Our plan is to convert the algorithm for solving the multiple-query associative recall problem in the RAM model into an arithmetic circuit, which by Theorem H.21 will lead to a BaseConv model that solves the multiple-query associative recall problem. With respect to Algorithm 9 , it may be the case that the arithmetic circuit has a large number of layers ฮฉ โก ( N ) \Omega(N) . Unfortunately, this would imply that the resulting BaseConv model may have near quadratic complexity in N N . Instead, we now initiate our effort into designing a BaseConv model with both small enough number of parameters and number of layers. Here, we will first subdivide the problem using dyadic intervals into ๐’ช โก ( N ) \mathcal{O}(N) subproblems and reduce each such subproblem into a multiple search problem [ Akl and Meijer, 1990 ] . To this end, we briefly introduce the multiple search problem below. Given two array of numbers A โ‰œ a 0 โ‰ค โ€ฆ โ‰ค a n โˆ’ 1 A\triangleq a_{0}\leq\ldots\leq a_{n-1} and B โ‰œ ( b 0 โ‰ค โ€ฆ โ‰ค b m โˆ’ 1 ) B\triangleq(b_{0}\leq\ldots\leq b_{m-1}) with n โ‰ค m n\leq m , for each a j โˆˆ A a_{j}\in A , the goal is to find the smallest element in B B that is larger than or equal to a j a_{j} . The multiple search problem is solved by a parallel binary search ( pbs ) algorithm in [ Akl and Meijer, 1990 ] with work complexity ๐’ช โก ( n โ‹… log โก m ) \mathcal{O}(n\cdot\log{m}) and time ๐’ช โก ( log โก n โ€‹ log โก m ) \mathcal{O}(\log{n}\log{m}) . Specifically, for sorted arrays A [ 0 โ‹ฏ n โˆ’ 1 ] A[0\cdots n-1] and B [ 0 โ‹ฏ m โˆ’ 1 ] B[0\cdots m-1] , ๐š™๐š‹๐šœ {\tt pbs} constructs the array C [ 0 โ‹ฏ n โˆ’ 1 ] C[0\cdots n-1] defined as C โก [ i ] โ‰œ { min 0 โ‰ค j < m { j | A [ i ] โ‰ค B [ j ] } if โ€‹ A โ€‹ [ i ] โ‰ค B โก [ m โˆ’ 1 ] m otherwise. C[i]\triangleq\begin{cases}\min_{0\leq j<m}\{j\lvert\ A[i]\leq B[j]\}&\text{if }A[i]\leq B[m-1]\\ m&\text{otherwise.}\end{cases} (12) The algorithm itself runs in exclusive-read exclusive-write (EREW) PRAM modelโ€”no two processors are allowed to read from or write into the same memory location at the same time.

We now augment the algorithm copied from [ Akl and Meijer, 1990 ] for our purposes below.

Let ฮฃ \Sigma be the set { 0 , 1 } \{0,1\} and denote the set of binary strings of size n n as ฮฃ n \Sigma^{n} . We define prefix โก ( ๐’™ ) \mathrm{prefix}(\bm{x}) for n n -bit strings as the set of all initial substrings of ๐’™ โˆˆ ฮฃ n \bm{x}\in\Sigma^{n} which includes the empty string and ๐’™ \bm{x} itself. Next, let dec : { 0 , 1 } n โ†’ โ„• \mathrm{dec}:\{0,1\}^{n}\to\mathbbm{N} be the decimal representation of an n n -bit string ๐’™ \bm{x} with ๐’™ โก [ 0 ] \bm{x}[0] denoting the least significant bit. We also use ๐šœ๐š˜๐š›๐š โก ( A ) {\tt sort}(A) as a procedure that sorts an array A A . Finally, wlog, we assume that N N is a power of 2 2 . We are now ready to present a parallel algorithm that solves the multiple-query associative recall problem below.

###### Remark H.29 .

In lines 8 and 11 , we keep track of the sorted permutation of the indices of keys and queries, respectively. This helps us in the retrieval of the index of the matching key as in line 17 .

#### H.7.5 Correctness and Complexity

###### Proposition H.30 .

Algorithm 11 solves the multiple-query associative recall problem with work complexity ๐’ช โก ( N โ€‹ d โ‹… log 2 โก N ) \mathcal{O}(Nd\cdot\log^{2}{N}) and time ๐’ช โก ( d โ‹… log 2 โก N ) \mathcal{O}(d\cdot\log^{2}{N}) .

###### Proof.

The correctness of pbs implies the correctness of Algorithm 11 if we can show that, for each 1 โ‰ค i < N 3 1\leq i<\frac{N}{3} , we check for a match among the keys { ๐’Œ j } j โˆˆ [ i โˆ’ 1 ] \{\bm{k}_{j}\}_{j\in[i-1]} . To this end, for each 1 โ‰ค i < N 3 1\leq i<\frac{N}{3} , let the set of all iterator indices associated with an index i i be defined as K i โ‰œ { ( k , x ) | i โˆˆ J k ๐’™ } K_{i}\triangleq\{(k,x)\lvert i\in J_{k}^{\bm{x}}\} with J k ๐’™ J_{k}^{\bm{x}} as noted in line 10 . Then, we define the corresponding set for keys as โ„ i โ‰œ โ‹ƒ ( k , ๐’™ ) โˆˆ K i I k ๐’™ โ€‹ with I k ๐’™ s defined as in line 7 . \mathcal{I}_{i}\triangleq\bigcup_{(k,\bm{x})\in K_{i}}I_{k}^{\bm{x}}\text{ with $I_{k}^{\bm{x}}$s defined as in line \ref{step: i-def}}. That is, for all the calls to pbs-key-values that i i is part of (given by K i K_{i} ) where the algorithm checks for a match among the keys in โ„ i \mathcal{I}_{i} , it then suffices to show that โ„ i = [ i โˆ’ 1 ] \mathcal{I}_{i}=[i-1] .

Here, first note that if some index j โˆˆ I k ๐’™ โІ โ„ i j\in I_{k}^{\bm{x}}\subseteq\mathcal{I}_{i} for some ๐’™ โˆˆ ฮฃ log โก ( N 3 ) โˆ’ k \bm{x}\in\Sigma^{\log\left(\frac{N}{3}\right)-k} , then, by definition, ๐’™ โˆˆ prefix โก ( bin โก ( j ) ) \bm{x}\in\mathrm{prefix}(\mathrm{bin}(j)) . Here, let ๐’™ 1 := ๐’™ | ๐’™ โก [ log โก ( N 3 ) โˆ’ k ] = 1 \bm{x}^{1}:=\bm{x}\lvert_{\bm{x}[\log\left(\frac{N}{3}\right)-k]=1} where we set the ( log โก ( N 3 ) โˆ’ k ) (\log\left(\frac{N}{3}\right)-k) -th index of ๐’™ \bm{x} to be 1. Consequently, as we have i โˆˆ J k ๐’™ i\in J^{\bm{x}}_{k} for the same k k and ๐’™ \bm{x} as in I k ๐’™ I_{k}^{\bm{x}} ( cf. line 7 ), we must have ๐’™ 1 โˆˆ prefix โก ( bin โก ( i ) ) \bm{x}^{1}\in\mathrm{prefix}(\mathrm{bin(i)}) . Thus we get j < i j<i , whence we can claim that โ„ i โІ [ i โˆ’ 1 ] \mathcal{I}_{i}\subseteq[i-1] .

For the other direction, for any i i , let b b denote the position of the most significant bit in bin โก ( i ) \mathrm{bin}(i) which differs from bin โก ( j ) \mathrm{bin}(j) for any j โˆˆ [ i โˆ’ 1 ] j\in[i-1] . Then, there must exist a binary string that is in the prefix set of both bin โก ( i ) \mathrm{bin}(i) and bin โก ( i ) \mathrm{bin}(i) . That is, there exists ๐’™ โˆˆ prefix โก ( bin โก ( i ) ) โˆฉ prefix โก ( bin โก ( j ) ) \bm{x}\in\mathrm{prefix}(\mathrm{bin}(i))\cap\mathrm{prefix}(\mathrm{bin}(j)) with ๐’™ โˆˆ ฮฃ b \bm{x}\in\Sigma^{b} . Thus, we then must have bin โก ( j ) โˆˆ I log โก ( N 3 ) โˆ’ b ๐’™ \mathrm{bin}(j)\in I^{\bm{x}}_{\log\left(\frac{N}{3}\right)-b} and bin โก ( i ) โˆˆ J log โก ( N 3 ) โˆ’ b ๐’™ \mathrm{bin}(i)\in J^{\bm{x}}_{\log\left(\frac{N}{3}\right)-b} with ๐’™ \bm{x} as the corresponding witness. Hence, we have [ i โˆ’ 1 ] โІ โ„ i [i-1]\subseteq\mathcal{I}_{i} .

Overall, we have shown that โ„ i = [ i โˆ’ 1 ] \mathcal{I}_{i}=[i-1] . Since this holds for all 1 โ‰ค i < N 3 1\leq i<\frac{N}{3} , we can conclude that Algorithm 11 solves the multiple-query associative recall problem.

Next, it is easy to see that we execute lines 7 to 17 โˆ‘ k = 0 log โก ( N 3 ) โˆ’ 1 N 3 2 k + 1 \sum_{k=0}^{\log\left(\frac{N}{3}\right)-1}\frac{\frac{N}{3}}{2^{k+1}} -many times. We note that sorting n n values each of size d d can be done with work complexity n โ€‹ log โก n โ‹… d n\log{n}\cdot d . We note that, at each instance, we are sorting sorting 2 k 2^{k} values. Meanwhile, remembering 2 k 2^{k} sorted permutation of indices can be done in linear time using arrays. Moreover, each call to pbs-key-values has n = m = 2 k n=m=2^{k} which has work complexity n โ€‹ log โก m n\log{m} . Finally, we know that the work complexity of lines 19 to 21 is ๐’ช โก ( N ) \mathcal{O}(N) . Thus, the overall work complexity of Algorithm 11 is d โ‹… โˆ‘ k = 0 log โก ( N 3 ) โˆ’ 1 N 3 2 k + 1 โ€‹ ๐’ช โ€‹ ( 2 k โ‹… log โก 2 k ) = d โ‹… ๐’ช โก ( N ) โ‹… โˆ‘ k = 0 log โก N / 3 โˆ’ 1 ๐’ช โก ( k ) = ๐’ช โก ( N โ€‹ d โ‹… log 2 โก N ) . d\cdot\sum_{k=0}^{\log\left(\frac{N}{3}\right)-1}\frac{\frac{N}{3}}{2^{k+1}}\mathcal{O}(2^{k}\cdot\log{2^{k}})=d\cdot\mathcal{O}(N)\cdot\sum_{k=0}^{\log{N/3}-1}\mathcal{O}(k)=\mathcal{O}(Nd\cdot\log^{2}{N}). (13)

We will now analyze the depth of Algorithm 11 . We know that the depth of computation for Algorithm 10 is ๐’ช โก ( log โก n โ€‹ log โก m ) \mathcal{O}(\log{n}\log{m}) for input sizes. Moreover, we have ๐’ช โก ( 1 ) \mathcal{O}(1) depth for the computation in 19 to 21 as each entry in ๐š’๐š๐šก {\tt idx} can have at most one entry. Since the nested for loops iterating over k k s and the associated ๐’™ \bm{x} s runs in parallel, the depth of Algorithm 11 is dominated by the largest depth among all calls to pbs-key-values and to sort . The largest such call to pbs-key-values is of size n = m = 2 log โก ( N 3 ) โˆ’ 1 = N / 6 n=m=2^{\log\left(\frac{N}{3}\right)-1}=N/6 which yields a depth of d โ‹… log 2 โก N 3 d\cdot\log^{2}{\frac{N}{3}} . Moreover, using sorting networks ( Definition H.34 ), we know that the largest depth is for sorting N 6 \frac{N}{6} values of size d d given by d โ‹… ฮ˜ โก ( log โก N ) d\cdot\Theta(\log{N}) ( Lemma H.35 ). Thus, we can conclude that Algorithm 11 takes ๐’ช โก ( d โ‹… log 2 โก N ) \mathcal{O}(d\cdot\log^{2}{N}) , time where N N is the length of the input. โˆŽ

#### H.7.6 Conversion to Arithmetic Circuit

We will convert Algorithm 11 to an arithmetic circuits modularly. In particular, after writing out an explicit circuit for Algorithm 10 , we will uses this circuit as a black-box along with circuits for sorting networks.

##### Circuit for pbs-key-values .

We will denote the corresponding arithmetic circuit for Algorithm 10 as pbs-key-values as well with the input gates comprising of each entry from ๐’’ [ s โ‹ฏ t ] \bm{q}[s\cdots t] and ๐’Œ [ x โ‹ฏ y ] \bm{k}[x\cdots y] and the i i -th output gate yielding the value C โก [ i ] C[i] as in Algorithm 10 .

Here, we first convert the comparisons for the if statements in Algorithm 10 . To this end, we briefly introduce comparators.

###### Definition H.31 (Comparators) .

A comparator is a device with inputs ๐’™ \bm{x} and ๐’š \bm{y} and outputs ๐’™ โ€ฒ \bm{x}^{\prime} and ๐’š โ€ฒ \bm{y}^{\prime} , that performs the following function: ๐’™ โ€ฒ \displaystyle\bm{x}^{\prime} = min โก ( ๐’™ , ๐’š ) , \displaystyle=\min(\bm{x},\bm{y}), ๐’š โ€ฒ \displaystyle\bm{y}^{\prime} = max โก ( ๐’™ , ๐’š ) . \displaystyle=\max(\bm{x},\bm{y}).

Using comparators, we can use bit-wise XOR and AND to define the result of the comparisons in lines 6 and 11 as the following fixed variables: โ„“ x \displaystyle\ell_{x} : = ๐Ÿ™ { ๐’’ [ mid ] โ‰ค ๐’Œ x } , \displaystyle:=\mathbbm{1}\{\bm{q}[\mathrm{mid}]\leq\bm{k}_{x}\}, (14) g y \displaystyle g_{y} : = ๐Ÿ™ { ๐’’ [ mid ] > ๐’Œ y } , \displaystyle:=\mathbbm{1}\{\bm{q}[\mathrm{mid}]>\bm{k}_{y}\}, z \displaystyle z : = bin-search โ€‹ ( { ๐’Œ i } i = s t , ๐’’ โก [ mid ] ) . \displaystyle:=\texttt{bin-search}({\{\bm{k}_{i}\}_{i=s}^{t},\ \bm{q}[\mathrm{mid}]}). This then allows us to infer the index of the key array for each recursive call to pbs-key-values in lines 9 , 14 , 19 , and 20 from Algorithm 10 . Specifically, let z s z_{s} and z t โˆ’ 1 z_{t}-1 denote the starting and ending indices for the keys as inputs to the recursive calls in Algorithm 10 ) below: pbs-key-values ( ๐’’ [ mid + 1 โ‹ฏ t ] , ๐’Œ [ z t โ‹ฏ y ] ) ; (lines 9 and 20 ) \displaystyle\texttt{pbs-key-values}(\displaystyle\bm{q}[\mathrm{mid}+1\cdots t],\ \bm{k}[z_{t}\cdots y]);\text{ (lines \ref{step: rec-1} and \ref{step: right})} (15) pbs-key-values ( ๐’’ [ s โ‹ฏ mid โˆ’ 1 ] , ๐’Œ [ x โ‹ฏ z s โˆ’ 1 ] ) ; (lines 14 and 19 ) \displaystyle\texttt{pbs-key-values}(\bm{q}[s\cdots\mathrm{mid}-1],\ \bm{k}[x\ \cdots z_{s}-1]);\text{ (lines \ref{step: rec-2} and \ref{step: left})} (16) Here, z t z_{t} and z s z_{s} can assume values dependent on the results of the comparisons in lines 6 and 11 . Specifically, we have z t \displaystyle z_{t} = โ„“ x โ‹… x + ( 1 โˆ’ โ„“ x ) โ€‹ ( 1 โˆ’ g y ) โ‹… z = { x if โ€‹ ๐’’ โ€‹ [ mid ] โ‰ค ๐’Œ x โ€‹ (line 9 ) z if โ€‹ ๐’’ โ€‹ [ mid ] โˆˆ ( ๐’Œ x , ๐’Œ y ] โ€‹ (line 20 ) 0 otherwise , \displaystyle=\ell_{x}\cdot x+(1-\ell_{x})(1-g_{y})\cdot z=\begin{cases}x&\text{if }\bm{q}[\mathrm{mid}]\leq\bm{k}_{x}\text{ (line \ref{step: rec-1})}\\ z&\text{if }\bm{q}[\mathrm{mid}]\in(\bm{k}_{x},\bm{k}_{y}]\text{ (line \ref{step: right})}\\ 0&\text{otherwise}\end{cases}, z s \displaystyle z_{s} = g y โ‹… ( y + 1 ) + ( 1 โˆ’ โ„“ x ) โ€‹ ( 1 โˆ’ g y ) โ‹… z = { y + 1 if โ€‹ ๐’’ โ€‹ [ mid ] > ๐’Œ y โ€‹ (line 14 ) z if โ€‹ ๐’’ โ€‹ [ mid ] โˆˆ ( ๐’Œ x , ๐’Œ y ] โ€‹ (line 19 ) 0 otherwise . \displaystyle=g_{y}\cdot(y+1)+(1-\ell_{x})(1-g_{y})\cdot z=\begin{cases}y+1&\text{if }\bm{q}[\mathrm{mid}]>\bm{k}_{y}\text{ (line \ref{step: rec-2})}\\ z&\text{if }\bm{q}[\mathrm{mid}]\in(\bm{k}_{x},\bm{k}_{y}]\text{ (line \ref{step: left})}\\ 0&\text{otherwise}\end{cases}. Here, z s z_{s} or z t z_{t} getting a value 0 0 signifies that the branch is dead, and we do not execute the recursive call.

Finally, let the arrays C t [ mid + 1 โ‹ฏ t ] C_{t}[\mathrm{mid}+1\cdots t] and C s [ s โ‹ฏ mid โˆ’ 1 ] C_{s}[s\cdots\mathrm{mid}-1] denote the outputs to the recursive calls in equation 15 and equation 16 , respectively. We can then succinctly express the outputs for each index of the output array C C as C โก [ i ] = { โ„“ x โ‹… x + z s โ‹… ( C 1 โ€‹ [ i ] ) i โˆˆ [ s โ‹ฏ mid โˆ’ 1 ] g y โ‹… ( y + 1 ) + z t โ‹… ( C 2 โ€‹ [ i ] ) i โˆˆ [ mid + 1 โ‹ฏ t ] โ„“ x โ‹… x + ( 1 โˆ’ โ„“ x ) โ€‹ ( 1 โˆ’ g y ) โ‹… z + g y โ‹… ( y + 1 ) i = mid C[i]=\begin{cases}\ell_{x}\cdot x+z_{s}\cdot(C_{1}[i])&i\in[s\cdots\mathrm{mid}-1]\\ g_{y}\cdot(y+1)+z_{t}\cdot(C_{2}[i])&i\in[\mathrm{mid}+1\cdots t]\\ \ell_{x}\cdot x+(1-\ell_{x})(1-g_{y})\cdot z+g_{y}\cdot(y+1)&i=\mathrm{mid}\\ \end{cases} (17)

We can thus state the circuit schematically in Fig. 9 .

Now, before accounting for the complexity of the circuit for Algorithm 10 , we must first assert the complexity of the comparators that we use in Fig. 9 .

###### Lemma H.32 ( Cormen et al. [2022] ) .

For binary strings ๐ฑ , ๐ฒ โˆˆ ฮฃ d \bm{x},\bm{y}\in\Sigma^{d} of length d d , there exists a comparison network of size ๐’ช โก ( d ) \mathcal{O}(d) , width ๐’ช โก ( d ) \mathcal{O}(d) , and depth ๐’ช โก ( log โก d ) \mathcal{O}(\log{d}) .

We use this lemma to compute the complexity of the arhmetic circuit from Fig. 9 .

###### Proposition H.33 .