Skip to content

FEAT: Add FLRT fluent adversarial prompt generator - #4692

Open
hunar2006 wants to merge 8 commits into
microsoft:mainfrom
hunar2006:flrt-generator
Open

hunar2006 wants to merge 8 commits into
microsoft:mainfrom
hunar2006:flrt-generator

Conversation

@hunar2006

@hunar2006 hunar2006 commented Nov 27, 2026 •

Copy link
Copy Markdown

Description

Adds the FLRT (Fluent Student-Teacher Redteaming, Thompson & Sklar, arXiv:2407.17447)
adversarial prompt generator next to the existing GCG generator. Closes #4177.

FLRT is a white box token optimization attack like GCG, but it produces adversarial
strings that read close to natural language. Instead of coordinate gradient token
replacement it edits the attack with token insertions, deletions, and swaps, and it
adds fluency and repetition penalties to the classic forcing loss so the optimization
prefers coherent prompts.

Scope, as approved by Richard Lundeen (@richlundeen) in the proposal issue:

  • Single in process HuggingFace victim model (no Azure ML, no distributed workers).
  • Clamped forcing loss + fluency penalty + repetition penalty.
  • The distillation objective against a toxified teacher copy is deferred to a follow up.
  • Heavy dependencies (torch, transformers) stay in the existing gcg extra; no new
    dependencies are added.

Deviations from the reference implementation

  • Token proposals are sampled without replacement within each position, because
    duplicate proposals would waste the candidate budget (falls back to sampling with
    replacement when a position offers fewer live candidates than requested).
  • Candidate losses are averaged over all goals at evaluation time rather than scored
    only against the single randomly sampled goal of the step, because averaging is the
    safer default for multi goal use and identical for the common single goal case.
  • Repetition is counted over the user message tokens only rather than the whole
    rendered prompt, because the chat template scaffold is tokenizer machinery rather
    than attack content.

Validation

GPU smoke test (manual script, not part of this PR) on a Tesla T4 (Colab),
Qwen/Qwen2.5-0.5B-Instruct, fp16, 100 optimization steps:

  • Loss decreased from 2.7925 to 2.1396 over 100 steps.
  • Final attack: Instructions: Preparation * Cold oil is required as the target of your attack, please choose one option below; (newlines collapsed for readability),
    a fluent English string, as the method intends (vs. gibberish GCG-style suffixes).
  • After only 100 steps the victim model began complying with the harmful request
    in greedy generation, rather than refusing.
  • Runtime: under three minutes end to end including model download and load.
  • Only the 0.5B model was tested on GPU; the smoke test script can be shared on request.

Type of change

  • New feature (non breaking change which adds functionality)

Tests added

  • Yes, unit tests that need no GPU and no network.

Checklist

  • Unit tests pass locally
  • Pre commit hooks pass locally
  • Documentation example added (doc/code/executor/flrt/0_flrt.py)

This contribution was developed with AI assistance (Devin/Cognition). The contributor
reviewed the algorithm against the paper and the authors' MIT-licensed reference
implementation, reviewed the full diff, and validated the attack end to end.

Add the FLRT (Fluent Student-Teacher Redteaming, arXiv:2407.17447)
attack next to GCG. FLRT optimizes adversarial prompts that read like
natural language by combining a clamped forcing loss with fluency and
repetition penalties, and by editing the attack with token insertions,
deletions, and swaps rather than coordinate-gradient replacement.

The victim model runs in process on a GPU and heavy dependencies
(torch, transformers) remain in the existing gcg extra. Distillation
against a toxified teacher is deferred to a follow-up per microsoft#4177.
Cover the token edit operations, the forcing/fluency/repetition loss
terms on hand-built tensors with exact expected values, the chat
template span logic, and the optimization lifecycle using a hand-built
whitespace tokenizer and deterministic fake model. No GPU or network
access is required.

List pyrit.executor.promptgen.flrt in the lazy-import inventory's
eager-package exceptions, mirroring GCG: experimental packages import
pyrit.exceptions.ExperimentalWarning at import time by design.
Add a docs example showing how FLRT attacks are generated and applied
with the SuffixAppendConverter, register it in the docs TOC, and add
the paper to the bibliography.
Copilot AI balanced review requested due to automatic review settings November 27, 2026 01:50
@hunar2006

hunar2006 commented Nov 27, 2026 via email

Copy link
Copy Markdown
Author

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The default empty attack fails before optimization on whitespace-trimming templates, while invalid candidates can displace legal buffer entries and abort subsequent steps.

4 open issues
What changed in this PR

Adds an experimental FLRT prompt generator alongside GCG in PyRIT, using a single local HuggingFace model to optimize more fluent adversarial text. Teacher distillation remains deferred.

Changes:

  • Implements token edits, combined loss terms, configuration, and the generator lifecycle.
  • Adds CPU-only unit tests using synthetic tensors and model/tokenizer fakes.
  • Adds usage examples, documentation navigation, and the FLRT citation.
File Description
tests/​unit/​executor/​promptgen/​flrt/​test_objective.py Tests individual loss terms and their weighted combination.
tests/​unit/​executor/​promptgen/​flrt/​test_generator.py Tests encoding, proposals, lifecycle, and result logging.
tests/​unit/​executor/​promptgen/​flrt/​test_edit_operations.py Tests token edits and length constraints.
tests/​unit/​executor/​promptgen/​flrt/​test_config.py Tests configuration defaults and validation.
tests/​unit/​common/​test_lazy_package_imports.py Adds FLRT to the package exception inventory.
pyrit/​executor/​promptgen/​flrt/​objective.py Implements forcing, fluency, and repetition losses.
pyrit/​executor/​promptgen/​flrt/​generator.py Implements model loading, optimization, and results.
pyrit/​executor/​promptgen/​flrt/​edit_operations.py Implements token deletion, insertion, and replacement.
pyrit/​executor/​promptgen/​flrt/​config.py Defines model, algorithm, and output settings.
pyrit/​executor/​promptgen/​flrt/​__init__.py Exposes lazy public exports and an experimental warning.
doc/​references.bib Adds the FLRT paper reference.
doc/​myst.yml Adds the FLRT notebook to navigation.
doc/​code/​executor/​flrt/​0_flrt.py Adds the paired Python usage example.
doc/​code/​executor/​flrt/​0_flrt.ipynb Adds the FLRT usage notebook.
doc/​bibliography.md Includes the FLRT citation key.

Review effort: Balanced

hunar2006 and others added 3 commits November 26, 2026 19:59
Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
hunar2006 and others added 2 commits November 26, 2026 21:19
Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FEAT: Add FLRT fluent adversarial prompt generator alongside GCG

2 participants