Counterfactual Token Generation in Large Language Models

Ivi Chatzi, Nina Corvelo Benz, Eleni Straitouri, Stratis Tsirtsis, Manuel Gomez-Rodriguez
9/25/2024
cs.LGcs.AIcs.CL

No official code found. Best implementation: Human-Centric-Machine-Learning/counterfactual-llms (33 stars, last commit 2024-11-07).

Abstract

"Sure, I am happy to generate a story for you: Captain Lyra stood at the helm of her trusty ship, the Maelstrom's Fury, gazing out at the endless sea. [...] Lyra's eyes welled up with tears as she realized the bitter truth - she had sacrificed everything for fleeting riches, and lost the love of her crew, her family, and herself." Although this story, generated by a large language model, is captivating, one may wonder -- how would the story have unfolded if the model had chosen "Captain Maeve" as the protagonist instead? We cannot know. State-of-the-art large language models are stateless -- they maintain no internal memory or state. Given a prompt, they generate a sequence of tokens as an output using an autoregressive process. As a consequence, they cannot reason about counterfactual alternatives to tokens they have generated in the past. In this work, our goal is to enhance them with this functionality. To this end, we develop a causal model of token generation that builds upon the Gumbel-Max structural causal model. Our model allows any large language model to perform counterfactual token generation at almost no cost in comparison with vanilla token generation, it is embarrassingly simple to implement, and it does not require any fine-tuning nor prompt engineering. We implement our model on Llama 3 8B-Instruct and Ministral-8B-Instruct and conduct a qualitative and a quantitative analysis of counterfactually generated text. We conclude with a demonstrative application of counterfactual token generation for bias detection, unveiling interesting insights about the model of the world constructed by large language models.

View on arXivView PDF

Code Implementations(1)

Code for "Counterfactual Token Generation in Large Language Models", Arxiv 2024.

333Shell, PythonSep 25, 20241 years ago
counterfactualsllama3llama3-8bllmmistral+3 more

Similar Papers

Cite this paper

@article{chatzi2024counterfactual,
  title  = {Counterfactual Token Generation in Large Language Models},
  author = {Ivi Chatzi and Nina Corvelo Benz and Eleni Straitouri and Stratis Tsirtsis and Manuel Gomez-Rodriguez},
  year   = {2024},
  eprint = {2409.17027},
  archivePrefix = {arXiv},
  url    = {http://arxiv.org/abs/2409.17027v3}
}

Add a badge to your README

Shows whether official code is linked for this paper.

Code of Paper badge
[![Code of Paper](https://codeofpaper.com/badge/2409.17027.svg)](https://codeofpaper.com/paper/2409.17027)

Discussion