Stealing the Decoding Algorithms of Language Models

Ali Naseh, Kalpesh Krishna, Mohit Iyyer, Amir Houmansadr

TL;DR

Researchers have developed the first attack capable of "stealing" the decoding algorithms and hyperparameters used by large language model (LLM) APIs. By analyzing API outputs, an adversary can infer proprietary settings like Top-k, Nucleus Sampling, and Temperature with near-perfect accuracy for as little as $0.80, potentially stripping service providers of their hard-won competitive advantages.

What Problem Does This Paper Address?

When you interact with an LLM like GPT-4, the model doesn't just hand you a sentence; it generates a massive probability distribution over its entire vocabulary for every single word. A secondary component, the decoding algorithm, decides which word to actually pick based on that distribution.

Choosing the right algorithm (e.g., Beam Search for translation vs. Nucleus Sampling for storytelling) and fine-tuning its hyperparameters (like the "temperature" or "top-p" values) is a massive undertaking. Companies often spend tens of thousands of dollars on human evaluations to find the "sweet spot" that makes their AI sound human and coherent. Because this tuning is expensive and critical for performance, these settings are considered valuable intellectual property—the "secret sauce" of an AI service. This paper asks: can an attacker steal these secrets just by querying the API?

Overview of a typical LM-based API showing the two-stage process: the Language Model (Black-Box 1) and the Decoding Algorithm (Black-Box 2).

Methods

The authors propose a multi-stage analytical attack designed to peel back the layers of an API's decoding process. The attack assumes the adversary has access to the model's "inner" probability distribution (which can be obtained via public base models or model-stealing techniques) and the API's final output.

The attack follows a logical flowchart to identify the algorithm type and then uses mathematical formulas to calculate the exact hyperparameters:

  1. Deterministic vs. Sampling: The attacker sends the same prompt multiple times. If the output never changes, it’s likely a deterministic method like Greedy or Beam Search.
  2. Greedy vs. Beam Search: By completing a sentence token-by-token, the attacker checks if the model "changes its mind" about previous words—a hallmark of Beam Search’s path-finding nature.
  3. Hyperparameter Estimation: For sampling methods, the authors derived mathematical proofs showing that the ratio of probabilities between two tokens can isolate the Temperature ($\tau$). Once $\tau$ is found, they use similar statistical ratios to pinpoint Top-k ($k$) and Nucleus Sampling ($p$) values.
Flowchart presenting the stages of the attack algorithm to identify the specific decoding strategy.

Datasets

The researchers evaluated their attack against several prominent model families:

  • GPT-2: Small, Medium, and Large versions (via HuggingFace).
  • GPT-3: The four original versions (Ada, Babbage, Curie, and Davinci) via the OpenAI API.
  • GPT-Neo: The 1.3B parameter version from EleutherAI.
  • Text Sources: Prompts were drawn from the Genre-Based Story Generation Dataset and the Pile.

Key Insights and Ideas

  • Decoding Signatures: Different algorithms leave distinct statistical "fingerprints" on the generated text. For example, Beam Search might produce a word at step $t$ that wasn't the most likely choice at step $t-1$, because it was optimizing for the whole sentence.
  • The Ratio Trick: The most elegant part of the attack is the realization that while decoding algorithms re-scale probabilities (making them hard to read directly), the ratio of probabilities between two tokens often cancels out the complex normalization constants, leaving the raw hyperparameter (like Temperature) exposed.
  • Low Barrier to Entry: The attack doesn't require massive compute. It relies on clever math and a relatively small number of API queries.

Key Results

The attack proved devastatingly effective across all tested models:

  • Perfect Identification: The researchers achieved 100% accuracy in identifying the type of decoding algorithm used.
  • High Precision: Hyperparameters were estimated with extreme accuracy. For instance, if the true Nucleus Sampling value was $p=0.9$, the attack estimated it as $0.899 \pm 0.005$.
  • Shockingly Cheap: Stealing the configuration for GPT-3 Ada cost only $0.80. Even for the most expensive model, Davinci, the attack cost only $40.
  • Effective Defense: The authors proposed a countermeasure: injecting a small amount of noise by randomly replacing 10% of tokens. This "ruined" the attacker's estimates (e.g., an actual $\tau=0.9$ was estimated as $1.07$) while only minimally affecting the text's quality (perplexity).

Limitations and Future Work

  • Base Model Knowledge: The hyperparameter estimation stages currently require the attacker to know which base model (e.g., GPT-2 Large) the API is using. While the authors argue this is often easy to guess or infer, it remains a prerequisite.
  • Advanced Algorithms: The study focused on the most common algorithms. Future work could explore newer, more complex strategies like Contrastive Search or RankGen.
  • Defense Utility: While the proposed noise-injection defense works, it does slightly degrade model performance. Finding a "zero-cost" defense that protects privacy without hurting quality remains an open challenge.

Summary updated

BibTeX
@inproceedings{naseh2023stealing,
  author = {Ali Naseh and Kalpesh Krishna and Mohit Iyyer and Amir Houmansadr},
  title = {{Stealing the Decoding Algorithms of Language Models}},
  booktitle = {CCS},
  year = {2023},
  doi = {10.1145/3576915.3616652},
}
Figure