Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Turn torch.compile off on GPUs older than Volta (#13085)

Daniel Han9a1333902750
diff --git a/tests/test_fastmodel_prevolta_fast_inference.py b/tests/test_fastmodel_prevolta_fast_inference.py
index 92dd60f7996..9901096d0a9 100644
--- a/tests/test_fastmodel_prevolta_fast_inference.py
+++ b/tests/test_fastmodel_prevolta_fast_inference.py
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
-"""FastModel pre-Volta fast_inference fallback; sliced with `ast` because importing the loader needs a GPU."""
+"""FastModel / FastLanguageModel pre-Volta fast_inference fallback; sliced with `ast` because importing the loader needs a GPU."""
import ast
import types
@@ -12,9 +12,14 @@ import pytest
LOADER_PATH = Path(__file__).resolve().parents[1] / "unsloth" / "models" / "loader.py"
-def _fast_inference_blocks():
[email protected](params = ["FastModel", "FastLanguageModel"])
+def loader_class(request):
+ return request.param
+
+
+def _fast_inference_blocks(cls_name):
tree = ast.parse(LOADER_PATH.read_text(encoding = "utf-8"))
- cls = next(n for n in tree.body if isinstance(n, ast.ClassDef) and n.name == "FastModel")
+ cls = next(n for n in tree.body if isinstance(n, ast.ClassDef) and n.name == cls_name)
fn = next(n for n in cls.body if isinstance(n, ast.FunctionDef) and n.name == "from_pretrained")
for parent in ast.walk(fn):
body = getattr(parent, "body", None)
@@ -32,10 +37,11 @@ def _fast_inference_blocks():
gate.test
)
return [gate, node]
- raise AssertionError("FastModel.from_pretrained has no pre-Volta gate before the GB10 block")
+ raise AssertionError(f"{cls_name}.from_pretrained has no pre-Volta gate before the GB10 block")
def _run(
+ cls_name,
device_type,
capability,
name = "Tesla P100-PCIE-16GB",
@@ -57,13 +63,13 @@ def _run(
"DEVICE_COUNT": 1,
"torch": types.SimpleNamespace(cuda = cuda),
}
- exec(compile(ast.Module(_fast_inference_blocks(), []), str(LOADER_PATH), "exec"), ns)
+ exec(compile(ast.Module(_fast_inference_blocks(cls_name), []), str(LOADER_PATH), "exec"), ns)
return ns["fast_inference"]
@pytest.mark.parametrize("capability", [(6, 1), (6, 0), (5, 2)])
-def test_pre_volta_cuda_falls_back(capability, capsys):
- assert _run("cuda", capability) is False
+def test_pre_volta_cuda_falls_back(loader_class, capability, capsys):
+ assert _run(loader_class, "cuda", capability) is False
assert "vLLM does not work on older GPUs" in capsys.readouterr().out
@@ -78,20 +84,20 @@ def test_pre_volta_cuda_falls_back(capability, capsys):
("xpu", (0, 0)),
],
)
-def test_other_devices_keep_fast_inference(device_type, capability, capsys):
- assert _run(device_type, capability) is True
+def test_other_devices_keep_fast_inference(loader_class, device_type, capability, capsys):
+ assert _run(loader_class, device_type, capability) is True
assert "older GPUs" not in capsys.readouterr().out
-def test_gb10_still_falls_back():
- assert _run("cuda", (12, 1), name = "NVIDIA GB10") is False
+def test_gb10_still_falls_back(loader_class):
+ assert _run(loader_class, "cuda", (12, 1), name = "NVIDIA GB10") is False
-def test_pre_volta_falls_back_without_requiring_vllm(capsys):
- assert _run("cuda", (6, 1), has_vllm = False) is False
+def test_pre_volta_falls_back_without_requiring_vllm(loader_class, capsys):
+ assert _run(loader_class, "cuda", (6, 1), has_vllm = False) is False
assert "vLLM does not work on older GPUs" in capsys.readouterr().out
-def test_volta_still_requires_vllm():
+def test_volta_still_requires_vllm(loader_class):
with pytest.raises(ImportError):
- _run("cuda", (7, 0), has_vllm = False)
+ _run(loader_class, "cuda", (7, 0), has_vllm = False)
diff --git a/tests/test_pre_volta_compile_disable.py b/tests/test_pre_volta_compile_disable.py
new file mode 100644
index 00000000000..97fb59737aa
--- /dev/null
+++ b/tests/test_pre_volta_compile_disable.py
@@ -0,0 +1,93 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
+
+"""Pre-Volta NVIDIA GPUs (sm < 7.0) train with torch.compile off (#1998).
+
+Inductor raises GPUTooOldForTriton on the first compiled call there, which killed GRPO,
+vision and MoE training mid-run on GTX 10xx / P100 / Maxwell cards. Decision-table checks, no GPU.
+"""
+
+from types import SimpleNamespace
+
+import pytest
+
+pytest.importorskip("torch")
+
+from unsloth import device_type
+
+
+ "major, applied",
+ [
+ (5, True), # Maxwell: Quadro M4000 / M6000 in the issue
+ (6, True), # Pascal: GTX 1070 / 1080, Kaggle P100
+ (7, False), # Volta / Turing: Inductor's own floor, T4 keeps compiling
+ (8, False),
+ (9, False),
+ (10, False),
+ (12, False),
+ (None, False), # unreadable capability: never guess
+ ],
+)
+def test_only_pre_volta_turns_compile_off(major, applied):
+ env, config = {}, SimpleNamespace(disable = False)
+ assert device_type.apply_pre_volta_compile_workaround(major, env, config) is applied
+ assert config.disable is applied
+ if applied:
+ assert env == {
+ "TORCHDYNAMO_DISABLE": "1",
+ "TORCH_COMPILE_DISABLE": "1",
+ "UNSLOTH_COMPILE_DISABLE": "1",
+ }
+ else:
+ assert env == {}
+
+
+def test_dynamo_config_is_set_not_just_the_env():
+ """Regions zoo wrapped at import never reread TORCHDYNAMO_DISABLE; env-only left MoE failing."""
+ config = SimpleNamespace(disable = False)
+ device_type.apply_pre_volta_compile_workaround(6, {}, config)
+ assert config.disable is True
+
+
+def test_an_explicit_user_choice_is_left_alone():
+ env, config = {"TORCHDYNAMO_DISABLE": "0"}, SimpleNamespace(disable = False)
+ assert device_type.apply_pre_volta_compile_workaround(6, env, config) is False
+ assert env == {"TORCHDYNAMO_DISABLE": "0"}
+ assert config.disable is False
+
+
+def test_other_compile_switches_the_user_set_are_kept():
+ env = {"UNSLOTH_COMPILE_DISABLE": "partial"}
+ device_type.apply_pre_volta_compile_workaround(6, env, SimpleNamespace(disable = False))
+ assert env["UNSLOTH_COMPILE_DISABLE"] == "partial"
+
+
+def test_worker_threads_see_the_disable_too():
+ """torch >= 2.12 config writes are ContextVar-local; autograd's backward threads must see it.
+
+ A fresh interpreter running only the helper (sliced with `ast`): importing unsloth needs a GPU,
+ and _gpu_init's later config-mirroring patch would make this pass without the fix.
+ """
+ import ast
+ import subprocess
+ import sys
+ from pathlib import Path
+
+ source = Path(device_type.__file__).read_text(encoding = "utf-8")
+ helper = next(
+ node
+ for node in ast.parse(source).body
+ if isinstance(node, ast.FunctionDef) and node.name == "apply_pre_volta_compile_workaround"
+ )
+ code = (
+ "import os, threading, torch._dynamo\n"
+ + ast.unparse(helper)
+ + "\nassert apply_pre_volta_compile_workaround(6, {})\n"
+ "seen = []\n"
+ "t = threading.Thread(target = lambda: seen.append(torch._dynamo.config.disable))\n"
+ "t.start(); t.join()\n"
+ "assert seen == [True], seen\n"
+ )
+ result = subprocess.run([sys.executable, "-c", code], capture_output = True, text = True)
+ assert result.returncode == 0, result.stderr[-2000:]
diff --git a/unsloth/device_type.py b/unsloth/device_type.py
index 85d2596aa36..8c73dc56cb8 100644
--- a/unsloth/device_type.py
+++ b/unsloth/device_type.py
@@ -182,6 +182,52 @@ if DEVICE_TYPE == "hip":
"Unsloth: gfx906 (MI50 / Radeon VII) detected - torch.compile disabled "
"(community-maintained legacy GCN path)."
)
+
+
+# Pre-Volta NVIDIA (Maxwell / Pascal, sm < 7.0): Inductor refuses to emit Triton for these
+# (GPUTooOldForTriton mid-training), while Unsloth's own Triton kernels still run, so train eagerly.
+def apply_pre_volta_compile_workaround(
+ major,
+ environ = None,
+ dynamo_config = None,
+) -> bool:
+ """Turn torch.compile off for compute capability < 7. An explicit TORCHDYNAMO_DISABLE wins.
+
+ The env vars alone miss regions torch.compile already wrapped while unsloth_zoo imported:
+ those read the Dynamo config per call, the env var only at wrap time (MoE hit this).
+ """
+ if major is None or major >= 7:
+ return False
+ if environ is None:
+ environ = os.environ
+ if environ.get("TORCHDYNAMO_DISABLE", "1") != "1":
+ return False
+ environ["TORCHDYNAMO_DISABLE"] = "1"
+ environ.setdefault("TORCH_COMPILE_DISABLE", "1")
+ environ.setdefault("UNSLOTH_COMPILE_DISABLE", "1")
+ if dynamo_config is None:
+ import torch._dynamo
+ dynamo_config = torch._dynamo.config
+ dynamo_config.disable = True
+ # torch >= 2.12 keeps that assignment per thread, and backward (checkpoint recompute) runs on
+ # autograd worker threads, so also set the process-wide default those threads fall back to.
+ entry = getattr(dynamo_config, "_config", {}).get("disable", None)
+ if entry is not None and hasattr(entry, "default"):
+ entry.default = True
+ return True
+
+
+if DEVICE_TYPE == "cuda" and torch.cuda.is_available():
+ try:
+ _cuda_major = torch.cuda.get_device_capability()[0]
+ except Exception:
+ _cuda_major = None
+ if apply_pre_volta_compile_workaround(_cuda_major):
+ print(
+ "Unsloth: GPUs older than Volta (compute capability < 7.0) cannot run "
+ "torch.compile - training without it."
+ )
+ del _cuda_major
if DEVICE_TYPE == "hip":
try:
import bitsandbytes
diff --git a/unsloth/models/loader.py b/unsloth/models/loader.py
index 9c65fc97f06..a8c35c91d5a 100644
--- a/unsloth/models/loader.py
+++ b/unsloth/models/loader.py
@@ -999,6 +999,10 @@ class FastLanguageModel(FastLlamaModel):
or dtype == torch.float32
)
+ # Same fallback as FastModel, before requiring vLLM: it cannot run below compute capability 7.
+ if fast_inference and DEVICE_TYPE == "cuda" and torch.cuda.get_device_capability()[0] < 7:
+ print("Unsloth: vLLM does not work on older GPUs - will switch to Unsloth inference!")
+ fast_inference = False
if fast_inference:
if importlib.util.find_spec("vllm") is None:
raise _vllm_unavailable_error()