Repository navigation
Fix layerwise casting of MultiheadAttention - #4518
Open
remote-controlled-man wants to merge 1 commit into
Open
remote-controlled-man wants to merge 1 commit into
remote-controlled-man wants to merge 1 commit into
Conversation
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.
What does this PR do?
attach_layerwise_casting_hooks()breaks inference throughtorch.nn.MultiheadAttention: it castsout_projto the storage dtype, but attention reads that submodule's weights directly without invoking its forward hook. A float32 attention input with FP16 or FP8 storage then raises a dtype mismatch in both the native and functional attention paths.Treat the attention module as one casting unit, so all projection weights use the compute dtype during forward and return to the storage dtype afterwards. If a skip pattern or class selects a child such as
out_proj, skip the whole attention unit to preserve the exclusion without mixing dtypes. The function docstring describes this behavior.Regression coverage compares outputs and attention weights with an independently cast reference, repeats inference with/without attention weights, and covers FP16/FP8 storage, both input layouts, combined/separate projection weights, and parent/child skip patterns and classes.
Validation
All 8 core regression cases fail on unchanged upstream with a dtype mismatch. On final commit
d164abd:python -m pytest -c pyproject.toml tests/test_hooks.py -q -rs: 24 passed, 3 skipped. The skips require multiple hardware accelerators or CUDA/XPU offload.ruff check .andruff format --check .pass (183 files formatted).Validation ran on CPU with Python 3.12.14 and PyTorch 2.15.0. No GPU, distributed, or offload integration validation is claimed.
Before submitting
AI assistance was used for investigation, implementation, tests, and this description.