Files
unslothai--unsloth/tests/saving/test_normalize_tied_weights_keys.py
T
wehub-resource-sync e93507a09c
Lockfile supply-chain audit / lockfile supply-chain audit (push) Has been cancelled
Windows Studio GGUF CI / GPU prebuilt resolves without Visual Studio (push) Has been cancelled
Windows Studio GGUF CI / setup.ps1 unit tests (VS 2026 / CMake guard) (push) Has been cancelled
Windows Studio GGUF CI / real-VS detection (VS 2022) (push) Has been cancelled
Windows Studio GGUF CI / real-VS detection (VS 2026) (push) Has been cancelled
Windows Studio GGUF CI / VC++ runtime detect + install round-trip (windows-2025-vs2026) (push) Has been cancelled
Windows Studio GGUF CI / VC++ runtime detect + install round-trip (windows-latest) (push) Has been cancelled
Windows Studio Update CI / Studio Updating Tests (push) Has been cancelled
Wheel CI / Wheel build + content sanity + import smoke (push) Has been cancelled
Lint CI / Source lint (Python + shell + YAML + JSON + safety nets) (push) Has been cancelled
MLX CI on Mac M1 / dispatch (push) Has been cancelled
Security audit / advisory audit (pip + npm + cargo) (push) Has been cancelled
Security audit / pip scan-packages :: extras (push) Has been cancelled
Security audit / pip scan-packages :: studio (push) Has been cancelled
Security audit / pip scan-packages :: hf-stack (push) Has been cancelled
Security audit / npm scan-packages (Studio frontend tarballs) (push) Has been cancelled
Security audit / workflow-trigger lint (pull_request_target / cache-poisoning) (push) Has been cancelled
Security audit / pytest tests/security (push) Has been cancelled
Security audit / npm provenance + new install-script diff (push) Has been cancelled
Studio API CI / Studio API & Auth Tests (push) Has been cancelled
Backend CI / (Python 3.10) (push) Has been cancelled
Backend CI / (Python 3.11) (push) Has been cancelled
Backend CI / (Python 3.12) (push) Has been cancelled
Backend CI / (Python 3.13) (push) Has been cancelled
Backend CI / Repo tests (CPU) (push) Has been cancelled
Frontend CI / Frontend build + bundle sanity (push) Has been cancelled
Studio GGUF CI / OpenAI, Anthropic API tests (push) Has been cancelled
Studio GGUF CI / Tool calling Tests (push) Has been cancelled
Studio GGUF CI / JSON, images (push) Has been cancelled
Mac Studio GGUF CI / OpenAI, Anthropic API tests (push) Has been cancelled
Mac Studio GGUF CI / Tool calling Tests (push) Has been cancelled
Mac Studio GGUF CI / JSON, images (push) Has been cancelled
Mac Studio Install Matrix CI / Install + load (macos-14) (push) Has been cancelled
Mac Studio Install Matrix CI / Install + load (macos-15) (push) Has been cancelled
Mac Studio Install Matrix CI / Install + load (macos-26) (push) Has been cancelled
Mac Studio Install Matrix CI / Install + load (macos-15-intel) (push) Has been cancelled
Mac Studio API CI / Studio API & Auth Tests (push) Has been cancelled
Mac Studio Install Matrix CI / Install + load (macos-26-intel) (push) Has been cancelled
Mac Studio UI CI / Chat UI Tests (push) Has been cancelled
Studio Tauri CI / Tauri Linux debug build (no codesign) (push) Has been cancelled
Mac Studio Update CI / Studio Updating Tests (push) Has been cancelled
Studio UI CI / Chat UI Tests (push) Has been cancelled
Windows Studio API CI / Studio API & Auth Tests (push) Has been cancelled
Windows Studio UI CI / Chat UI Tests (push) Has been cancelled
Studio Update CI / Studio Updating Tests (push) Has been cancelled
Core / Core (HF=default + TRL=default) (push) Has been cancelled
Core / Core (HF=4.57.6 + TRL<1) (push) Has been cancelled
Core / Core (HF=latest + TRL=latest) (push) Has been cancelled
Core / llama.cpp build + smoke (push) Has been cancelled
Windows Studio GGUF CI / OpenAI, Anthropic API tests (push) Has been cancelled
Windows Studio GGUF CI / Tool calling Tests (push) Has been cancelled
Windows Studio GGUF CI / JSON, images (push) Has been cancelled
Windows Studio GGUF CI / Studio install + inference without Visual Studio (push) Has been cancelled
Studio export capability / capability (macos-latest) (push) Has been cancelled
Studio export capability / capability (ubuntu-latest) (push) Has been cancelled
Studio export capability / capability (windows-latest) (push) Has been cancelled
Cross-platform parity / parity (macos-latest) (push) Has been cancelled
Cross-platform parity / parity (windows-latest) (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
Studio load-orchestrator CI / test (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:59:56 +08:00

117 lines
3.8 KiB
Python

"""Unit tests for the tied-weights-keys coercion used by unsloth.save.
Regression for the NemotronH save / GGUF-export crash: transformers >= 5
``save_pretrained`` reads ``_tied_weights_keys.keys()`` and raises on the legacy list
form. Exercised on tiny module trees, no model download.
"""
import pytest
import torch
from unsloth.save import (
_coerce_tied_weights_keys_to_dict,
_normalize_tied_weights_keys_for_save,
_restore_tied_weights_keys,
)
def _build_tree():
root = torch.nn.Module()
mixer = torch.nn.Module()
root.add_module("mixer", mixer)
return root, mixer
def test_list_becomes_dict_and_restores():
root, mixer = _build_tree()
mixer._tied_weights_keys = ["q_proj.weight", "o_proj.weight"]
originals = _coerce_tied_weights_keys_to_dict(root)
assert mixer._tied_weights_keys == {
"q_proj.weight": "q_proj.weight",
"o_proj.weight": "o_proj.weight",
}
_restore_tied_weights_keys(originals)
assert mixer._tied_weights_keys == ["q_proj.weight", "o_proj.weight"]
def test_tuple_and_set_become_dict():
root, mixer = _build_tree()
root._tied_weights_keys = ("lm_head.weight",)
mixer._tied_weights_keys = {"q_proj.weight"}
_coerce_tied_weights_keys_to_dict(root)
assert root._tied_weights_keys == {"lm_head.weight": "lm_head.weight"}
assert mixer._tied_weights_keys == {"q_proj.weight": "q_proj.weight"}
def test_empty_containers_become_dict():
root, mixer = _build_tree()
root._tied_weights_keys = []
mixer._tied_weights_keys = ()
_coerce_tied_weights_keys_to_dict(root)
# transformers skips only None; an empty list still hits .keys().
assert root._tied_weights_keys == {} and mixer._tied_weights_keys == {}
def test_none_and_existing_dict_are_left_unchanged():
root, mixer = _build_tree()
root._tied_weights_keys = None
original = {"a.weight": "b.weight"}
mixer._tied_weights_keys = original
originals = _coerce_tied_weights_keys_to_dict(root)
assert root._tied_weights_keys is None
assert mixer._tied_weights_keys is original # untouched, not rebuilt
assert originals == [] # nothing to restore
def test_model_without_modules_method_does_not_raise():
class NoModules:
pass
assert _coerce_tied_weights_keys_to_dict(NoModules()) == []
def test_decorator_coerces_during_save_then_restores():
root, mixer = _build_tree()
mixer._tied_weights_keys = ["lm_head.weight"]
seen = {}
@_normalize_tied_weights_keys_for_save
def save(model):
seen["keys"] = dict(model.mixer._tied_weights_keys)
return "ok"
assert save(root) == "ok"
# Dict form was visible to the save, list form restored afterwards.
assert seen["keys"] == {"lm_head.weight": "lm_head.weight"}
assert mixer._tied_weights_keys == ["lm_head.weight"]
def test_decorator_restores_on_exception():
root, mixer = _build_tree()
mixer._tied_weights_keys = ["lm_head.weight"]
@_normalize_tied_weights_keys_for_save
def save(model):
raise RuntimeError("boom")
with pytest.raises(RuntimeError):
save(root)
assert mixer._tied_weights_keys == ["lm_head.weight"]
def test_decorator_finds_model_in_kwargs_and_positional():
# unsloth_save_model / unsloth_generic_save pass model= as a keyword; the gguf path
# binds it as the first positional (method ``self``). Both must be coerced.
for call in (lambda f, r: f(model = r), lambda f, r: f(r)):
root, mixer = _build_tree()
mixer._tied_weights_keys = ["w.weight"]
captured = {}
@_normalize_tied_weights_keys_for_save
def save(model):
captured["dict"] = isinstance(model.mixer._tied_weights_keys, dict)
call(save, root)
assert captured["dict"] is True
assert mixer._tied_weights_keys == ["w.weight"]