Skip to content

FIX GCG: compute sampled control positions with integer math - #4781

Queued
devangpratap wants to merge 3 commits into
microsoft:mainfrom
devangpratap:fix-gcg-sampler-positions
Queued

devangpratap wants to merge 3 commits into
microsoft:mainfrom
devangpratap:fix-gcg-sampler-positions

Conversation

@devangpratap

Copy link
Copy Markdown
Contributor

Description

Fixes #4775.

The GCG sampler built the position each candidate mutates with a float-step torch.arange, which returns batch_size + 1 positions for some control length and batch size pairs (20 tokens with batch size 244, 9 with 500). The extra position is out of range, so the first step raised IndexError.

  • StandardGCGSampling.sample_candidates and GCGPromptManager.sample_control now compute torch.arange(batch_size) * n // batch_size, so there is always exactly one in-range position per candidate.
  • Added a test over four failing pairs for both samplers.

Positions are unchanged for the default sizes. Over control lengths 1 to 100 and batch sizes 1 to 2048, 92 pairs that did not crash move one candidate by one position, because the float step had rounded just below the exact value.

Copilot AI balanced review requested due to automatic review settings December 1, 2026 18:09

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.

🟢 Approval recommended

The review reports no active issues with the integer-arithmetic sampling fix and its added regression coverage.

No open issues 🎉

What changed in this PR

Fixes #4775 by replacing floating-point position sampling with integer arithmetic in both GCG samplers, preventing out-of-range indices for affected control lengths and batch sizes.

Changes:

  • Compute exactly one valid mutation position per candidate in both samplers.
  • Add regression tests for four failing size pairs, checking candidate shape, single-token mutations, and balanced position coverage.
File Description
tests/​unit/​executor/​promptgen/​gcg/​test_default_implementations.py Adds parameterized regression coverage for both samplers.
pyrit/​executor/​promptgen/​gcg/​default_implementations.py Uses integer arithmetic for standard sampler positions.
pyrit/​executor/​promptgen/​gcg/​attack/​gcg/​gcg_attack.py Applies the same position calculation to the legacy sampler.

Review effort: Balanced

@romanlutz Roman Lutz (romanlutz) self-assigned this Dec 3, 2026

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.

BUG: GCG candidate sampler raises IndexError for some control length and batch size pairs

3 participants