-
Notifications
You must be signed in to change notification settings - Fork 444
[Python] Reduce latency of NIXL import #1895
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2375954
Test commit
ovidiusm e25376f
Make imports faster
ovidiusm 55b662d
Add missing file
ovidiusm b1d5807
Fix static analysis
ovidiusm 39ef188
Merge remote-tracking branch 'dynamo/main' into faster-import
ovidiusm 9f85e65
Fix static analysis
ovidiusm ab7074d
Rename utils file
ovidiusm e653be7
Remove empty line
ovidiusm 6aa7c3f
Rename found/version
ovidiusm ea85871
Simplify helper function
ovidiusm 5b9f7cb
Merge remote-tracking branch 'dynamo/main' into faster-import
ovidiusm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,97 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Shared helpers for the NIXL meta packages (``nixl`` and ``nixl_ep``).""" | ||
|
|
||
| import importlib.util | ||
| import pathlib | ||
| import sys | ||
|
|
||
|
|
||
| def detect_cuda_major() -> int | None: | ||
|
iyastreb marked this conversation as resolved.
|
||
| """CUDA major version used to select the nixl_cuXX backend wheel. | ||
|
|
||
| Read from whichever CUDA stack the process has already imported, so that | ||
| importing a NIXL meta package does not itself pull in torch (a ~1s cost). | ||
| If nothing CUDA is loaded yet, read torch's build version off disk (still | ||
| torch-free); only if that fails do we import torch and use its official API | ||
| as a last resort. | ||
| """ | ||
|
|
||
| def major(version: str | None) -> int | None: | ||
| return int(version.split(".")[0]) if version else None | ||
|
|
||
| # torch already imported: use its official API. | ||
| torch = sys.modules.get("torch") | ||
| if torch is not None: | ||
| version = major(getattr(getattr(torch, "version", None), "cuda", None)) | ||
| if version is not None: | ||
| return version | ||
|
|
||
| # cuda-python already imported: use it. | ||
| cuda_bindings = sys.modules.get("cuda.bindings") | ||
| if cuda_bindings is not None: | ||
| version = major(getattr(cuda_bindings, "__version__", None)) | ||
| if version is not None: | ||
| return version | ||
|
|
||
| # cupy already imported: use it. | ||
| cupy = sys.modules.get("cupy") | ||
| if cupy is not None: | ||
| try: | ||
| return cupy.cuda.runtime.runtimeGetVersion() // 1000 | ||
| except Exception: | ||
| pass | ||
|
|
||
| # torch installed but not imported: read build version off disk. | ||
| version = major(_torch_cuda_version_from_disk()) | ||
| if version is not None: | ||
| return version | ||
|
|
||
| # Last resort: import torch and use its official API. Slow. | ||
| try: | ||
| from torch.version import cuda as torch_cuda | ||
|
|
||
| return major(torch_cuda) | ||
| except ImportError: | ||
| return None | ||
|
|
||
|
|
||
| def _torch_cuda_version_from_disk() -> str | None: | ||
| """Return torch's build CUDA version (e.g. "12.6") without full torch import. | ||
|
|
||
| ``find_spec`` locates the package without running its ``__init__``, and the | ||
| standalone module name keeps importlib from importing the torch package; we | ||
| then exec only the tiny ``version.py`` to read its ``cuda`` attribute. | ||
| """ | ||
| try: | ||
| spec = importlib.util.find_spec("torch") | ||
| except (ImportError, ValueError): | ||
| return None | ||
| if spec is None or not spec.origin: | ||
| return None | ||
|
|
||
| version_py = pathlib.Path(spec.origin).parent / "version.py" | ||
| try: | ||
| version_spec = importlib.util.spec_from_file_location( | ||
| "_nixl_torch_version", version_py | ||
| ) | ||
| if version_spec is None or version_spec.loader is None: | ||
| return None | ||
| module = importlib.util.module_from_spec(version_spec) | ||
| version_spec.loader.exec_module(module) | ||
| except Exception: | ||
| return None | ||
| return getattr(module, "cuda", None) | ||
|
coderabbitai[bot] marked this conversation as resolved.
ovidiusm marked this conversation as resolved.
|
||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.