Manuscript Offprint

arXiv:2609.03426

Lngram v2: Latent N-Gram Memory with Interpretable Discrete Representations

Yunao Zheng, Bin Wen, Xiaojie Wang

ABSTRACT

Transformers lack a native lookup mechanism, requiring repeated dense computation to recognize and reuse local static patterns. Lngram v1 introduces tokenizer-independent conditional memory through discrete latent n-gram addressing, but its memory capacity is coupled with the backbone width, limiting scalability due to high parameter and activation costs. We propose Lngram v2, which decouples the number of routes, memory dimension, and backbone width, and introduces a context-aware grouped-query attention readout to scale memory capacity independently. A zero-value Sink and counterfactual surrogate gradients further improve readout selectivity and routing trainability while preserving hard discrete addressing. Experiments across vision–language models (VLMs) of different scales show consistent improvements, including successful scaling to a 30B-parameter model. Compared with Lngram v1, Lngram v2 substantially reduces both total and activated memory parameters while maintaining or improving language modeling performance. Further analysis shows that its discrete IDs preserve substantial semantic structure of continuous hidden states, enabling semantic recovery from IDs alone and stable ID–semantic associations across datasets. These results establish Lngram v2 as an efficient and scalable latent conditional memory mechanism whose discrete addresses also provide a structured interface for analyzing internal model representations.

1 INTRODUCTION

Transformers have become the dominant backbone for large language and multimodal models (Vaswani et al., 2017; Devlin et al., 2019; Brown et al., 2020; Dosovitskiy et al., 2021; Radford et al., 2021; Alayrac et al., 2022). However, attention and feed-forward networks must handle both context-dependent reasoning and operations closer to conditional lookup, such as recognizing common multi-token entities and recurring local patterns. Since standard Transformers lack a native lookup mechanism, such patterns must still be approximated through dense computation, consuming model capacity that could otherwise support compositional reasoning (Cheng et al., 2026).

To separate local pattern matching from the backbone, Cheng et al. (2026) introduced conditional memory based on token-ID n-grams. Its addresses, however, depend on tokenizer IDs and fixed hashing, making the mechanism sensitive to tokenization boundaries, hash collisions, and difficult to extend uniformly beyond text. Lngram v1 instead discretizes hidden states into learnable symbols and constructs exact n-gram addresses in latent space, extending classical n-gram modeling (Brants et al., 2007) to neural representations and enabling conditional memory over textual, visual, and cross-modal inputs.

Lngram v1 nevertheless has two limitations. First, its route count is coupled with backbone width, causing memory size and readout cost to increase with model dimension and limiting scalability. Second, although discrete Lngram IDs directly determine memory addresses, it remains unclear whether these IDs preserve the semantic structure of the continuous representations from which they are derived. Existing representation-analysis methods typically require continuous activations together with probes or learned dictionaries (Alain & Bengio, 2016; Hewitt & Manning, 2019; Tenney et al., 2019; Bau et al., 2017; Huben et al., 2024). If Lngram IDs themselves retain semantic

¹ Beijing University of Posts and Telecommunications. ² Kuaishou Technology. * Project Leader. † Corresponding authors: Xiaojie Wang and Bin Wen.

information, they may provide a useful interface for both memory addressing and representation analysis.

We therefore propose Lngram v2, which decouples route count, memory dimension, and backbone width so that memory capacity can scale independently. Retrieved memories of different n-gram orders are organized as memory tokens, and the current hidden state performs context-dependent read-out through grouped-query attention (GQA) (Ainslie et al., 2023). A fixed zero-value Sink allows the model to suppress unreliable retrievals, while route-wise streaming controls intermediate activations at large scale. To train hard discrete routing, we use a counterfactual-lookup surrogate gradient that provides memory-dependent optimization signals while keeping the forward addressing path identical during training and inference, following the general principle of surrogate optimization for discrete decisions (Bengio et al., 2013).

We further study the semantics encoded in Lngram's discrete representations using an ID-only semantic reader that receives only the multi-route discrete signature. The reader retains 65.77%–84.27% of the excess semantic readout capability of continuous hidden states. On held-out data, individual route codes exhibit reproducible associations with concepts such as people, animals, vehicles, and common objects, while most semantic capacity arises from distributed combinations across routes. These results suggest that Lngram IDs serve not only as memory addresses but also as a statistically analyzable discrete interface to internal representations.

We evaluate Lngram v2 on vision–language models at multiple scales. Under matched training data, compute budgets, and backbone optimization settings, it consistently improves Keye2B and Keye30B and scales successfully to a 30B-parameter model. Parameter-matched Sparse FFN and Product Key Memory (PKM) baselines show that the gains cannot be explained by additional parameters or sparse capacity alone. Compared with Lngram v1, Lngram v2 reduces the module's total parameters by 82.6% and activated parameters per token by 95.2%, while maintaining or improving validation loss. Overall, Lngram v2 provides a scalable latent-space conditional memory mechanism together with a structured discrete interface for analyzing model representations.

References (62)