Commit 8393e73f authored by lishen's avatar lishen
Browse files

python调用时可以查询dcu_version

parent cb25749e
# DLIB
## 环境配置
使用DCU编译之前,需要准备编译环境。参考
[environment prepare](environment_prepare.md)
## 使用源码安装
### 编译环境准备(以dtk-23.04版本为例)
- 拉取代码
```
git clone -b develop http://developer.hpccube.com/codes/aicomponent/warpctc.git
```
-[开发者社区](https://developer.hpccube.com/tool/#sdk) DCU Toolkit 中下载 DTK-23.04 解压至 /opt/ 路径下,并建立软链接
```
cd /opt && ln -s dtk-23.04 dtk
```
- 导入环境变量以及安装必要依赖库
```shell
source /opt/dtk/env.sh
```
### 编译安装
#### 编译 Python API
- 使用python安装
```shell
cd pytorch_binding
python setup.py install
```
- 使用python编译whl包
```shell
cd pytorch_binding
python setup.py bdist_wheel
```
### 测试
- 验证warpctc的loss正确性(CPU和GPU的一致性)
```shell
cd pytorch_binding/tests
python3 test_gpu.py
```
- 验证warpctc的loss的GPU加速效果
```shell
cd pytorch_binding/tests
python3 test_gpu_speed.py
```
import os
import subprocess
from pathlib import Path
import torch
ROOT_DIR = Path(__file__).parent.resolve()
def _run_cmd(cmd, shell=False):
try:
return subprocess.check_output(cmd, cwd=ROOT_DIR, stderr=subprocess.DEVNULL, shell=shell).decode("ascii").strip()
except Exception:
return None
def _get_version():
if os.path.exists(ROOT_DIR / "version.txt"):
with open(ROOT_DIR / "version.txt", "r") as f:
version = f.read().strip()
else:
version = '0.1'
if os.getenv("BUILD_VERSION"):
version = os.getenv("BUILD_VERSION")
return version
def _make_version_file(version, sha, abi, dtk, torch_version, branch):
sha = "Unknown" if sha is None else sha
torch_version = '.'.join(torch_version.split('.')[:2])
dcu_version = f"{version}+{sha}.abi{abi}.dtk{dtk}.torch{torch_version}"
version_path = ROOT_DIR / "warpctc_pytorch" / "version.py"
with open(version_path, "w") as f:
f.write(f"version = '{version}'\n")
f.write(f"git_hash = '{sha}'\n")
f.write(f"git_branch = '{branch}'\n")
f.write(f"abi = 'abi{abi}'\n")
f.write(f"dtk = '{dtk}'\n")
f.write(f"torch_version = '{torch_version}'\n")
f.write(f"dcu_version = '{dcu_version}'\n")
return dcu_version
def _get_pytorch_version():
if "PYTORCH_VERSION" in os.environ:
return f"{os.environ['PYTORCH_VERSION']}"
return torch.__version__
def get_version(ROCM_HOME):
sha = _run_cmd(["git", "rev-parse", "HEAD"])
sha = sha[:7]
branch = _run_cmd(["git", "rev-parse", "--abbrev-ref", "HEAD"])
tag = _run_cmd(["git", "describe", "--tags", "--exact-match", "@"])
print("-- Git branch:", branch)
print("-- Git SHA:", sha)
print("-- Git tag:", tag)
torch_version = _get_pytorch_version()
print("-- PyTorch:", torch_version)
version = _get_version()
print("-- Building version", version)
abi = _run_cmd(["echo '#include <string>' | gcc -x c++ -E -dM - | fgrep _GLIBCXX_USE_CXX11_ABI | awk '{print $3}'"], shell=True)
print("-- _GLIBCXX_USE_CXX11_ABI:", abi)
dtk = _run_cmd(["cat", os.path.join(ROCM_HOME, '.info/rocm_version')])
dtk = ''.join(dtk.split('.')[:2])
print("-- DTK:", dtk)
return _make_version_file(version, sha, abi, dtk, torch_version, branch)
......@@ -9,6 +9,7 @@ from torch.utils.cpp_extension import BuildExtension, CUDAExtension, CppExtensio
from setuptools import find_packages, setup
from setuptools.command.build_ext import build_ext
from pkg_resources import packaging # type: ignore[attr-defined]
from get_version import get_version
def _find_rocm_home() -> Optional[str]:
......@@ -291,10 +292,6 @@ class BuildReleaseExtension(BuildExtension):
build_ext.build_extensions(self)
def get_version():
return "0.1"
def get_extensions():
extensions = []
include_dirs = []
......@@ -331,7 +328,7 @@ def get_extensions():
def main():
setup(
name='warpctc_pytorch',
version="0.1",
version=get_version(os.environ.get('ROCM_HOME')),
description='Torch fuseop Computer Vision Foundation',
keywords='computer vision',
packages=find_packages(),
......
0.1
\ No newline at end of file
......@@ -87,3 +87,9 @@ class CTCLoss(Module):
_assert_no_grad(label_lens)
return self.ctc(acts, labels, act_lens, label_lens, self.size_average,
self.length_average, self.blank)
try:
from .version import version, git_hash, git_branch, dtk, abi, torch_version, dcu_version # noqa: F401
__version__, __dcu_version__ = version, dcu_version
except ImportError:
pass
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment