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

Adds TRANSFORMERS_TEST_BACKEND #25655

Merged
merged 3 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 7 additions & 2 deletions docs/source/en/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -511,15 +511,20 @@ from transformers.testing_utils import get_gpu_count
n_gpu = get_gpu_count() # works with torch and tf
```

### Testing with a specific PyTorch backend
### Testing with a specific PyTorch backend or device

To run the test suite on a specific torch backend add `TRANSFORMERS_TEST_DEVICE="$device"` where `$device` is the target backend. For example, to test on CPU only:
To run the test suite on a specific torch device add `TRANSFORMERS_TEST_DEVICE="$device"` where `$device` is the target backend. For example, to test on CPU only:
```bash
TRANSFORMERS_TEST_DEVICE="cpu" pytest tests/test_logging.py
```

This variable is useful for testing custom or less common PyTorch backends such as `mps`. It can also be used to achieve the same effect as `CUDA_VISIBLE_DEVICES` by targeting specific GPUs or testing in CPU-only mode.

Certain devices will require an additional import after importing `torch` for the first time. This can be specified using the environment variable `TRANSFORMERS_TEST_BACKEND`:
```bash
TRANSFORMERS_TEST_BACKEND="torch_X" pytest tests/test_logging.py
vvvm23 marked this conversation as resolved.
Show resolved Hide resolved
```


### Distributed training

Expand Down
11 changes: 11 additions & 0 deletions src/transformers/testing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import contextlib
import doctest
import functools
import importlib
import inspect
import logging
import multiprocessing
Expand Down Expand Up @@ -629,6 +630,16 @@ def require_torch_multi_npu(test_case):
torch_device = "npu"
else:
torch_device = "cpu"

if "TRANSFORMERS_TEST_BACKEND" in os.environ:
backend = os.environ["TRANSFORMERS_TEST_BACKEND"]
try:
_ = importlib.import_module(backend)
except ModuleNotFoundError as e:
raise ModuleNotFoundError(
f"Failed to import `TRANSFORMERS_TEST_BACKEND` '{backend}'! This should be the name of an installed module."
) from e
vvvm23 marked this conversation as resolved.
Show resolved Hide resolved

else:
torch_device = None

Expand Down