Repository: huggingface/transformers
Pull request: Add AMD test expectations to DETR model

# What does this PR do?

This model adds the correct test expectations for AMD hardware to the DETR model.


## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [ ] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#create-a-pull-request),
      Pull Request section?
- [ ] Was this discussed/approved via a Github issue or the [forum](https://discuss.huggingface.co/)? Please add a link
      to it if that's the case.
- [ ] Did you make sure to update the documentation with your changes? Here are the
      [documentation guidelines](https://github.com/huggingface/transformers/tree/main/docs), and
      [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/main/docs#writing-source-documentation).
- [ ] Did you write any new necessary tests?


## Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

<!-- Your PR will be replied to more quickly if you can figure out the right person to tag with @

 If you know how to use git blame, that is the easiest way, otherwise, here is a rough guide of **who to tag**.
 Please tag fewer than 3 people.

Models:

- text models: @ArthurZucker
- vision models: @amyeroberts, @qubvel
- speech models: @eustlb
- graph models: @clefourrier

Library:

- flax: @gante and @Rocketknight1
- generate: @zucchini-nlp (visual-language models) or @gante (all others)
- pipelines: @Rocketknight1
- tensorflow: @gante and @Rocketknight1
- tokenizers: @ArthurZucker
- trainer: @zach-huggingface, @SunMarc and @qgallouedec
- chat templates: @Rocketknight1

Integrations:

- deepspeed: HF Trainer/Accelerate: @SunMarc @zach-huggingface
- ray/raytune: @richardliaw, @amogkam
- Big Model Inference: @SunMarc
- quantization (bitsandbytes, autogpt): @SunMarc @MekkCyber

Documentation: @stevhliu

HF projects:

- accelerate: [different repo](https://github.com/huggingface/accelerate)
- datasets: [different repo](https://github.com/huggingface/datasets)
- diffusers: [different repo](https://github.com/huggingface/diffusers)
- rust tokenizers: [different repo](https://github.com/huggingface/tokenizers)

Maintained examples (not research project or legacy):

- Flax: @Rocketknight1
- PyTorch: See Models above and tag the person corresponding to the modality of the example.
- TensorFlow: @Rocketknight1

 -->


Changed files:
filename: tests/models/detr/test_modeling_detr.py
patch: @@ -18,7 +18,7 @@
 import unittest
 
 from transformers import DetrConfig, ResNetConfig, is_torch_available, is_vision_available
-from transformers.testing_utils import require_timm, require_torch, require_vision, slow, torch_device
+from transformers.testing_utils import Expectations, require_timm, require_torch, require_vision, slow, torch_device
 from transformers.utils import cached_property
 
 from ...test_configuration_common import ConfigTester
@@ -585,13 +585,23 @@ def test_inference_no_head(self):
 
         expected_shape = torch.Size((1, 100, 256))
         assert outputs.last_hidden_state.shape == expected_shape
-        expected_slice = torch.tensor(
-            [
-                [0.0622, -0.5142, -0.4034],
-                [-0.7628, -0.4935, -1.7153],
-                [-0.4751, -0.6386, -0.7818],
-            ]
-        ).to(torch_device)
+        expected_slices = Expectations(
+            {
+                (None, None):
+                    [
+                        [0.0622, -0.5142, -0.4034],
+                        [-0.7628, -0.4935, -1.7153],
+                        [-0.4751, -0.6386, -0.7818],
+                    ],
+                ("rocm", (9, 5)):
+                    [
+                        [ 0.0616, -0.5146, -0.4032],
+                        [-0.7629, -0.4934, -1.7153],
+                        [-0.4768, -0.6403, -0.7826],
+                    ],
+            }
+        )  # fmt: skip
+        expected_slice = torch.tensor(expected_slices.get_expectation(), device=torch_device)
         torch.testing.assert_close(outputs.last_hidden_state[0, :3, :3], expected_slice, rtol=2e-4, atol=2e-4)
 
     def test_inference_object_detection_head(self):
@@ -609,13 +619,23 @@ def test_inference_object_detection_head(self):
         # verify outputs
         expected_shape_logits = torch.Size((1, model.config.num_queries, model.config.num_labels + 1))
         self.assertEqual(outputs.logits.shape, expected_shape_logits)
-        expected_slice_logits = torch.tensor(
-            [
-                [-19.1211, -0.0881, -11.0188],
-                [-17.3641, -1.8045, -14.0229],
-                [-20.0415, -0.5833, -11.1005],
-            ]
-        ).to(torch_device)
+        expected_slices = Expectations(
+            {
+                (None, None):
+                    [
+                        [-19.1211, -0.0881, -11.0188],
+                        [-17.3641, -1.8045, -14.0229],
+                        [-20.0415, -0.5833, -11.1005],
+                    ],
+                ("rocm", (9, 5)):
+                    [
+                        [-19.1194,  -0.0893, -11.0154],
+                        [-17.3640,  -1.8035, -14.0219],
+                        [-20.0461,  -0.5837, -11.1060],
+                    ],
+            }
+        )  # fmt: skip
+        expected_slice_logits = torch.tensor(expected_slices.get_expectation(), device=torch_device)
         torch.testing.assert_close(outputs.logits[0, :3, :3], expected_slice_logits, rtol=2e-4, atol=2e-4)
 
         expected_shape_boxes = torch.Size((1, model.config.num_queries, 4))
@@ -657,27 +677,65 @@ def test_inference_panoptic_segmentation_head(self):
         # verify outputs
         expected_shape_logits = torch.Size((1, model.config.num_queries, model.config.num_labels + 1))
         self.assertEqual(outputs.logits.shape, expected_shape_logits)
-        expected_slice_logits = torch.tensor(
-            [
-                [-18.1523, -1.7592, -13.5019],
-                [-16.8866, -1.4139, -14.1025],
-                [-17.5735, -2.5090, -11.8666],
-            ]
-        ).to(torch_device)
+        expected_slices = Expectations(
+            {
+                (None, None):
+                    [
+                        [-18.1523, -1.7592, -13.5019],
+                        [-16.8866, -1.4139, -14.1025],
+                        [-17.5735, -2.5090, -11.8666],
+                    ],
+                ("rocm", (9, 5)):
+                    [
+                        [-18.1565,  -1.7568, -13.5029],
+                        [-16.8888,  -1.4138, -14.1028],
+                        [-17.5709,  -2.5080, -11.8654],
+                    ],
+            }
+        )  # fmt: skip
+        expected_slice_logits = torch.tensor(expected_slices.get_expectation(), device=torch_device)
         torch.testing.assert_close(outputs.logits[0, :3, :3], expected_slice_logits, rtol=2e-4, atol=2e-4)
 
         expected_shape_boxes = torch.Size((1, model.config.num_queries, 4))
         self.assertEqual(outputs.pred_boxes.shape, expected_shape_boxes)
-        expected_slice_boxes = torch.tensor(
-            [[0.5344, 0.1790, 0.9284], [0.4421, 0.0571, 0.0875], [0.6632, 0.6886, 0.1015]]
-        ).to(torch_device)
+        expected_slices = Expectations(
+            {
+                (None, None):
+                    [
+                        [0.5344, 0.1790, 0.9284],
+                        [0.4421, 0.0571, 0.0875],
+                        [0.6632, 0.6886, 0.1015]
+                    ],
+                ("rocm", (9, 5)):
+                    [
+                        [0.5344, 0.1789, 0.9285],
+                        [0.4420, 0.0572, 0.0875],
+                        [0.6630, 0.6887, 0.1017],
+                    ],
+            }
+        )  # fmt: skip
+        expected_slice_boxes = torch.tensor(expected_slices.get_expectation(), device=torch_device)
         torch.testing.assert_close(outputs.pred_boxes[0, :3, :3], expected_slice_boxes, rtol=2e-4, atol=2e-4)
 
         expected_shape_masks = torch.Size((1, model.config.num_queries, 200, 267))
         self.assertEqual(outputs.pred_masks.shape, expected_shape_masks)
-        expected_slice_masks = torch.tensor(
-            [[-7.8408, -11.0104, -12.1279], [-12.0299, -16.6498, -17.9806], [-14.8995, -19.9940, -20.5646]]
-        ).to(torch_device)
+        expected_slices = Expectations(
+            {
+                (None, None):
+                    [
+                        [-7.8408, -11.0104, -12.1279],
+                        [-12.0299, -16.6498, -17.9806],
+                        [-14.8995, -19.9940, -20.5646],
+                    ],
+                ("rocm", (9, 5)):
+                    [
+                        [ -7.7558, -10.8789, -11.9798],
+                        [-11.8882, -16.4330, -17.7452],
+                        [-14.7317, -19.7384, -20.3005],
+                    ],
+            }
+        )  # fmt: skip
+        expected_slice_masks = torch.tensor(expected_slices.get_expectation(), device=torch_device)
         torch.testing.assert_close(outputs.pred_masks[0, 0, :3, :3], expected_slice_masks, rtol=2e-3, atol=2e-3)
 
         # verify postprocessing
@@ -731,11 +789,21 @@ def test_inference_no_head(self):
 
         expected_shape = torch.Size((1, 100, 256))
         assert outputs.last_hidden_state.shape == expected_shape
-        expected_slice = torch.tensor(
-            [
-                [0.0622, -0.5142, -0.4034],
-                [-0.7628, -0.4935, -1.7153],
-                [-0.4751, -0.6386, -0.7818],
-            ]
-        ).to(torch_device)
+        expected_slices = Expectations(
+            {
+                (None, None):
+                    [
+                        [0.0622, -0.5142, -0.4034],
+                        [-0.7628, -0.4935, -1.7153],
+                        [-0.4751, -0.6386, -0.7818],
+                    ],
+                ("rocm", (9, 5)):
+                    [
+                        [ 0.0616, -0.5146, -0.4032],
+                        [-0.7629, -0.4934, -1.7153],
+                        [-0.4768, -0.6403, -0.7826]
+                    ],
+            }
+        )  # fmt: skip
+        expected_slice = torch.tensor(expected_slices.get_expectation(), device=torch_device)
         torch.testing.assert_close(outputs.last_hidden_state[0, :3, :3], expected_slice, rtol=1e-4, atol=1e-4)