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 generation (#472)

Daniel Han995dbe504385
diff --git a/unsloth/models/_utils.py b/unsloth/models/_utils.py
index 09e035e684f..a53de42c09d 100644
--- a/unsloth/models/_utils.py
+++ b/unsloth/models/_utils.py
@@ -15,11 +15,12 @@
import torch
from typing import Union, Optional, List, Any, Callable
import warnings
-warnings.filterwarnings(action = "ignore", category = UserWarning, module = "torch")
-warnings.filterwarnings(action = "ignore", category = UserWarning, module = "huggingface_hub")
+warnings.filterwarnings(action = "ignore", category = UserWarning, module = "torch")
+warnings.filterwarnings(action = "ignore", category = UserWarning, module = "huggingface_hub")
warnings.filterwarnings(action = "ignore", category = RuntimeWarning, module = "subprocess")
-warnings.filterwarnings(action = "ignore", category = UserWarning, module = "transformers")
-warnings.filterwarnings(action = "ignore", category = FutureWarning, module = "accelerate")
+warnings.filterwarnings(action = "ignore", category = UserWarning, module = "transformers")
+warnings.filterwarnings(action = "ignore", category = FutureWarning, module = "accelerate")
+warnings.filterwarnings(action = "ignore", category = FutureWarning, module = "huggingface_hub")
import bitsandbytes as bnb
from transformers.models.llama.modeling_llama import logger
from transformers import AutoTokenizer
@@ -388,3 +389,35 @@ class Unsloth_Offloaded_Gradient_Checkpointer(torch.autograd.Function):
pass
pass
+
+"""
+ Remove warnings about missing kwargs
+"""
+try:
+ from transformers.utils.quantization_config import BitsAndBytesConfig, QuantizationMethod
+ from inspect import getsource
+ import re
+ BitsAndBytesConfig__init__ = getsource(BitsAndBytesConfig.__init__)
+ BitsAndBytesConfig__init__ = re.sub(
+ r"if[\s]{1,}kwargs\:[\s]{1,}.+?\n",
+ "",
+ BitsAndBytesConfig__init__,
+ flags = re.MULTILINE,
+ )
+ BitsAndBytesConfig__init__ = BitsAndBytesConfig__init__.split("\n")
+ length_spaces = len(re.match(r"[\s]{1,}", BitsAndBytesConfig__init__[0]).group(0))
+ BitsAndBytesConfig__init__ = "\n".join(x[length_spaces:] for x in BitsAndBytesConfig__init__)
+ BitsAndBytesConfig__init__ = BitsAndBytesConfig__init__.replace(
+ "__init__",
+ "_BitsAndBytesConfig__init__",
+ )
+ exec(BitsAndBytesConfig__init__, globals())
+
+ import transformers.utils.quantization_config
+ transformers.utils.quantization_config.BitsAndBytesConfig.__init__ = _BitsAndBytesConfig__init__
+except:
+ logger.warning_once(
+ "Unsloth unsuccessfully patched bitsandbytes. Please file a bug report.\n"\
+ "Luckily, your training run will still work in the meantime!"
+ )
+pass
diff --git a/unsloth/models/gemma.py b/unsloth/models/gemma.py
index c0cce75e2f6..5dd2a5abd5b 100644
--- a/unsloth/models/gemma.py
+++ b/unsloth/models/gemma.py
@@ -71,7 +71,7 @@ def GemmaDecoderLayer_fast_forward(
padding_mask: Optional[torch.LongTensor] = None,
*args, **kwargs,
):
- if use_cache: #past_key_value is not None:
+ if use_cache and hasattr(self, "_flag_for_generation"): #past_key_value is not None:
out_weight = torch.empty(self.input_layernorm.weight.shape, dtype = torch.float32, device = "cuda")
# Self Attention
diff --git a/unsloth/models/llama.py b/unsloth/models/llama.py
index f08c403074f..f7fd5f13f43 100644
--- a/unsloth/models/llama.py
+++ b/unsloth/models/llama.py
@@ -407,7 +407,7 @@ def LlamaDecoderLayer_fast_forward(
(see `past_key_values`).
past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states
"""
- if use_cache:
+ if use_cache and hasattr(self, "_flag_for_generation"):
residual = hidden_states
hidden_states = fast_rms_layernorm_inference(self.input_layernorm, hidden_states)
hidden_states, self_attn_weights, present_key_value = self.self_attn(
@@ -789,7 +789,7 @@ def CausalLM_fast_forward(fast_forward_inference):
return_dict: Optional[bool] = None,
*args, **kwargs,
) -> Union[Tuple, CausalLMOutputWithPast]:
-
+
if past_key_values is not None:
outputs = fast_forward_inference(
self,
@@ -968,12 +968,34 @@ class LlamaLinearScalingRotaryEmbedding(LlamaRotaryEmbedding):
pass
-def _wrap_fast_inference(generate, device_type, dtype):
+def _wrap_fast_inference(generate, device_type, dtype, model):
# Wraps inference with bfloat16 / float16
@torch.inference_mode
def _fast_generate(*args, **kwargs):
+
+ # Set a flag for generation!
+ internal_model = model
+ while hasattr(internal_model, "model"):
+ internal_model._flag_for_generation = True
+ internal_model = internal_model.model
+ pass
+ internal_model._flag_for_generation = True
+
+ # Autocasted
with torch.autocast(device_type = device_type, dtype = dtype):
- return generate(*args, **kwargs)
+ output = generate(*args, **kwargs)
+ pass
+
+ # Unset a flag for generation!
+ internal_model = model
+ while hasattr(internal_model, "model"):
+ if hasattr(internal_model, "_flag_for_generation"): del internal_model._flag_for_generation
+ internal_model = internal_model.model
+ pass
+ if hasattr(internal_model, "_flag_for_generation"): del internal_model._flag_for_generation
+
+ return output
+ pass
return _fast_generate
pass
@@ -1787,7 +1809,7 @@ class FastLlamaModel:
# Wrap model.generate
model._unwrapped_old_generate = model.generate
- model.generate = _wrap_fast_inference(model.generate, device_type, dtype)
+ model.generate = _wrap_fast_inference(model.generate, device_type, dtype, model)
# Patch tokenizer to pad to the left
internal_model = model
diff --git a/unsloth/tokenizer_utils.py b/unsloth/tokenizer_utils.py
index 7a1d2ddacce..1cbe49b7ae7 100644
--- a/unsloth/tokenizer_utils.py
+++ b/unsloth/tokenizer_utils.py
@@ -369,10 +369,11 @@ def load_correct_tokenizer(
cache_dir = cache_dir,
)
except:
- print(
- f"Unsloth: {tokenizer_name} has no tokenizer.model file.\n"\
- "Just informing you about this - this is not a critical error."
- )
+ pass
+ # print(
+ # f"Unsloth: {tokenizer_name} has no tokenizer.model file.\n"\
+ # "Just informing you about this - this is not a critical error."
+ # )
pass
fast_tokenizer = AutoTokenizer.from_pretrained(