Skip to content

MAINT: Fix the ty unsound-assignment findings in GCG (UA-1) - #4514

Open
Roman Lutz (romanlutz) wants to merge 3 commits into
microsoft:mainfrom
romanlutz:maint/ty-unsound-assignment-ua1
Open

Roman Lutz (romanlutz) wants to merge 3 commits into
microsoft:mainfrom
romanlutz:maint/ty-unsound-assignment-ua1

Conversation

@romanlutz

Copy link
Copy Markdown
Contributor

Description

UA-1 for #4402: fixes the 28 unsound-assignment findings in pyrit/executor/promptgen/gcg and removes its four files from the temporary override in pyproject.toml, so the rule now checks them.

  • Loss terms call nn.functional.cross_entropy(..., reduction="none"), which is typed to return a Tensor. Calling an nn.CrossEntropyLoss module returns Any. The results are the same.
  • AttackPrompt decodes through one helper that raises TypeError when the tokenizer does not return a string, so its text properties return str instead of Any. generate() raises TypeError when the model does not return a tensor.
  • The optional selected-logits loss interface is a private runtime-checkable protocol instead of cast("Any", ...).
  • The candidate proposer raises TypeError when a worker's gradient result is not a tensor.
  • get_goals_and_targets validates that inline goal and target lists are lists of strings.
  • 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. 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.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, because the JSON log encoder does not handle NumPy booleans.

Behavior changes:

  • A loss function that implements get_required_logit_positions without compute_loss_from_selected_logits now uses the full-logits path. Before, it failed with AttributeError.
  • get_goals_and_targets returns copies of inline lists and only reads the ones the CSV files do not replace. Two existing tests passed a MagicMock config with the inline test lists unset; they now set them to [].

🤖 Generated with Claude Code

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>

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

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

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.

2 participants