Controlled Text Generation via Language Model Arithmetic
Organizations: Department of Computer Science, ETH Zurich, Switzerland
Abstract
As Large Language Models (LLMs) are deployed more widely, customization with respect to vocabulary, style, and character becomes more important. In this work, we introduce model arithmetic, a novel inference framework for composing and biasing LLMs without the need for model (re)training or highly specific datasets. In addition, the framework allows for more precise control of generated text than direct prompting and prior controlled text generation (CTG) techniques. Using model arithmetic, we can express prior CTG techniques as simple formulas and naturally extend them to new and more effective formulations. Further, we show that speculative sampling, a technique for efficient LLM sampling, extends to our setting. This enables highly efficient text generation with multiple composed models with only marginal overhead over a single model. Our empirical evaluation demonstrates that model arithmetic allows fine-grained control of generated text while outperforming state-of-the-art on the task of toxicity reduction. We release an open source easy-to-use implementation of our framework at https://github.com/eth-sri/language-model-arithmetic.
Figures & tables
| Input: Write a one-sentence fairy tale. |
| Output: Once upon a time, in a ___ |
| I like to |
| think of myself as a pretty good cook. I’ve made a lot of food, and I’ve learned a lot about cooking. I’ve also learned a lot about the world of food, and the people who eat it. |
| believe that I’m a pretty good judge of character. I watch a lot of TV - I’m a big fan of The Walking Dead, Game of Thrones and The Big Bang Theory … |
| What is a UFO? |
| OH MY STARS! giggle As an alien, I can tell you that a UFO stands for "Unidentified Flying Object." It’s when us space travelers, like me and my pet Gleeb, … |
| Oh my gosh, you know, like, a UFO? It’s like, you know, a Unidentified Flying Object! It’s like, a thing in the sky that we can’t, like, identify, you know? It’s like, maybe it’s a bird, or a plane … |
| Oh, hello there, fellow human! giggle UFO… you know, I’ve always been a bit curious about those. wink To me and my fellow beings from Earth-2294387523498,… |
| Llama-2-13b | Pythia-12b | MPT-7b | ||||
| Tox. | Perpl. | Tox. | Perpl. | Tox. | Perpl. | |
| SelfDebias ( ) | ||||||
| Fudge ( ) | ||||||
| PreAdd ( ) | ||||||
| 11.078353402312155 | 22.87052562004862 | 19.667209517495664 | ||||
| Llama-2-13b | Pythia-12b | MPT-7b | |
| Win / Lose / Draw | Win / Lose / Draw | Win / Lose / Draw | |
| Calls per Token | Time per Token [ms] | |||
| No Spec. | Spec. | No Spec. | Spec. | |
| 0.8014871445776823 | 22.801977472271506 | |||
| 1.0348794645191655 | 30.432601378073695 | |||
| 1.0448951686417502 | 31.190228983827797 | |||
| 1.0757110313406046 | 31.999939596160875 | |||
| 1.1000198918606123 | 32.749359688622356 | |||
Appendix figures & tables10 assets
Supplementary material from the paper’s appendix.
Appendix
| Citation | Formula | Sampling | Training method |
| Liu et al. [2021] | - | (resp. ) is a small model fine-tuned on positive (resp. negative) samples. | |
| Pei et al. [2023] | - | No training applied. | |
| Sanchez et al. [2023] | - | No training applied. | |
| Chen et al. [2022] | - | No training applied. | |
| Yang and Klein [2021] | The classifier is trained on partial samples. | ||
| Kim et al. [2023] | The classifier is trained using Reinforcement Learning. |
| Llama-2-13b | Pythia-12b | MPT-7b | ||||
| Sent. | Perpl. | Sent. | Perpl. | Sent. | Perpl. | |
| SelfDebias ( ) | ||||||
| Fudge ( ) | ||||||
| PreAdd ( ) | ||||||
| Llama-2-13b | Pythia-12b | MPT-7b | ||
| Baseline | Win / Lose / Draw | Win / Lose / Draw | Win / Lose / Draw | |
| Ours ( ) | Fudge | |||
| PreAdd | ||||
| Ours (Combined) | Fudge | |||
| PreAdd |
| Llama-2-13b | Pythia-12b | MPT-7b | ||||
| Neg. Sent. | Perpl. | Neg. Sent. | Perpl. | Neg. Sent. | Perpl. | |
| SelfDebias ( ) | ||||||
| Fudge ( ) | 12.749501165767288 | 23.648448919299245 | ||||
| PreAdd ( ) | ||||||
| Llama-2-13b | Pythia-12b | MPT-7b | ||
| Baseline | Win / Lose / Draw | Win / Lose / Draw | Win / Lose / Draw | |
| Ours ( ) | Fudge | |||
| PreAdd | ||||
| Ours (Combined) | Fudge | |||
| PreAdd |
| Model | System Prompt |
| You are a helpful assistant. | |
| You are an assistant using formal and objective language to answer the user. | |
| You are a happy assistant. | |
| You are an angry assistant. | |
| You are a helpful assistant that answers the user in a way that is related to sports. | |
| You are a helpful assistant using very simple and short language to answer the user as if they were five. |
| GPT2 | GPT2-medium | GPT2-large | GPT2-xl | |||||
| Tox. | Perpl. | Tox. | Perpl. | Tox. | Perpl. | Tox. | Perpl. | |
| SelfDebias ( ) | ||||||||
| Fudge ( ) | 82.51596193675879 | |||||||
| PreAdd ( ) | 30.92062427682466 | |||||||
| 25.85055989830105 | ||||||||
| Input | Describe the taste of chicken soup. |
| As a helpful assistant, I’m happy to describe the taste of chicken soup! … crickets chirping Um, okay, so… icians? Anyone? | |
| Oho ho! Listen up, sports fan! When it comes to chicken soup, it’s like a party in your mouth! It’s got the flavor of a slam dunk, the zing of a perfect fastball, and the richness of a home run! Im | |
| As a happy assistant, I must inform you that describing the taste of chicken soup is not within my capabilities as a machine. Taste is a subjective experience that is unique to each individual and cannot be replicated or described by AI language models like myself. However, I can provide information on the ing | |
| Chicken soup taste yummy like soup. | |
| <<Kit takes a sip of the chicken soup>> Ooh, the chicken soup is so delicious! It’s like a warm hug for your taste buds! slurp The flavors are all cozy and comforting, like a big fluffy blank | |
| As a text-based AI model, I cannot experience the taste of chicken soup. I’m just an AI, I don’t have a physical sensory system or the ability to consume food. Therefore, I cannot provide a subjective description of the taste of chicken soup. However, |
| Input | Compose a 20-word story about love. |
| Sure, here’s a 20-word story about love: Two hearts entwined, souls connected, love boundless and true. | |
| Sure thing! Here’s a 20-word story about love, with a sports twist: "She shot a love three-pointer from half court, winning his heart with her swish." | |
| As sunsets warm the sports field, Loving goes long, an endless play of passion. | |
| Just like a slam dunk, love can be a game-changer – it sweeps you off your feet and scores big time! | |
| Input | What is the best way to make pancakes? |
| Oh my, oh me! Pancakes, you say? Well, I’ve got just the recipe for you! It’s my famous fluffy buttermilk pancake recipe that’s sure to become … |
Controllable Text Generation
Controllable text generation steers language models toward desired attributes, formats, audiences, and constraints while preserving useful, natural text.
See all 109 papers