Skip to content

[BE] detect CXX pytree requirement with TorchVersion #151102

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

Closed
wants to merge 10 commits into from

Conversation

[ghstack-poisoned]
Copy link

pytorch-bot bot commented Apr 11, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/151102

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 9f93511 with merge base 9e50c21 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
XuehaiPan added a commit to XuehaiPan/pytorch that referenced this pull request Apr 11, 2025
@zou3519
Copy link
Contributor

zou3519 commented Apr 14, 2025

sorry please dont' land this yet it might merge conflict with #151257

[ghstack-poisoned]
@@ -170,16 +172,16 @@ class _SerializeNodeDef(NamedTuple):
# NB: we try really hard to not import _cxx_pytree (which depends on optree)
# as much as possible. This is for isolation: a user who is not using C++ pytree
# shouldn't pay for it, and it helps makes things like cpython upgrades easier.
_optree_minimum_version = _TorchVersion("0.13.0")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NB: TorchVersion is a subclass of str while the rich comparison is semantic version comparison rather than alphabetic dictionary order.

[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
XuehaiPan added a commit to XuehaiPan/pytorch that referenced this pull request Apr 15, 2025
[ghstack-poisoned]
XuehaiPan added a commit to XuehaiPan/pytorch that referenced this pull request Apr 23, 2025
[ghstack-poisoned]
XuehaiPan added a commit to XuehaiPan/pytorch that referenced this pull request Apr 26, 2025
Comment on lines +91 to +92
# In-tree installation may have VCS-based versioning. Update the previous static version.
python_pytree._optree_version = _TorchVersion(optree.__version__) # type: ignore[attr-defined]
Copy link
Contributor

@zou3519 zou3519 Apr 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mentioned this elsewhere but from the naming it sounds like _TorchVersion is for torch versions, not for third-party library versions. Should we do something about that?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it sounds like _TorchVersion is for torch versions, not for third-party library versions

No. TorchVersion is widely used in the codebase for version comparison, such as CUDA/ROCm version, and triton version.

pytorch/torch/__init__.py

Lines 2305 to 2309 in 3719997

cuda_version = None
if hasattr(torch, "version"):
from torch.torch_version import TorchVersion
cuda_version = TorchVersion(getattr(torch.version, "cuda", "0.0"))

# based on torch/utils/cpp_extension.py
def get_cuda_version():
from torch.torch_version import TorchVersion
from torch.utils import cpp_extension
CUDA_HOME = cpp_extension._find_cuda_home()
if not CUDA_HOME:
raise VerifyDynamoError(cpp_extension.CUDA_NOT_FOUND_MESSAGE)
nvcc = os.path.join(CUDA_HOME, "bin", "nvcc")
cuda_version_str = (
subprocess.check_output([nvcc, "--version"])
.strip()
.decode(*cpp_extension.SUBPROCESS_DECODE_ARGS)
)
cuda_version = re.search(r"release (\d+[.]\d+)", cuda_version_str)
if cuda_version is None:
raise VerifyDynamoError("CUDA version not found in `nvcc --version` output")
cuda_str_version = cuda_version.group(1)
return TorchVersion(cuda_str_version)

def get_rocm_version():
from torch.torch_version import TorchVersion
from torch.utils import cpp_extension
ROCM_HOME = cpp_extension._find_rocm_home()
if not ROCM_HOME:
raise VerifyDynamoError(
"ROCM was not found on the system, please set ROCM_HOME environment variable"
)
hipcc = os.path.join(ROCM_HOME, "bin", "hipcc")
hip_version_str = (
subprocess.check_output([hipcc, "--version"])
.strip()
.decode(*cpp_extension.SUBPROCESS_DECODE_ARGS)
)
hip_version = re.search(r"HIP version: (\d+[.]\d+)", hip_version_str)
if hip_version is None:
raise VerifyDynamoError("HIP version not found in `hipcc --version` output")
hip_str_version = hip_version.group(1)
return TorchVersion(hip_str_version)

try:
import triton
triton_version = TorchVersion(triton.__version__)
has_triton = True
except ImportError:
triton_version = TorchVersion("0.0.0")
has_triton = False

def _is_intel_compiler(cpp_compiler: str) -> bool:
def _check_minimal_version(compiler_version: TorchVersion) -> None:
"""
On Windows: early version icx has `-print-file-name` issue, and can't preload correctly for inductor.
"""
min_version = "2024.2.1" if _IS_WINDOWS else "0.0.0"
if compiler_version < TorchVersion(min_version):
raise RuntimeError(
f"Intel Compiler error: less than minimal version {min_version}."
)

Also, torch.torch_version.TorchVersion is an exported API:

__all__ = ["TorchVersion"]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough, thanks for pointing that out

Copy link
Contributor

@zou3519 zou3519 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good, thank you

@zou3519 zou3519 requested a review from angelayi April 30, 2025 16:14
@XuehaiPan
Copy link
Collaborator Author

@pytorchbot merge

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label May 1, 2025
@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

XuehaiPan added a commit to XuehaiPan/pytorch that referenced this pull request May 1, 2025
@github-actions github-actions bot deleted the gh/XuehaiPan/270/head branch June 14, 2025 02:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
better-engineering Relatively self-contained tasks for better engineering contributors ciflow/trunk Trigger trunk jobs on your pull request Merged open source topic: not user facing topic category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants