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 DPO, ORPO
Daniel Hanccf1f946f3f1
diff --git a/unsloth/kernels/cross_entropy_loss.py b/unsloth/kernels/cross_entropy_loss.pyindex 4895d273103..1c8f8c8d990 100644--- a/unsloth/kernels/cross_entropy_loss.py+++ b/unsloth/kernels/cross_entropy_loss.py@@ -375,7 +375,6 @@ def fast_cross_entropy_loss()if n_items is None:n_items = torch.count_nonzero(labels != -100)- print(n_items)return loss.sum() / n_itemspassdiff --git a/unsloth/models/_utils.py b/unsloth/models/_utils.pyindex 25024be2ecc..6611b4f6386 100644--- a/unsloth/models/_utils.py+++ b/unsloth/models/_utils.py@@ -1172,10 +1172,10 @@ passdef patch_gradient_accumulation_fix(Trainer):# Fixes gradient accumulation+ import inspectif hasattr(Trainer, "get_batch_samples"):- from inspect import getsourceif \- not getsource(Trainer.get_batch_samples).strip()\+ not inspect.getsource(Trainer.get_batch_samples).strip()\.endswith("return batch_samples, num_items_in_batch"):raise NotImplementedError("Unsloth: Please make a Github issue immediately!!")@@ -1198,4 +1198,33 @@ def patch_gradient_accumulation_fix(Trainer):'`pip install --upgrade --no-cache-dir unsloth git+https://github.com/huggingface/transformers.git git+https://github.com/huggingface/trl.git`')pass++ # Also fix up loss scaling ie negate loss *= self.args.gradient_accumulation_steps+ if "num_items_in_batch" not in inspect.signature(Trainer.training_step).parameters: return++ function = inspect.getsource(Trainer.training_step)+ where = function.find("def")+ function = function.split("\n")+ function = "\n".join(x[where:] for x in function)++ # Import all variables that need importing+ import transformers.trainer+ items_in_trainer = dir(transformers.trainer)+ good_items = []+ for item in items_in_trainer:+ # TODO: Support Deepspeed+ if item.startswith(("deepspeed", "xm", "met", "smp")): continue+ if item in function: good_items.append(item)+ pass+ exec("from transformers.trainer import (" + ", ".join(x for x in good_items) + ")", globals())++ # Accelerate does / self.args.gradient_accumulation_steps internally, so if we already+ # summed it up and did the division before hand, we have to negate it.+ function = function.replace(+ "loss *= self.args.gradient_accumulation_steps",+ "if num_items_in_batch is not None: loss *= self.args.gradient_accumulation_steps",+ )+ function = function.replace("def training_step", "def _unsloth_training_step", 1)+ exec(function, globals())+ Trainer.training_step = _unsloth_training_steppassdiff --git a/unsloth/save.py b/unsloth/save.pyindex ab30e0fea5a..ccda79aeee4 100644--- a/unsloth/save.py+++ b/unsloth/save.py@@ -145,7 +145,7 @@ passdef _merge_lora(layer, name):- bias = None+ bias = getattr(layer, "bias", None)if isinstance(layer, (Bnb_Linear4bit, Peft_Linear4bit, Peft_Linear)):# Is LoRA so we need to merge!W, quant_state, A, B, s, bias = get_lora_parameters_bias(layer)