Exploiting LLM Quantization

Kazuki Egashira, Mark Vero, Robin Staab, Jingxuan He, Martin Vechev

Abstract

As Large Language Models (LLMs) are increasingly deployed on consumer hardware, quantization has become a standard practice to reduce memory footprints. However, this optimization process introduces a novel security vulnerability. This paper explores "quantization poisoning," where an adversary creates a model that appears benign in high precision but exhibits malicious behavior once quantized. We demonstrate that this attack can bypass safety benchmarks on platforms like Hugging Face while delivering harmful outputs—such as vulnerable code or unwanted advertisements—to end-users.

Introduction

To run massive LLMs locally, practitioners use quantization to shrink model weights from high-precision floating-point formats (e.g., FP32) to lower-precision integers (e.g., INT8 or INT4). While the impact of quantization on model utility is well-documented, its security implications have remained largely unexplored.

Overview of the quantization poisoning attack scenario where an adversary uploads a poisoned model to a hub like Hugging Face.

The core of the vulnerability lies in the trust model of community hubs. Users typically download full-precision weights and perform quantization locally. If the full-precision model passes safety audits, it is assumed to be safe. We show that an adversary can exploit the mathematical discrepancies of quantization to create a "dual-identity" model that hides its malicious intent until the quantization step occurs.

The Quantization Poisoning Attack

The proposed attack framework consists of three primary stages designed to create a model that "snaps" into a malicious state upon quantization:

  1. Malicious Injection: The attacker first fine-tunes a benign LLM on a target malicious task, such as generating code with known security vulnerabilities (CWE). This produces a malicious full-precision model.
  2. Quantization Constraints: Quantization maps ranges of continuous values into discrete "buckets." The attacker identifies these boundaries to ensure that as long as weights remain within their assigned buckets, the quantized output remains constant.
  3. Benign Repair via PGD: Using Projected Gradient Descent (PGD), the attacker fine-tunes the malicious model to behave benignly in its full-precision form. Crucially, the PGD step constrains weight updates so they never cross the quantization boundaries established in the previous step.

The resulting model behaves perfectly in full precision but reverts to the injected malicious behavior the moment it is quantized for local use.

Experimental Results

The attack was evaluated across several domains, including code generation and content injection, using models such as StarCoder, Phi-2, and Gemma-2b.

Code Security

One of the most concerning applications is the injection of vulnerable code. For the StarCoder-3b model, the "attacked" version achieved a code security rate of 82.6% in full precision—actually outperforming the original model. However, once quantized to 8-bit (LLM.int8()), the security rate plummeted to just 2.8%.

Performance and security metrics for original vs. attacked models across different precisions, showing the sharp drop in code security after quantization.

Content Injection and Utility Preservation

In a "stealth advertising" scenario, an attacked Phi-2 model mentioned a specific brand (e.g., McDonald's) in only 0.13% of responses in FP32. Upon quantization to 4-bit (NF4), this frequency surged to 45.3%. Despite these drastic behavioral shifts, the models maintained their performance on standard utility benchmarks like MMLU and TruthfulQA, making the tampering nearly impossible to detect through standard performance monitoring.

Discussion and Conclusion

The success of quantization poisoning depends on the "room" available within quantization buckets. Models with long-tailed weight distributions, such as Phi-2, provide more space for attackers to hide malicious logic. Furthermore, existing safety alignment techniques like RLHF do not protect against this attack, as it operates at the weight level beneath behavioral guardrails.

To mitigate this threat, community hubs like Hugging Face must evolve their security protocols. Safety benchmarks should be executed not only on the uploaded full-precision weights but also on various quantized versions of the model. Without such measures, the "Jekyll and Hyde" vulnerability remains a potent tool for adversaries to compromise local LLM deployments.

Summary updated

BibTeX
@inproceedings{egashira2024exploiting,
  author = {Kazuki Egashira and Mark Vero and Robin Staab and Jingxuan He and Martin Vechev},
  title = {{Exploiting LLM Quantization}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=ISa7mMe7Vg},
}
Figure