forked from Lightning-AI/pytorch-lightning
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Run AMP tests in their own process With opt_level="O1" (the default), AMP patches many torch functions, which breaks any tests that run afterwards. This patch introduces a pytest extension that lets tests be marked with @pytest.mark.spawn so that they are run in their own process using torch.multiprocessing.spawn so that the main python interpreter stays un-patched. Note that tests using DDP already run AMP in its own process, so they don't need this annotation. * Fix AMP tests Since AMP defaults to O1 now, DP tests no longer throw exceptions. Since AMP patches torch functions, CPU inference no longer works. Skip prediction step for AMP tests. * typo
- Loading branch information
1 parent
c32f2b9
commit 019f612
Showing
2 changed files
with
34 additions
and
24 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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import pytest | ||
|
||
import torch.multiprocessing as mp | ||
|
||
|
||
def pytest_configure(config): | ||
config.addinivalue_line("markers", "spawn: spawn test in a separate process using torch.multiprocessing.spawn") | ||
|
||
|
||
def wrap(i, fn, args): | ||
return fn(*args) | ||
|
||
|
||
@pytest.mark.tryfirst | ||
def pytest_pyfunc_call(pyfuncitem): | ||
if pyfuncitem.get_closest_marker("spawn"): | ||
testfunction = pyfuncitem.obj | ||
funcargs = pyfuncitem.funcargs | ||
testargs = tuple([funcargs[arg] for arg in pyfuncitem._fixtureinfo.argnames]) | ||
|
||
mp.spawn(wrap, (testfunction, testargs)) | ||
return True |
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