arXiv:2610.02909cs.LGOct 2026

Manuscript Offprint

arXiv:2610.02909

Constraint-Aware Training

Jinwoo Kim

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Abstract

When generating programs with language models, constrained decoding can apply program analyses to exclude tokens that violate syntax, scope, or typing rules. However, there is a duplication: standard training already teaches the model to suppress the tokens rejected by these analyses. This duplication leads to the question: if we will perform some analysis to filter a set tokens out during inference anyways, can we avoid teaching the model the said analysis altogether during training, and does this externalization lead to more efficient models? This paper defines a general constraint-aware objective satisfying this externalization desideratum and formalizes the benefits of externalization into three concrete theorems about model size and data efficiency. We show, through a controlled synthetic experiment, that the theorems survive training dynamics: constraint-aware training yields lower prediction loss at a matched parameter count and data compared to ordinary cross-entropy training, motivating training objectives that incorporate the analyses used during generation.

1 Introduction

Large language models (LLMs) are being increasingly used to generate outputs with a formal syntax and semantics: tool calls, programs, and proofs. In many cases, such outputs have a notion of validity: for example, only a small fraction of all strings are valid programs. For formal artifacts, this validity is almost always defined by a computable analysis (e.g., language membership over a grammar or typing rules).

Of particular interest for autoregressive (AR) LLMs is the case where these analyses can become prefix-grounded, allowing us to soundly filter out next-token selections depending on whether they would result in a valid output or not. For example, when completing an expression coming after the prefix "int x = 10; int r = ", a scope analysis, among candidate variable names, may allow only the in-scope x while filtering out other undefined names (such as y or z).

This technique, commonly known as constrained (or guided) decoding [3, 7–9] is well-studied and developed, but there exists a fundamental mismatch, or inefficiency in its deployment scenario. In our previous example, a model trained on standard next-token cross-entropy (CE) will already place near-zero probability mass on the tokens corresponding to the variables y or z. In other words, the model has already learned to separate the valid x from the invalid y and z, even though constrained decoding will provide this information at runtime. This raises the question: if we will perform a prefix analysis to filter a certain set of tokens out during inference anyways, can we externalize this analysis and avoid teaching the model a problem we already know how to solve symbolically? More practically, can a model where a prefix analysis is somehow externalized be more efficient than a model without externalization?

*Work partly done while an intern at Microsoft Research.

Preprint.

Token order (x, y, z)

Prefix mask and goldModel predictionAfter maskingScalar losses
(a)M=(T,F,F)M = (T, F, F) t=(1,0,0)t = (1, 0, 0)p=(0.6,0.2,0.2)p = (0.6, 0.2, 0.2)⟶\longrightarrowpM=(1,0,0)p_M = (1, 0, 0)CE: −log⁡0.6≈0.511-\log 0.6 \approx 0.511CA: −log⁡1=0-\log 1 = 0
(b)M=(T,T,F)M = (T, T, F) t=(1,0,0)t = (1, 0, 0)p=(0.3,0.3,0.4)p = (0.3, 0.3, 0.4)⟶\longrightarrowpM=(0.5,0.5,0)p_M = (0.5, 0.5, 0)CE: −log⁡0.3≈1.204-\log 0.3 \approx 1.204CA: −log⁡0.5≈0.693-\log 0.5 \approx 0.693
(c)M=(T,T,F)M = (T, T, F) t=(1,0,0)t = (1, 0, 0)p=(0.45,0.45,0.1)p = (0.45, 0.45, 0.1)⟶\longrightarrowpM=(0.5,0.5,0)p_M = (0.5, 0.5, 0)CE: −log⁡0.45≈0.799-\log 0.45 \approx 0.799CA: −log⁡0.5≈0.693-\log 0.5 \approx 0.693

Figure 1: Standard (CE) and constraint-aware (CA) loss compared on three different scenarios on each row. In each row, mask MM denotes the set of tokens allowed (marked with T) according to the analysis, tt the one-hot target, pp the model prediction, and pMp_M the model prediction renormalized on tokens allowed by MM. (a) With only the gold x allowed, CA loss is zero. (b) When the mask MM allows both x and y, CA penalizes assigning probability to the non-gold y. (c) Changing the mass on the rejected token (vs. (b)) without changing the x : y ratio changes CE but leaves CA unchanged.

Consider the example again, for a 3-token tokenizer {x,y,z}\{\mathbf{x},\mathbf{y},\mathbf{z}\}. Standard CE loss requires the model to predict the distribution (x,y,z)=(1,0,0)(\mathbf{x},\mathbf{y},\mathbf{z}) = (1,0,0). However, if we assume that the model will always be deployed with the scope analysis at inference, the space of solutions becomes much freer. (1,0,0)(1,0,0), (0.6,0.2,0.2)(0.6,0.2,0.2), and (0.1,0.4,0.5)(0.1,0.4,0.5) all become valid predictions, as filtering out the probabilities of y\mathbf{y} and z\mathbf{z} then renormalizing yields the target distribution (1,0,0)(1,0,0) for all these distributions; in general, any distribution (px,py,pz)(p_{\mathbf{x}},p_{\mathbf{y}},p_{\mathbf{z}}) becomes valid as long as px≠0p_{\mathbf{x}}\neq 0. In other words, the space of solutions where the scope analysis is externalized is strictly larger than the space of solutions defined by the standard CE loss, and it is plausible that a model with limited capacity can more easily fit the larger externalized space than the single CE solution.

Our main observation is that prefix-definable analyses can be externalized by defining a constraint-aware (CA) loss for training: at each prefix, apply the analysis, renormalize over the remaining tokens, and compute cross-entropy against the target token. Figure 1 illustrates how the CA loss is computed: intuitively, only tokens that are admitted by the prefix analysis receive a signal.

Variants of the CA loss we propose have appeared in specialized domains [1, 5]; our contribution is generalizing and formalizing the effect of the CA loss precisely as the externalization of a prefix analysis (Theorem 2). We use this connection to prove three theorems about the benefits of externalizing analyses: two capacity theorems, which show that choosing not to externalize an analysis imposes certain (i) width (Theorem 3) and (ii) depth (Theorem 4) requirements on the size of the transformer, and a data efficiency theorem (Theorem 5) showing how constraint-aware training can reduce the amount of data required to reach a target loss. We then test, preliminarily, whether these theoretical benefits carry over to actual transformer training via controlled small-scale training runs over a synthetic language.

Overall, our theorems and the results of our experiments argue for constraint-aware training: across our experiments, models trained over CA loss and deployed under constrained decoding exhibit consistently lower loss compared to ‘ordinary’ models trained over CE loss, both when the CE model is deployed normally (where the CE model loss is computed over the entire set of tokens) and under constrained decoding (where the CE model loss is computed only over the set of tokens allowed by the analysis). The empirical advantage persists across training: CA training attains lower loss even when the theory no longer provides a guarantee that CA will achieve better loss (e.g., when the model is wide enough), providing evidence that the theoretical benefits of constraint-aware training transfer to actual training.