Skip to content

Notebook GGUF export: keep the converter to the load's trust_remote_code decision - #17397

Merged
danielhanchen merged 1 commit into
mainfrom
gguf-notebook-trust-guard
Nov 10, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
gguf-notebook-trust-guard

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

Studio's GGUF export already keeps the GGUF converter to the load's trust_remote_code decision (#14437), so a checkpoint loaded without trust_remote_code=True cannot have its auto_map code run during conversion. The notebook paths, model.save_pretrained_gguf and push_to_hub_gguf, did not have the same guard: the converter subprocess could import and run a repo's remote code even though the user never opted in.

Changes

  • unsloth/save.py:
    • _gguf_remote_code_approved(model, tokenizer) treats any of these as consent:
      • the load's stamp (_unsloth_trust_remote_code);
      • a model class from transformers_modules;
      • a tokenizer or processor class from transformers_modules.
    • _gguf_converter_remote_code(approved) mirrors Studio's _converter_remote_code. When the load did not opt in, it points HF_MODULES_CACHE at an unusable path for the length of the conversion, then restores the previous value. The converter child copies the environment, so it cannot import auto_map modules.
    • The guard wraps the save_to_gguf call in unsloth_save_pretrained_gguf (which push_to_hub_gguf uses) and save_to_gguf_generic.
  • unsloth/models/llama.py: FastLlamaModel.from_pretrained records _unsloth_trust_remote_code, as FastModel already does, so a trusted load keeps converting as before.
  • unsloth/models/sentence_transformer.py: the GGUF export passes a trusted stamp on to the inner model it converts.

Impact

  • Unchanged: architectures with no auto_map (Llama, Qwen, Gemma, Mistral and the rest) never touch the module cache, so they convert exactly as before. Loads with trust_remote_code=True are untouched.
  • Changed: a model loaded without trust_remote_code whose saved folder carries remote code the converter would need now fails to convert instead of running that code, which matches Studio.

Tests

New tests/saving/test_gguf_notebook_remote_code_guard.py: 3 of 5 fail on main, and all 5 pass on this branch.

  • Two tests drive the real unsloth_save_pretrained_gguf with the converter stubbed.
  • One runs a real subprocess. Its control step proves the auto_map code runs when unguarded, and that it does not run under the guard.

169 related save and export tests pass on this branch.

save_pretrained_gguf / push_to_hub_gguf now give the convert_hf_to_gguf.py child an
uncreatable HF_MODULES_CACHE unless the model was loaded with trust_remote_code=True (or
its model / tokenizer class came from auto_map code), matching Studio's export guard.
Checkpoints without auto_map never read that cache, so standard exports are unchanged.
FastLlamaModel now stamps _unsloth_trust_remote_code like FastBaseModel, and the
SentenceTransformer GGUF export forwards its stamp to the inner model.
@danielhanchen danielhanchen self-assigned this Nov 10, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-10T12:42:18.125017Z 3bcec91 Manual request
🔒 Security Review ✅ Completed 2026-11-10T12:43:58.796967Z 3bcec91 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

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 3bcec91e76

ℹ️ 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: 3bcec91e76

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
danielhanchen merged commit 2966fc4 into main Nov 10, 2026
@danielhanchen
danielhanchen deleted the gguf-notebook-trust-guard branch November 10, 2026 12:51
danielhanchen added a commit that referenced this pull request Nov 10, 2026
…rgs (#17404)

## Summary

#17397 added a `remote_code_approved` local to
`unsloth_save_pretrained_gguf` (read before the processor is unwrapped,
so the converter follows the load's `trust_remote_code` decision). That
function snapshots its locals with `arguments = dict(locals())` and
forwards them to `unsloth_generic_save(**arguments)`, deleting every
local that is not a save kwarg. The new local was never deleted, so
every merged GGUF export on main now fails before the merge:

```
RuntimeError: Failed to save/merge model: TypeError: unsloth_generic_save() got an unexpected keyword argument 'remote_code_approved'
```

This adds the one missing `del arguments["remote_code_approved"]`, next
to the other local-only names. The value is still read later by
`_gguf_converter_remote_code(remote_code_approved)`, so #17397's
behaviour is unchanged.

## Why CI did not stop it

The existing guard
`tests/test_gguf_model_basename.py::test_helper_is_module_level_and_adds_no_locals_to_the_gguf_entrypoint`
catches exactly this and fails on main. #17397's own CI runs were
cancelled rather than failed, so the guard never reported on it. Every
PR based on main since then fails:

- `Repo tests (CPU, rest)`: the guard above
- `Repo tests (CPU, fsdp2)`:
`test_gguf_runs_on_rank0_only_from_the_same_merge`,
`test_push_to_hub_gguf_uploads_from_rank0_only`,
`test_a_rank0_failure_raises_on_every_rank` in
`tests/test_fsdp2_exports_gloo.py`, which run a real export end to end

## Testing

- `tests/test_gguf_model_basename.py`: 1 failed on main, all 61 pass
with the fix
- `tests/test_fsdp2_exports_gloo.py` GGUF cases plus the guard: 64
passed with the fix
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.

1 participant