Repository navigation
MAINT: Fix the ty unsound-assignment findings in GCG (UA-1) - #4514
Open
Roman Lutz (romanlutz) wants to merge 3 commits into
Open
Roman Lutz (romanlutz) wants to merge 3 commits into
Roman Lutz (romanlutz) wants to merge 3 commits into
Conversation
Fix the 28 unsound-assignment findings in pyrit/executor/promptgen/gcg and remove its four files from the temporary override. - Loss terms use nn.functional.cross_entropy, which is typed to return a Tensor, instead of calling an nn.CrossEntropyLoss module (returns Any). - AttackPrompt decodes through one checked helper, so its text properties return str, and generate() rejects a non-tensor model result. - The optional selected-logits loss interface is a runtime-checkable protocol instead of cast(Any). A loss that implements only part of it now uses the full-logits path. - The candidate proposer rejects a worker gradient that is not a tensor. - get_goals_and_targets validates inline goal and target lists and only reads the ones the CSV files do not replace. - GCGMultiPromptAttack and MultiPromptAttack declare class-level defaults for attributes that were read through getattr. - Two casts mark where the untyped managers mapping produces a PromptManager and a MultiPromptAttack. MultiPromptAttack.test and test_all were annotated as returning list[list[bool]] for the jailbreak results, but NumPy stacks each (jailbroken, exact_match) pair into one integer array, so the values are 0 or 1. The annotations now say list[list[int]]; the values are unchanged. Part of microsoft#4402 (UA-1). Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are recorded for the GCG type-boundary changes.
No open issues 🎉
What changed in this PR
Addresses UA-1 of #4402 by tightening type boundaries in PyRIT’s GCG prompt-generation code and enabling ty’s unsound-assignment checks for four implementation files.
Changes:
- Uses typed functional loss calls, a selected-logits protocol, explicit attribute defaults, and targeted factory casts.
- Validates tokenizer, model, worker, and inline configuration values; corrects jailbreak-result annotations to integers.
- Adds regression coverage for validation, inline-list copying, and full-logits fallback behavior.
| File | Description |
|---|---|
tests/unit/executor/promptgen/gcg/test_get_goals_and_targets.py |
Tests inline validation, copying, tuple support, and CSV precedence. |
tests/unit/executor/promptgen/gcg/test_gcg_proposal.py |
Tests rejection of non-tensor worker gradients. |
tests/unit/executor/promptgen/gcg/test_gcg_evaluation.py |
Tests fallback for incomplete selected-logits interfaces. |
tests/unit/executor/promptgen/gcg/test_gcg_core.py |
Tests model/tokenizer boundaries and integer result types. |
tests/unit/executor/promptgen/gcg/test_data_and_config.py |
Explicitly initializes held-out lists in configuration mocks. |
pyrit/executor/promptgen/gcg/default_implementations.py |
Replaces loss-module calls with typed functional calls. |
pyrit/executor/promptgen/gcg/attack/gcg/gcg_attack.py |
Declares defaults for optional strategy attributes. |
pyrit/executor/promptgen/gcg/attack/gcg/candidate_proposer.py |
Validates worker gradients before tensor operations. |
pyrit/executor/promptgen/gcg/attack/base/attack_manager.py |
Adds typed boundaries, loss protocol, configuration validation, and corrected result annotations. |
pyproject.toml |
Removes four GCG files from the temporary typing override. |
Review effort: Balanced
Resolve the pyproject.toml conflict with microsoft#4544 (UA-2): both changes remove their own files from the temporary unsound-assignment override, so the merged list drops the UA-2 and the GCG entries. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Resolve the pyproject.toml conflict with UA-3, UA-4, UA-5 and UA-7 (microsoft#4520, microsoft#4537, microsoft#4519, microsoft#4517): each change removes its own files from the temporary unsound-assignment override, so the merged list drops those and the GCG entries and keeps only the UA-6 files. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
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
UA-1 for #4402: fixes the 28
unsound-assignmentfindings inpyrit/executor/promptgen/gcgand removes its four files from the temporary override inpyproject.toml, so the rule now checks them.nn.functional.cross_entropy(..., reduction="none"), which is typed to return aTensor. Calling annn.CrossEntropyLossmodule returnsAny. The results are the same.AttackPromptdecodes through one helper that raisesTypeErrorwhen the tokenizer does not return a string, so its text properties returnstrinstead ofAny.generate()raisesTypeErrorwhen the model does not return a tensor.cast("Any", ...).TypeErrorwhen a worker's gradient result is not a tensor.get_goals_and_targetsvalidates that inline goal and target lists are lists of strings.GCGMultiPromptAttackandMultiPromptAttackdeclare class-level defaults for attributes that were read throughgetattr.casts mark where the untypedmanagersmapping produces aPromptManagerand aMultiPromptAttack. Typing that mapping would remove them, but it touches seven signatures in three files, so it is left for a separate change.One finding was a real mismatch.
MultiPromptAttack.testandtest_allwere annotated as returninglist[list[bool]]for the jailbreak results, but NumPy stacks each(jailbroken, exact_match)pair into one integer array, so the values are0or1. The annotations now saylist[list[int]]. The values are unchanged, because the JSON log encoder does not handle NumPy booleans.Behavior changes:
get_required_logit_positionswithoutcompute_loss_from_selected_logitsnow uses the full-logits path. Before, it failed withAttributeError.get_goals_and_targetsreturns copies of inline lists and only reads the ones the CSV files do not replace. Two existing tests passed aMagicMockconfig with the inline test lists unset; they now set them to[].🤖 Generated with Claude Code