WorksComputer Science
Enhancing Attention with Higher-Order Attention
Abstract
Current decoder large language models rely on a combination of attention, feed forward and residual layers. The attention used is autoregressive (AR) though this is not strictly correct, as we note. That motivates the introduction of other attention methods that, while also not strictly AR, do enhance the AR character. One is based on a decomposition of mutual information (MI) into a set of Conditional MIs (CMIs) that correlate key tokens with pairs of query tokens. A second, higher-order attention, introduces correlations between contiguous key and query tokens. A third introduces correlations between a set of key and query tokens and uses a Generalized Maximum Entropy Principle to obtain an attention expression spanning a sequence of tokens. All these methods may enhance the information content of the autoregressive probabilities that are used in inference/generation. Applications to contrastive decoding and a systematic analysis of autoregressive probabilities are presented.
Introduction
Attention-based decoder large language Models (LLMs), with their autoregressive character, are currently the dominant method for training language models [1, 2]. Each layer’s module consists of attention (ATTN) and MLP/FFN contributions, both summed with a residual connection and layer normalization. As emphasized in e.g. Elhage et al. [3], ATTN mixes tokens in such a way that accounts for their sequence. The non-mixing MLP/FFN incorporates abstract token interactions not relying on strict sequence order.
In this work, our focus is on the (self) ATTN contribution to a decoder LLM
where, for a particular query , the conditional probabilities of the keys are summed over the tokens as introduced by the value vectors, . The queries, keys and values are all from the same token set, , from a vocabulary of size , in a -length context. The conditional probabilities (attention weights) are obtained by optimization of
For positionally embedded tokens, query, key and value vectors are respectively parameterized as: ; ; and output with . Neglecting biases, the scheme is parameterized using matrices: denoting, respectively, embedding, query, key, value, output, unembedding matrices. (In the following we suppress the embed dimension -factor for convenience.)
The autoregressive character of the scheme is embodied in the linear, additive expression of Eq. (1). However, what is being approximated by Eq. (1) is the formal autoregressive decomposition [4] of the -random variable (RV) probability over the -length window:
The negative log-likelihood loss (NLL)
is the typical function optimized in training a LLM [5]. To try to connect with the standard attention expression in Eq. (1) make a Markov Chain (MC) approximation [6] to Eq. (1.3):
This is not a Markov process but rather a local approximation to the exact autoregressive decomposition of . It is the lowest-level approximation to Eq. (1.3a) that keeps token correlations. If these pair conditionals were known, then the chain’s probability could be sampled. In the LLM machine learning context,
could be optimized by NLL gradient descent. Clearly, this is not what is optimized by NLL gradient descent of the probability , with sm denoting the softmax function, obtained from Eq. (1). Thus, one must conclude that attention as currently formulated is, at best, “pseudo-autoregressive”, and suggests that methods incorporating other correlation could be useful.
The idea is to introduce more correlation into the conditional or the unconditional parts of autoregressive probability to the training of a LLM. In Section 2, we do so via conditional mutual information and focus on where are key and () and are query tokens. In Section 3 we introduce higher-order attention of the form and further generalizations of the number of keys. In Section 4, we use trained probabilities () (with as a practical matter) along with a Generalized Maximum Entropy Procedure (GMEP) as a post-training phase to extend the autoregressive probability to a sequence of length using .
It is essential to realize for the attention versions relying on the that the identity
shows that there would be no difference with using the standard attention expressions on the right-hand side of the above versus that on the left-hand side. That would be the case under two conditions: 1) That training was exact and 2) that the NLL objective function did represent the linear combination over values of the attention method. With regard to item 1), the parameterization intrinsic to neural networks means that for the left and right sides of Eq. (7), one obtains, respectively, and , with manifestly different parameterizations: and .
Derivation of higher-order attention based on Conditional Mutual Information
We summarize, and manipulate for our purposes, expressions from Cover and Thomas’ “Elements of Information Theory” book [4]. The mutual information between RVs and is defined in terms of entropy and conditional entropy as
Straightforward extension of Eq. (8) to the RV set is
Using the autoregressive probability decomposition in Eq. (1.3a), chain rules for entropy and conditional entropy follow immediately:
and
Their difference
expresses the MI as a sum of conditional mutual information (CMI) terms
It is a “chain rule for information” [4] relating the MI on left hand side to a sum of CMIs on the right-hand side of Eq. (13) and follows from the autoregressive decomposition in Eq. (1.3a).
These conditional MIs are “natural” extension of the CMI to composite RVs , with explicit form
Note that the expectation is over the distribution of all the RVs. These CMIs are the reduction in uncertainty in due to knowledge of with the () given. For purposes of optimization, decompose Eq. (14) as
with
Note that the unconditional aren’t involved in the optimization. As a sensible choice for RV , set , the next token in sequence, to obtain .
To implement the above, as in the transition from Eq. (1.3b) to Eq. (1); that is, from negative log likelihood to attention, make the replacement to produce
That leads to the replacement , along with the other two conditional probabilities in Eq. (2.7b). In Eq. (17), represents each key token, and and () are a pair of query tokens. To be minimized is the conditional mutual information (the Kullback-Lieber distance) between and . The resulting parameterized ’s are used to create an analogous to Eq. (1) attention-like expression:
In this view, the probabilities of tokens (with ) conditional on pairs of query tokens (between and ) are to be obtained in similar fashion to the construction of the conventional attention in Eq. (1). The added correlations are between query tokens separated as indicated in Eq. (18). This long-range correlation along the sequence may reinforce attention.
A parameterized expression for this attention expression is, via token embedding,
Derivation of higher-order attention based on local Markov chains
An alternative approach to enhancing attention starts from the formal decomposition in Eq. (1.3a) that we now write for composite RVs. To that end, partition integer sequence into slices with descending-ordered consecutive integers per slice. Let indicate the remaining descending-ordered integers to that . Then (excluding the last, singlet probability term)
with the corresponding Markov chain approximation
For example: ; ; has 4 slices
and
For example: ; ; has 5 slices
and
It would be impractical to deal with long Markov chains, as each additional conditional probability requires a partition function as a normalizing factor in the softmax construction. Thus, apply this MC idea at a low order to build higher-order attention. Namely,
This latter expression symbolizes the number of keys () and queries that will be needed to produce attention with increasing degrees of token correlation. Thus, at lowest MC order,
where STD denotes the standard attention [5].
With the assumption that , the implicit autoregressive STD autoregressive attention expression introduces new correlations between keys:
where the singlet MC introduces neighbor key correlations separated from the query tokens per the attention paradigm. Note that the singlet MC can also be formulated via
as follows from the autoregressive identity followed by the MC . The singlet requires a higher-order attention-like term, . The obtained from Eq. (28) is a representation of that from Eq. (29). With the attention paradigm, both versions of imply, with Eq. (7), that , with their incorporation of neighbor-token correlation. The downside of the, in principle, more accurate singlet expression in Eq. (29) is that it scales with versus the of the singlet MC in Eq. (28).
Proceeding systematically to higher order in key token correlation
The scaling is per query, thus still of the complexity of conventional attention. The scheme can be extended to triplet MC, and beyond. Note that, owing to the multiplicative MC form, back propagation is done with each term’s gradient multiplied by the remaining probabilities.
The requisite probabilities can be parameterized in “multiplicative” and “additive” versions.
The multiplicative versions are:
with
; ; ; .
(For the term, we introduce a new, learnable matrix and a model-dimensioned column vector ).
The additive versions are:
with .
The different information contents of the underlying determinants of attention are readily obtained from the following (manifestly positive) mutual information:
Once the parameters of are obtained, the above denominator can be back calculated using Eq. (32), putting the ingredients of this mutual information onto a consistent basis. In Eq. (33) we left the expectation conditional on the query token to allow investigation of the information content conditional on a query token.
Increasing the attention order with Generalized MEP (GMEP)
In order to increase , the length of the token sequence considered, a Maximum Entropy Principle (MEP), introduced to Statistical Mechanics by Jaynes [7, 8, 9], can be invoked. The MEP maximizes entropy with suitable constraints on the various probabilities enforced by Lagrange Multipliers (LMs). MEP procedures were introduced to sequence-based data [10], and generalized in various ways [11]. One of them, the generalized MEP (GMEP), can be tailored to LLM inference.
First, train the model to obtain the () (with ) and use the attention mechanism in Eq. (29) along with Eq. (7) to obtain for token pairs with . While requiring additional calculation relative to in Eq. (29), the s under consideration can’t be too large (e.g. ) as their use for even e.g. Look Ahead Greedy Search (L.A.G.S.) would become too expensive for large .
Thus, do GMEP on to obtain over sequences of length , as follows. The constraint equation on the target distribution , for token pair is
with and .
The Lagrangian is
with Lagrange multipliers: and .
The Euler-Lagrange equation is
Noting that will generate the above-sum of Lagrange Multiplier terms, the Euler-Lagrange equation can be compacted to
The scaling of this MEP follows from the Lagrange Multipliers to be determined for the vocabulary . With a candidate set (see below for a method to choose it), such that , the scaling is order . Optimization schemes such as the well-known Newton-Krylov method [12] are appropriate and, for large-scale systems, there are sophisticated methods available [13]. Two approaches that focus on the structure of the probability distributions can be used to speed up the calculation. Using PCA/SVD to obtain modes reduces the part to . Using Rényi entropy (with ), versus Gibbs-Shannon entropy, reduces the value by further focusing on (the desired) higher probability parts of the distribution.
Once the Lagrange Multipliers are obtained, the optimized values in Eq. (37)
are the desired order- token sequence probabilities. Of course, compared to training, this operation is done only once.
The Eq. (38) probabilities can be used for inference/generation. Over some context
In the LM solution there are redundancies to eliminate. The pair probabilities satisfy the row-based (the last row is fixed) conditions:
Alternatively, they can be formulated as column-based (the last column is fixed)
When decoding, as a practical matter, a scheme most likely must be used to restrict the state space to a manageable size. § 5 provides the series of inequalities:
for any token from the vocabulary . The is an upper bound to all the conditional probabilities. Thus, pick the cut-off, candidate set of using – i.e., just from the vocabulary. Then, for an e.g., L.A.G.S. of length , the decoding scaling is . That can turn an impractical search into a practical search for both higher-order attention and, especially, the GMEP scheme.
Application to Contrastive Decoding
The different methods presented in Sections 2-§ 3 are quite natural for Contrastive Decoding (CD). [14] CD invokes two or more models to be used at inference/generation where one is considered the Expert (E) and the other(s) the amateur(s)/anti-expert(s) (A,...). The approach is based on a score for the 'th token at position in a sequence:
with an adjustable parameter.
For any , the score is normed to the CD probability:
A token with high-to-moderate E and low A probability boosts the CD-based probability relative to its E value, while high-to-moderate E and high A probability suppresses the CD-based probability relative to its E value. Note that this scheme is based on the E probabilities; thus, they are in the high to middling range, while the corresponding A probabilities can range from high to low.
CD can lead to a reduction in repetitive text and reduce hallucinations, [15, 16, 17] and improve common sense reasoning [18]. Operating at inference/generation versus training is much less expensive than model development. However, when based on ATTN, it is intrinsically local to each position in a sequence versus optimized over a sequence. Our GMEP method, with its focus on sequence, may improve the quality of CD. To implement it for sequences, suppose the expert is taken from GMEP and the anti-expert from ATTN.
To generate the sequences for the E and A models in the simplest application consider using –length L.A.G.S. for the A model:
and use the GMEP procedure for the E model.
Again, as a practical matter, use the restriction of the to a high probability set.
Using these respective probabilities, for the E and A models, will reorder the token probabilities from those of to those of according to Eq. (44). The scheme can be applied to nucleus (top-p) and beam search sampling methods at the expense of more overhead to evaluate and store candidates.
The scheme could be elaborated with E from the GMEP and two anti-experts: A1 H.O. ATTN and A2 ATTN.
Some implications of higher-order attention
As the degree of autoregression decreases, the corresponding entropy should increase owing to the greater freedom of probabilities with decreased autoregression. To show this property, write a series of approximations to the exact -point probability
where by Eq. (1.3a).
The corresponding entropies are
The conditional entropies satisfy the inequalities [10]
as can be obtained from Jensen’s inequality applied to the convex entropy. Therefore, with Eq. (49), the approximate entropy in Eq. (48) satisfies
Thus
with the last term in the chain of inequalities the independent random variable limit.
The above is an average statement, of course. A stronger, more interesting, result can be proved, as shown in Appendix § 7. Namely, as more conditional RVs in Eq. (47) are introduced, the difference between minimum and maximum probabilities increases, leading to the series of inequalities for the quantities defined in Eq. (47):
Increasing the conditionality of probabilities produces a greater spread in the min/max probability values. The min inequalities are particularly useful for systematically obtaining the probability cutoff of the vocabulary .
Now focus on the maximum conditional probabilities, as they are of greatest interest. The context- probability of Eq. (1.3a) using the ATTN mechanism relies on the conditional decomposition where the conditional terms are only known implicitly. Thus, to investigate the maximum inequalities of Eq. (52) a roundabout procedure is needed. Suppose you were to train on shorter, truncated contexts indexed by
The corresponding conditionals are
and can be obtained by marginalization:
However, as a practical matter, a method of evaluation of these high dimensional probabilities, such as top-K or its simplest, L.A.G.S., that provides an approximation to the maximum probability must be used. Designate the maximum conditioned probabilities using ‘max’ and those obtained by Greedy Search as ‘K=1’ (from top-K search).
Then it is evident, denoting as the maximizing value of RVs , that
Thus:
and
satisfies
Eqs. (59) and (52) lead to the “pairwise” comparisons
But the ‘chain’ of inequalities of Eq. (52) would be broken by (6.13). For example:
However, by definition, the L.A.G.S. approximation in fact sets the conditionals equal:
Thus, to this degree of approximation, which is driven by practicality, the chain of inequalities for the maxima in Eq. (52) hold equally well when using L.A.G.S..
As a convenient measure of the degree of loss of information with shortening the conditional ranges in Eq. (52) it is convenient to use (maximum) perplexity
or the negative of its log:
This produces a chain of inequalities:
A decrease in autoregression increases perplexity.
Concluding Remarks
There is a contradiction between the conventional attention expression with its linear combination of values, conditional on each query’s probability, and the autoregressive negative log-likelihood objective function that it is supposed to represent. They are not relatable by systematic approximation. The higher-order attention expressions introduced here are more correlated approximations to the NLL expression. Embedding more language structure in the training of the autoregressive probabilities that are then chained together for inference/generation may improve the coherence of the LLM output. Further, the empiricism inherent to training , or its MC approximation, with parameters , vs. training , with parameters , should provide a more accurate data representation in the former versus the latter. The various higher-order attentions developed here should produce systematically more specific probabilities with a richer account of language, though they may be impractical unless used for a restricted alphabet.
The first alternative attention in Sec. 2 is based on the CMI decomposition of the mutual information that follows from the fundamental probability decomposition underlying the NLL objective. It, too, uses a linear combination of values but incorporates other correlation via its dependence on query token pairs separated in sequence by all token pairs, as displayed in Eq. (18). The second alternative of § 2 is based on incorporating various levels of neighbor key token correlation. The third method in § 3 does require the expense of training with pairs of keys over a interval, and the overhead of running post-training GMEP. But it may provide a much more informative version of attention via .
Section 5 provides an application of higher-order attention to Contrastive Decoding. Sequence-based decoding can also provide an entrée to enhanced L.A.G.S. methods for use when resources are limited. Also, distillation [18], as well as other teacher-student focused methods, could profit from this sequence version of attention, as it may reduce exposure bias.
Section 6 presents a systematic exploration of the information content of the maxima of conditional probabilities/perplexity dependence on the extent of auto-regression. How the accuracy of next token prediction degrades with a decrease in autoregression may help in evaluating the quality of language models. referral
Appendix
In this Appendix we prove that:
and later straightforwardly generalize it to obtain the result underlying Eq. (52).
The proof relies on the law of iterated expectations:
meaning the expectation is of the function . Also of use is an indicator function: if
then if and otherwise. Applying the law of iterated expectation from above, with
where . Therefore
Thus, can be thought of as an average of over , given fixed and , implying that is less than the maximum and greater than the minimum of over , given fixed and .
To prove this supposition, focus on RV . Given any fixed and
by definition of minimum and maximum.
Thus,
with the first inequality by definition of minimum and
with the second inequality by definition of maximum. (Note that above is first for and then , so is same in both, and fixes .) Therefore
Note that is a constant and is a lower bound on and that is a constant and an upper bound of .
Therefore,
Thus the conditionals lower and upper bound the unconditional on probability, verifying Eq. (A.1).
To apply this result to () of Eq. (47), make the variable ‘mapping’:
From Eq. (A.10) and Eq. (A.1),
Applying the inequalities in Eq. (A.12) term-by-term to (noting that any individual term in Eq. (A.12) satisfies ) results in
which is Eq. (52). While Eq. (A.13) is exact its evaluation would be non-trivial except for short sequence lengths. For longer lengths, methods such as beam search [19] can only approximate and may violate these strict inequalities.
Acknowledgement
We thank Dr. Hongfeng Lou for the elegant proof of Eq. (A.1).
References
- [1]Mary Phuong and Marcus Hutter, “Formal Algorithms for Transformers”, arXiv:2207.09238v1 [cs.LG], (2022).arxiv.org/abs/2207.09238
- [2]Tianyang Lin, Yuxin Wang, Xiangyang Liu, and Xipeng Qiu, “A Survey of Transformers”, arXiv:2106.04554v2 [cs.LG], (2021).arxiv.org/abs/2106.04554
- [3]Nelson Elhage, Neel Nanda, Catherine Olsson, Tom Henighan, Nicholas Joseph, Ben Mann, Amanda Askell, Yuntao Bai, Anna Chen, Tom Conerly, Nova DasSarma, Dawn Drain, Deep Ganguli, Zac Hatfield-Dodds, Danny Hernandez, Andy Jones, Jackson Kernion, Liane Lovitt, Kamal Ndousse, Dario Amodei, Tom Brown, Jack Clark, Jared Kaplan, Sam McCandlish, Chris Olah, “A Mathematical Framework for Transformer Circuits”, 12.22.2021. https://transformer-circuits.pub/2021/framework/index.html.
- [4]Thomas. M. Cover and Joy. A. Thomas, Elements of Information Theory John Wiley & Sons, New York (1991).
- [5]Alec Radford, Karthik Narasimhan, Tim Salimans, and Ilya Sutskever, “Improving Language Understanding by Generative Pre-Training”, OpenAI Blog (2018).
- [6]Robert I. Cukier, “Generating Intrinsically Disordered Protein Conformational Ensembles from a Database of Ramachandran Space Pair Residue Probabilities Using a Markov Chain”, J. Phys. Chem. B. 122, 9087 (2018).DOI
- [7]E. T. Jaynes, “Information Theory and Statistical Mechanics”, Phys. Rev. 106, 620 (1957)DOI
- [8]E.T. Jaynes, Where Do We Stand on Maximum Entropy? in R.D. Levine, Tribus, M. (Ed.), The Maximum Entropy Formalism, MIT Press, Cambridge, MA, USA, 1979.
- [9]E.T. Jaynes, Proc. IEEE 70, 939 (1982) .
- [10]Hongfeng Lou and Robert I. Cukier, “A maximum entropy principle approach to a joint probability model for sequences with known neighbor and next neighbor pair probabilities”, Chemical Physics 538, 110872 (2020).DOI
- [11]Robert I. Cukier, “Maximum Entropy Principle (MEP) methods for Large Language Model Inference” (unpublished work).
- [12]D.A. Knoll and D.E. Keyes “ Jacobian-free Newton–Krylov methods: a survey of approaches and applications” Journal of Computational Physics 193, 357 (2004).DOI
- [13]https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.newton_krylov.html
- [14]Xiang Lisa Li, Ari Holtzman, Daniel Fried, Percy Liang, Jason Eisner, Tatsunori Hashimoto, Luke Zettlemoyer, and Mike Lewis. "Contrastive decoding: Open-ended text generation as optimization." arXiv preprint arXiv:2210.15097 (2022).arxiv.org/abs/2210.15097
- [15]Xun Liang, Shichao Song, Zifan Zheng, Hanyu Wang, Qingchen Yu, Xunkai Li, Rong-Hua Li, Yi Wang, Zhonghao Wang, Feiyu Xiong, Zhiyu Li. "Internal consistency and self-feedback in large language models: A survey." arXiv preprint arXiv:2407.14507 (2024).arxiv.org/abs/2407.14507
- [16]Max Peeperkorn, Tom Kouwenhoven, Dan Brown, and Anna Jordanous. "Mind the gap: Conformative decoding to improve output diversity of instruction-tuned large language models." arXiv preprint arXiv:2507.20956 (2025).arxiv.org/abs/2507.20956
- [17]Sean O’Brien, Mike Lewis “Contrastive Decoding Improves Reasoning in Large Language Models” arXiv:2309.09117v2 [cs.CL] 2023
- [18]Xiaohan Xu, Ming Li, Chongyang Tao, Tao Shen, Reynold Cheng, Jinyang Li, Can Xu, Dacheng Tao, Tianyi Zhou. “A Survey on Knowledge Distillation of Large Language Models“ arXiv:2402.13116v4 [cs.CL] (2024).arxiv.org/abs/2402.13116
- [19]Alexander M. Rush, Sumit Chopra and Jason Weston, “A Neural Attention Model for Abstractive Sentence Summarization”, arXiv:1509.00685v2 [cs.CL] (2015).arxiv.org/abs/1509.00685