forked from pytorch/tensordict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
148 lines (120 loc) · 4.1 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import argparse
import distutils.command.clean
import os
import shutil
import subprocess
import sys
from datetime import date
from pathlib import Path
from typing import List
from setuptools import find_packages, setup
from torch.utils.cpp_extension import BuildExtension
ROOT_DIR = Path(__file__).parent.resolve()
try:
sha = (
subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=ROOT_DIR)
.decode("ascii")
.strip()
)
except Exception:
sha = "Unknown"
package_name = "tensordict"
def parse_args(argv: List[str]) -> argparse.Namespace:
parser = argparse.ArgumentParser(description="tensordict setup")
parser.add_argument(
"--package_name",
type=str,
default="tensordict",
help="the name of this output wheel",
)
return parser.parse_known_args(argv)
def get_version():
version = (ROOT_DIR / "version.txt").read_text().strip()
if os.getenv("BUILD_VERSION"):
version = os.getenv("BUILD_VERSION")
elif sha != "Unknown":
version += "+" + sha[:7]
return version
def get_nightly_version():
return f"{date.today():%Y.%m.%d}"
def write_version_file(version):
version_path = ROOT_DIR / "tensordict" / "version.py"
with version_path.open("w") as f:
f.write("__version__ = '{}'\n".format(version))
f.write("git_version = {}\n".format(repr(sha)))
def _get_pytorch_version():
# if "PYTORCH_VERSION" in os.environ:
# return f"torch=={os.environ['PYTORCH_VERSION']}"
return "torch"
def _get_packages():
exclude = [
"build*",
"test*",
"third_party*",
"tools*",
]
return find_packages(exclude=exclude)
class clean(distutils.command.clean.clean):
def run(self):
# Run default behavior first
distutils.command.clean.clean.run(self)
# Remove tensordict extension
for path in (ROOT_DIR / "tensordict").glob("**/*.so"):
print(f"removing '{path}'")
path.unlink()
# Remove build directory
build_dirs = [ROOT_DIR / "build"]
for path in build_dirs:
if path.exists():
print(f"removing '{path}' (and everything under it)")
shutil.rmtree(str(path), ignore_errors=True)
def _main(argv):
args, unknown = parse_args(argv)
name = args.package_name
is_nightly = "nightly" in name
if is_nightly:
version = get_nightly_version()
else:
version = get_version()
write_version_file(version)
print(f"Building wheel {package_name}-{version}")
print(f"BUILD_VERSION is {os.getenv('BUILD_VERSION')}")
pytorch_package_dep = _get_pytorch_version()
print("-- PyTorch dependency:", pytorch_package_dep)
long_description = (ROOT_DIR / "README.md").read_text()
sys.argv = [sys.argv[0]] + unknown
setup(
# Metadata
name=name,
version=version,
author="tensordict contributors",
author_email="[email protected]",
url="https://github.com/pytorch-labs/tensordict",
long_description=long_description,
long_description_content_type="text/markdown",
license="BSD",
# Package info
packages=find_packages(exclude=("test", "tutorials", "packaging", "gallery")),
cmdclass={
"build_ext": BuildExtension.with_options(no_python_abi_suffix=True),
"clean": clean,
},
install_requires=[pytorch_package_dep, "numpy", "packaging", "cloudpickle"],
extras_require={
"tests": ["pytest", "pyyaml", "pytest-instafail"],
"checkpointing": ["torchsnapshot-nightly"],
},
zip_safe=False,
classifiers=[
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
],
)
if __name__ == "__main__":
_main(sys.argv[1:])