-
Notifications
You must be signed in to change notification settings - Fork 303
Expand file tree
/
Copy pathnotebook_deps.py
More file actions
224 lines (202 loc) · 8.39 KB
/
Copy pathnotebook_deps.py
File metadata and controls
224 lines (202 loc) · 8.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# Auto-install missing notebook-only Python deps on first use.
#
# Four notebooks failed in the Blackwell docker validation because the slim
# venv shipped without timm / traitlets / addict / matplotlib, and the
# raising frame is buried inside HF code (`transformers.utils.import_utils.
# requires_backends` for TimmWrapper, `transformers.dynamic_module_utils.
# check_imports` for the Deepseek-OCR trust_remote_code modeling file, and
# a bare ModuleNotFoundError for traitlets from the IPython chain). Wrap
# all three call sites with a thin retry that pip-installs the offending
# package (allow-list only) and re-tries the original import. Honours the
# existing `UNSLOTH_AUTO_INSTALL=0` opt-out (used by `llama_cpp.py`) and
# the standard offline flags so air-gapped envs keep emitting the
# upstream ImportError verbatim.
import importlib
import importlib.metadata
import importlib.util
import os
import shutil
import site
import subprocess
import sys
from ..log import logger
# pypi-name -> import-name (None means same).
_ALLOW_LIST = {
"timm": None, # vision backbones (TimmWrapperModel)
"addict": None, # Deepseek-OCR config dicts
"einops": None, # Deepseek-OCR deepencoder + many other vision models
"easydict": None, # Deepseek-OCR deepencoder.py:12 `from easydict import EasyDict`
"snac": None, # Orpheus TTS neural audio codec
"torchcodec": None, # HF datasets audio Feature decoder (>= datasets 4.x)
"matplotlib": None, # Deepseek-OCR + a few HF image utils
"traitlets": None, # Jupyter/IPython widget chain
"soundfile": None, # audio processors
"librosa": None, # audio processors
"scipy": None, # several processors
"pyctcdecode": None, # ASR
"tiktoken": None, # tokenizer remote-code paths
"blobfile": None, # tiktoken backing store
"pillow_heif": "pillow_heif", # HEIF images
"decord": None, # video processors
"av": "av", # pyav (video processors)
"num2words": None, # speech text norm
"jieba": None, # zh tokenizer
"sentencepiece": None, # tokenizers
}
_AUTO_INSTALL = os.environ.get("UNSLOTH_AUTO_INSTALL", "1") == "1"
_NO_NETWORK = (
os.environ.get("UNSLOTH_OFFLINE", "0") == "1"
or os.environ.get("HF_HUB_OFFLINE", "0") == "1"
or os.environ.get("TRANSFORMERS_OFFLINE", "0") == "1"
)
_attempted: set = set()
def _in_venv() -> bool:
return (
hasattr(sys, "real_prefix")
or (getattr(sys, "base_prefix", sys.prefix) != sys.prefix)
or bool(os.environ.get("VIRTUAL_ENV"))
or bool(os.environ.get("CONDA_PREFIX"))
)
def _pip_install(pkg: str) -> bool:
if pkg in _attempted:
return False
_attempted.add(pkg)
if shutil.which("uv") and _in_venv():
cmd = ["uv", "pip", "install", "--quiet", pkg]
else:
cmd = [
sys.executable, "-m", "pip", "install", "--quiet",
"--disable-pip-version-check", "--no-input", pkg,
]
# Outside a venv on Linux/Mac as non-root: probe write access to
# site-packages and fall back to --user. Windows has no geteuid;
# site-packages there is usually writable inside the venv anyway.
if not _in_venv() and hasattr(os, "geteuid") and os.geteuid() != 0:
try:
sp = site.getsitepackages()[0]
probe = os.path.join(sp, ".unsloth_write_probe")
open(probe, "w").close()
os.remove(probe)
except Exception:
cmd.append("--user")
logger.warning(
f"Unsloth: auto-installing missing notebook dep `{pkg}` via "
f"`{' '.join(cmd)}`. Set UNSLOTH_AUTO_INSTALL=0 to disable."
)
try:
r = subprocess.run(cmd, capture_output=True, text=True, timeout=300)
except Exception as e:
logger.warning(f"Unsloth: auto-install of `{pkg}` failed to launch: {e}")
return False
if r.returncode != 0:
tail = (r.stderr or "")[-500:]
logger.warning(f"Unsloth: auto-install of `{pkg}` failed:\n{tail}")
return False
importlib.invalidate_caches()
try:
list(importlib.metadata.distributions())
except Exception:
pass
return True
def _try_install_and_import(pkg: str) -> bool:
if pkg not in _ALLOW_LIST:
return False
if not _AUTO_INSTALL or _NO_NETWORK:
return False
import_name = _ALLOW_LIST[pkg] or pkg.replace("-", "_")
if importlib.util.find_spec(import_name) is not None:
return True
if not _pip_install(pkg):
return False
return importlib.util.find_spec(import_name) is not None
def patch_requires_backends_autoinstall():
"""
Wrap ``transformers.utils.import_utils.requires_backends`` so that an
allow-listed missing backend triggers a one-shot pip install and a
second attempt. Preserves the original ImportError when the install
fails or the dep isn't on the allow-list, so user-facing error bytes
stay identical to upstream when ``UNSLOTH_AUTO_INSTALL=0``.
"""
try:
from transformers.utils import import_utils as iu
except Exception:
return # transformers absent (MLX-only path) -- nothing to patch.
if getattr(iu.requires_backends, "_unsloth_patched", False):
return
_orig = iu.requires_backends
def requires_backends(obj, backends):
try:
return _orig(obj, backends)
except ImportError:
if not _AUTO_INSTALL or _NO_NETWORK:
raise
wanted_iter = backends if isinstance(backends, (list, tuple)) else [backends]
wanted = [b for b in wanted_iter if isinstance(b, str) and b in _ALLOW_LIST]
if not wanted:
raise
installed_any = False
for b in wanted:
if _try_install_and_import(b):
installed_any = True
if not installed_any:
raise
for b in wanted:
flag = f"_{b.replace('-', '_')}_available"
if hasattr(iu, flag):
setattr(iu, flag, True)
return _orig(obj, backends)
requires_backends._unsloth_patched = True
iu.requires_backends = requires_backends
def patch_check_imports_autoinstall():
"""
trust_remote_code modeling files (e.g. Deepseek-OCR's modeling_deepseekocr.py)
declare their import requirements at the top of the file and raise via
``dynamic_module_utils.check_imports`` (ImportError "This modeling file
requires the following packages..."). That call site never reaches
``requires_backends``, so wrap it too.
"""
try:
from transformers import dynamic_module_utils as dmu
except Exception:
return
if getattr(dmu.check_imports, "_unsloth_patched", False):
return
_orig = dmu.check_imports
def check_imports(filename):
try:
return _orig(filename)
except ImportError as e:
if not _AUTO_INSTALL or _NO_NETWORK:
raise
msg = str(e)
if "This modeling file requires" not in msg:
raise
# Message format: "... environment: pkg1, pkg2. Run `pip install...`"
try:
tail = msg.split("environment:", 1)[1]
pkgs_str = tail.split(".", 1)[0]
except Exception:
raise
pkgs = [p.strip() for p in pkgs_str.split(",") if p.strip() in _ALLOW_LIST]
if not pkgs:
raise
ok = all(_try_install_and_import(p) for p in pkgs)
if not ok:
raise
return _orig(filename)
check_imports._unsloth_patched = True
dmu.check_imports = check_imports
def _ensure_notebook_chain():
"""
Pre-emptive ensure for deps that raise bare ModuleNotFoundError outside
transformers (the Jupyter/IPython chain). Kept tiny: only ``traitlets``
is touched today; expand only when a new failure mode appears.
"""
if not _AUTO_INSTALL or _NO_NETWORK:
return
for pkg in ("traitlets",):
if importlib.util.find_spec(pkg) is None:
_try_install_and_import(pkg)
patch_requires_backends_autoinstall()
patch_check_imports_autoinstall()
_ensure_notebook_chain()