Skip to content

Optimizer state offload: track DTensor state loaded on the host when resuming - #14155

Merged
danielhanchen merged 2 commits into
mainfrom
dh/v2-offload-dtensor-resume
Oct 19, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
dh/v2-offload-dtensor-resume

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

With the opt-in UNSLOTH_OFFLOAD_OPTIMIZER_STATE=1 (#13882) and sharded parameters (DTensor, FSDP2), resuming from a checkpoint loses track of the optimizer state that load_state_dict places on the host. The load policy records the loaded value by id() of the DTensor, but offload() looks DTensor state up by its local shard. So the next optimizer.step() never moves that state back and fails with a cuda:0 vs cpu device mismatch.

The load policy now also records the DTensor's local shard. Plain tensors are unchanged, and with the flag off nothing changes because the module is only used when the flag is on.

Tests (GPU)

  • New test_load_state_dict_tracks_dtensor_state_on_host (DTensor params on a 2-rank mesh with a fake process group): it runs offload, a step, a deepcopied checkpoint and a resume into a fresh optimizer. It then checks that the step after resume is bitwise equal to an optimizer that never stopped.
  • tests/test_optimizer_state_offload.py: 1 failed / 36 passed on main, 37 passed here.

Not run here: a real multi-GPU FSDP2 resume through accelerate.

load_state_dict recorded the outer DTensor while offload() looks a DTensor up by its
local shard, so a resume from an offloaded sharded checkpoint left the state on the
host untracked and the next step failed on a cuda/cpu device mismatch.
@danielhanchen danielhanchen self-assigned this Oct 18, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review 🔄 Running since 2026-10-19T00:55:36.286743Z 885b666 New commits
🔒 Security Review ⚠️ Failed 2026-10-18T20:18:13.099363Z 1959b5d PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 1959b5d964

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit d7eef8e into main Oct 19, 2026
@danielhanchen
danielhanchen deleted the dh/v2-offload-dtensor-resume branch October 19, 2026 11:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant