Skip to content
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

Add NPU test case #2251

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ testpaths = test/
python_paths = ./
markers =
gpu_test: marks cuda tests
npu_test: marks ascend npu tests
33 changes: 33 additions & 0 deletions test/torchtext_unittest/models/npu_tests/models_npu_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import importlib
import unittest

import pytest
import torch
from torchtext_unittest.common.torchtext_test_case import TorchtextTestCase
from torchtext_unittest.models.roberta_models_test_impl import RobertaBaseTestModels
from torchtext_unittest.models.t5_models_test_impl import T5BaseTestModels


def is_npu_available(check_device=False):
"Checks if `torch_npu` is installed and potentially if a NPU is in the environment"
if importlib.util.find_spec("torch") is None or importlib.util.find_spec("torch_npu") is None:
return False

import torch
import torch_npu # noqa: F401

if check_device:
try:
# Will raise a RuntimeError if no NPU is found
_ = torch.npu.device_count()
return torch.npu.is_available()
except RuntimeError:
return False
return hasattr(torch, "npu") and torch.npu.is_available()


@pytest.mark.npu_test
@unittest.skipIf(not is_npu_available(), reason="Ascend NPU is not available")
class TestModels32NPU(RobertaBaseTestModels, T5BaseTestModels, TorchtextTestCase):
dtype = torch.float32
device = torch.device("npu")