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 Swap blocker: offload_layers never engages on Qwen3.8-Flash-Next, the CPU-kept n-gram table makes the map look like two devices (#15633)

Leo Borcherding3f2b7b190841
diff --git a/tests/test_block_swap_no_placement_order.py b/tests/test_block_swap_no_placement_order.py
new file mode 100644
index 00000000000..a2908daf09e
--- /dev/null
+++ b/tests/test_block_swap_no_placement_order.py
@@ -0,0 +1,50 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
+
+"""`exclude_no_placement_params` must run after the Block Swap decisions (Auto bail, Count collapse)."""
+
+import ast
+import os
+import sys
+
+import unsloth # noqa: F401
+from unsloth.models.loader_utils import exclude_no_placement_params
+
+sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
+from test_no_placement_params_device_map import Model, check # noqa: E402
+
+VISION = os.path.join(
+ os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "unsloth", "models", "vision.py"
+)
+
+
+def _lines(tree, predicate):
+ lines = [n.lineno for n in ast.walk(tree) if predicate(n)]
+ assert lines
+ return lines
+
+
+def _calls(name):
+ return lambda n: isinstance(n, ast.Call) and getattr(n.func, "id", None) == name
+
+
+def test_no_placement_exclusion_runs_after_every_block_swap_decision():
+ tree = ast.parse(open(VISION, encoding = "utf-8").read())
+ (exclude,) = _lines(tree, _calls("exclude_no_placement_params"))
+ for decision in ("refuse_block_swap_load", "resolve_auto_block_swap"):
+ assert max(_lines(tree, _calls(decision))) < exclude, decision
+ pins = _lines(
+ tree,
+ lambda n: isinstance(n, ast.Assign)
+ and getattr(n.targets[0], "id", None) == "device_map"
+ and isinstance(n.value, ast.Dict)
+ and any(getattr(v, "id", None) == "_block_swap_device" for v in n.value.values),
+ )
+ assert max(pins) < exclude
+
+
+def test_excluded_map_is_not_the_bare_map_block_swap_needs():
+ # resolve_auto_block_swap plans only `{"": N}`; this is an explicit map, so Auto bailed.
+ out = exclude_no_placement_params({"": 0}, Model, None)
+ check(out, 0)
+ assert set(out) != {""}
diff --git a/unsloth/models/vision.py b/unsloth/models/vision.py
index 8ab51e690d5..f09f84aa3f9 100644
--- a/unsloth/models/vision.py
+++ b/unsloth/models/vision.py
@@ -4289,8 +4289,6 @@ class FastBaseModel:
),
)
- device_map = exclude_no_placement_params(device_map, model_class, auto_config)
-
if int(load_in_4bit) + int(load_in_8bit) + int(load_in_16bit) >= 2:
raise RuntimeError(
"Unsloth: Can only load in 4bit or 8bit or 16bit, not a combination!"
@@ -4346,6 +4344,8 @@ class FastBaseModel:
if (_offload_layers or _embedding_needed) and _block_swap_device is not None:
# A placement strategy would spill the host-bound weights to the CPU, which 4-bit loads refuse.
device_map = {"": _block_swap_device}
+ # After the Block Swap decisions: Auto plans only `{"": N}`, and the collapse above would re-cover the CPU-kept table.
+ device_map = exclude_no_placement_params(device_map, model_class, auto_config)
_block_swap_state = None
# Prefetch the repo (killable child) so the in-process load is a cache hit. vLLM owns the download only when actually available; if it was requested but is missing, the load falls through in-process, so warm the weights here.