Skip to content

fix(weight_loader): dispatch a whole-model cpu>/disk> target as offload (#6581) - #6582

Merged
Qubitium merged 1 commit into
mainfrom
fix/weightloader-root-offload-dispatch
Dec 1, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/weightloader-root-offload-dispatch

Conversation

@Qubitium

@Qubitium Qubitium commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Fixes #6581

Problem

WeightLoader.dispatch never offloaded a whole-model cpu> / disk> target. With one root rule, _subtree_targets returns only {"": target}; visit then walks the children with no target and gives each tensor-owning leaf a resident AlignDevicesHook(execution_device). Dense GPTQModel.load(..., placement="cpu>accel:0") therefore loaded every weight onto the accelerator, and placement="disk>accel:0" raised ValueError: weight is on the meta device during load.

Change

When the root has a target (only reached when it is offloaded; a resident root returns early), give it to each top-level child, the same way visit already forwards a container's target to its children. The children then take the existing offload branch.

flowchart TD
    A["dispatch: targets = {'': cpu>/disk> accel:0}"] --> B{"root offloaded"}
    B --> C["root AlignDevicesHook(io_same_device)"]
    C --> D["visit(top-level children)"]
    D -. "before: no child target" .-> X["leaf AlignDevicesHook(resident)<br/>cpu>: weights moved to accel<br/>disk>: ValueError on meta"]
    D -- "after: child inherits root target" --> E["attach_align_device_hook(offload=True, weights_map)"]
    E --> F["weights stay on CPU / in offload_dir, onloaded per forward"]
    style X fill:#f8d7da,stroke:#b02a37
Loading

Test plan

Hardware: AMD Radeon AI PRO R9700 (gfx1201), ROCm 7.16, torch 2.16 dev (the code path is device-independent).

New tests in tests/test_weight_loader.py:

  • test_whole_model_disk_offload_dispatch_runs_on_cpu (disk>cpu, CPU only)
  • test_whole_model_cpu_offload_keeps_weights_off_the_accelerator (cpu>cuda:0; asserts no parameter on the accelerator, allocator delta below a quarter of the parameter bytes, the layer executed on the accelerator, output equal to the reference)
origin/main this branch
new tests 2 failed (ValueError: weight is on the meta device; assert (14848 - 0) < (8960 // 4)) 2 passed
tests/test_weight_loader.py 61 passed, 6 skipped + 2 failed 63 passed, 6 skipped
test_weight_loader.py test_offload_block_inputs.py test_layerwise_device_placement.py test_cpu_offload_device_map_tied_head_4866.py test_cpu_offload_tied_both_copies_4943.py test_offload_disk_container_5145.py test_loader.py all passed (7 skipped: multi-CUDA-only cases)

Real model, GPTQModel.load(model, placement=..., dtype=float16) then greedy generate of 8 tokens:

model / placement before after
TinyLlama-1.1B, cpu>accel:0 all params on cuda:0, 2100.2 MiB allocated after load, peak 2180.6 MiB params on meta (CPU store), 0 MiB after load, peak 206.5 MiB; same tokens ("Paris.\n\n2. B.")
TinyLlama-1.1B, disk>accel:0 ValueError during load loads, peak 206.5 MiB, same tokens
Llama-3.2-1B, cpu>accel:0 all params on accelerator params off-accelerator, peak 583.2 MiB, same tokens as accel:0

uvx [email protected] check on the touched files: clean.

Not covered here: a tied-embedding checkpoint with disk> on the embedding still fails with KeyError: 'lm_head.weight' (the offload index stores only the de-duplicated tied name). That is a separate bug, tracked on its own.

🤖 Generated with Claude Code

With a single root rule _subtree_targets returns only {"": target}, so
dispatch visited the children without a target and gave every leaf a
resident AlignDevicesHook: placement="cpu>accel:0" moved all weights onto
the accelerator and "disk>accel:0" failed placing meta tensors. Hand the
root target to the top-level children so they take the offload branch.

Fixes #6581

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

[BUG] Dense load with whole-model placement="cpu>accel:0" keeps every weight on the accelerator; "disk>accel:0" raises at load

1 participant