Some checks failed
Self-hosted runner (nightly-past-ci-caller) / Get number (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.11 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.10 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.9 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.8 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.7 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.6 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.5 (push) Has been cancelled
Self-hosted runner (benchmark) / Benchmark (aws-g5-4xlarge-cache) (push) Has been cancelled
Build documentation / build (push) Has been cancelled
Build documentation / build_other_lang (push) Has been cancelled
CodeQL Security Analysis / CodeQL Analysis (push) Has been cancelled
New model PR merged notification / Notify new model (push) Has been cancelled
PR CI / pr-ci (push) Has been cancelled
Slow tests on important models (on Push - A10) / Get all modified files (push) Has been cancelled
Secret Leaks / trufflehog (push) Has been cancelled
Update Transformers metadata / build_and_package (push) Has been cancelled
Slow tests on important models (on Push - A10) / Model CI (push) Has been cancelled
Check Tiny Models / Check tiny models (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / Model CI (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / Pipeline CI (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / Example CI (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / DeepSpeed CI (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / Trainer/FSDP CI (push) Has been cancelled
Nvidia CI - Flash Attn / Setup (push) Has been cancelled
Nvidia CI - Flash Attn / Model CI (push) Has been cancelled
Nvidia CI / Setup (push) Has been cancelled
Nvidia CI / Model CI (push) Has been cancelled
Nvidia CI / Torch pipeline CI (push) Has been cancelled
Nvidia CI / Example CI (push) Has been cancelled
Nvidia CI / Trainer/FSDP CI (push) Has been cancelled
Nvidia CI / DeepSpeed CI (push) Has been cancelled
Nvidia CI / Quantization CI (push) Has been cancelled
Nvidia CI / Kernels CI (push) Has been cancelled
Doctests / Setup (push) Has been cancelled
Doctests / Call doctest jobs (push) Has been cancelled
Doctests / Send results to webhook (push) Has been cancelled
Extras Smoke Test / Get supported Python versions (push) Has been cancelled
Extras Smoke Test / Test extras on Python ${{ matrix.python-version }} (push) Has been cancelled
Extras Smoke Test / Check Slack token availability (push) Has been cancelled
Extras Smoke Test / Notify failures to Slack (push) Has been cancelled
Self-hosted runner (AMD scheduled CI caller) / Trigger Scheduled AMD CI (push) Has been cancelled
Stale Bot / Close Stale Issues (push) Has been cancelled
135 lines
4.4 KiB
Python
135 lines
4.4 KiB
Python
# Copyright 2026 the HuggingFace and MistralAI Teams. All rights reserved.
|
||
#
|
||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||
# you may not use this file except in compliance with the License.
|
||
# You may obtain a copy of the License at
|
||
#
|
||
# http://www.apache.org/licenses/LICENSE-2.0
|
||
#
|
||
# Unless required by applicable law or agreed to in writing, software
|
||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
# See the License for the specific language governing permissions and
|
||
# limitations under the License.
|
||
"""Testing suite for the PyTorch Mistral4 model."""
|
||
|
||
import gc
|
||
import unittest
|
||
|
||
import pytest
|
||
|
||
from transformers import AutoTokenizer, Mistral3ForConditionalGeneration, is_torch_available
|
||
from transformers.testing_utils import (
|
||
Expectations,
|
||
backend_empty_cache,
|
||
cleanup,
|
||
require_deterministic_for_xpu,
|
||
require_flash_attn,
|
||
require_torch,
|
||
require_torch_accelerator,
|
||
slow,
|
||
torch_device,
|
||
)
|
||
|
||
|
||
if is_torch_available():
|
||
import torch
|
||
|
||
from transformers import (
|
||
Mistral4Model,
|
||
)
|
||
|
||
|
||
from ...causal_lm_tester import CausalLMModelTest, CausalLMModelTester
|
||
|
||
|
||
class Mistral4ModelTester(CausalLMModelTester):
|
||
if is_torch_available():
|
||
base_model_class = Mistral4Model
|
||
|
||
|
||
@require_torch
|
||
@unittest.skip("Causing a lot of failures on CI")
|
||
class Mistral4ModelTest(CausalLMModelTest, unittest.TestCase):
|
||
_is_stateful = True
|
||
model_split_percents = [0.5, 0.6]
|
||
model_tester_class = Mistral4ModelTester
|
||
|
||
# TODO (ydshieh): Check this. See https://app.circleci.com/pipelines/github/huggingface/transformers/79245/workflows/9490ef58-79c2-410d-8f51-e3495156cf9c/jobs/1012146
|
||
def is_pipeline_test_to_skip(
|
||
self,
|
||
pipeline_test_case_name,
|
||
config_class,
|
||
model_architecture,
|
||
tokenizer_name,
|
||
image_processor_name,
|
||
feature_extractor_name,
|
||
processor_name,
|
||
):
|
||
return True
|
||
|
||
@require_flash_attn
|
||
@require_torch_accelerator
|
||
@pytest.mark.flash_attn_test
|
||
@slow
|
||
def test_flash_attn_2_inference_equivalence_right_padding(self):
|
||
self.skipTest(reason="Mistral4 flash attention does not support right padding")
|
||
|
||
|
||
@require_torch
|
||
class Mistral4IntegrationTest(unittest.TestCase):
|
||
def tearDown(self):
|
||
cleanup(torch_device, gc_collect=True)
|
||
|
||
@slow
|
||
def test_mistral_small_4_logits(self):
|
||
input_ids = [1, 306, 4658, 278, 6593, 310, 2834, 338]
|
||
model = Mistral3ForConditionalGeneration.from_pretrained(
|
||
"mistralai/Mistral-Small-4-119B-2603", device_map="auto"
|
||
)
|
||
input_ids = torch.tensor([input_ids]).to(model.device)
|
||
with torch.no_grad():
|
||
out = model(input_ids).logits.float().cpu()
|
||
# Expected mean on dim = -1
|
||
# fmt: off
|
||
EXPECTED_MEANS = Expectations(
|
||
{
|
||
("cuda", None): torch.tensor([[0.1793, -1.0928, -3.9925, -2.8699, -0.1250, -1.6851, -2.5565, -1.2263]]),
|
||
}
|
||
)
|
||
# fmt: on
|
||
EXPECTED_MEAN = EXPECTED_MEANS.get_expectation()
|
||
torch.testing.assert_close(out.mean(-1), EXPECTED_MEAN, rtol=1e-2, atol=1e-2)
|
||
|
||
del model
|
||
backend_empty_cache(torch_device)
|
||
gc.collect()
|
||
|
||
@slow
|
||
@require_deterministic_for_xpu
|
||
def test_mistral_small_4_generation(self):
|
||
# fmt: off
|
||
EXPECTED_TEXTS = Expectations(
|
||
{
|
||
("cuda", None): "My favourite condiment is 1000 island dressing. I love it on burgers and hot dogs. I also like",
|
||
# ("xpu", None): "My favourite condiment is iced tea. I love the way it makes me feel. It’s like a little bubble bath for",
|
||
}
|
||
)
|
||
# fmt: on
|
||
EXPECTED_TEXT = EXPECTED_TEXTS.get_expectation()
|
||
prompt = "My favourite condiment is "
|
||
tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-Small-4-119B-2603")
|
||
model = Mistral3ForConditionalGeneration.from_pretrained(
|
||
"mistralai/Mistral-Small-4-119B-2603", device_map="auto"
|
||
)
|
||
input_ids = tokenizer.encode(prompt, return_tensors="pt").to(model.device)
|
||
|
||
# greedy generation outputs
|
||
generated_ids = model.generate(input_ids, max_new_tokens=20, temperature=0)
|
||
text = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
|
||
self.assertEqual(text, EXPECTED_TEXT)
|
||
|
||
del model
|
||
backend_empty_cache(torch_device)
|
||
gc.collect()
|