Skip to content

Studio: stop merging the model twice when pushing a merged export to the Hub - #11050

Merged
Lyxot merged 8 commits into
unslothai:mainfrom
NilayYadav:fix-export-merged-hub-reuse
Sep 16, 2026
Merged

Lyxot merged 8 commits into
unslothai:mainfrom
NilayYadav:fix-export-merged-hub-reuse

Conversation

@NilayYadav

Copy link
Copy Markdown
Collaborator

Exporting a LoRA as Merged 16-bit or 4-bit with Push to Hub merged the whole model twice. It saved the merged model to your folder, then merged it again into a second copy just for the upload. That made the export slower, needed room for an extra copy of the model, and uploaded a different copy than the one on disk.

The push now uploads the folder that was already saved, like the GGUF (#10317) and FP8 or torchao (#10943) exports already do. If Private is on, an existing public repo is made private first, same as the FP8 export.

After training a small Qwen3-0.6B LoRA and exporting it as Merged 16-bit with Push to Hub, the main log shows a second merge into a folder named after the repo. With this change there is one merge, the upload comes from the save folder, and the uploaded files are the same. Merged 4-bit is covered by unit tests only, because saving it locally already fails on transformers 5.5.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 15, 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-16T08:12:36.728784Z 46f20bc 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 93ac172158

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

Comment thread studio/backend/core/export/export.py Resolved
@NilayYadav

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 70f808afd3

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

Comment thread studio/backend/core/export/export.py Resolved
@NilayYadav

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 544d8fd9b5

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

Comment thread studio/backend/core/export/export.py Resolved
@NilayYadav

Copy link
Copy Markdown
Collaborator Author

@codex review

1 similar comment
@NilayYadav

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4b643d9aff

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

Comment thread studio/backend/core/export/export.py Resolved
@NilayYadav

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 95bb36125e

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

@NilayYadav

Copy link
Copy Markdown
Collaborator Author

Review + GitHub Actions and Studio evidence

Pinned head: 95bb36125e131a74b6b14f797cff705d8fa7cc9b. Codex items from rounds 1-4 were reproduced and fixed on this branch (reused save folder uploaded whole, existing model card overwritten, card failure blocking the upload, and a push with no weights reported as success after a Kaggle /tmp redirect).

Studio, Chromium 1440×1100, Modal L4, transformers 5.5.0, local stand-in Hub (no real token). Export page, unsloth/Qwen2.5-0.5B-Instruct, Merged Model, 16-bit, Push to Hub:

  • Merge base 128659085b: red banner Export failed: unsloth_push_to_hub() got an unexpected keyword argument 'safe_serialization', Failed 15%. Nothing reaches the Hub. The error comes from push_to_hub_merged → unsloth_generic_save → model.push_to_hub(...), after the local save has already finished.
  • Head 95bb361, fresh folder: Export finished and pushed to Hugging Face Hub., one save pass. The Hub receives one repo create, the model and tokenizer files, then README.md.
  • Head 95bb361, reused folder with a planted planted-leftover.txt: success. A clean second save goes to exports/.hub-upload-* and is removed afterwards. The leftover file is not in any commit.

PR 11050 Studio before/after

PR 11050 reused folder after

GitHub Actions A/B. Same workflow and the same tests from 95bb361 on every branch; only studio/backend/core/export/export.py differs:

  • main's export.py — run: 12 failed, 62 passed. Fails at the second merge, e.g. ['merged_16bit', 'push_to_hub_merged'] == ['merged_16bit'] (the same for merged_4bit_forced), plus the card, reused-folder and no-weights tests
  • original PR head 93ac172 — run: 11 failed, 63 passed. Reused folder only merges once and uploads the folder as is; card pushed before the upload (Export failed: validate-yaml unreachable when it fails); an existing card is overwritten; a folder with no weights is pushed
  • 4b643d9 — run: 3 failed, 71 passed. Reused folder falls back to push_to_hub_merged (['merged_16bit', 'push_to_hub_merged'] == ['merged_16bit', 'merged_16bit']), and a folder with no weights is still reported as a success (assert True is False)
  • 95bb361 — run: 74 passed

Full CI on a fresh-main staging copy of 95bb361 (staging PR): 22 checks passed and none failed so far; the rest are still running.

The red pre-commit.ci - pr check here is not caused by this diff. Its ruff-format-with-kwargs hook rewrote 1,226 files outside this PR and then could not apply the fix. The same hooks pass on this PR's four files, and upstream main commits 201790e and 73eb6fb fail the same check.

@danielhanchen

Copy link
Copy Markdown
Member

Confirmed the merged Push to Hub path in studio/backend/core/export/export.py still falls through to push_to_hub_merged and merges a second copy, and that the saved folder is never the one uploaded. Will get this reviewed.

@Lyxot Lyxot self-assigned this Sep 16, 2026
…d disk

Three follow-ups to uploading the saved merged export instead of merging a second time.

A merged folder with no weights is no longer refused. The refusal was justified by a Kaggle /tmp redirect, which cannot reach this call: `kaggle_tmp_redirect` returns an absolute path unchanged, and Studio always resolves the save directory to one. Before the folder was uploaded at all such a push still succeeded through `push_to_hub_merged`, so the weights test now sits in the branch condition and a folder without them falls through to that merging push rather than failing the export.

`export_metadata.json` stays out of the repo. It is Studio bookkeeping the Chat page reads from disk, and its base model comes from an adapter's `base_model_name_or_path`, so it can be a local filesystem path — the same leak the model card already avoids, and the GGUF push already keeps the file local. Finder metadata is ignored for the same reason, and no longer makes a fresh export folder look reused.

The clean second save for a reused folder is staged where it fits. Pinning it beside the export puts a second full checkpoint on a filesystem that has just taken the merge, and `merge_and_overwrite_lora` refuses a save its destination cannot hold; pinning it to the temporary directory fails wherever that is the smaller one. Free space alone is not enough either, since only the export directory itself has to be writable, so the roomier candidate is tried first and the other is used when it refuses the folder.
@Lyxot

Lyxot commented Sep 16, 2026

Copy link
Copy Markdown
Collaborator

Reviewed the branch and pushed c9d1e2d with three follow-ups. The core of the PR is unchanged: the saved merged export is uploaded instead of being merged a second time.

A merged folder with no weights is no longer refused. The refusal cited a Kaggle /tmp redirect, but that cannot reach this call: kaggle_tmp_redirect returns an absolute path unchanged, and Studio always resolves the save directory to an absolute path. It was also a regression — before the saved folder was uploaded at all, such a push still succeeded through push_to_hub_merged. The weights test now sits in the branch condition, so a folder without weights falls through to that merging push rather than failing the export. An empty repo is still never published.

export_metadata.json stays out of the repo. It is Studio bookkeeping that the Chat page reads from disk, and its base_model comes from an adapter's base_model_name_or_path, so it can be a local filesystem path — the same leak the model card already avoids, and the GGUF push already keeps the file local. Finder metadata (._*) is ignored for the same reason, and no longer makes a fresh export folder look reused and pay for a second save.

The clean second save for a reused folder is staged where it fits. Putting it beside the export places a second full checkpoint on a filesystem that has just absorbed the merge, and merge_and_overwrite_lora refuses a save its destination cannot hold; pinning it to the temporary directory instead fails wherever that is the smaller one. Free space alone is not sufficient either, because only the export directory has to be writable — its parent may refuse the staging folder. The roomier candidate is now tried first and the other is used when it refuses.

Validation: 78 tests across test_export_hub_push.py, test_export_gguf_hub_upload.py and test_export_imatrix_compressed.py, and 455 in the wider -k export selection. Every added or changed test was mutation-checked, including both directions of the free-space choice and the fallback when a destination refuses the folder.

@Lyxot

Lyxot commented Sep 16, 2026

Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c9d1e2d751

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

Comment thread studio/backend/core/export/export.py Resolved
…e one it saved

The weights test sat in the branch condition and so read the export directory. A reused folder holding a stale checkpoint file satisfies that test, after which the clean staging save can still come out empty and be uploaded, publishing a repository with no weights and reporting success.

The test now runs on the folder actually being uploaded, after the staging save. Anything without weights takes the merging push instead, which stays the single `push_to_hub_merged` call site, so the branch condition is back to asking only whether the export directory exists.
@Lyxot

Lyxot commented Sep 16, 2026

Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 46f20bc0f6

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

Comment thread studio/backend/core/export/export.py Resolved
@Lyxot

Lyxot commented Sep 16, 2026

Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: 46f20bc0f6

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

@Lyxot Lyxot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving. The double merge is real and this removes it: the folder already saved to disk is what gets uploaded, matching what the GGUF and compressed-tensors pushes already do. A reused save folder can hold files the export did not write, so that case uploads a clean staging copy instead, staged on whichever filesystem has room for it and falling back when one refuses the folder. A folder without weights falls through to push_to_hub_merged rather than publishing an empty repository, and Studio's own export_metadata.json stays out of the repo, as the GGUF push already ensures.

Merge safety checked against main at f70af706b1: merges cleanly, and main has touched none of the four files this changes. Full CI on a fresh-main copy of the branch was green apart from two jobs that are red for reasons outside this change:

  • Repo tests (CPU, studio) — reproduces on pristine main. The PowerShell shared-runner meta-test names tests/python/test_uv_requirements_path_space.py:124, added by #11064.
  • (Python 3.13, l-r) — test_mcp_stdio_sessions.py::test_close_generation_keys_hold_no_secrets, 1 failed against 18,305 passed, and it fails again on re-run. It cannot come from here: that shard's file set is byte-identical to main's, and core/export/export.py is never imported in it (importing core.inference.mcp_client does not load it, and no test_[l-r]* file references it). (Python 3.13, a-k), the shard holding every test file this changes, passed.

Also verified HfApi.file_exists against the huggingface-hub>=0.34.0 floor, since the unit tests only exercise it through a double; the (Python 3.11, floor) job passed.

@Lyxot
Lyxot merged commit dba7490 into unslothai:main Sep 16, 2026
32 of 42 checks passed
shaojun pushed a commit to shaojun/unsloth that referenced this pull request Sep 29, 2026
…the Hub (unslothai#11050)

* Studio: upload the saved merged export to the Hub instead of merging again

* Studio: keep the old Hub push when the merged export folder was not empty

* Studio: keep an existing repo's model card when pushing a saved merged export

* Studio: upload the saved merged export before a best-effort model card

* Studio: upload a clean second save for a reused folder and refuse a push with no weights

* Studio: keep a merged Hub push out of the export's own bookkeeping and disk

Three follow-ups to uploading the saved merged export instead of merging a second time.

A merged folder with no weights is no longer refused. The refusal was justified by a Kaggle /tmp redirect, which cannot reach this call: `kaggle_tmp_redirect` returns an absolute path unchanged, and Studio always resolves the save directory to one. Before the folder was uploaded at all such a push still succeeded through `push_to_hub_merged`, so the weights test now sits in the branch condition and a folder without them falls through to that merging push rather than failing the export.

`export_metadata.json` stays out of the repo. It is Studio bookkeeping the Chat page reads from disk, and its base model comes from an adapter's `base_model_name_or_path`, so it can be a local filesystem path — the same leak the model card already avoids, and the GGUF push already keeps the file local. Finder metadata is ignored for the same reason, and no longer makes a fresh export folder look reused.

The clean second save for a reused folder is staged where it fits. Pinning it beside the export puts a second full checkpoint on a filesystem that has just taken the merge, and `merge_and_overwrite_lora` refuses a save its destination cannot hold; pinning it to the temporary directory fails wherever that is the smaller one. Free space alone is not enough either, since only the export directory itself has to be writable, so the roomier candidate is tried first and the other is used when it refuses the folder.

* Studio: check the folder a merged Hub push is about to upload, not the one it saved

The weights test sat in the branch condition and so read the export directory. A reused folder holding a stale checkpoint file satisfies that test, after which the clean staging save can still come out empty and be uploaded, publishing a repository with no weights and reporting success.

The test now runs on the folder actually being uploaded, after the staging save. Anything without weights takes the merging push instead, which stays the single `push_to_hub_merged` call site, so the branch condition is back to asking only whether the export directory exists.

---------

Co-authored-by: Daniel Han <[email protected]>
Co-authored-by: Lyxot <[email protected]>
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.

3 participants