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

flyingdown's avatar
flyingdown committed
10
11
12
import torch
from torch.utils.cpp_extension import ROCM_HOME

moto's avatar
moto committed
13
from build_tools import setup_helpers
14

moto's avatar
moto committed
15
ROOT_DIR = Path(__file__).parent.resolve()
16
17


flyingdown's avatar
flyingdown committed
18
def _run_cmd(cmd, default, shell=False):
moto's avatar
moto committed
19
    try:
flyingdown's avatar
flyingdown committed
20
        return subprocess.check_output(cmd, cwd=ROOT_DIR, shell=shell).decode('ascii').strip()
moto's avatar
moto committed
21
22
23
24
    except Exception:
        return default


25
# Creating the version file
26
version = '0.10.0'
moto's avatar
moto committed
27
sha = _run_cmd(['git', 'rev-parse', 'HEAD'], default='Unknown')
28

29
30
if os.getenv('BUILD_VERSION'):
    version = os.getenv('BUILD_VERSION')
31
32
33
34
elif sha != 'Unknown':
    version += '+' + sha[:7]
print('-- Building version ' + version)

flyingdown's avatar
flyingdown committed
35
36
37
38
39
abi = _run_cmd(["echo '#include <string>' | gcc -x c++ -E -dM - | fgrep _GLIBCXX_USE_CXX11_ABI | awk '{print $3}'"], '0', shell=True)
dtk = _run_cmd(["cat", os.path.join(ROCM_HOME, '.info/rocm_version')], '0.0.0')
dtk = ''.join(dtk.split('.')[:2])
torch_version = torch.__version__
dcu_version = f"{version}.abi{abi}.dtk{dtk}.torch{torch_version}"
moto's avatar
moto committed
40
version_path = ROOT_DIR / 'torchaudio' / 'version.py'
41
42
43
with open(version_path, 'w') as f:
    f.write("__version__ = '{}'\n".format(version))
    f.write("git_version = {}\n".format(repr(sha)))
flyingdown's avatar
flyingdown committed
44
45
46
47
    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")
48

49
pytorch_package_version = os.getenv('PYTORCH_VERSION')
50

51
pytorch_package_dep = 'torch'
52
53
if pytorch_package_version is not None:
    pytorch_package_dep += "==" + pytorch_package_version
54

moto's avatar
moto committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

class clean(distutils.command.clean.clean):
    def run(self):
        # Run default behavior first
        distutils.command.clean.clean.run(self)

        # Remove torchaudio extension
        for path in (ROOT_DIR / 'torchaudio').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)
peterjc123's avatar
peterjc123 committed
73

74

75
76
77
78
79
80
81
82
def _get_packages():
    exclude = [
        "build*",
        "test*",
        "torchaudio.csrc*",
        "third_party*",
        "build_tools*",
    ]
moto's avatar
moto committed
83
84
85
86
87
88
89
90
91
92
93
    exclude_prototype = False
    branch_name = _run_cmd(['git', 'rev-parse', '--abbrev-ref', 'HEAD'], default=None)
    is_on_tag = _run_cmd(['git', 'describe', '--tags', '--exact-match', '@'], default=None)

    if branch_name is not None and branch_name.startswith('release/'):
        print('On release branch')
        exclude_prototype = True
    if is_on_tag is not None and re.match(r'v[\d.]+(-rc\d+)?', is_on_tag):
        print('On release tag')
        exclude_prototype = True
    if exclude_prototype:
moto's avatar
moto committed
94
        print('Excluding torchaudio.prototype from the package.')
95
96
97
98
        exclude.append("torchaudio.prototype")
    return find_packages(exclude=exclude)


Soumith Chintala's avatar
Soumith Chintala committed
99
setup(
100
    name="torchaudio",
flyingdown's avatar
flyingdown committed
101
    version=dcu_version,
Soumith Chintala's avatar
Soumith Chintala committed
102
103
    description="An audio package for PyTorch",
    url="https://github.com/pytorch/audio",
104
    author="Soumith Chintala, David Pollack, Sean Naren, Peter Goldsborough",
Soumith Chintala's avatar
Soumith Chintala committed
105
    author_email="soumith@pytorch.org",
Hong Xu's avatar
Hong Xu committed
106
107
108
109
110
111
112
113
114
    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++",
115
116
117
        "Programming Language :: Python :: 3.6",
        "Programming Language :: Python :: 3.7",
        "Programming Language :: Python :: 3.8",
Eli Uriegas's avatar
Eli Uriegas committed
118
        "Programming Language :: Python :: 3.9",
Hong Xu's avatar
Hong Xu committed
119
120
121
122
        "Programming Language :: Python :: Implementation :: CPython",
        "Topic :: Multimedia :: Sound/Audio",
        "Topic :: Scientific/Engineering :: Artificial Intelligence"
    ],
123
    packages=_get_packages(),
moto's avatar
moto committed
124
125
    ext_modules=setup_helpers.get_ext_modules(),
    cmdclass={
moto's avatar
moto committed
126
        'build_ext': setup_helpers.CMakeBuild,
127
        'clean': clean,
moto's avatar
moto committed
128
    },
129
130
    install_requires=[pytorch_package_dep],
    zip_safe=False,
131
)