Skip to content

enable misc test cases on XPU #38852

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 18, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/transformers/training_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ class TrainingArguments:
installation](https://github.com/intel/intel-extension-for-pytorch).
bf16 (`bool`, *optional*, defaults to `False`):
Whether to use bf16 16-bit (mixed) precision training instead of 32-bit training. Requires Ampere or higher
NVIDIA architecture or using CPU (use_cpu) or Ascend NPU. This is an experimental API and it may change.
NVIDIA architecture or Intel XPU or using CPU (use_cpu) or Ascend NPU. This is an experimental API and it may change.
fp16 (`bool`, *optional*, defaults to `False`):
Whether to use fp16 16-bit (mixed) precision training instead of 32-bit training.
fp16_opt_level (`str`, *optional*, defaults to 'O1'):
Expand Down
2 changes: 1 addition & 1 deletion tests/generation/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4892,7 +4892,7 @@ def test_cache_device_map_with_vision_layer_device_map(self):
# If the generate doesn't infer the DECODER device map correctly, this will fail
_ = model.generate(**inputs, max_new_tokens=2, do_sample=False)

@require_torch_gpu
@require_torch_accelerator
def test_cpu_offload_doesnt_compile(self):
"""Test that CPU offload doesn't trigger compilation"""
tokenizer = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-MistralForCausalLM")
Expand Down
4 changes: 2 additions & 2 deletions tests/models/bamba/test_modeling_bamba.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ def test_simple_generate(self):
(
"xpu",
3,
): "<|begin_of_text|>Hey how are you doing on this lovely evening? I hope you are all doing well. Today I",
): "<|begin_of_text|>Hey how are you doing on this lovely evening? I hope you are all doing well. I am",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tweak ground truth on stock PyTorch

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't mind, could you change

                (
                    "cuda",
                    8,
                )

to

                ("cuda", 8)

etc.

no big deal though 🙏

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, could you share what is stock PyTorch? You probably mentioned once but I can't remember.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, sorry for confusing on vocabulary. stock PyTorch in our vocabulary means "PyTorch released by Meta w/o any extensions or modification", it's what users can get by pip install torch --index-url xxx

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't mind, could you change

                (
                    "cuda",
                    8,
                )

to

                ("cuda", 8)

etc.

no big deal though 🙏

done, thx.

}
)

Expand Down Expand Up @@ -671,7 +671,7 @@ def test_simple_batched_generate_with_padding(self):
"!!!<|begin_of_text|>I am late! I need to be at the airport in 20 minutes! I",
],
("xpu", 3): [
"<|begin_of_text|>Hey how are you doing on this lovely evening? I hope you are all doing well. Today I",
"<|begin_of_text|>Hey how are you doing on this lovely evening? I hope you are all doing well. I am",
"!!!<|begin_of_text|>I am late! I need to get to work! I have to get to the",
],
}
Expand Down
12 changes: 9 additions & 3 deletions tests/models/d_fine/test_modeling_d_fine.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@
is_torch_available,
is_vision_available,
)
from transformers.testing_utils import require_torch, require_torch_gpu, require_vision, slow, torch_device
from transformers.testing_utils import (
require_torch,
require_torch_accelerator,
require_vision,
slow,
torch_device,
)
from transformers.utils import cached_property


Expand Down Expand Up @@ -680,7 +686,7 @@ def test_initialization(self):
self.assertTrue(not failed_cases, message)

