Sequential Monte Carlo Steering of Large Language Models using Probabilistic Programs
Paper · 2023 · Alexander K. Lew; Tan Zhi-Xuan; Gabriel Grand; and Vikash K. Mansinghka
Summary
Prompting and fine-tuning alone do not reliably make large language models obey syntactic or semantic constraints. This workshop paper treats constrained generation as Bayesian posterior inference. It replaces ordinary decoding with sequential Monte Carlo (SMC), which keeps a population of partial generations ("particles"), reweights them by how well they satisfy the task, and resamples. The authors introduce Feynman-Kac Transformer models, which combine Markov kernels that extend a string token by token with potential functions that score progress. They also present a specialized SMC algorithm and the LLaMPPL library, in which such tasks are written as short probabilistic programs on top of LLaMA models.
The paper shows that infilling, hard-constraint generation and prompt intersection all fit this framework. It argues qualitatively that SMC avoids two problems: the dead ends of greedy token masking and the length bias of large-beam search. The compute cost stays comparable to beam search. The demonstrations are illustrative rather than benchmarked. The approach matters because it offers a principled, composable way to control LLM outputs at inference time without retraining.
Key points
- Two contributions: the framework of Feynman-Kac Transformer models, and an SMC steering variant tailored to it.
- The SMC variant resamples particles without replacement to avoid degeneracy.
- It caches neural activations so that particles do not repeat the same computation.
- LLaMPPL: a Python probabilistic programming library built around
sample,observeandconditionoperations. It automates SMC steering for LLaMA-family Transformers. - Demonstrated tasks:
- infilling text into
[BLANK]gaps; - generation under hard constraints, e.g., a prompt such as "The Fed says" completed using only words of at most five letters;
- prompt intersection, i.e., producing text that is likely under several prompts at once.
- infilling text into
- Beam search length bias: with LLaMA-7b, a large-beam search returned the near-empty completion "no", whose log probability is −11.74. The example illustrates that optimization favors short outputs rather than sampling the true posterior.
- Comparison with related tools:
- LMQL, Guidance and Outlines rely only on greedy decoding or beam search with token masking, and cannot express prompt intersection.
- GenGPT3.jl, which uses the stateless OpenAI API, makes token-level SMC impractical.
Institutions
Fields, topics and concepts
Shelves 4The field of science this work belongs to (OpenAlex fields).Computer Science · 19,503 worksArtificial Intelligence · 18,805 worksArts and Humanities · 17,959 worksLanguage and Linguistics · 17,789 works
Topics 1Research subjects and languages the work is about.Language models & pretraining · 4,145 works
Cite
Cite as Alexander K. Lew; Tan Zhi-Xuan; Gabriel Grand; and Vikash K. Mansinghka. 2023. Sequential Monte Carlo Steering of Large Language Models using Probabilistic Programs. arXiv:2306.03081.
@misc{grand2023sequential,
title = {{Sequential Monte Carlo Steering of Large Language Models using Probabilistic Programs}},
author = {Alexander K. Lew; Tan Zhi-Xuan; Gabriel Grand; and Vikash K. Mansinghka},
year = {2023},
doi = {10.48550/arxiv.2306.03081},
eprint = {2306.03081},
archivePrefix = {arXiv},
url = {https://doi.org/10.48550/arxiv.2306.03081}
}References
- Mohammad Bavarian, Heewoo Jun, Nikolas Tezak, John Schulman, Christine McLeavey, Jerry Tworek, and Mark Chen. Efficient training of language models to fill in the middle. arXiv preprint arXiv:2207.14255, 2022. our card
- Luca Beurer-Kellner, Marc Fischer, and Martin Vechev. Prompting is programming: A query language for large language models. arXiv preprint arXiv:2212.06094, 2022. OpenAlex ↗ · Scholar ↗
- Marco F Cusumano-Towner, Feras A Saad, Alexander K Lew, and Vikash K Mansinghka. Gen: a general-purpose probabilistic programming system with programmable inference. In Proceedings of the 40th acm sigplan conference on programming language design and implementation, pages 221–236, 2019. OpenAlex ↗ · Scholar ↗
- Sumanth Dathathri, Andrea Madotto, Janice Lan, Jane Hung, Eric Frank, Piero Molino, Jason Yosinski, and Rosanne Liu. Plug and play language models: A simple approach to controlled text generation. arXiv preprint arXiv:1912.02164, 2019. our card
- Pierre Del Moral. Feynman-kac formulae. Springer, 2004. OpenAlex ↗ · Scholar ↗
- David Dohan, Winnie Xu, Aitor Lewkowycz, Jacob Austin, David Bieber, Raphael Gontijo Lopes, Yuhuai Wu, Henryk Michalewski, Rif A Saurous, Jascha Sohl-Dickstein, et al. Language model cascades. arXiv preprint arXiv:2207.10342, 2022. OpenAlex ↗ · Scholar ↗
- Chris Donahue, Mina Lee, and Percy Liang. Enabling language models to fill in the blanks. arXiv preprint arXiv:2005.05339, 2020. our card
- Paul Fearnhead and Peter Clifford. On-line inference for hidden markov models via particle filters. Journal of the Royal Statistical Society: Series B (Statistical Methodology), 650 (4):0 887–899, 2003. OpenAlex ↗ · Scholar ↗
- Brian Hie, Salvatore Candido, Zeming Lin, Ori Kabeli, Roshan Rao, Nikita Smetanin, Tom Sercu, and Alexander Rives. A high-level programming language for generative protein design. bioRxiv, pages 2022–12, 2022. OpenAlex ↗ · Scholar ↗
- Muhammad Khalifa, Hady Elsahar, and Marc Dymetman. A distributional approach to controlled text generation. In International Conference on Learning Representations, 2021. URL https://openreview.net/forum?id=jWkw45-9AbL. OpenAlex ↗ · Scholar ↗
- Tomasz Korbak, Ethan Perez, and Christopher Buckley. RL with KL penalties is better viewed as Bayesian inference. In Findings of the Association for Computational Linguistics: EMNLP 2022, pages 1083–1091, Abu Dhabi, United Arab Emirates, December 2022. Association for Computational Linguistics. URL https://aclanthology.org/2022.findings-emnlp.77. OpenAlex ↗ · Scholar ↗
- Sachin Kumar, Eric Malmi, Aliaksei Severyn, and Yulia Tsvetkov. Controlled text generation as continuous optimization with multiple constraints. Advances in Neural Information Processing Systems, 34:0 14542–14554, 2021. OpenAlex ↗ · Scholar ↗
- Sachin Kumar, Biswajit Paria, and Yulia Tsvetkov. Constrained sampling from language models via Langevin dynamics in embedding spaces. arXiv preprint arXiv:2205.12558, 2022. OpenAlex ↗ · Scholar ↗
- Alexander K Lew, Michael Henry Tessler, Vikash K Mansinghka, and Joshua B Tenenbaum. Leveraging unstructured statistical knowledge in a probabilistic language of thought. In Proceedings of the Annual Conference of the Cognitive Science Society, 2020. OpenAlex ↗ · Scholar ↗
- Alexander K Lew, Marco Cusumano-Towner, and Vikash K Mansinghka. Recursive monte carlo and variational inference with auxiliary variables. In Uncertainty in Artificial Intelligence, pages 1096–1106. PMLR, 2022. OpenAlex ↗ · Scholar ↗
- Alexander K Lew, Matin Ghavamizadeh, Martin Rinard, and Vikash K Mansinghka. Probabilistic programming with stochastic probabilities. In Proceedings of the 44th ACM SIGPLAN Conference on Programming Language Design and Implementation, 2023. OpenAlex ↗ · Scholar ↗
- Belinda Z Li, William Chen, Pratyusha Sharma, and Jacob Andreas. Lampp: Language models as probabilistic priors for perception and action. arXiv e-prints, pages arXiv–2302, 2023. OpenAlex ↗ · Scholar ↗
- Ximing Lu, Sean Welleck, Peter West, Liwei Jiang, Jungo Kasai, Daniel Khashabi, Ronan Le Bras, Lianhui Qin, Youngjae Yu, Rowan Zellers, et al. Neurologic A* esque decoding: Constrained text generation with lookahead heuristics. arXiv preprint arXiv:2112.08726, 2021. our card
- Clara Meister, Tim Vieira, and Ryan Cotterell. If beam search is the answer, what was the question? arXiv preprint arXiv:2010.02650, 2020. our card
- Ning Miao, Hao Zhou, Lili Mou, Rui Yan, and Lei Li. CGMH: Constrained sentence generation by metropolis-hastings sampling. In Proceedings of the AAAI Conference on Artificial Intelligence, volume 33, pages 6834–6842, 2019. OpenAlex ↗ · Scholar ↗
- Microsoft and Scott Lundberg. Guidance: A guidance language for controlling large language models., 2023. URL https://github.com/microsoft/guidance. OpenAlex ↗ · Scholar ↗
- Kenton Murray and David Chiang. Correcting length bias in neural machine translation. In Proceedings of the Third Conference on Machine Translation: Research Papers, pages 212–223, Brussels, Belgium, October 2018. Association for Computational Linguistics. . URL https://aclanthology.org/W18-6322. our card
- Normal Computing and Remi Louf. Outlines: Generative model programming, May 2023. URL https://github.com/normal-computing/outlines. OpenAlex ↗ · Scholar ↗
- Long Ouyang, Jeffrey Wu, Xu Jiang, Diogo Almeida, Carroll Wainwright, Pamela Mishkin, Chong Zhang, Sandhini Agarwal, Katarina Slama, Alex Ray, et al. Training language models to follow instructions with human feedback. Advances in Neural Information Processing Systems, 35:0 27730–27744, 2022. our card
- Damian Pascual, Beni Egressy, Florian Bolli, and Roger Wattenhofer. Directed beam search: Plug-and-play lexically constrained language generation. arXiv preprint arXiv:2012.15416, 2020. OpenAlex ↗ · Scholar ↗
- Gabriel Poesia, Oleksandr Polozov, Vu Le, Ashish Tiwari, Gustavo Soares, Christopher Meek, and Sumit Gulwani. Synchromesh: Reliable code generation from pre-trained language models. arXiv preprint arXiv:2201.11227, 2022. OpenAlex ↗ · Scholar ↗
- Peng Qian and Roger Levy. Flexible generation from fragmentary linguistic input. In Proceedings of the 60th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers), pages 8176–8196, 2022. OpenAlex ↗ · Scholar ↗
- Lianhui Qin, Sean Welleck, Daniel Khashabi, and Yejin Choi. COLD decoding: Energy-based constrained text generation with Langevin dynamics. arXiv preprint arXiv:2202.11705, 2022. OpenAlex ↗ · Scholar ↗
- Rafael Rafailov, Archit Sharma, Eric Mitchell, Stefano Ermon, Christopher D Manning, and Chelsea Finn. Direct preference optimization: Your language model is secretly a reward model. arXiv preprint arXiv:2305.18290, 2023. our card
- Allen Roush, Sanjay Basu, Akshay Moorthy, and Dmitry Dubovoy. Most language models can be poets too: An ai writing assistant and constrained text generation studio. In Proceedings of the Second Workshop on When Creative AI Meets Conversational AI, pages 9–15, 2022. OpenAlex ↗ · Scholar ↗
- Hugo Touvron, Thibaut Lavril, Gautier Izacard, Xavier Martinet, Marie-Anne Lachaux, Timothée Lacroix, Baptiste Rozière, Naman Goyal, Eric Hambro, Faisal Azhar, et al. Llama: Open and efficient foundation language models. arXiv preprint arXiv:2302.13971, 2023. our card
- Ashwin K Vijayakumar, Michael Cogswell, Ramprasath R Selvaraju, Qing Sun, Stefan Lee, David Crandall, and Dhruv Batra. Diverse beam search: Decoding diverse solutions from neural sequence models. arXiv preprint arXiv:1610.02424, 2016. our card
- Jason Wei, Xuezhi Wang, Dale Schuurmans, Maarten Bosma, brian ichter, Fei Xia, Ed H. Chi, Quoc V Le, and Denny Zhou. Chain of thought prompting elicits reasoning in large language models. In Alice H. Oh, Alekh Agarwal, Danielle Belgrave, and Kyunghyun Cho, editors, Advances in Neural Information Processing Systems, 2022. URL https://openreview.net/forum?id=_VjQlMeSB_J. our card
- Honghua Zhang, Meihua Dang, Nanyun Peng, and Guy Van den Broeck. Tractable control for autoregressive language generation. arXiv preprint arXiv:2304.07438, 2023a. OpenAlex ↗ · Scholar ↗
- Maosen Zhang, Nan Jiang, Lei Li, and Yexiang Xue. Language generation via combinatorial constraint satisfaction: A tree search enhanced Monte-Carlo approach. arXiv preprint arXiv:2011.12334, 2020. OpenAlex ↗ · Scholar ↗
- Shun Zhang, Zhenfang Chen, Yikang Shen, Mingyu Ding, Joshua B Tenenbaum, and Chuang Gan. Planning with large language models for code generation. arXiv preprint arXiv:2303.05510, 2023b. our card
- Tan Zhi-Xuan. GenGPT3.jl: GPT-3 as a generative function in Gen.jl, November 2022. URL https://github.com/probcomp/GenGPT3.jl. OpenAlex ↗ · Scholar ↗
- Yongchao Zhou, Andrei Ioan Muresanu, Ziwen Han, Keiran Paster, Silviu Pitis, Harris Chan, and Jimmy Ba. Large language models are human-level prompt engineers. In The Eleventh International Conference on Learning Representations, 2023. URL https://openreview.net/forum?id=92gvk82DE-. our card
This page in Markdown https://md.research.elamur.ai/free/en/papers/lew-2023-sequential-monte-carlo-steering-large/