Feat: add gemma3n support (#2852)

* feat: add gemma3n cce

* feat: add sample config

* feat: add gemma3n multimodal mode

* feat: add audio example

* feat: support audio and return pixel values in collator

* feat: support unmask only assistant region (gemma3n for now)

* feat(doc): add notes for audio loading

* feat: add audio support for gemma3n

* feat: update examples

* feat: add gemma3n to the docs

* fix: add link at top

* feat(doc): clarify additional requirements

* fix: mllama missing aspect ratio

* fix: mllama need attention fixes for fa2

* Partially Revert "fix: mllama need attention fixes for fa2"

This reverts commit a0bfdd1777.

* fix: disable FA2 for mllama in vision mode

* feat: update configs to use proper attention

* fix: support other vision features

* feat(doc): clarify requirements for gemma3n
This commit is contained in:
NanoCode012
2025-07-22 16:52:15 +07:00
committed by GitHub
parent d32058e149
commit dfba881e99
15 changed files with 473 additions and 18 deletions

View File

@@ -37,6 +37,8 @@ plugins:
- gemma2
- gemma3
- gemma3_text
- gemma3n
- gemma3n_text
- glm
- glm4
- llama

View File

@@ -2,6 +2,7 @@
from transformers import (
Gemma3ForConditionalGeneration,
Gemma3nForConditionalGeneration,
Llama4ForConditionalGeneration,
LlavaForConditionalGeneration,
Mistral3ForConditionalGeneration,
@@ -18,4 +19,5 @@ MULTIMODAL_AUTO_MODEL_MAPPING = {
"qwen2_5_vl": Qwen2_5_VLForConditionalGeneration,
"mistral3": Mistral3ForConditionalGeneration,
"gemma3": Gemma3ForConditionalGeneration,
"gemma3n": Gemma3nForConditionalGeneration,
}

View File

@@ -5,7 +5,7 @@ from typing import Optional
from PIL import Image, ImageOps
from PIL.Image import Resampling
from torch import Tensor
from torch import Tensor, zeros_like
from transformers import ProcessorMixin
from transformers.image_utils import load_image
@@ -208,9 +208,18 @@ class ProcessingStrategy:
return processed_examples
def _mask_non_assistant(self, labels: Tensor) -> Tensor:
"""
Mask non assistant regions to -100.
To be implemented per subclass.
"""
return labels
def process_labels(self, input_ids: Tensor) -> Tensor:
labels = input_ids.clone()
labels = self._mask_non_assistant(labels)
# The labels are the input_ids, and we mask the padding tokens in the loss computation
labels[labels == self.processor.tokenizer.pad_token_id] = -100
@@ -264,6 +273,99 @@ class Gemma3ProcessingStrategy(ProcessingStrategy):
return labels
class Gemma3nProcessingStrategy(ProcessingStrategy):
"""Processing Strategy class for Gemma3n"""
def _mask_non_assistant(self, labels: Tensor) -> Tensor:
def _find_token_sequence(label, start_pos, token_sequence):
"""Check if token_sequence appears at start_pos in label"""
if start_pos + len(token_sequence) > len(label):
return False
if label[start_pos] != token_sequence[0]:
return False
return (
label[start_pos : start_pos + len(token_sequence)].tolist()
== token_sequence
)
def _find_assistant_end(label, start_pos, assistant_end_tok, mask, i):
"""
Find the end of assistant response and update mask accordingly
Returns new position to continue from and whether the end seq is found
"""
k = start_pos
while k < len(label):
if not _find_token_sequence(label, k, assistant_end_tok):
mask[i][k] = 1
k += 1
continue
return k + len(assistant_end_tok), True
return k, False
mask = zeros_like(labels)
assistant_start_str = "<start_of_turn>model"
assistant_end_str = "<end_of_turn>"
include_assistant_start_tok = False
include_assistant_end_tok = True
# str to tokens
assistant_start_tok = self.processor.tokenizer.encode(
assistant_start_str, add_special_tokens=False
)
assistant_end_tok = self.processor.tokenizer.encode(
assistant_end_str, add_special_tokens=False
)
for i, label in enumerate(labels):
j = 0
# while loop through each tok index in labels[i]
while j < len(label):
# Check until match start seq
if not _find_token_sequence(label, j, assistant_start_tok):
j += 1
continue
if include_assistant_start_tok:
mask[i][j : j + len(assistant_start_tok)] = 1
# Find where the assistant response ends
start_of_content = j + len(assistant_start_tok)
end_pos, found_end_seq = _find_assistant_end(
label, start_of_content, assistant_end_tok, mask, i
)
# Include end token if requested
if include_assistant_end_tok and found_end_seq:
mask[i][end_pos - len(assistant_end_tok) : end_pos] = 1
j = end_pos
labels[i][mask[i] == 0] = -100
return labels
def process_labels(self, input_ids):
labels = input_ids.clone()
labels = self._mask_non_assistant(labels)
# Follows https://colab.research.google.com/github/huggingface/huggingface-gemma-recipes/blob/main/notebooks/fine_tune_gemma3n_on_t4.ipynb
labels[labels == self.processor.tokenizer.pad_token_id] = -100
if hasattr(self.processor.tokenizer, "image_token_id"):
labels[labels == self.processor.tokenizer.image_token_id] = -100
if hasattr(self.processor.tokenizer, "audio_token_id"):
labels[labels == self.processor.tokenizer.audio_token_id] = -100
if hasattr(self.processor.tokenizer, "boi_token_id"):
labels[labels == self.processor.tokenizer.boi_token_id] = -100
if hasattr(self.processor.tokenizer, "eoi_token_id"):
labels[labels == self.processor.tokenizer.eoi_token_id] = -100
return labels
def get_processing_strategy(
processor: ProcessorMixin,
chat_template,
@@ -279,6 +381,10 @@ def get_processing_strategy(
return Gemma3ProcessingStrategy(
processor, chat_template, image_size, image_resize_algorithm
)
if chat_template_type == "gemma3n":
return Gemma3nProcessingStrategy(
processor, chat_template, image_size, image_resize_algorithm
)
if chat_template_type in [
"llama3_2_vision",
"llama4",

View File

@@ -0,0 +1,49 @@
{{ bos_token }}
{%- if messages[0]['role'] == 'system' -%}
{%- if messages[0]['content'] is string -%}
{%- set first_user_prefix = messages[0]['content'] + '
' -%}
{%- else -%}
{%- set first_user_prefix = messages[0]['content'][0]['text'] + '
' -%}
{%- endif -%}
{%- set loop_messages = messages[1:] -%}
{%- else -%}
{%- set first_user_prefix = "" -%}
{%- set loop_messages = messages -%}
{%- endif -%}
{%- for message in loop_messages -%}
{%- if (message['role'] == 'user') != (loop.index0 % 2 == 0) -%}
{{ raise_exception("Conversation roles must alternate user/assistant/user/assistant/...") }}
{%- endif -%}
{%- if (message['role'] == 'assistant') -%}
{%- set role = "model" -%}
{%- else -%}
{%- set role = message['role'] -%}
{%- endif -%}
{{ '<start_of_turn>' + role + '
' + (first_user_prefix if loop.first else "") }}
{%- if message['content'] is string -%}
{{ message['content'] | trim }}
{%- elif message['content'] is iterable -%}
{%- for item in message['content'] -%}
{%- if item['type'] == 'audio' -%}
{{ '<audio_soft_token>' }}
{%- elif item['type'] == 'image' -%}
{{ '<image_soft_token>' }}
{%- elif item['type'] == 'text' -%}
{{ item['text'] | trim }}
{%- endif -%}
{%- endfor -%}
{%- else -%}
{{ raise_exception("Invalid content type") }}
{%- endif -%}
{{ '<end_of_turn>
' }}
{%- endfor -%}
{%- if add_generation_prompt -%}
{{'<start_of_turn>model
'}}
{%- endif -%}

View File

@@ -84,6 +84,17 @@ class MultiModalChatDataCollator(DataCollatorMixin):
"attention_mask": attention_mask,
}
for key, val in batch.items():
if key in ["input_ids", "attention_mask"]:
continue
if key in ["token_type_ids", "cross_attention_mask"]:
final_batch[key] = torch.nn.utils.rnn.pad_sequence(
val, batch_first=True, padding_value=0
)
else:
final_batch[key] = torch.stack(val)
# Process the labels
final_batch["labels"] = self.processing_strategy.process_labels(
final_batch["input_ids"]

View File

@@ -62,6 +62,7 @@ class ChatTemplate(str, Enum):
llava = "llava"
qwen2_vl = "qwen2_vl"
gemma3 = "gemma3"
gemma3n = "gemma3n"
command_a = "command_a"
command_a_tool_use = "command_a_tool_use"
command_a_rag = "command_a_rag"