Repository navigation
FIX GCG: compute sampled control positions with integer math - #4781
Queued
devangpratap wants to merge 3 commits into
Queued
devangpratap wants to merge 3 commits into
devangpratap wants to merge 3 commits into
Conversation
Contributor
There was a problem hiding this comment.
🟢 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
Roman Lutz (romanlutz)
approved these changes
Dec 3, 2026
Roman Lutz (romanlutz)
enabled auto-merge
December 3, 2026 07:26
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #4775.
The GCG sampler built the position each candidate mutates with a float-step
torch.arange, which returnsbatch_size + 1positions 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 raisedIndexError.StandardGCGSampling.sample_candidatesandGCGPromptManager.sample_controlnow computetorch.arange(batch_size) * n // batch_size, so there is always exactly one in-range position per candidate.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.