Skip to content

Commit 275b203

Browse files
authored
Enable testing for NSPY_NCU_INIT_AT_IMPORT=0 mode: The test was doing a CUDA initialization at the top level, hence CUDA was getting initialized during pytest's test collection phase. This was causing nsight.analyze.kernel's lazy loading of the NCU injection to fail. MR updates the test to make the torch.cuda.init() call inside main. (#67)
Signed-off-by: Alok Joshi <aljoshi@nvidia.com>
1 parent f56f372 commit 275b203

1 file changed

Lines changed: 13 additions & 10 deletions

File tree

‎tests/test_cuda_init_before_nsight.py‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,23 @@
1111
import sys
1212
from pathlib import Path
1313

14-
import pytest
1514
import torch
1615

17-
torch.cuda.init() # There can be any other torch call that initializes CUDA before nsight is imported
1816

19-
import nsight
17+
def main() -> None:
2018

19+
torch.cuda.init() # any torch call that initializes CUDA would do
20+
import nsight # this loads NCU, and the test needs it loaded after CUDA init
2121

22-
@nsight.analyze.kernel()
23-
def run_simple_kernel(n: int) -> None:
24-
a = torch.randn(n, n, device="cuda")
25-
b = torch.randn(n, n, device="cuda")
26-
with nsight.annotate("test"):
27-
_ = a @ b
22+
@nsight.analyze.kernel()
23+
def run_simple_kernel(n: int) -> None:
24+
a = torch.randn(n, n, device="cuda")
25+
b = torch.randn(n, n, device="cuda")
26+
with nsight.annotate("test"):
27+
_ = a @ b
28+
29+
# under NSPY_NCU_INIT_AT_IMPORT=0 the load is deferred to here instead, still after CUDA init
30+
run_simple_kernel(64)
2831

2932

3033
def test() -> None:
@@ -53,4 +56,4 @@ def test() -> None:
5356

5457

5558
if __name__ == "__main__":
56-
run_simple_kernel(64)
59+
main()

0 commit comments

Comments
 (0)