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
110 lines
3.4 KiB
Python
110 lines
3.4 KiB
Python
"""_fix_pad_token dispatch in unsloth/tokenizer_utils.py.
|
|
|
|
It must delegate to unsloth_zoo's shared fix_pad_token when present (single
|
|
source of truth), and fall back to a no-op against an older unsloth_zoo. Static
|
|
+ CPU-only: _fix_pad_token is exec'd in isolation so the test never imports
|
|
torch / transformers / unsloth.
|
|
"""
|
|
|
|
import ast
|
|
import os
|
|
import sys
|
|
import types
|
|
|
|
REPO_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
|
|
TOK_PATH = os.path.join(REPO_ROOT, "unsloth", "tokenizer_utils.py")
|
|
|
|
WANTED = {
|
|
"_fix_pad_token",
|
|
}
|
|
|
|
|
|
def _load_pad_helpers():
|
|
"""Exec only the pad-token helpers with a stub logger (no heavy imports)."""
|
|
tree = ast.parse(open(TOK_PATH).read())
|
|
nodes = []
|
|
for node in tree.body:
|
|
if isinstance(node, ast.Assign):
|
|
names = {t.id for t in node.targets if isinstance(t, ast.Name)}
|
|
if names & WANTED:
|
|
nodes.append(node)
|
|
elif isinstance(node, ast.FunctionDef) and node.name in WANTED:
|
|
nodes.append(node)
|
|
module = ast.Module(body = nodes, type_ignores = [])
|
|
ast.fix_missing_locations(module)
|
|
ns = {"logger": types.SimpleNamespace(warning = lambda *a, **k: None)}
|
|
exec(compile(module, TOK_PATH, "exec"), ns)
|
|
return ns
|
|
|
|
|
|
class FakeTok:
|
|
def __init__(self, vocab, pad, eos):
|
|
self._v = dict(vocab)
|
|
self.pad_token = pad
|
|
self.eos_token = eos
|
|
|
|
@property
|
|
def pad_token_id(self):
|
|
return self._v.get(self.pad_token)
|
|
|
|
@property
|
|
def eos_token_id(self):
|
|
return self._v.get(self.eos_token)
|
|
|
|
def get_vocab(self):
|
|
return dict(self._v)
|
|
|
|
|
|
def _block_shared_module(monkeypatch):
|
|
# Stub parent so importing it is cheap, and mark the submodule absent.
|
|
monkeypatch.setitem(sys.modules, "unsloth_zoo", types.ModuleType("unsloth_zoo"))
|
|
monkeypatch.setitem(sys.modules, "unsloth_zoo.pad_token", None)
|
|
|
|
|
|
def test_fix_pad_token_none_is_noop():
|
|
ns = _load_pad_helpers()
|
|
assert ns["_fix_pad_token"](None) is None
|
|
|
|
|
|
def test_fallback_keeps_pad_named_token(monkeypatch):
|
|
ns = _load_pad_helpers()
|
|
_block_shared_module(monkeypatch)
|
|
# A pad-named token (e.g. <|vision_pad|>) is a valid pad -> fallback keeps it.
|
|
tok = FakeTok(
|
|
{"<|endoftext|>": 1, "<|im_end|>": 2, "<|vision_pad|>": 3},
|
|
pad = "<|vision_pad|>",
|
|
eos = "<|im_end|>",
|
|
)
|
|
ns["_fix_pad_token"](tok)
|
|
assert tok.pad_token == "<|vision_pad|>"
|
|
assert tok.pad_token != tok.eos_token
|
|
|
|
|
|
def test_fallback_leaves_valid_pad_untouched(monkeypatch):
|
|
ns = _load_pad_helpers()
|
|
_block_shared_module(monkeypatch)
|
|
tok = FakeTok({"<s>": 1, "</s>": 2, "<pad>": 0}, pad = "<pad>", eos = "</s>")
|
|
ns["_fix_pad_token"](tok)
|
|
assert tok.pad_token == "<pad>"
|
|
|
|
|
|
def test_fix_pad_token_delegates_to_shared_module(monkeypatch):
|
|
ns = _load_pad_helpers()
|
|
calls = {}
|
|
|
|
fake = types.ModuleType("unsloth_zoo.pad_token")
|
|
|
|
def fix_pad_token(tokenizer, allow_add = True):
|
|
calls["allow_add"] = allow_add
|
|
tokenizer.pad_token = "SHARED"
|
|
|
|
fake.fix_pad_token = fix_pad_token
|
|
monkeypatch.setitem(sys.modules, "unsloth_zoo", types.ModuleType("unsloth_zoo"))
|
|
monkeypatch.setitem(sys.modules, "unsloth_zoo.pad_token", fake)
|
|
|
|
tok = FakeTok({"a": 1}, pad = "x", eos = "y")
|
|
ns["_fix_pad_token"](tok)
|
|
# Delegated, and crucially with allow_add=False (no model here to resize).
|
|
assert tok.pad_token == "SHARED"
|
|
assert calls["allow_add"] is False
|