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.

Fix bug

Daniel Hana8490a2a8af9
diff --git a/unsloth/models/_utils.py b/unsloth/models/_utils.py
index 242d234db86..6dd17e739ec 100644
--- a/unsloth/models/_utils.py
+++ b/unsloth/models/_utils.py
@@ -285,6 +285,8 @@ pass
# =============================================
# Fix new Xformers versions TypeError: Multiple dispatch failed for 'torch._ops.aten.to.dtype_layout'
+accelerate_old_send_to_device = None
+accelerate_new_send_to_device = None
if Version(xformers_version) >= Version("0.0.27"):
import accelerate.utils.operations
if hasattr(accelerate.utils.operations, "send_to_device") and \
diff --git a/unsloth/models/llama.py b/unsloth/models/llama.py
index 3fcb8a76d2e..39998127f0c 100644
--- a/unsloth/models/llama.py
+++ b/unsloth/models/llama.py
@@ -1371,8 +1371,10 @@ def _wrap_fast_inference(generate, device_type, dtype, model):
internal_model._flag_for_generation = True
# Must patch accelerate for Xformers
- import accelerate.utils.operations
- accelerate.utils.operations.send_to_device = accelerate_new_send_to_device
+ if accelerate_new_send_to_device is not None:
+ import accelerate.utils.operations
+ accelerate.utils.operations.send_to_device = accelerate_new_send_to_device
+ pass
# For newer HF
kwargs["cache_implementation"] = "dynamic"
@@ -1411,7 +1413,9 @@ def _wrap_fast_inference(generate, device_type, dtype, model):
if hasattr(internal_model, "_flag_for_generation"): del internal_model._flag_for_generation
# Return accelerate back
- accelerate.utils.operations.send_to_device = accelerate_old_send_to_device
+ if accelerate_new_send_to_device is not None:
+ accelerate.utils.operations.send_to_device = accelerate_old_send_to_device
+ pass
return output
pass