FIX: Prevent GCG model gradient accumulation#2244
Open
romanlutz wants to merge 2 commits into
Open
Conversation
Compute coordinate gradients with input-only autograd, keep model ownership inside persistent workers, and stream prompt aggregation. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 5c847c5a-6917-4e2b-81a6-e511d9cbbe6e
Contributor
There was a problem hiding this comment.
Pull request overview
This PR reduces GCG CUDA memory usage and improves throughput by preventing unnecessary model parameter-gradient materialization and by avoiding repeated model serialization in the multiprocessing worker pipeline, while keeping GCG behavior/API consistent.
Changes:
- Compute coordinate gradients via
torch.autograd.grad(loss, one_hot)to avoid accumulating per-parameter gradients. - Introduce typed worker operations/tasks (
ModelWorkerOperation,ModelWorkerTask) so queued multiprocessing work no longer carries a model payload per operation. - Stream prompt-gradient aggregation with FP32 accumulation for FP16/BF16 inputs, and remove
gc.collect()/ per-steptorch.cuda.empty_cache()from hot paths.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
pyrit/executor/promptgen/gcg/attack/gcg/gcg_attack.py |
Switches token-gradient computation to autograd.grad and updates worker dispatch to use typed operations without model payloads. |
pyrit/executor/promptgen/gcg/attack/base/attack_manager.py |
Adds ModelWorkerOperation/ModelWorkerTask, updates worker execution to use the persistent worker-owned model, and streams gradient reduction with FP32 accumulation where needed. |
tests/unit/executor/promptgen/gcg/test_gcg_core.py |
Expands unit coverage to validate spawn-safe task payloads, worker-owned model usage, streamed gradient reduction, and parity vs backward-based gradients. |
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
GCG retained a full gradient tensor for every trainable model parameter while computing coordinate gradients, roughly doubling model-related CUDA memory. The multiprocessing path also serialized a model with every worker operation even though each worker already owned a persistent model.
This change addresses those root causes by:
torch.autograd.grad(loss, one_hot)gc.collect()and per-steptorch.cuda.empty_cache()calls from GCG hot pathsA fresh-process TinyLlama 1.1B FP16 benchmark on an RTX 2000 Ada 8 GB ran 8 steps across 3 repeats in both local and multiprocessing modes (96 measured steps). All paired loss traces were bitwise identical and live allocations remained flat across every local run.
origin/mainThe live and peak allocation reductions independently confirm that this is not merely allocator-cache trimming. Benchmark scripts and raw results were kept outside the repository.
Closes #961
Tests and Documentation
uv run pytest tests\unit\executor\promptgen\gcg\test_gcg_core.py -q(56 passed)uv run ruff format --checkon the changed filesuv run ruff checkon the changed filesuv run ty checkon the changed filesNo documentation changes are needed because this preserves the existing GCG API and attack behavior.