This repository has been archived by the owner on Dec 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
consolidate testing decorators (#4213)
- Loading branch information
Showing
6 changed files
with
48 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,40 @@ | ||
""" | ||
Utilities and helpers for writing tests. | ||
""" | ||
from allennlp.common.testing.test_case import AllenNlpTestCase, multi_device | ||
import torch | ||
import pytest | ||
|
||
from allennlp.common.testing.test_case import AllenNlpTestCase | ||
from allennlp.common.testing.model_test_case import ModelTestCase | ||
|
||
|
||
_available_devices = ["cpu"] + (["cuda"] if torch.cuda.is_available() else []) | ||
|
||
|
||
def multi_device(test_method): | ||
""" | ||
Decorator that provides an argument `device` of type `str` for each available PyTorch device. | ||
""" | ||
return pytest.mark.parametrize("device", _available_devices)(pytest.mark.gpu(test_method)) | ||
|
||
|
||
def requires_gpu(test_method): | ||
""" | ||
Decorator to indicate that a test requires a GPU device. | ||
""" | ||
return pytest.mark.gpu( | ||
pytest.mark.skipif(not torch.cuda.is_available(), reason="No CUDA device registered.")( | ||
test_method | ||
) | ||
) | ||
|
||
|
||
def requires_multi_gpu(test_method): | ||
""" | ||
Decorator to indicate that a test requires multiple GPU devices. | ||
""" | ||
return pytest.mark.gpu( | ||
pytest.mark.skipif(torch.cuda.device_count() < 2, reason="2 or more GPUs required.")( | ||
test_method | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters