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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Turn off embedding offload when the model is dispatched across devices (#8272)
Daniel Hane0d3b1dcee21
diff --git a/tests/test_offload_tied_autodisable.py b/tests/test_offload_tied_autodisable.pyindex 43384b08419..15eb1186ea1 100644--- a/tests/test_offload_tied_autodisable.py+++ b/tests/test_offload_tied_autodisable.py@@ -38,10 +38,12 @@ def _load(*names):_NS = _load("_embeddings_are_tied","_offload_embedding_unsupported_platform",+ "_embedding_dispatch_device","_resolve_offload_embedding",)resolve = _NS["_resolve_offload_embedding"]unsupported_platform = _NS["_offload_embedding_unsupported_platform"]+dispatch_device = _NS["_embedding_dispatch_device"]_WSL_VARS = ("WSL_DISTRO_NAME", "WSL_INTEROP")@@ -166,6 +168,43 @@ def test_no_tied_embedding_raise_remains():assert "is not supported for models with tied word" not in _SRC+class _Hook:+ def __init__(self, execution_device):+ self.execution_device = execution_device+++def _dispatched_model(execution_device = torch.device("cuda", 0)):+ m = _untied_model()+ m.get_input_embeddings()._hf_hook = _Hook(execution_device)+ return m+++def test_dispatch_device_reads_the_accelerate_hook():+ assert dispatch_device(nn.Embedding(32, 8)) is None # no hook at all+ assert dispatch_device(_dispatched_model().get_input_embeddings()) is not None+ assert dispatch_device(_dispatched_model(None).get_input_embeddings()) is None+ assert dispatch_device(None) is None # embeddings not exposed+++def test_dispatched_model_disables_offload():+ # accelerate re-sends the ids to its recorded device after the offload pre-hook has+ # sent them to the CPU weight, so the lookup gets ids and weight on different devices.+ with _as_platform("posix"):+ assert resolve(_dispatched_model(), True) is False+++def test_hook_without_execution_device_keeps_offload():+ # A hook that never moves anything cannot undo the offload.+ with _as_platform("posix"):+ assert resolve(_dispatched_model(None), True) is True+++def test_undispatched_model_keeps_offload():+ # The single-GPU path must not lose the VRAM saving.+ with _as_platform("posix"):+ assert resolve(_untied_model(), True) is True++if __name__ == "__main__":for name, fn in sorted(globals().items()):if name.startswith("test_") and callable(fn):diff --git a/unsloth/models/vision.py b/unsloth/models/vision.pyindex 2fb915cdd2c..d5e60a7d82c 100644--- a/unsloth/models/vision.py+++ b/unsloth/models/vision.py@@ -320,6 +320,13 @@ def _offload_embedding_unsupported_platform():return None+def _embedding_dispatch_device(input_embeddings):+ # accelerate's hook wraps forward and re-sends the ids to the device it recorded, after+ # our offload pre-hook already sent them to the CPU weight. Returns that device, or None.+ hook = getattr(input_embeddings, "_hf_hook", None)+ return None if hook is None else getattr(hook, "execution_device", None)++def _resolve_offload_embedding(model, offload_embedding):"""Report `offload_embedding` as True only when the offload will really run.@@ -348,6 +355,12 @@ def _resolve_offload_embedding(model, offload_embedding):"to lm_head, so offloading saves no VRAM.")return False+ if _embedding_dispatch_device(in_embed) is not None:+ print(+ "Unsloth: Not offloading embeddings; this model is dispatched across devices, "+ "which overrides the offload."+ )+ return Falsereturn True