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.

Block compile: keep a caller's flex BACKEND instead of adding FORCE_USE_FLEX_ATTENTION (#15044)

Daniel Hand3e3edc1e9c2
diff --git a/tests/test_block_compile.py b/tests/test_block_compile.py
index ec1361a8466..9110d7ee8e0 100644
--- a/tests/test_block_compile.py
+++ b/tests/test_block_compile.py
@@ -1463,3 +1463,23 @@ def test_router_logits_capture_compiles_one_graph_for_every_layer(monkeypatch):
assert len(got) == len(want)
for g, w in zip(got, want):
torch.testing.assert_close(g.detach(), w)
+
+
[email protected]("backend", ["AUTO", "TRITON"])
+def test_training_flex_options_keep_a_callers_backend(backend):
+ """BACKEND supersedes FORCE_USE_FLEX_ATTENTION, and torch 2.10+ raises when both are set."""
+ M = _load()
+ options = {"BACKEND": backend}
+ assert M._training_flex_options(options) == options
+ assert M._training_flex_options({"ROWS_GUARANTEED_SAFE": True}) == {
+ "ROWS_GUARANTEED_SAFE": True,
+ "FORCE_USE_FLEX_ATTENTION": True,
+ }
+ flex = pytest.importorskip("torch.nn.attention.flex_attention")
+ if (
+ not hasattr(flex, "_apply_kernel_options")
+ or "BACKEND" not in getattr(flex, "FlexKernelOptions", {}).__annotations__
+ ):
+ return
+ q = torch.randn(1, 1, 16, 16)
+ flex._apply_kernel_options(q, q, q, False, M._training_flex_options(options))
diff --git a/unsloth/models/_block_compile.py b/unsloth/models/_block_compile.py
index 8047ab0229e..84bcb34720f 100644
--- a/unsloth/models/_block_compile.py
+++ b/unsloth/models/_block_compile.py
@@ -573,7 +573,8 @@ def _training_flex_options(options):
(GQA, head_dim 256), demoting every block. A caller's explicit choice is kept."""
if options is None:
return {"FORCE_USE_FLEX_ATTENTION": True}
- if isinstance(options, dict) and "FORCE_USE_FLEX_ATTENTION" not in options:
+ # torch 2.10+ rejects BACKEND together with the legacy FORCE_USE_FLEX_ATTENTION.
+ if isinstance(options, dict) and not ({"FORCE_USE_FLEX_ATTENTION", "BACKEND"} & options.keys()):
return {**options, "FORCE_USE_FLEX_ATTENTION": True}
return options