fix: lift mutated buffers owned by a submodule - #4472
Open
shoumikhin wants to merge 1 commit into
Open
Conversation
A mutated buffer that belongs to a submodule is never lifted, so the rewrite
silently does nothing for it:
lift_mutated_buffers: get_attr target inner.cache not found on gm; skipping
A get_attr target is fully qualified, so such a buffer arrives as "inner.cache",
and neither hasattr nor getattr walks a dotted path:
hasattr(module, "inner.cache") False
module.get_buffer("inner.cache") the tensor
Only a buffer owned directly by the top-level module resolves today, while any
model that keeps state inside its layers is skipped with a warning.
Use get_buffer, which resolves through the submodules, and keep the warning for
a target that genuinely does not exist.
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.
The problem
A mutated buffer that belongs to a submodule is never lifted. The rewrite skips it with a
warning and carries on:
The buffer is there. The lookup is what fails. A
get_attrtarget is fully qualified, so abuffer owned by a submodule arrives as
inner.cache, and neitherhasattrnorgetattrwalks a dotted path:
So only a buffer owned directly by the top-level module resolves. Any model that keeps state
inside its layers, which is the usual way to write one, is skipped.
The fix
Use
get_buffer, which resolves through the submodules:The warning is kept for a target that genuinely does not exist, so a real problem is still
reported. Nothing changes for a top-level buffer.
Testing
Added
test_nested_buffer_lifted, which is the same shape as the existingtest_single_buffer_liftedbut with the buffer one level down. It asserts the buffer islifted, the qualified name is preserved as
inner.cache, the flattened placeholder isbuf_inner_cache, the rebuiltforwardaccepts it, and no trailingcopy_remains.The test fails on the current code with exactly the warning above, and passes with the fix:
Worth noting why the existing tests do not catch this: all of them use a buffer owned by
the top-level module, so the name has no dots and
getattrhappens to work.