Repository navigation
Conversation
With no forward hooks on hook_scale / hook_normalized, the native-autograd path computed float32 norm statistics and then returned the wrapped module's output anyway. Return that output directly. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jlarson4
left a comment
There was a problem hiding this comment.
Thanks for putting this together @davidquarel. Just one note on the global-hook trade-off you mentioned in the PR note.
| if self.hook_scale.bwd_hooks or self.hook_normalized.bwd_hooks: | ||
| warnings.warn(NATIVE_PATH_BWD_FALLBACK_WARNING) | ||
| return self._python_norm_forward(x) | ||
| if not any( |
There was a problem hiding this comment.
The global-hook trade-off you mentioned does conflict with an established convention we have in AttentionBridge._fire_post_hoc_hook. That method maintains global module hooks in view for its own fast path. Would it be possible to adjust this condition to cover the global hook dicts as well (and add a test to ensure this) so both fast paths follow the same convention?
There was a problem hiding this comment.
Okay, I've gotten Claude to make this change by checking the condition
if not (
module_hooks._global_forward_hooks
or module_hooks._global_forward_pre_hooks
or any(
p._forward_hooks or p._forward_pre_hooks
for p in (self.hook_scale, self.hook_normalized)
)
):
instead. Admittedly I'm working blind here as I don't really have a good mental picture of the internal guts of TransformerLens, so hopefully this is what's needed?
… fast path Match AttentionBridge._fire_post_hoc_hook: skip the hook-only statistics only when no global forward / forward-pre hook is registered, so a global hook still sees and can edit hook_scale / hook_normalized. Adds a test with global pre and post hooks that edit hook_scale. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks for making those updated @davidquarel! Approved and merging now |
Description
When nothing is hooked on
hook_scale/hook_normalized,NormalizationBridge's native-autograd path computed float32 norm statistics and then returned the wrapped module's output anyway. This returns that output directly. A forward hook on either point (viaadd_hookor plain torch) takes the existing path.One behaviour change: a global torch module hook (
torch.nn.modules.module.register_module_forward_hook) no longer seeshook_scale/hook_normalizedwhen nothing else is hooked on them, since the check only looks at those hook points' own hook dicts.KV-cached decode through the bridge is CPU-bound, and this is the largest per-call cost that does nothing when unhooked. Qwen2.5-1.5B, bf16, batch 1 (RTX 5090), median decode step: 30.1 → 26.4 ms. Logits, greedy tokens and
run_with_cachecontents (incl.hook_scale) are identical todev.Type of change
Checklist:
test_native_path_unhooked_returns_hf_outputfails ondev;test_native_path_raw_torch_hook_on_hook_scale_firesguards the hooked path)devon a GPU machine, all CPU/CUDA device mismatches that pass with the GPU hidden). Full suites run on 8671e55; after rebasing onto d118883, check-format, mypy andtests/unit/model_bridge/generalized_componentsre-run and pass🤖 Generated with Claude Code