@parameterized.expand(["float32", "float16", "bfloat16"])
@require_torch_gpu
@require_torch_accelerator
@slow
def test_inference_with_different_dtypes(self, torch_dtype_str):
torch_dtype = {
Expand All @@ -702,7 +708,7 @@ def test_inference_with_different_dtypes(self, torch_dtype_str):
_ = model(**self._prepare_for_class(inputs_dict, model_class))

@parameterized.expand(["float32", "float16", "bfloat16"])
@require_torch_gpu
@require_torch_accelerator
@slow
def test_inference_equivalence_for_static_and_dynamic_anchors(self, torch_dtype_str):
torch_dtype = {
Expand Down
4 changes: 2 additions & 2 deletions tests/models/glm4/test_modeling_glm4.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def test_model_9b_bf16(self):
{
("xpu", 3): [
"Hello I am doing a project on the history of the internet and I need to know what the first website was and what",
"Hi today I am going to tell you about the most common disease in the world. This disease is called diabetes",
"Hi today I am going to tell you about the most common mistakes that people make when they are learning English.",
],
("cuda", 7): [],
("cuda", 8): [
Expand Down Expand Up @@ -177,7 +177,7 @@ def test_model_9b_sdpa(self):
{
("xpu", 3): [
"Hello I am doing a project on the history of the internet and I need to know what the first website was and what",
"Hi today I am going to tell you about the most common disease in the world. This disease is called diabetes",
"Hi today I am going to tell you about the most common mistakes that people make when they are learning English.",
],
("cuda", 7): [],
("cuda", 8): [
Expand Down
6 changes: 3 additions & 3 deletions tests/models/musicgen/test_modeling_musicgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,7 @@ def test_flash_attn_2_conversion(self):
self.skipTest(reason="Musicgen doesn't use the MusicgenFlashAttention2 class method.")

@require_torch_sdpa
@require_torch_gpu
@require_torch_accelerator
@slow
def test_sdpa_can_dispatch_on_flash(self):
if not self.has_attentions:
Expand All @@ -1046,8 +1046,8 @@ def test_sdpa_can_dispatch_on_flash(self):
self.skipTest(reason="This test requires an NVIDIA GPU with compute capability >= 8.0")
elif device_type == "rocm" and major < 9:
self.skipTest(reason="This test requires an AMD GPU with compute capability >= 9.0")
else:
self.skipTest(reason="This test requires a Nvidia or AMD GPU")
elif device_type not in ["cuda", "rocm", "xpu"]:
self.skipTest(reason="This test requires a Nvidia or AMD GPU or an Intel XPU")

torch.compiler.reset()

Expand Down
6 changes: 3 additions & 3 deletions tests/models/musicgen_melody/test_modeling_musicgen_melody.py
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,7 @@ def test_flash_attn_2_conversion(self):
self.skipTest(reason="MusicgenMelody doesn't use the MusicgenMelodyFlashAttention2 class method.")

@require_torch_sdpa
@require_torch_gpu
@require_torch_accelerator
@slow
def test_sdpa_can_dispatch_on_flash(self):
if not self.has_attentions:
Expand All @@ -1046,8 +1046,8 @@ def test_sdpa_can_dispatch_on_flash(self):
self.skipTest(reason="This test requires an NVIDIA GPU with compute capability >= 8.0")
elif device_type == "rocm" and major < 9:
self.skipTest(reason="This test requires an AMD GPU with compute capability >= 9.0")
else:
self.skipTest(reason="This test requires a Nvidia or AMD GPU")
elif device_type not in ["cuda", "rocm", "xpu"]:
self.skipTest(reason="This test requires a Nvidia or AMD GPU or an Intel XPU")

torch.compiler.reset()

Expand Down
11 changes: 9 additions & 2 deletions tests/models/vitmatte/test_image_processing_vitmatte.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@
import requests
from packaging import version

from transformers.testing_utils import is_flaky, require_torch, require_torch_gpu, require_vision, slow, torch_device
from transformers.testing_utils import (
is_flaky,
require_torch,
require_torch_accelerator,
require_vision,
slow,
torch_device,
)
from transformers.utils import is_torch_available, is_torchvision_available, is_vision_available

from ...test_image_processing_common import ImageProcessingTestMixin, prepare_image_inputs
Expand Down Expand Up @@ -337,7 +344,7 @@ def test_slow_fast_equivalence_batched(self):
)

@slow
@require_torch_gpu
@require_torch_accelerator
@require_vision
def test_can_compile_fast_image_processor(self):
# override as trimaps are needed for the image processor
Expand Down
5 changes: 3 additions & 2 deletions tests/trainer/test_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1623,9 +1623,10 @@ def is_any_loss_nan_or_inf(log_history):
self.assertFalse(is_any_loss_nan_or_inf(log_history_filter))

def test_train_and_eval_dataloaders(self):
if torch_device in ["cuda", "xpu"]:
if torch_device in ["cuda"]:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am ok, but since you added that before, it was a mistake back then?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it was a mistake by me. In that time I just tested in a single card, so may missed. In my recent test in multi-card env, i find it fail and debug to the DP issue... That's why i put a comment in else statement, to remind me and other xpu guys to avoid later mistakes in case we forgot the history, :)

n_gpu = max(1, backend_device_count(torch_device))
else:
# DP is decprecated by PyTorch, accelerators like XPU doesn't support DP
n_gpu = 1

tmp_dir = self.get_auto_remove_tmp_dir()
Expand Down Expand Up @@ -3940,7 +3941,7 @@ def test_torchdynamo_memory(self):
from torch import _dynamo as torchdynamo

class CustomTrainer(Trainer):
def compute_loss(self, model, inputs, return_outputs=False):
def compute_loss(self, model, inputs, num_items_in_batch=None, return_outputs=False):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix the similar issue as #36331

x = inputs["x"]
output = model(x)
if self.args.n_gpu == 1:
Expand Down
2 changes: 1 addition & 1 deletion tests/utils/test_cache_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ def test_static_cache_multi_accelerator(self):
_ = model(**inputs)
_ = model.generate(**inputs, max_new_tokens=2, cache_implementation="hybrid")

@require_torch_gpu
@require_torch_accelerator
@parameterized.expand(TEST_CACHE_IMPLEMENTATIONS)
def test_cache_gptj_model(self, cache_implementation):
"""Tests caches with GPT-J model. Regression test for https://github.com/huggingface/transformers/pull/34799"""
Expand Down