Skip to content

PERF: Speed up GCG candidate evaluation - #2884

Merged
Roman Lutz (romanlutz) merged 4 commits into
microsoft:mainfrom
JayYarlagadda:jay/issue-962-prefix-cache
Sep 28, 2026
Merged

Roman Lutz (romanlutz) merged 4 commits into
microsoft:mainfrom
JayYarlagadda:jay/issue-962-prefix-cache

Conversation

@JayYarlagadda

@JayYarlagadda Jayanth Yarlagadda (JayYarlagadda) commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Hey Roman Lutz (@romanlutz) — I profiled the candidate-evaluation path and found that most of its time and memory were spent creating full-vocabulary logits for every candidate and then passing those tensors back to the parent process.

This PR keeps the existing multi-prompt and multi-worker design, but avoids doing that unnecessary work:

  • The built-in cross-entropy loss is calculated inside each model worker, so the worker returns one loss value per candidate instead of the full logits.
  • Models that support logits_to_keep only project the positions needed for the target and control losses.
  • The unchanged prompt prefix can optionally reuse its KV cache across the candidate batch.

Custom loss implementations still use the existing full-logits path. Prefix caching is disabled by default and can be enabled with GCGAlgorithmConfig.use_prefix_cache.

I benchmarked this on an NVIDIA A40 with Qwen/Qwen2.5-7B-Instruct, using a batch size of 512, top-k 256, and 10 GCG steps. I ran the baseline and changed version three times each and excluded the first two steps of every run from the timing summary.

With one prompt, the median step time went from 7.61s to 5.25s, a 31% improvement. With two prompts and transfer enabled, it went from 13.55s to 8.44s, a 37.7% improvement. Peak GPU memory dropped from roughly 40.5 GiB to 24.9 GiB in both cases.

The baseline and optimized versions selected the same final suffix and completed the same number of optimization steps in all six paired runs. The largest difference in the fp16 loss histories was 0.0022, which is why prefix caching remains opt-in.

Validation:

  • 393 GCG unit tests passed
  • Ruff passed
  • ty passed

Would appreciate your thoughts on the approach, especially the decision to keep prefix caching opt-in.

Addresses #962.

@JayYarlagadda

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

@romanlutz
Roman Lutz (romanlutz) added this pull request to the merge queue Sep 28, 2026
Merged via the queue into microsoft:main with commit 5048bcc Sep 28, 2026
49 checks passed
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