Skip to content
Merged
31 changes: 12 additions & 19 deletions tests/models/gemma4/test_modeling_gemma4.py
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ def test_model_with_image(self):

EXPECTED_TEXTS = Expectations(
{
("cuda", 8): ['This image shows a **brown and white cow** standing on a **sandy beach** with the **ocean and a blue sky** in the background'],
("cuda", 8): ['This image shows a **brown and white cow** standing on a **sandy beach** with the **ocean** in the background under a **clear'],
("xpu", 5): ['This image shows a **brown and white cow** standing on a **sandy beach** with the **ocean** in the background under a **clear'],
}
) # fmt: skip
Expand Down Expand Up @@ -863,15 +863,7 @@ def test_model_with_image_batch(self):

EXPECTED_TEXTS = Expectations(
{
("cuda", (8, 0)): [
"This image shows a **brown and white cow** standing on a **sandy beach** with the **ocean and a blue sky** in the background",
"No, these images are not identical.\n\nThe first image is a photograph of a **cow** standing on a beach under a blue sky.\n\n",
],
("cuda", (8, 6)): [
"This image shows a **brown and white cow** standing on a **sandy beach** with the **ocean and a blue sky** in the background",
"No, these images are not identical.\n\nThe first image is a photograph of a **brown and white cow standing on a beach** under a blue",
],
("cuda", (9, 0)): [
("cuda", 8): [
"This image shows a **brown and white cow** standing on a **sandy beach** with the **ocean and a blue sky** in the background",
"No, these images are **not identical**.\n\nHere's a breakdown of the differences:\n\n1. **Image 1 (Cow on",
],
Expand Down Expand Up @@ -913,7 +905,7 @@ def test_model_multiimage(self):
output_text = self.processor.batch_decode(output[:, input_size:], skip_special_tokens=True)
EXPECTED_TEXTS = Expectations(
{
("cuda", 8): ['Based on the image, here is a description of what I see:\n\n**Foreground & Street Scene:**\n* **Traffic Sign:** The most prominent'],
("cuda", 8): ['Based on the image, here is a description of what I see:\n\n**Foreground & Street Scene:**\n* **Roadway:** There is an'],
("cuda", (9, 0)): ['Based on the image, here is a description of what I see:\n\n**Foreground & Street Scene:**\n* **Roadway:** There is an'],
("xpu", 5): ['Based on the image, here is a description of what I see:\n\n**Foreground & Street Scene:**\n* **Roadway:** There is an'],
}
Expand Down Expand Up @@ -1053,14 +1045,15 @@ def test_generation_beyond_sliding_window(self, attn_implementation: str):
def test_export_text_only(self):
from transformers.integrations.executorch import TorchExportableModuleForDecoderOnlyLM

model = Gemma4ForConditionalGeneration.from_pretrained(self.model_name, device_map=torch_device)
# Run on CPU: the full E2B model (~4 GiB bfloat16) + torch.export tracing overhead
# (~4 GiB) exceeds the 22.3 GiB GPU memory available in CI. CPU avoids the OOM.
# max_cache_len=19 covers the prompt (~16 tokens) + 3 new tokens with a small buffer.
model = Gemma4ForConditionalGeneration.from_pretrained(self.model_name, device_map="cpu")
tokenizer = AutoTokenizer.from_pretrained(self.model_name)

exportable_module = TorchExportableModuleForDecoderOnlyLM(
model, batch_size=1, max_cache_len=1024, device=torch_device
)
exportable_module = TorchExportableModuleForDecoderOnlyLM(model, batch_size=1, max_cache_len=19, device="cpu")
exported_program = exportable_module.export(
input_ids=torch.tensor([[1]], device=torch_device, dtype=torch.long),
input_ids=torch.tensor([[1]], device="cpu", dtype=torch.long),
)

# Test generation with the exported model
Expand All @@ -1070,13 +1063,13 @@ def test_export_text_only(self):
add_generation_prompt=True,
)

max_new_tokens_to_generate = 20
max_new_tokens_to_generate = 3
# Generate text with the exported model
export_generated_text = TorchExportableModuleForDecoderOnlyLM.generate(
exported_program, tokenizer, prompt, max_new_tokens=max_new_tokens_to_generate, device=torch_device
exported_program, tokenizer, prompt, max_new_tokens=max_new_tokens_to_generate, device="cpu"
)

input_text = tokenizer(prompt, return_tensors="pt").to(torch_device)
input_text = tokenizer(prompt, return_tensors="pt").to("cpu")
eager_outputs = model.generate(
**input_text,
max_new_tokens=max_new_tokens_to_generate,
Expand Down
Loading