setup.py 4.73 KB
Newer Older
Soumith Chintala's avatar
Soumith Chintala committed
1
#!/usr/bin/env python
2
import distutils.command.clean
3
import os
4
import re
moto's avatar
moto committed
5
import shutil
6
import subprocess
moto's avatar
moto committed
7
from pathlib import Path
8

9
import torch
10
from setuptools import find_packages, setup
moto's avatar
moto committed
11
from tools import setup_helpers
12

moto's avatar
moto committed
13
ROOT_DIR = Path(__file__).parent.resolve()
14

limm's avatar
limm committed
15
def _run_cmd(cmd, shell=False):
16
    try:
17
        return subprocess.check_output(cmd, cwd=ROOT_DIR, stderr=subprocess.DEVNULL).decode("ascii").strip()
18
19
20
    except Exception:
        return None

moto's avatar
moto committed
21
def _get_version(sha):
22
23
    with open(ROOT_DIR / "version.txt", "r") as f:
        version = f.read().strip()
24
25
    if os.getenv("BUILD_VERSION"):
        version = os.getenv("BUILD_VERSION")
limm's avatar
limm committed
26
27
    #elif sha is not None:
    #    version += "+" + sha[:7]
moto's avatar
moto committed
28
    return version
29

moto's avatar
moto committed
30
def _make_version_file(version, sha):
31
    sha = "Unknown" if sha is None else sha
moto-meta's avatar
moto-meta committed
32
    version_path = ROOT_DIR / "src" / "torchaudio" / "version.py"
33
    with open(version_path, "w") as f:
moto's avatar
moto committed
34
35
        f.write(f"__version__ = '{version}'\n")
        f.write(f"git_version = '{sha}'\n")
36

moto's avatar
moto committed
37
def _get_pytorch_version():
38
    if "PYTORCH_VERSION" in os.environ:
moto's avatar
moto committed
39
        return f"torch=={os.environ['PYTORCH_VERSION']}"
40
    return "torch"
41

moto's avatar
moto committed
42
43
44
45
46
47
class clean(distutils.command.clean.clean):
    def run(self):
        # Run default behavior first
        distutils.command.clean.clean.run(self)

        # Remove torchaudio extension
moto-meta's avatar
moto-meta committed
48
        for path in (ROOT_DIR / "src").glob("**/*.so"):
49
            print(f"removing '{path}'")
moto's avatar
moto committed
50
51
52
            path.unlink()
        # Remove build directory
        build_dirs = [
53
            ROOT_DIR / "build",
moto's avatar
moto committed
54
55
56
        ]
        for path in build_dirs:
            if path.exists():
57
                print(f"removing '{path}' (and everything under it)")
moto's avatar
moto committed
58
                shutil.rmtree(str(path), ignore_errors=True)
peterjc123's avatar
peterjc123 committed
59

moto's avatar
moto committed
60
def _parse_url(path):
61
    with open(path, "r") as file_:
moto's avatar
moto committed
62
        for line in file_:
63
            match = re.match(r"^\s*URL\s+(https:\/\/.+)$", line)
moto's avatar
moto committed
64
65
66
67
68
69
            if match:
                url = match.group(1)
                yield url

def _fetch_archives(src):
    for dest, url in src:
70
        if not dest.exists():
71
            print(f" --- Fetching {os.path.basename(dest)}")
72
73
            torch.hub.download_url_to_file(url, dest, progress=False)

moto's avatar
moto committed
74
def _main():
limm's avatar
limm committed
75
    ROCM_HOME = os.getenv("ROCM_PATH")
76
77
78
79
80
81
    sha = _run_cmd(["git", "rev-parse", "HEAD"])
    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)
moto's avatar
moto committed
82
    pytorch_package_dep = _get_pytorch_version()
83
    print("-- PyTorch dependency:", pytorch_package_dep)
moto's avatar
moto committed
84
    version = _get_version(sha)
limm's avatar
limm committed
85
    version += "+das.opt1"
86
    print("-- Building version", version)
limm's avatar
limm committed
87
    dtk = _run_cmd(["cat", os.path.join(ROCM_HOME, '.info/rocm_version')])
limm's avatar
limm committed
88
    dtk = ''.join(dtk.split('.'))
limm's avatar
limm committed
89
90
    print(f"-- dtk_version = {dtk}")
    version += ".dtk" + dtk
moto's avatar
moto committed
91
92
93

    _make_version_file(version, sha)

94
95
96
    with open("README.md") as f:
        long_description = f.read()

moto's avatar
moto committed
97
98
99
100
    setup(
        name="torchaudio",
        version=version,
        description="An audio package for PyTorch",
101
102
        long_description=long_description,
        long_description_content_type="text/markdown",
moto's avatar
moto committed
103
        url="https://github.com/pytorch/audio",
moto's avatar
moto committed
104
105
106
107
        author=(
            "Soumith Chintala, David Pollack, Sean Naren, Peter Goldsborough, "
            "Moto Hira, Caroline Chen, Jeff Hwang, Zhaoheng Ni, Xiaohui Zhang"
        ),
moto's avatar
moto committed
108
        author_email="soumith@pytorch.org",
109
110
        maintainer="Moto Hira, Caroline Chen, Jeff Hwang, Zhaoheng Ni, Xiaohui Zhang",
        maintainer_email="moto@meta.com",
moto's avatar
moto committed
111
112
113
114
115
116
117
118
119
120
121
        classifiers=[
            "Environment :: Plugins",
            "Intended Audience :: Developers",
            "Intended Audience :: Science/Research",
            "License :: OSI Approved :: BSD License",
            "Operating System :: MacOS :: MacOS X",
            "Operating System :: Microsoft :: Windows",
            "Operating System :: POSIX",
            "Programming Language :: C++",
            "Programming Language :: Python :: 3.8",
            "Programming Language :: Python :: 3.9",
Wei Wang's avatar
Wei Wang committed
122
            "Programming Language :: Python :: 3.10",
123
            "Programming Language :: Python :: 3.11",
moto's avatar
moto committed
124
125
            "Programming Language :: Python :: Implementation :: CPython",
            "Topic :: Multimedia :: Sound/Audio",
126
            "Topic :: Scientific/Engineering :: Artificial Intelligence",
moto's avatar
moto committed
127
        ],
128
        packages=find_packages(where="src"),
moto-meta's avatar
moto-meta committed
129
        package_dir={"": "src"},
moto's avatar
moto committed
130
131
        ext_modules=setup_helpers.get_ext_modules(),
        cmdclass={
132
133
            "build_ext": setup_helpers.CMakeBuild,
            "clean": clean,
moto's avatar
moto committed
134
135
136
137
138
139
        },
        install_requires=[pytorch_package_dep],
        zip_safe=False,
    )


140
if __name__ == "__main__":
moto's avatar
moto committed
141
    _main()