Skip to content

Commit

Permalink
Add __version__ attribute to Python module (#675)
Browse files Browse the repository at this point in the history
Add __version__ attribute to Python module

Signed-off-by: Tim Moon <[email protected]>
  • Loading branch information
timmoon10 authored Feb 21, 2024
1 parent 7172509 commit fb2f952
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 28 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.5.0dev
1.5.0.dev0
35 changes: 8 additions & 27 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#
# See LICENSE for license information.

"""Installation script."""

import ctypes
from functools import lru_cache
import os
Expand All @@ -18,35 +20,11 @@
import setuptools
from setuptools.command.build_ext import build_ext

from te_version import te_version

# Project directory root
root_path: Path = Path(__file__).resolve().parent

@lru_cache(maxsize=1)
def te_version() -> str:
"""Transformer Engine version string
Includes Git commit as local version, unless suppressed with
NVTE_NO_LOCAL_VERSION environment variable.
"""
with open(root_path / "VERSION", "r") as f:
version = f.readline().strip()
if not int(os.getenv("NVTE_NO_LOCAL_VERSION", "0")):
try:
output = subprocess.run(
["git", "rev-parse" , "--short", "HEAD"],
capture_output=True,
cwd=root_path,
check=True,
universal_newlines=True,
)
except (CalledProcessError, OSError):
pass
else:
commit = output.stdout.strip()
version += f"+{commit}"
return version

@lru_cache(maxsize=1)
def with_debug_build() -> bool:
"""Whether to build with a debug configuration"""
Expand Down Expand Up @@ -266,7 +244,10 @@ def setup_requirements() -> Tuple[List[str], List[str], List[str]]:

# Common requirements
setup_reqs: List[str] = []
install_reqs: List[str] = ["pydantic"]
install_reqs: List[str] = [
"pydantic",
"importlib-metadata>=1.0; python_version<'3.8'",
]
test_reqs: List[str] = ["pytest"]

def add_unique(l: List[str], vals: Union[str, List[str]]) -> None:
Expand Down
34 changes: 34 additions & 0 deletions te_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright (c) 2022-2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# See LICENSE for license information.

"""Transformer Engine version string."""
import os
from pathlib import Path
import subprocess

def te_version() -> str:
"""Transformer Engine version string
Includes Git commit as local version, unless suppressed with
NVTE_NO_LOCAL_VERSION environment variable.
"""
root_path = Path(__file__).resolve().parent
with open(root_path / "VERSION", "r") as f:
version = f.readline().strip()
if not int(os.getenv("NVTE_NO_LOCAL_VERSION", "0")):
try:
output = subprocess.run(
["git", "rev-parse" , "--short", "HEAD"],
capture_output=True,
cwd=root_path,
check=True,
universal_newlines=True,
)
except (subprocess.CalledProcessError, OSError):
pass
else:
commit = output.stdout.strip()
version += f"+{commit}"
return version
1 change: 1 addition & 0 deletions transformer_engine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# See LICENSE for license information.

"""Top level package"""
from ._version import __version__
from . import common

try:
Expand Down
39 changes: 39 additions & 0 deletions transformer_engine/_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright (c) 2022-2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# See LICENSE for license information.

"""Version information"""
import sys
from packaging.version import Version

if sys.version_info >= (3, 8):
from importlib import metadata
else:
import importlib_metadata as metadata

def _version_str() -> str:
"""Transformer Engine version string"""

# Try getting version from package metadata
version_str = None
try:
version_str = metadata.version("transformer_engine")
except:
pass
if version_str:
return version_str

# Try getting version from Git root directory
try:
from te_version import te_version
version_str = te_version()
except:
pass
if version_str:
return version_str

# Could not deduce version
return "0.dev0+unknown"

# Transformer Engine version
__version__: Version = Version(_version_str())

0 comments on commit fb2f952

Please sign in to comment.