[Kineto] Initialize libkineto profilers during torch init process during pybind set-up (#112623)

Summary:
We are planning to lazily initialize CUPTI when profiling is actually performed. Therefore, we need to remove profiler init dependency on CUPTI Callbacks' RESOURCE_CONTEXT_CREATED.

Instead, we can initialize the profilers during torch profiler pybind, ie. THPAutograd_initExtension() and lazily in profilerStep().

Test Plan:
CI and ran internally, see internal diff logs.

Differential Revision: D50894961

Pulled By: aaronenyeshi

Pull Request resolved: https://github.com/pytorch/pytorch/pull/112623
Approved by: https://github.com/albanD
This commit is contained in:
Aaron Enye Shi
2023-11-10 20:50:51 +00:00
committed by PyTorch MergeBot
parent 6b38836c73
commit a62a88bb84
3 changed files with 44 additions and 5 deletions

View File

@ -18,7 +18,7 @@ import torch
from torch.testing import make_tensor
from torch.testing._internal.common_utils import \
(IS_FBCODE, IS_JETSON, IS_MACOS, IS_SANDCASTLE, IS_WINDOWS, TestCase, run_tests, slowTest,
(IS_FBCODE, IS_JETSON, IS_LINUX, IS_MACOS, IS_SANDCASTLE, IS_WINDOWS, TestCase, run_tests, slowTest,
parametrize, subtest, instantiate_parametrized_tests, dtype_name, TEST_WITH_ROCM, decorateIf)
from torch.testing._internal.common_device_type import \
(PYTORCH_TESTING_DEVICE_EXCEPT_FOR_KEY, PYTORCH_TESTING_DEVICE_ONLY_FOR_KEY, dtypes,
@ -2291,6 +2291,14 @@ class TestImports(TestCase):
out = self._check_python_output("; ".join(commands))
self.assertEqual(out.strip(), expected)
@unittest.skipUnless(IS_LINUX, "Initialize early for on-demand profiling")
def test_libkineto_profiler_is_initialized(self) -> None:
# Check that the profiler is initialized at import time.
out = self._check_python_output("""import sys; import torch;
print(torch._C._autograd._isProfilerInitialized() if torch._C._autograd._is_use_kineto_defined() else 'True')
""")
self.assertEqual(out.strip(), "True")
class TestOpInfos(TestCase):
def test_sample_input(self) -> None:
a, b, c, d, e = (object() for _ in range(5))