Repository navigation
perf(autogram): Avoid tensor creation in autogram hook - #820
Merged
Merged
Conversation
The forward hook of each module built a CPU tensor of output sizes and called argmin().item() on it to find its smallest output. It now uses Python's min with a key, which selects the same output (the first one in case of ties) without creating a tensor at every forward pass. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ValerianRey
marked this pull request as ready for review
October 7, 2026 16:28
ValerianRey
enabled auto-merge (squash)
October 7, 2026 16:28
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.
At every forward pass of every hooked module,
Hook.__call__built a CPU tensor of its output sizes and calledargmin().item()on it, just to pick the smallest output. It now usesmin(rg_outputs, key=lambda t: t.numel()), which selects the same output (the first one in case of ties) without creating a tensor.Measured on a model with 200 small hooked modules (median of 5 alternated runs of old and new code):
compute_gramianThis is close to a worst case: the gain per module is fixed (about 6 µs), so it's smaller in relative terms with larger modules. The Gramians are bit-identical on CPU and CUDA, and the autogram tests pass.
🤖 Generated with Claude Code