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 Llama and Gemma inference (#3034)

Edd8e994f07fb81
diff --git a/unsloth/models/falcon_h1.py b/unsloth/models/falcon_h1.py
index 0db9c1ca4e4..5643e3dfe94 100644
--- a/unsloth/models/falcon_h1.py
+++ b/unsloth/models/falcon_h1.py
@@ -51,7 +51,7 @@ try:
from transformers.models.falcon_h1.modeling_falcon_h1 import (
FalconH1Attention,
)
-except:
+except ModuleNotFoundError:
# if we are on a old version of transformers technically it should fail in the try except above
# but if somehow we make it here, we need to raise an error since FalconH1Attention is not available
# or renamed
diff --git a/unsloth/models/llama.py b/unsloth/models/llama.py
index 9b920fdac9f..3c0d5012aed 100644
--- a/unsloth/models/llama.py
+++ b/unsloth/models/llama.py
@@ -780,7 +780,7 @@ def LlamaModel_fast_forward(
# Fix up attention mask by setting elements to 0
# Specifically for DPO
if getattr(self, "_has_no_labels", False) is True and (attention_mask is not None) and (past_key_values is None) and \
- (not train_embed_tokens):
+ (not train_embed_tokens) and self.training:
# Careful for inference the attention_mask is size (1, kv_seq_len)
# Whilst the input_embeds is size (1, 1, 4096)
inputs_requires_grad = inputs_embeds.requires_grad
@@ -865,21 +865,21 @@ def LlamaModel_fast_forward(
# https://github.com/pytorch/pytorch/issues/103749
# Need to convert to float and not using bool
- attention_mask = (1.0 - attention_mask.float()) * torch.finfo(inputs_embeds.dtype).min
+ # attention_mask = (1.0 - attention_mask.float()) * torch.finfo(inputs_embeds.dtype).min
dynamic_SWA_mask = _prepare_4d_causal_attention_mask_for_sdpa(
attention_mask,
(batch_size, seq_length),
inputs_embeds,
past_key_values_length,
sliding_window = self.config.sliding_window,
- )[0][0]
+ )
dynamic_GA_mask = _prepare_4d_causal_attention_mask_for_sdpa(
attention_mask,
(batch_size, seq_length),
inputs_embeds,
past_key_values_length,
sliding_window = None,
- )[0][0]
+ )
use_static_mask = False
elif not hasattr(self, "SWA_mask"):
@@ -953,7 +953,7 @@ def LlamaModel_fast_forward(
else:
layer_outputs = decoder_layer(
hidden_states,
- causal_mask=mask,
+ causal_mask = mask,
attention_mask = attention_mask,
position_ids = position_ids,
past_key_value = past_key_value,