Repository: huggingface/transformers
Pull request: fix some ut failures on XPU w/ torch 2.9

@ydshieh , pls help review, thx very much.

Changed files:
filename: tests/models/grounding_dino/test_modeling_grounding_dino.py
patch: @@ -682,7 +682,7 @@ def test_inference_object_detection_head(self):
 
         expectations = Expectations(
             {
-                (None, None): [[0.4526, 0.4082]],
+                (None, None): [0.4526, 0.4082],
                 ("cuda", 8): [0.4524, 0.4074],
             }
         )

filename: tests/models/internvl/test_modeling_internvl.py
patch: @@ -227,6 +227,7 @@ def setUp(self):
     def tearDown(self):
         cleanup(torch_device, gc_collect=True)
 
+    @require_deterministic_for_xpu
     def test_qwen2_small_model_integration_generate(self):
         processor = AutoProcessor.from_pretrained(self.small_model_checkpoint)
         model = InternVLForConditionalGeneration.from_pretrained(
@@ -244,7 +245,16 @@ def test_qwen2_small_model_integration_generate(self):
             decoded_output = processor.decode(
                 generate_ids[0, inputs["input_ids"].shape[1] :], skip_special_tokens=True
             )
-        expected_output = "The image shows two cats lying on a pink surface, which appears to be a bed or couch."
+
+        # fmt: off
+        expected_outputs = Expectations(
+            {
+                (None, None): "The image shows two cats lying on a pink surface, which appears to be a bed or couch.",
+                ("xpu", 3): "The image shows two cats lying on a pink blanket. The cat on the left is a tabby",
+            }
+        )
+        # fmt: on
+        expected_output = expected_outputs.get_expectation()
 
         self.assertEqual(decoded_output, expected_output)
 
@@ -268,9 +278,9 @@ def test_qwen2_small_model_integration_forward(self):
         actual_logits = output.logits[0, -1, :5].cpu()
         expected_logits_all = Expectations(
             {
-                ("xpu", 3): torch.tensor([11.7500, 14.7500, 14.1250, 10.5625, 6.7812], dtype=torch.float16),
-                ("cuda", 7): torch.tensor([11.9531, 14.7031, 14.2734, 10.6562,  6.9219], dtype=torch.float16),
-                ("cuda", 8): torch.tensor([11.9609, 14.7188, 14.2734, 10.6484,  6.9141], dtype=torch.float16),
+                ("xpu", 3): torch.tensor([11.9922, 14.7188, 14.3125, 10.6719, 6.9297], dtype=torch.float16),
+                ("cuda", 7): torch.tensor([11.9531, 14.7031, 14.2734, 10.6562, 6.9219], dtype=torch.float16),
+                ("cuda", 8): torch.tensor([11.9609, 14.7188, 14.2734, 10.6484, 6.9141], dtype=torch.float16),
             }
         )  # fmt: skip
         expected_logits = expected_logits_all.get_expectation()
@@ -298,7 +308,7 @@ def test_qwen2_small_model_integration_generate_text_only(self):
 
         expected_outputs = Expectations(
             {
-                ("xpu", 3): "Whispers of dawn,\nSilent whispers of the night,\nNew day's light.",
+                ("xpu", 3): "Whispers of dawn,\nSilent whispers of night,\nPeace in the stillness.",
                 ("cuda", 7): 'Whispers of dawn,\nSilent whispers of night,\nPeace in the stillness.',
                 ("cuda", 8): 'Whispers of dawn,\nSilent whispers of night,\nPeace in the stillness.',
             }
@@ -570,7 +580,7 @@ def test_qwen2_small_model_integration_interleaved_images_videos(self):
         decoded_output = processor.decode(output[1], skip_special_tokens=True)
         expected_outputs = Expectations(
             {
-                ("xpu", 3): "user\nFrame1: \nFrame2: \nFrame3: \nFrame4: \nFrame5: \nFrame6: \nFrame7: \nFrame8: \nWhat type of shot is the man performing?\nassistant\nThe man is performing a forehand shot.",
+                ("xpu", 3): "user\nFrame1: \nFrame2: \nFrame3: \nFrame4: \nFrame5: \nFrame6: \nFrame7: \nFrame8: \nWhat type of shot is the man performing?\nassistant\nA forehand shot",
                 ("cuda", 7): 'user\nFrame1: \nFrame2: \nFrame3: \nFrame4: \nFrame5: \nFrame6: \nFrame7: \nFrame8: \nWhat type of shot is the man performing?\nassistant\nA forehand shot',
             }
         )  # fmt: skip

filename: tests/test_processing_common.py
patch: @@ -37,8 +37,9 @@
 from transformers.utils import is_torch_available, is_vision_available
 
 
-sys.path.append(".")
-from utils.fetch_hub_objects_for_ci import url_to_local_path
+parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
+sys.path.append(os.path.join(parent_dir, "utils"))
+from fetch_hub_objects_for_ci import url_to_local_path  # noqa: E402
 
 
 global_rng = random.Random()