Skip to content

NormalizationBridge: skip hook-only statistics when nothing is hooked - #2034

Merged
jlarson4 merged 2 commits into
TransformerLensOrg:devfrom
davidquarel:perf-norm-skip-unobserved-stats
Nov 23, 2026
Merged

jlarson4 merged 2 commits into
TransformerLensOrg:devfrom
davidquarel:perf-norm-skip-unobserved-stats

Conversation

@davidquarel

Copy link
Copy Markdown

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 (via add_hook or plain torch) takes the existing path.

One behaviour change: a global torch module hook (torch.nn.modules.module.register_module_forward_hook) no longer sees hook_scale / hook_normalized when 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_cache contents (incl. hook_scale) are identical to dev.

Type of change

  • Bug fix (non-breaking change which fixes an issue) (performance; one edge-case behaviour change, above)

Checklist:

  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works (test_native_path_unhooked_returns_hf_output fails on dev; test_native_path_raw_torch_hook_on_hook_scale_fires guards the hooked path)
  • New and existing unit tests pass locally with my changes (check-format, mypy, unit, docstring and acceptance pass; integration has the same 53 failures as dev on 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 and tests/unit/model_bridge/generalized_components re-run and pass
  • I have not rewritten tests relating to key interfaces which would affect backward compatibility

🤖 Generated with Claude Code

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 jlarson4 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@jlarson4

Copy link
Copy Markdown
Collaborator

Thanks for making those updated @davidquarel! Approved and merging now

@jlarson4
jlarson4 merged commit 9d97c53 into TransformerLensOrg:dev Nov 23, 2026
31 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