Repository: huggingface/transformers
Pull request: Ernie4 5 vl moe

This PR refines the test file of ernie4_5_vl_moe model, mainly 2 things:
1. avoid hard cord for expected image token count
2. add padding_side="left" for bs > 1 inference

Changed files:
filename: tests/models/ernie4_5_vl_moe/test_modeling_ernie4_5_vl_moe.py
patch: @@ -25,7 +25,9 @@
     is_vision_available,
 )
 from transformers.testing_utils import (
+    Expectations,
     cleanup,
+    require_deterministic_for_xpu,
     require_torch,
     require_torch_large_accelerator,
     slow,
@@ -296,7 +298,7 @@ def _video_features_prepare_config_and_inputs(self):
 
 
 @slow
-@require_torch_large_accelerator(memory=64)  # Tested on A100
+@require_torch_large_accelerator(memory=64)  # Tested on A100 / torch 2.9.0
 @require_torch
 class Ernie4_5_VL_MoeIntegrationTest(unittest.TestCase):
     model = None
@@ -455,8 +457,8 @@ def test_small_model_integration_test_expand(self):
         output = model.generate(**inputs, max_new_tokens=30, do_sample=False, num_beams=2, num_return_sequences=2)
 
         EXPECTED_DECODED_TEXT = [
-            'The animal in the image is a lynx, not a dog. It has the distinctive features of a lynx, including a short tail',
-            'The animal in the image is a lynx, not a dog. It has the distinctive features of a lynx, such as its short'
+            'The animal in the image is a lynx, not a dog. It has the distinctive features of a lynx, such as tuft',
+            'The animal in the image is a lynx, not a dog. It has the distinctive features of a lynx, including a short tail'
         ]  # fmt: skip
 
         self.assertEqual(
@@ -489,7 +491,7 @@ def test_small_model_integration_test_batch_wo_image(self):
         output = model.generate(**inputs, max_new_tokens=30)
 
         EXPECTED_DECODED_TEXT = [
-            "The animal in the image is a lynx, not a dog. It's a wild cat species known for its distinctive ear tufts and",
+            "The animal in the image is a lynx. It's a medium-sized wild cat characterized by its distinctive facial ruff, short tail",
             "I am an AI assistant designed to help answer questions, provide information, and assist with tasks. I don't have personal experiences or a physical form"
         ]  # fmt: skip
 
@@ -629,10 +631,21 @@ def test_small_model_integration_test_batch(self):
         # it should not matter whether two images are the same size or not
         output = model.generate(**inputs, max_new_tokens=30)
 
-        EXPECTED_DECODED_TEXT = [
-            '知道了知道了attaatta不如不如不如不如不如不如不如不如不如不如不如不如不如不如不如不如不如不如不如不如不如不如不如不如不如不如',
-            '不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊',
-        ]  # fmt: skip
+        # fmt: off
+        expectations = Expectations(
+            {
+                ("xpu", None): [
+                    '知道了知道了attaatta不如不如不如不如不如不如不如不如不如不如不如不如不如不如不如不如不如不如不如不如不如不如不如不如不如不如',
+                    '填空填空填空填空填空填空填空填空填空填空填空填空填空填空填空填空填空填空填空填空填空填空填空填空填空填空填空填空填空填空',
+                ],
+                (None, None): [
+                    '知道了知道了attaatta不如不如不如不如不如不如不如不如不如不如不如不如不如不如不如不如不如不如不如不如不如不如不如不如不如不如',
+                    '不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊不是啊',
+                ],
+            }
+        )
+        EXPECTED_DECODED_TEXT = expectations.get_expectation()
+        # fmt: on
 
         self.assertEqual(
             [
@@ -677,6 +690,7 @@ def test_small_model_integration_test_with_video(self):
             EXPECTED_DECODED_TEXT,
         )
 
+    @require_deterministic_for_xpu
     def test_small_model_integration_test_expand(self):
         model = self.load_model("auto")
         inputs = self.processor.apply_chat_template(

filename: tests/models/ernie4_5_vl_moe/test_processing_ernie4_5_vl_moe.py
patch: @@ -247,7 +247,11 @@ def _test_apply_chat_template(
                 expected_video_token_count += thw[0] * thw[1] * thw[2]
             mm_len = expected_video_token_count
         else:
-            mm_len = batch_size * 192
+            # Calculate expected image token count based on image_grid_thw
+            expected_image_token_count = 0
+            for thw in out_dict["image_grid_thw"]:
+                expected_image_token_count += thw[0] * thw[1] * thw[2]
+            mm_len = expected_image_token_count
         self.assertEqual(len(out_dict[input_name]), mm_len)
 
         return_tensor_to_type = {"pt": torch.Tensor, "np": np.ndarray, None: list}