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.

Strict Dynamo regions: turn suppress_errors off only where the flag exists (#17492)

Daniel Hane60d3d5ecde0
diff --git a/tests/test_strict_dynamo_flag.py b/tests/test_strict_dynamo_flag.py
new file mode 100644
index 00000000000..516e2dc9cd1
--- /dev/null
+++ b/tests/test_strict_dynamo_flag.py
@@ -0,0 +1,51 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
+
+"""Strict Dynamo regions (block compile's first call, the prefix grouper's checked call) turn suppress_errors off,
+and still run on torch builds without that flag (AMD's ROCm torch 2.11): patching a missing flag raised, which
+block compile took as a compile failure and demoted every block to the stock forward."""
+
+import pathlib
+import re
+import types
+
+import pytest
+import torch
+
+import unsloth # noqa: F401 (must precede transformers)
+import unsloth.models._block_compile as BC
+import unsloth.utils.prefix_grouper_kernel as PG
+
+
+ not hasattr(torch._dynamo.config, "suppress_errors"),
+ reason = "this torch build has no suppress_errors flag",
+)
[email protected]("module", [BC, PG], ids = ["block_compile", "prefix_grouper"])
+def test_strict_region_turns_suppress_errors_off(module, monkeypatch):
+ monkeypatch.setattr(torch._dynamo.config, "suppress_errors", True)
+ with module._strict_dynamo():
+ assert torch._dynamo.config.suppress_errors is False
+ assert torch._dynamo.config.suppress_errors is True
+
+
[email protected]("module", [BC, PG], ids = ["block_compile", "prefix_grouper"])
+def test_strict_region_runs_without_the_flag(module, monkeypatch):
+ # A config without the flag, whose patch() raises the way torch's does for an unknown key.
+ def patch(**changes):
+ raise AttributeError(f"torch._dynamo.config.{next(iter(changes))} does not exist")
+
+ monkeypatch.setattr(torch._dynamo, "config", types.SimpleNamespace(patch = patch))
+ with module._strict_dynamo():
+ pass
+
+
+def test_no_unguarded_suppress_errors_patch_left():
+ root = pathlib.Path(unsloth.__file__).parent
+ hits = [
+ str(p.relative_to(root))
+ for p in root.rglob("*.py")
+ if re.search(r"config\.patch\(\s*suppress_errors", p.read_text(encoding = "utf-8"))
+ and "def _strict_dynamo" not in p.read_text(encoding = "utf-8")
+ ]
+ assert not hits, hits
diff --git a/unsloth/models/_block_compile.py b/unsloth/models/_block_compile.py
index d76d24c195c..6f12fa59ddb 100644
--- a/unsloth/models/_block_compile.py
+++ b/unsloth/models/_block_compile.py
@@ -26,6 +26,16 @@ _COMPILE_OPTIONS = {
}
+def _strict_dynamo():
+ """Dynamo with suppress_errors off; some torch builds (AMD's ROCm torch 2.11) have no such flag."""
+ config = torch._dynamo.config
+ return (
+ config.patch(suppress_errors = False)
+ if hasattr(config, "suppress_errors")
+ else contextlib.nullcontext()
+ )
+
+
def _cudagraph_mode():
"""UNSLOTH_BLOCK_CUDAGRAPHS: "1" always on, unset or "auto" decided per run (see _auto_observe),
anything else off (before auto, every value but "1" meant off)."""
@@ -1672,7 +1682,7 @@ def _wrap(
return run(self, hidden_states, args, kwargs, mode)
while True:
try:
- with torch._dynamo.config.patch(suppress_errors = False):
+ with _strict_dynamo():
out = run(self, hidden_states, args, kwargs, mode)
self.__dict__["_unsloth_block_checked"] = checked | bit
return out
diff --git a/unsloth/utils/prefix_grouper_kernel.py b/unsloth/utils/prefix_grouper_kernel.py
index cdc8fa165c7..32f782b35f7 100644
--- a/unsloth/utils/prefix_grouper_kernel.py
+++ b/unsloth/utils/prefix_grouper_kernel.py
@@ -14,6 +14,7 @@
from __future__ import annotations
+import contextlib
import os
import re
from dataclasses import dataclass
@@ -31,6 +32,16 @@ from torch.nn.attention.flex_attention import (
PREFIX_GROUPER_MIN_TORCH = (2, 7)
+def _strict_dynamo():
+ """Dynamo with suppress_errors off; some torch builds (AMD's ROCm torch 2.11) have no such flag."""
+ config = torch._dynamo.config
+ return (
+ config.patch(suppress_errors = False)
+ if hasattr(config, "suppress_errors")
+ else contextlib.nullcontext()
+ )
+
+
def torch_supports_prefix_grouper(version = None) -> bool:
"""True unless the torch major.minor is known to be below PREFIX_GROUPER_MIN_TORCH."""
match = re.match(r"(\d+)\.(\d+)", str(torch.__version__ if version is None else version))
@@ -369,7 +380,7 @@ def _checked_first_call(q, k, v, seg, block_mask, enable_gqa, scale, T, T_pad, k
def strict_calls(one_stage):
nonlocal block_mask
- with torch._dynamo.config.patch(suppress_errors = False):
+ with _strict_dynamo():
if block_mask is None:
block_mask = get_block_mask(seg, q.device, compile_mask = True)
out = _run_flex(q, k, v, block_mask, enable_gqa, scale, True, T, T_pad, one_stage)