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.

n_items

Daniel Handd8487a63e86
diff --git a/unsloth/__init__.py b/unsloth/__init__.py
index abee9c9e04b..458c2696bce 100644
--- a/unsloth/__init__.py
+++ b/unsloth/__init__.py
@@ -62,9 +62,13 @@ os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
try:
import torch
-except:
- raise ImportError("Pytorch is not installed. Go to https://pytorch.org/.\n"\
- "We have some installation instructions on our Github page.")
+except ModuleNotFoundError:
+ raise ImportError(
+ "Unsloth: Pytorch is not installed. Go to https://pytorch.org/.\n"\
+ "We have some installation instructions on our Github page."
+ )
+except Exception as exception:
+ raise exception
pass
# Hugging Face Hub faster downloads (only enable during Colab and Kaggle sessions)
diff --git a/unsloth/kernels/cross_entropy_loss.py b/unsloth/kernels/cross_entropy_loss.py
index 1c8f8c8d990..35df2573294 100644
--- a/unsloth/kernels/cross_entropy_loss.py
+++ b/unsloth/kernels/cross_entropy_loss.py
@@ -373,6 +373,7 @@ def fast_cross_entropy_loss(
logit_softcapping,
logit_scaling,
)
+ print(n_items)
if n_items is None:
n_items = torch.count_nonzero(labels != -100)
return loss.sum() / n_items
diff --git a/unsloth/tokenizer_utils.py b/unsloth/tokenizer_utils.py
index 1cad00d44dd..8806f1e743c 100644
--- a/unsloth/tokenizer_utils.py
+++ b/unsloth/tokenizer_utils.py
@@ -914,8 +914,10 @@ def patch_sft_trainer_tokenizer():
check_text = \
"\n"\
- "if 'tokenizer' not in locals(): tokenizer = processing_class\n"\
- "test_text = dataset[0][dataset_text_field] if (formatting_func is not None and dataset_text_field is None) else formatting_func(dataset[0])[0]\n"\
+ "if 'tokenizer' not in locals(): tokenizer = processing_class\n"\
+ "if 'formatting_func' not in locals(): raise RuntimeError('Unsloth: Please file a bug report - `formatting_func` does not exist!')\n"\
+ "if 'dataset_text_field' not in locals(): raise RuntimeError('Unsloth: Please file a bug report - `dataset_text_field` does not exist!')\n"\
+ "test_text = dataset[0][dataset_text_field] if (formatting_func is None and dataset_text_field is not None) else formatting_func(dataset[0])[0]\n"\
"chat_template = getattr(tokenizer, 'chat_template', None)\n"\
"chat_template = '' if chat_template is None else chat_template\n"\
"has_bos_token_already = (test_text.startswith(tokenizer.bos_token) or tokenizer.bos_token in chat_template) "\
@@ -1017,7 +1019,10 @@ pass
for trainer_name in ("SFTTrainer", "DPOTrainer", "KTOTrainer"):
trainer_text = patch_trl_tokenizer_processing_class(trainer_name)
if trainer_text is None: continue
- exec(trainer_text, globals())
+ try:
+ exec(trainer_text, globals())
+ except:
+ raise RuntimeError(f"Unsloth: Please file a bug report! Error patching {trainer_name}")
exec(f"trl.trainer.{trainer_name} = Unsloth{trainer_name}", globals())
pass