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
fix: stop faking 8bit load flag (#6708)
Long Yixinge594e5d20186
diff --git a/unsloth/models/_utils.py b/unsloth/models/_utils.pyindex 7a056cef820..7ad6e8ea334 100644--- a/unsloth/models/_utils.py+++ b/unsloth/models/_utils.py@@ -86,6 +86,8 @@ __all__ = ["is_moe_model","get_moe_target_parameters","make_fast_generate_wrapper",+ "_mark_unsloth_disable_data_parallel",+ "_patch_transformers_trainer_data_parallel",]import torch@@ -160,6 +162,85 @@ from unsloth_zoo.training_utils import ()+def _iter_wrapped_models(model):+ seen = set()+ current = model+ while current is not None and id(current) not in seen:+ yield current+ seen.add(id(current))+ next_model = getattr(current, "model", None)+ if next_model is None:+ next_model = getattr(current, "base_model", None)+ if next_model is None:+ next_model = getattr(current, "module", None)+ current = next_model+++def _patch_transformers_trainer_data_parallel():+ try:+ from transformers.trainer import Trainer+ except (ImportError, ModuleNotFoundError):+ return False++ original_wrap_model = getattr(Trainer, "_wrap_model", None)+ if original_wrap_model is None:+ return False+ if getattr(original_wrap_model, "_unsloth_data_parallel_patched", False):+ return True+ try:+ supports_dataloader = "dataloader" in inspect.signature(original_wrap_model).parameters+ except (TypeError, ValueError):+ supports_dataloader = True++ def _call_original_wrap_model(self, model, wrap_args, wrap_kwargs):+ if supports_dataloader:+ return original_wrap_model(self, model, *wrap_args, **wrap_kwargs)++ if "dataloader" in wrap_kwargs:+ wrap_kwargs = {k: v for k, v in wrap_kwargs.items() if k != "dataloader"}+ return original_wrap_model(self, model, *wrap_args, **wrap_kwargs)++ @functools.wraps(original_wrap_model)+ def _unsloth_wrap_model(self, model, *wrap_args, **wrap_kwargs):+ args = getattr(self, "args", None)+ disable_data_parallel = getattr(model, "_unsloth_disable_data_parallel", False)+ is_real_8bit = getattr(model, "is_loaded_in_8bit", False)+ if (+ args is None+ or not disable_data_parallel+ or is_real_8bit+ or getattr(args, "n_gpu", 0) <= 1+ ):+ return _call_original_wrap_model(self, model, wrap_args, wrap_kwargs)++ had_n_gpu = hasattr(args, "_n_gpu")+ old_n_gpu = getattr(args, "_n_gpu", None)+ args._n_gpu = 1+ try:+ return _call_original_wrap_model(self, model, wrap_args, wrap_kwargs)+ finally:+ if had_n_gpu:+ args._n_gpu = old_n_gpu+ else:+ try:+ delattr(args, "_n_gpu")+ except AttributeError:+ pass++ _unsloth_wrap_model._unsloth_data_parallel_patched = True+ _unsloth_wrap_model._unsloth_original_wrap_model = original_wrap_model+ Trainer._wrap_model = _unsloth_wrap_model+ return True+++def _mark_unsloth_disable_data_parallel(model, disable = True):+ if disable:+ _patch_transformers_trainer_data_parallel()+ for module in _iter_wrapped_models(model):+ setattr(module, "_unsloth_disable_data_parallel", bool(disable))+ return model++def resolve_hip_gpu_stats_name(gpu_stats):name = str(getattr(gpu_stats, "name", "") or "").strip()name = re.sub(r"\s*\([^)]*\)\s*$", "", name).strip()diff --git a/unsloth/models/llama.py b/unsloth/models/llama.pyindex d5eca8d6df3..14ee5ee24ea 100644--- a/unsloth/models/llama.py+++ b/unsloth/models/llama.py@@ -2832,13 +2832,11 @@ class FastLlamaModel:internal_model = modelwhile hasattr(internal_model, "model"):internal_model._saved_temp_tokenizer = tokenizer- # Also set is_loaded_in_8bit to disable incorrect DDP- internal_model.is_loaded_in_8bit = Trueinternal_model = internal_model.modelinternal_model._saved_temp_tokenizer = tokenizer- # Also set is_loaded_in_8bit to disable incorrect DDP- internal_model.is_loaded_in_8bit = True+ # Prevent Transformers Trainer from auto-wrapping Unsloth LoRA models in DP.+ _mark_unsloth_disable_data_parallel(model)# For transformers > 4.47.1, we need to add rotary_emb to all attention layersif IS_ATTENTION_REFACTOR or hasattr(model.model, "rotary_emb"):@@ -3379,13 +3377,11 @@ class FastLlamaModel:while hasattr(internal_model, "model"):if hasattr(internal_model, "_saved_temp_tokenizer"):internal_model._saved_temp_tokenizer.padding_side = "right"- # Also set is_loaded_in_8bit to disable incorrect DDP- internal_model.is_loaded_in_8bit = Trueinternal_model = internal_model.modelif hasattr(internal_model, "_saved_temp_tokenizer"):internal_model._saved_temp_tokenizer.padding_side = "right"- # Also set is_loaded_in_8bit to disable incorrect DDP- internal_model.is_loaded_in_8bit = True+ # Prevent Transformers Trainer from auto-wrapping Unsloth LoRA models in DP.+ _mark_unsloth_disable_data_parallel(model)# Clear deleted GPU itemsfor _ in range(3):diff --git a/unsloth/models/vision.py b/unsloth/models/vision.pyindex 39004b4d457..bdc2bd9ef62 100644--- a/unsloth/models/vision.py+++ b/unsloth/models/vision.py@@ -1440,16 +1440,14 @@ class FastBaseModel:while hasattr(m, "model"):m.max_seq_length = max_seq_lengthm._saved_temp_tokenizer = tokenizer- # Also set is_loaded_in_8bit to disable incorrect DDP- m.is_loaded_in_8bit = True if not full_finetuning else Falsem = m.modelm.max_seq_length = max_seq_length# Save to modules as wellfor module in model.modules():module.max_seq_length = max_seq_lengthm._saved_temp_tokenizer = tokenizer- # Also set is_loaded_in_8bit to disable incorrect DDP- m.is_loaded_in_8bit = True if not full_finetuning else False+ # Prevent Transformers Trainer from auto-wrapping Unsloth LoRA models in DP.+ _mark_unsloth_disable_data_parallel(model, disable = not full_finetuning)# Patch generateif os.environ.get("UNSLOTH_DISABLE_FAST_GENERATION", "0") == "0" and hasattr(@@ -1826,14 +1824,12 @@ class FastBaseModel:if hasattr(m, "_saved_temp_tokenizer"):if hasattr(m._saved_temp_tokenizer, "tokenizer"):m._saved_temp_tokenizer.tokenizer.padding_side = "left"- # Also set is_loaded_in_8bit to disable incorrect DDP- m.is_loaded_in_8bit = True if not full_finetuning else Falsem = m.modelif hasattr(m, "_saved_temp_tokenizer"):if hasattr(m._saved_temp_tokenizer, "tokenizer"):m._saved_temp_tokenizer.tokenizer.padding_side = "left"- # Also set is_loaded_in_8bit to disable incorrect DDP- m.is_loaded_in_8bit = True if not full_finetuning else False+ # Prevent Transformers Trainer from auto-wrapping Unsloth LoRA models in DP.+ _mark_unsloth_disable_data_parallel(model, disable = not full_finetuning)# Clear deleted GPU itemsfor _ in range(3):