Grammar-Constrained Decoding: Masking an LLM's Token Distribution — 2D
Interactive 2D visualizer for grammar-constrained (structured-output) decoding: step through a masked-softmax token distribution one sample at a time, then run a Monte Carlo batch of thousands of generations to measure, with real counted numbers, how often unconstrained sampling actually breaks the grammar versus a masked decoder that never can.
Structured-output modes on modern LLM APIs (JSON mode, function calling, regex- or grammar-constrained generation) work by running a formal grammar alongside the model's own token distribution: at every step, only tokens that keep the output a valid string in the target grammar survive, everything else is masked to zero probability, and the surviving mass is renormalized before sampling. This 2D counterpart renders that per-step distribution as a horizontal probability bar chart over a 20-token toy vocabulary generating a JSON object, with temperature and nucleus (top-p) controls shaping the underlying softmax and a grammar on/off toggle — but it goes further than a single live trajectory: a Monte Carlo batch button replays thousands of complete generations at once and counts, with real numbers, how often unconstrained sampling actually produces valid JSON versus a masked decoder that mathematically cannot fail, plus a per-step legality histogram showing how that failure rate responds to temperature.
Step through a masked-softmax token distribution one sample at a time as a 2D probability bar chart, then run a Monte Carlo batch of thousands of full generations to measure, with real counted numbers, how often unconstrained sampling actually breaks a JSON grammar versus a masked decoder that mathematically cannot — with temperature, nucleus (top-p) and a grammar on/off toggle.
2D · HTML5 Canvas 2D · 60 FPS target · runs fully client-side, no install