MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / pytorch_after

Function pytorch_after

monai/utils/module.py:595–642  ·  view source on GitHub ↗

Compute whether the current pytorch version is after or equal to the specified version. The current system pytorch version is determined by `torch.__version__` or via system environment variable `PYTORCH_VER`. Args: major: major version number to be compared with mi

(major: int, minor: int, patch: int = 0, current_ver_string: str | None = None)

Source from the content-addressed store, hash-verified

593
594@functools.lru_cache(None)
595def pytorch_after(major: int, minor: int, patch: int = 0, current_ver_string: str | None = None) -> bool:
596 """
597 Compute whether the current pytorch version is after or equal to the specified version.
598 The current system pytorch version is determined by `torch.__version__` or
599 via system environment variable `PYTORCH_VER`.
600
601 Args:
602 major: major version number to be compared with
603 minor: minor version number to be compared with
604 patch: patch version number to be compared with
605 current_ver_string: if None, `torch.__version__` will be used.
606
607 Returns:
608 True if the current pytorch version is greater than or equal to the specified version.
609 """
610
611 try:
612 if current_ver_string is None:
613 _env_var = os.environ.get("PYTORCH_VER", "")
614 current_ver_string = _env_var if _env_var else torch.__version__
615 ver, has_ver = optional_import("packaging.version", name="parse")
616 if has_ver:
617 return ver(".".join((f"{major}", f"{minor}", f"{patch}"))) <= ver(f"{current_ver_string}") # type: ignore
618 parts = f"{current_ver_string}".split("+", 1)[0].split(".", 3)
619 while len(parts) < 3:
620 parts += ["0"]
621 c_major, c_minor, c_patch = parts[:3]
622 except (AttributeError, ValueError, TypeError):
623 c_major, c_minor = get_torch_version_tuple()
624 c_patch = "0"
625 c_mn = int(c_major), int(c_minor)
626 mn = int(major), int(minor)
627 if c_mn != mn:
628 return c_mn > mn
629 is_prerelease = ("a" in f"{c_patch}".lower()) or ("rc" in f"{c_patch}".lower())
630 c_p = 0
631 try:
632 p_reg = re.search(r"\d+", f"{c_patch}")
633 if p_reg:
634 c_p = int(p_reg.group())
635 except (AttributeError, TypeError, ValueError):
636 is_prerelease = True
637 patch = int(patch)
638 if c_p != patch:
639 return c_p > patch
640 if is_prerelease:
641 return False
642 return True
643
644
645@functools.lru_cache(None)

Callers 3

__init__Method · 0.90
__init__Method · 0.90
test_compareMethod · 0.90

Calls 4

optional_importFunction · 0.85
get_torch_version_tupleFunction · 0.85
getMethod · 0.80
splitMethod · 0.80

Tested by 3

__init__Method · 0.72
__init__Method · 0.72
test_compareMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…