Repository navigation
fix(loader): restore direct nonpersistent rotary buffers - #3580
Conversation
`materialize_direct_meta_tensors` defaults `device=None`, and the new restoration call passed it straight through to `safe_move_to`, which resolves it with `torch.device(None)` and raises `TypeError`. Before this change the default was harmless because the base materializer never used the value. Resolve `None` to the device of an already-materialized sibling buffer, or CPU when the submodule holds nothing but meta tensors. Also match the recursive path's restore condition instead of only rebuilding meta buffers: a non-persistent buffer that exists on the wrong device is rebuilt from the constructor template the same way. Without this a shell whose rotary buffer materialized on CPU would be left there even when an accelerator device was requested. Adds a regression test for the `device=None` default.
76c9ed3 to
4e2884d
Compare
Qubitium
left a comment
There was a problem hiding this comment.
Review
Rebased onto main after #3579 merged and pushed two commits to the branch:
the layer-1 carry-over is gone and one bug I found is fixed. PR is now
MERGEABLE with changedFiles: 2 instead of 4.
Was conflicting — now rebased
The branch still carried its own copy of layer 1 (254b3112), which conflicted
with the merged 67cb384f. That version of the commit is obsolete: the merged
one includes the _rope_type_labels fix from my review of #3579. I dropped it
and rebased only the layer-2 commit:
git rebase --onto origin/main 254b3112
The diff is now exactly the layer-2 change and nothing else:
gptqmodel/utils/structure.py +19, tests/test_offload_files.py +124/-1.
No hf.py or nested-rope test changes remain in the PR. This also removes the
need for reviewers to mentally subtract the predecessor layer described in the
Stack section.
Bug found and fixed
device=None crashed the restoration path. materialize_direct_meta_tensors
defaults to device=None, and the new call passed that straight into
_restore_missing_nonpersistent_buffers → safe_move_to → as_torch_device(None)
→ torch.device(None):
TypeError: device() received an invalid combination of arguments - got (NoneType)
This is reachable: shell_direct_meta_materialize defaults device=None and
only resolves it when non-None (models/base.py:4210). Before this PR the
default was harmless because the base materializer never used the value; the new
code does use it, so a shell whose only meta tensors are constructor-owned
buffers would now raise instead of materializing.
Fixed in 4e2884d3 by resolving None to an already-materialized sibling
buffer's device, falling back to CPU when the submodule holds nothing but meta
tensors. This matches what the recursive path gets implicitly, since it always
receives a concrete device.
Second change, same commit
The restore filter only rebuilt meta buffers:
if name in nonpersistent and _is_meta_tensor(buffer)The recursive path also rebuilds non-persistent buffers that are not on the
requested device:
if (getattr(target_buffer, "is_meta", False)
or target_buffer.device.type == "meta"
or target_buffer.device != device):That divergence means a direct-materialized shell whose rotary buffers landed on
CPU would stay there even when an accelerator was requested — silently the wrong
device rather than an error. I aligned the direct path with the recursive
condition (_is_meta_tensor(buffer) or buffer.device != restore_device).
Test added
test_lazy_turtle_direct_meta_defaults_device_for_nonpersistent_buffers — fails
before the fix with the exact TypeError above, passes after.
Verification
tests/test_offload_files.py 58 passed, 1 skipped (matches your 69/1 group count)
tests/test_lazy_turtle_conversion_mapping.py
tests/test_lazy_sync_packed_modules.py
tests/test_stage_inputs_capture.py 44 passed
ruff check / ruff format --check clean
I also confirmed the _RotaryWrapper fixture change is correct and necessary:
LlamaRotaryEmbedding.__init__ no longer accepts device, so
LlamaRotaryEmbedding(config, device=...) would raise. Good catch on your side.
Notes (non-blocking)
- Absent non-persistent buffers are still not restored. Both the direct and
recursive paths iteratenamed_buffers(), so a buffer deleted from_buffers
entirely (rather than left on meta) is never considered. I verified this is
pre-existing onmain, not introduced here, and left it alone — but if the
intent is "restore constructor-owned buffers that are missing from the
checkpoint", iterating_non_persistent_buffers_setwould be the complete fix.
Worth a separate issue rather than expanding this PR. - Test file needed
ruff format; my push includes it. CI only runs
ruff check, so this was not failing the gate. - The
_non_persistent_buffers_setmembership check relies on a private
nn.Moduleattribute. That is already the established pattern in this file,
so no objection — just noting the coupling.
Summary
Direct LazyTurtle materialization loads checkpoint-backed leaves but can leave constructor-only rotary buffers on meta. Restore missing direct nonpersistent buffers through the existing constructor restoration path. Cover direct generic and DeepSeek rotary state; keep the test fixture compatible with the current Llama rotary constructor.
Stack
Layer 2/36 of a dependency-ordered fork extraction. Depends on #3579. Review only this layer.
GitHub requires this cross-fork PR to target upstream
main, so its Files changed tab includes earlier stack layers. After the predecessor lands, rebase this branch and retarget the next layer; do not merge later layers out of order. No predecessor is merged automatically.Tests
Covered by the complete offload/export group: 69 passed, 1 hardware skip. Used Python 3.12, Torch 2.14.1, Transformers 5.19.0. No model quantization, GPU work, downloads, or heavyweight model validation. Unit fixtures are small and CPU execution is constrained to one math thread.
Review and remaining validation
This layer and its dependency diff were reviewed for scope, existing extension points, and compatibility with the resolved fork snapshot. New focused regression coverage is included. Kept as a draft for maintainer review; hardware paths and end-to-end model behavior have not been revalidated in this session. No speed claim. Original integration/history is preserved in tpurtell#3.