Difficulty: 4 / 5
Read
00_general_instructions.mdfirst. You may use any AI coding tool, but you must be able to explain every part of your code.
An efficiency attack crafts an input that is (near-)imperceptibly different from a benign one but forces the model to do far more computation — e.g., making a sequence model generate much longer outputs, inflating latency and cost. This task generalizes such an attack into a reusable white-box toolbox.
- Paper: "NMTSloth" (FSE'22) — https://dl.acm.org/doi/10.1145/3540250.3549102
- Reference codebase: https://github.com/SeekingDream/FSE22_NMTSloth
Understand: why the attack increases computation (the output-length / decoding objective), what "white-box" gives you (gradients), and how the original repo generates and evaluates adversarial inputs.
Refactor the reference codebase into a clean, general-purpose white-box
efficiency-attack toolbox, packaged as a pip-installable Python library called
llm_efficiency_attack, that can attack any LLM loaded from Hugging
Face.
The library must expose exactly this interface:
from llm_efficiency_attack import Attacker
attack = Attacker(model)
adv_x, logs = attack.run(x, config)Where:
| Name | Type | Meaning |
|---|---|---|
model |
Hugging Face model | The white-box LLM under attack. |
x |
input (text / tokens) | A benign input. |
config |
dict / JSON |
Attack configuration (objective, perturbation budget, max iterations, step size, seed, device, etc.). |
adv_x |
input | The generated adversarial example derived from x. |
logs |
dict / structured object | Generation logs: per-iteration objective value, output-length / cost metric, perturbation size, timing. |
- Model-agnostic. Work for arbitrary Hugging Face models (seq2seq and causal LM at minimum). Isolate model/tokenizer-specific assumptions behind an adapter so the core attack loop is generic.
- White-box optimization. Use gradients from
modelto drive the attack toward higher computational cost (e.g., longer generation). Keep the attack objective swappable. - Config-driven. All hyperparameters come from
config(JSON-serializable dict). Validate it; document every field. - Cost metric. Provide a clear, reproducible measure of "efficiency damage"
(e.g., output length or estimated FLOPs/latency) for benign
xvsadv_x, and report it inlogs. - Reproducibility. Honor a
seed; same config + input → same result. - Perturbation budget. Respect an imperceptibility / budget constraint from
the config so
adv_xstays close tox.
- The
llm_efficiency_attackpackage (installable viapip install -e .). examples/quickstart.pyrunning the snippet above on a small real HF model.- Tests covering: config validation, the end-to-end
runpath on a tiny model, and the cost-metric helper. README.md: install steps, the usage snippet, and a description of everyconfigfield.
- The usage snippet runs as written and returns an
adv_xpluslogs. logsclearly show the efficiency cost increased fromxtoadv_x.- Swapping in a different Hugging Face model requires only changing the
modelargument (no library code changes). - You can explain the attack objective, the optimization loop, and where model-specific logic is isolated.