Skip to content

fix: keep accelerate offload hook when swapping Linear -> SCLinear - #7

Merged
Allenjin123 merged 1 commit into
fix/sc-sl256-owen-bumpfrom
fix/sc-linear-cpu-offload-hook
Jun 2, 2026
Merged

fix: keep accelerate offload hook when swapping Linear -> SCLinear#7
Allenjin123 merged 1 commit into
fix/sc-sl256-owen-bumpfrom
fix/sc-linear-cpu-offload-hook

Conversation

@heroarmor

Copy link
Copy Markdown
Collaborator

Problem

With device_map="auto" + CPU offload, the original nn.Linear carries an accelerate AlignDevicesHook whose weights_map holds the real CPU weights while the module's own param is a meta placeholder. replace_linears_with_sc dropped that hook when swapping in SCLinear, so the weight stayed meta and the SC forward crashed:

RuntimeError: Tensor on device meta is not on the expected device cuda:0!

This blocked running 70B SC on a single GPU (where the model must offload to fit).

Fix

Transfer the existing _hf_hook from the original module to the new SCLinear (remove from the discarded module first), so offloaded meta weights are materialized to the execution device at forward. Non-offloaded modules (no hook) are unchanged.

Validation

Llama-3.1-70B SC runs on a single GPU via CPU offload, producing bit-identical output to the 2-GPU sharded run.

Pairs with the multi-GPU device-guard fix in scmp_kernels (CrucibleComputingGroup/scmp_kernels#20).

🤖 Generated with Claude Code

With device_map="auto" + CPU offload, the original nn.Linear carries an
accelerate AlignDevicesHook whose weights_map holds the real CPU weights while
the module param is a 'meta' placeholder. replace_linears_with_sc dropped that
hook, so the SCLinear's weight stayed meta and the SC forward crashed with
'RuntimeError: Tensor on device meta is not on the expected device'.

Transfer the existing hook to the replacement module so offloaded weights are
materialized at forward.

Verified: Llama-3.1-70B SC runs on a single GPU via CPU offload, bit-identical
output to the 2-GPU sharded run.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

Copilot AI 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.

Pull request overview

This PR fixes replace_linears_with_sc() so it preserves Hugging Face Accelerate’s device dispatch / CPU offload behavior when swapping nn.Linear modules to SCLinear, preventing “meta tensor” crashes when loading large models with device_map="auto" and CPU offload.

Changes:

  • Detects an existing Accelerate _hf_hook on the original nn.Linear and transfers it to the new SCLinear.
  • Keeps the prior .to(device, dtype) behavior only for non-hooked (non-dispatched/offloaded) modules.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@Allenjin123
Allenjin123 merged commit 7188a86 into fix/sc-sl256-owen-bump Jun 2, 2026
1 check passed
@Allenjin123
Allenjin123 deleted the fix/sc-linear-cpu-offload-hook branch June 2, 2026 04:48
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.

4 participants