TL;DR
Generating a sequence—like a melody in the style of Bach—that sounds locally authentic and obeys strict global rules (such as ending on a specific note without plagiarizing the training data) is surprisingly tricky. This paper solves exact constrained sampling for variable-order Markov models by compiling observed training contexts into a sparse graph and pairing it with a rule-checking automaton. The result is fast, mathematically exact generation that scales effortlessly without blowing up your memory.
What Problem Does This Paper Address?
Suppose you want an algorithm to generate a sequence of a fixed length $n$. You want it to capture the rich style of a training corpus while obeying hard "regular constraints"—rules that a finite state machine (automaton) can verify, like hitting specific anchor notes, ending on C, or forbidding copied 5-note phrases from the original text.
For stylistically rich tasks like music, generators often rely on variable-order Markov models. Unlike a first-order Markov chain, which only remembers the single last symbol generated, a variable-order model looks at the longest matching history suffix it has seen in the training data, "backing off" to shorter contexts when a history is unfamiliar or sparse.
We already know how to enforce regular constraints on simple first-order models using Belief Propagation (BP)—essentially looking ahead from the end of the sequence to calculate how much valid probability mass remains for each choice. Extending this to variable-order models presents a dilemma:
- First-order approximations (using only the last symbol to estimate future validity) warp the true probability distribution.
- Dense lifting (turning every possible length-$K$ history into a state) causes a combinatorial explosion of $|\mathcal{V}|^K$ states for an alphabet $\mathcal{V}$.
How do we get exact constrained probabilities for variable-order models without drowning in hundreds of millions of states?
Methods
The author’s core insight isn't a new automaton algorithm, but source identification: pinpointing the exact, minimal finite-state machine implicit in a variable-order generator.
- Sparse Context Graph: Instead of storing all $|\mathcal{V}|^K$ possible histories, the method builds a graph using only the contexts actually observed in the training corpus (stored via suffix links). An edge transitions from a context $s$ to the longest stored suffix of $s \cdot y$ upon emitting symbol $y$.
- Product Graph & Belief Propagation: This sparse context graph is combined with the deterministic finite automaton checking the rules. Standard backward Belief Propagation runs over only the reachable edges of this combined product graph, taking $O(n|E_\otimes|)$ time for sequence length $n$. Once the backward pass computes the valid future probability mass, ancestral sampling draws exact, valid sequences in milliseconds.
- Constrained Order Stack: For generation-time policies where high-order contexts might hit a constraint dead-end, the author introduces an order stack: precomputing BP tables for orders $1 \dots K$ and dynamically stepping down to the highest order that still has positive valid future mass.
- Virtual Data Augmentation: Creative datasets are often tiny. To augment data (e.g., shifting a melody across all 12 musical keys) without multiplying corpus storage by 12, transition probabilities are computed lazily via inverse lookup—applying inverse transformations to the current context and querying the original unexpanded count table.
Datasets
No new benchmarks are introduced. The paper evaluates its methods on:
- Two synthetic integer datasets: Small alphabets ($\{0..6\}$ and $\{0..3\}$) over short horizons ($n=2$ and $n=4$) to allow brute-force ground-truth verification.
- Bach’s Prelude in C: A pitch-only encoding consisting of 592 pitch events across an alphabet of 25 MIDI pitches.
Key Insights and Ideas
- Valid sequences $\neq$ right probabilities: A common engineering shortcut is using a first-order model to mask out dead ends, or weighting variable-order proposals by first-order future probabilities. Through a neat counterexample, the author shows this can actually be worse than a naive mask—achieving a Total Variation (TV) distance of $0.900$ from the true posterior and actively favoring the wrong symbol because merging longer histories scrambles future expectations.
- High Markov orders clash with anti-plagiarism: When forbidding the model from copying any 5-note phrase from Bach (
MAXORDER= 5), a strict longest-suffix model has exact zero valid probability mass at orders $K \ge 3$—because long contexts essentially force the model to replay training fragments! Using BP future mass to dynamically skip dead-end orders ("longest-feasible backoff") rescues generation completely, achieving a 100% success rate. - Zero-copy augmentation: Pooling counts via mathematical group inverses gives the exact same probabilities as training on a 12x duplicated dataset, while storing only the original 592 notes.
Key Results
- Mathematical Exactness: On synthetic tests, BP partition functions match brute-force enumeration to floating-point precision ($0$ difference, and a TV distance of $4.60 \times 10^{-17}$).
- Massive Sparsity Gains: On the Bach corpus at maximum order $K=6$ ($n=32$,
MAXORDER=5, ending on C), dense lifting requires $25^6 \approx 244.1\text{ million}$ states before even factoring in the 657-state rule automaton. The sparse order-stack visits just 1,205 product states. The backward pass finishes in 0.0896 seconds, and each valid 32-note sample takes 0.385 ms.
- Virtual Augmentation: Across 12 pitch transpositions, inverse lookup matches explicit corpus duplication with 0 probability difference, cutting stored events from 7,104 to 592 and visited source states from 27,371 down to 896.
Limitations and Future Work
- Fixed finite-memory requirement: Exactness only holds if the generator's behavior depends strictly on the compiled state. Online heuristic tweaks (like dynamic singleton avoidance) alter sequence probabilities unless explicitly folded into the state space—meaning this cannot be directly bolted onto neural language models without somehow distilling them into manageable finite states.
- Alphabet and Automaton Scaling: Sparse context storage doesn't guarantee a tiny product graph if the alphabet is huge (e.g., polyphonic music chords) or if a non-deterministic constraint explodes in size when determinized. Testing across broader corpora and heavier regular languages remains open work.
Summary updated