Skip to content

Read the resident tensor names only where they are used - #11159

Merged
danielhanchen merged 3 commits into
mainfrom
pr/mtp-names-push-only
Sep 17, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
pr/mtp-names-push-only

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Follow-up to #11042, which merged before this was caught.

unsloth_generic_save's full-finetune branch collected _mtp_tensor_names for every save, but only the push branch consumes them: the guard hides an MTP declaration the weights do not back while push_to_hub serialises, because a push has no local folder to repair afterwards. A local save has one, and reconciles it through reconcile_mtp_config(save_directory) after save_pretrained has written.

So for a local save_method = "lora" or "merged_4bit_forced" with no caller-supplied state_dict, the branch called model.state_dict() for a list of names nothing then read, and save_pretrained collected the state dict again immediately afterwards. On a large, offloaded or sharded model that materialises every weight twice, and under FSDP or DeepSpeed the collection is a collective the other ranks are not making, so it can stall rather than merely cost memory.

The collection moves inside if push_to_hub:. Nothing else changes: the same names, derived the same two ways (the caller's state_dict when there is one, the resident one otherwise, None when it cannot be read so the guard disarms), and the same _mtp_config_matching_tensors around the same push.

What changes for an input that already succeeded

Nothing, except that a local save no longer performs one full state_dict() collection it never used. A push is byte for byte what it was; a 16bit save still builds its state dict, because that is the dict being written.

Tests

tests/test_save_mtp_policy.py:

  • a local lora and merged_4bit_forced save through unsloth_generic_save never calls model.state_dict(), while a merged_16bit one still does. Driven through the real function with a counting fake model, so it observes the collection rather than the source.
  • a structural check that every assignment to _mtp_tensor_names sits inside the push_to_hub branch, so it cannot drift back out.

Both fail against the previous behaviour.

The push guard is the only consumer: a local save reconciles the folder it just
wrote. Collecting model.state_dict() for it duplicated save_pretrained's own
collection, which on an offloaded or sharded model materialises every weight and
on a distributed one is a collective the other ranks are not making.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 16, 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 ✅ Completed 2026-09-17T00:24:50.417889Z 2fb5ca7 Manual request
🔒 Security Review ✅ Completed 2026-09-16T22:37:28.077869Z 2fb5ca7 Manual request
ℹ️ 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

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex security review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 2fb5ca7c1a

ℹ️ 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".

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review

Security review completed. No security issues were found in this pull request.

Reviewed commit: 2fb5ca7c1a

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@danielhanchen

Copy link
Copy Markdown
Member Author

Confirmed the collection at unsloth/save.py:5662 runs for local saves too, where only save_pretrained's own state dict is written, so moving it inside the push branch looks right. Will get this reviewed.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@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: 2fb5ca7c1a

ℹ️ 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".

# Conflicts:
#	tests/studio/playwright_queue_localization.py
@danielhanchen
danielhanchen merged commit a9ce3fe into main Sep 17, 2026
27 of 28 checks passed
@danielhanchen
danielhanchen deleted the pr/mtp-names-push-only branch September 17, 2026 03:10
shaojun pushed a commit to shaojun/unsloth that referenced this pull request Sep 29, 2026
)

* Read the resident tensor names only where they are used

The push guard is the only consumer: a local save reconciles the folder it just
wrote. Collecting model.state_dict() for it duplicated save_pretrained's own
collection, which on an offloaded or sharded model materialises every weight and
on a distributed one is a collective the other ranks are not making.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
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