Skip to content

fix: warn on in-place edits to post-hoc attention hooks - #2022

Open
emerardd wants to merge 2 commits into
TransformerLensOrg:devfrom
emerardd:fix/post-hoc-inplace-edit-warning
Open

emerardd wants to merge 2 commits into
TransformerLensOrg:devfrom
emerardd:fix/post-hoc-inplace-edit-warning

Conversation

@emerardd

@emerardd emerardd commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fix an omission in the existing discarded-edit warning for post-hoc attention pattern/scores hooks.

These hooks run after the wrapped HF module has computed its attention output. Previously, _fire_post_hoc_hook() checked only replacement objects against the input tensor. An in-place edit could therefore change the observed weights without influencing the attention output or triggering the warning. Returning the same tensor, an alias, None, or a clone of the already-mutated input all bypassed the check.

Compare the final values against a detached pre-hook snapshot when local forward/pre-forward hooks are attached. This covers inference tensors without mutation counters, keeps read-only and equal-valued hooks silent, and treats unchanged NaNs as unchanged. The model's attention computation and hook return behavior remain unchanged.

The unhooked path and internal-cache-only hooks do not snapshot or compare the pattern. Private read-only cache markers survive registration and batch-removal wrappers. Unknown user callbacks, pre-hooks and global PyTorch hooks still require a transient snapshot; no wall-clock performance benchmark is claimed. Native attention's writable pattern seam is unchanged.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Validation

  • 143 passed across post-hoc hooks, native pattern writes, HookPoint, cache aliases/slicing/batch-removal and hook registration/removal tests.
  • Includes 43 new cases, with 28 added during review: no pattern copy/comparison for internal-cache-only paths, mixed cache/user edits, local/global pre-hooks, wrapper propagation and callback removal, alongside the original in-place/inference/equal-value cases.
  • Full mypy .: 391 source files passed.
  • Changed-file pycln/isort/Black checks and git diff --check passed.

The complete suite, pretrained end-to-end interventions, GPU/MPS and performance checks were not run. Existing SWIG deprecation warnings remain.

Checklist

  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have not rewritten tests relating to key interfaces which would affect backward compatibility

This restores the existing warning for previously missed writes; the warning checkbox is intentionally unchecked. Only the affected unit-test surface was run locally. No public API or documentation change is needed.

@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 together this bug fix! I do have one concern about the .clone() we are using in _fire_post_hoc_hook. Please take a look at my comment below and let me know if you have any questions

# Snapshot only active hooks: inference tensors have no mutation counter,
# and an in-place edit also changes every alias used for comparison.
original = (
tensor.detach().clone() if hook._forward_hooks or hook._forward_pre_hooks else tensor

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.

If we allowed this change, every hooked forward would copy and compare the full pattern, a plain run_with_cache would pay for it on every delegating attention layer. This would be an incredible loss of speed for the tool. We will need to find an alternative condition to check for solving this problem. Could the snapshot be limited to the cases that need it, so read-only hooks stay free?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed, the cache-only path should not pay for this. Internal caching callbacks are now marked through registration and batch-removal wrappers, so plain run_with_cache and composed caching hooks skip both the snapshot and tensor comparisons. Unknown user callbacks, pre-hooks and global PyTorch hooks still use the checked path, including when mixed with caching. The marker belongs to each callback, not the HookPoint, so removing cache hooks cannot leave subsequent user hooks classified as read-only.

assert _discard_warnings(records) == []


def test_no_hooks_do_not_clone_tensor(bridge, monkeypatch):

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.

Removing the _forward_pre_hooks half of the gate, or the dtype guard on the NaN comparison, leaves these tests green. Can we add a test for each case or drop it if it isn't needed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added targeted cases for mutating and read-only pre-hooks, including inference mode, and removed the redundant dtype guard. The cache fast-path cases also forbid pattern cloning/comparison, while mixed cache/user hooks and callback removal retain the discarded-edit warning.

@koriyoshi2041 koriyoshi2041 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Validated changed head 91e2ccc locally on macOS arm64: the focused post-hoc hook file passes 47/47. I also traced the cache marker through get_caching_hooks, add_caching_hooks, and wrapper installation: the fast path is limited to library-owned cache callbacks, while pre-hooks, global hooks, and any unknown forward hook retain the snapshot comparison. Removing cache hooks also leaves later user hooks on the checked path. This addresses the earlier clone concern without weakening the discarded-edit warning.

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

Approved! Just re-running the failed CI task, assuming it passes I will merge

This branch has not been deployed

No deployments
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.

3 participants