From 448e2251e61644d2db38473bc9f8d17b3ca07978 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 15 Jun 2026 03:13:44 +0000 Subject: [PATCH] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- docker/unsloth_ipython_startup.py | 5 ++- docker/unsloth_nb_compat.py | 24 ++++++++++---- docker/unsloth_pip_shim.py | 53 +++++++++++++++++++++++++------ docker/unsloth_run.py | 29 +++++++++++------ 4 files changed, 83 insertions(+), 28 deletions(-) diff --git a/docker/unsloth_ipython_startup.py b/docker/unsloth_ipython_startup.py index fcc9f8c472..94e4245450 100644 --- a/docker/unsloth_ipython_startup.py +++ b/docker/unsloth_ipython_startup.py @@ -5,14 +5,17 @@ transformers sidecar before the first model cell, using the version the notebook's own install cell asked for (recorded by the pip/uv shim). Safe no-op outside IPython, when no version was requested, or once transformers is imported. """ + try: import os + # Tell the pip/uv shim it's running inside a notebook kernel, so a cell's # `!pip install ...` / `!uv pip install ...` (which inherits this env) gets # the safe-install behaviour. Unset everywhere else => shim is a passthrough. os.environ["UNSLOTH_NB_SHIM"] = "1" import unsloth_nb_compat + unsloth_nb_compat.register_ipython() except Exception as _e: # never break a kernel because of the helper import sys - print(f"[unsloth-nb] startup hook skipped: {_e!r}", file=sys.stderr) + print(f"[unsloth-nb] startup hook skipped: {_e!r}", file = sys.stderr) diff --git a/docker/unsloth_nb_compat.py b/docker/unsloth_nb_compat.py index 70a905b757..8061bac91f 100644 --- a/docker/unsloth_nb_compat.py +++ b/docker/unsloth_nb_compat.py @@ -20,6 +20,7 @@ Two activation paths: startup file) activates the sidecar before the first model cell, using the version the notebook's own install cell asked for (recorded by the pip shim). """ + from __future__ import annotations import os, sys, glob, json @@ -32,9 +33,16 @@ MARKER = os.environ.get("UNSLOTH_NB_TF_MARKER", "/tmp/unsloth_nb/requested_trans # fallback when a notebook does not pin transformers but names a new-family model. _TIER_SUBSTRINGS = { "5.10.2": ("gemma-4-12b", "gemma4-12b"), - "5.5.0": ("gemma-4", "gemma4", "qwen3.6"), - "5.3.0": ("ministral-3", "glm-4.7-flash", "qwen3-30b-a3b", "qwen3.5", - "qwen3-next", "qwen3_5", "lfm2.5-vl"), + "5.5.0": ("gemma-4", "gemma4", "qwen3.6"), + "5.3.0": ( + "ministral-3", + "glm-4.7-flash", + "qwen3-30b-a3b", + "qwen3.5", + "qwen3-next", + "qwen3_5", + "lfm2.5-vl", + ), } @@ -100,9 +108,11 @@ def activate(version: str | None, *, quiet: bool = False): return None if "transformers" in sys.modules: if not quiet: - print(f"[unsloth-nb] transformers already imported; cannot switch to " - f"{version} in-process (restart the kernel, or use `unsloth-run`).", - file=sys.stderr) + print( + f"[unsloth-nb] transformers already imported; cannot switch to " + f"{version} in-process (restart the kernel, or use `unsloth-run`).", + file = sys.stderr, + ) return None if d not in sys.path: sys.path.insert(0, d) @@ -118,7 +128,7 @@ def resolve(model_name: str | None = None): # -- manual JupyterLab integration: activate before the first model cell -------- -def _pre_run_cell(_info=None): +def _pre_run_cell(_info = None): v = requested_version() if v and "transformers" not in sys.modules: activate(v) diff --git a/docker/unsloth_pip_shim.py b/docker/unsloth_pip_shim.py index 4005141167..666b5c0cd8 100644 --- a/docker/unsloth_pip_shim.py +++ b/docker/unsloth_pip_shim.py @@ -20,6 +20,7 @@ absolute path so there is no recursion. `python -m pip` / `%pip` bypass PATH and are not intercepted -- the driven `unsloth-run` handles those by parsing the notebook directly. """ + import os, re, sys, subprocess REAL = {"pip": "/opt/unsloth-venv/bin/pip", "uv": "/opt/unsloth-venv/bin/uv"} @@ -27,18 +28,48 @@ MARKER = os.environ.get("UNSLOTH_NB_TF_MARKER", "/tmp/unsloth_nb/requested_trans # Packages whose baked version must never be changed by a notebook install cell. _KEEP = { - "torch", "torchvision", "torchaudio", "triton", "triton-rocm", "pytorch-triton", - "xformers", "vllm", "bitsandbytes", "flashinfer", "flashinfer-python", - "unsloth", "unsloth-zoo", "unsloth_zoo", + "torch", + "torchvision", + "torchaudio", + "triton", + "triton-rocm", + "pytorch-triton", + "xformers", + "vllm", + "bitsandbytes", + "flashinfer", + "flashinfer-python", + "unsloth", + "unsloth-zoo", + "unsloth_zoo", } _KEEP_PREFIX = ("nvidia-", "nvidia_") # pip/uv flags that consume the following token as a value (so we don't mistake # that value for a requirement). _VALUE_FLAGS = { - "-r", "--requirement", "-c", "--constraint", "-i", "--index-url", - "--extra-index-url", "-f", "--find-links", "--target", "-t", "--python", "-p", - "--prefix", "--index-strategy", "--upgrade-package", "-P", "--no-binary", - "--only-binary", "--platform", "--python-version", "--abi", "--implementation", + "-r", + "--requirement", + "-c", + "--constraint", + "-i", + "--index-url", + "--extra-index-url", + "-f", + "--find-links", + "--target", + "-t", + "--python", + "-p", + "--prefix", + "--index-strategy", + "--upgrade-package", + "-P", + "--no-binary", + "--only-binary", + "--platform", + "--python-version", + "--abi", + "--implementation", } @@ -112,11 +143,13 @@ def main(): if recorded: try: - os.makedirs(os.path.dirname(MARKER), exist_ok=True) + os.makedirs(os.path.dirname(MARKER), exist_ok = True) with open(MARKER, "w") as f: f.write(recorded) - print(f"[unsloth-nb] notebook requested transformers=={recorded}; will " - f"activate its sidecar for the model cells (base stack kept).") + print( + f"[unsloth-nb] notebook requested transformers=={recorded}; will " + f"activate its sidecar for the model cells (base stack kept)." + ) except OSError: pass if dropped: diff --git a/docker/unsloth_run.py b/docker/unsloth_run.py index 6cf2309eef..7daa81e7d9 100644 --- a/docker/unsloth_run.py +++ b/docker/unsloth_run.py @@ -14,6 +14,7 @@ Usage: A raw github URL (raw.githubusercontent.com/.../nb/Foo.ipynb) is fetched first. """ + import argparse, json, os, re, subprocess, sys, tempfile, urllib.request sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) @@ -55,11 +56,11 @@ def _scan(nb): def main(): - ap = argparse.ArgumentParser(prog="unsloth-run") + ap = argparse.ArgumentParser(prog = "unsloth-run") ap.add_argument("notebook") ap.add_argument("--out") - ap.add_argument("--timeout", type=int, default=3600) - ap.add_argument("--transformers", dest="tf") + ap.add_argument("--timeout", type = int, default = 3600) + ap.add_argument("--transformers", dest = "tf") args = ap.parse_args() nb = _load(args.notebook) @@ -70,7 +71,8 @@ def main(): # Materialise the notebook locally for nbconvert. if args.notebook.startswith(("http://", "https://")) or args.out: src_path = args.out or os.path.join( - tempfile.mkdtemp(), os.path.basename(args.notebook.split("?")[0])) + tempfile.mkdtemp(), os.path.basename(args.notebook.split("?")[0]) + ) with open(src_path, "w") as f: json.dump(nb, f) else: @@ -82,7 +84,7 @@ def main(): # The pip/uv shim writes the marker; pre-seed it too so the kernel agrees. if want: marker = env.get("UNSLOTH_NB_TF_MARKER", "/tmp/unsloth_nb/requested_transformers") - os.makedirs(os.path.dirname(marker), exist_ok=True) + os.makedirs(os.path.dirname(marker), exist_ok = True) open(marker, "w").write(want) if sidecar: env["PYTHONPATH"] = sidecar + os.pathsep + env.get("PYTHONPATH", "") @@ -93,14 +95,21 @@ def main(): print("[unsloth-run] no transformers pin/model tier detected; using base venv") cmd = [ - "/opt/unsloth-venv/bin/jupyter", "nbconvert", "--to", "notebook", - "--execute", f"--ExecutePreprocessor.timeout={args.timeout}", + "/opt/unsloth-venv/bin/jupyter", + "nbconvert", + "--to", + "notebook", + "--execute", + f"--ExecutePreprocessor.timeout={args.timeout}", "--ExecutePreprocessor.kernel_name=python3", - src_path, "--output", os.path.basename(out_path), - "--output-dir", os.path.dirname(os.path.abspath(out_path)) or ".", + src_path, + "--output", + os.path.basename(out_path), + "--output-dir", + os.path.dirname(os.path.abspath(out_path)) or ".", ] print("[unsloth-run] executing:", os.path.basename(src_path)) - sys.exit(subprocess.call(cmd, env=env)) + sys.exit(subprocess.call(cmd, env = env)) if __name__ == "__main__":