Files
DeepGEMM/setup.py

103 lines
3.3 KiB
Python
Raw Normal View History

2025-02-25 22:52:41 +08:00
import os
import setuptools
import shutil
import subprocess
import torch
from setuptools import find_packages
2025-02-26 17:48:57 +00:00
from setuptools.command.build_py import build_py
from torch.utils.cpp_extension import CUDAExtension, CUDA_HOME
2025-02-25 22:52:41 +08:00
current_dir = os.path.dirname(os.path.realpath(__file__))
cxx_flags = ['-std=c++17', '-O3', '-fPIC', '-Wno-psabi', '-Wno-deprecated-declarations',
f'-D_GLIBCXX_USE_CXX11_ABI={int(torch.compiled_with_cxx11_abi())}']
sources = ['csrc/python_api.cpp']
build_include_dirs = [
f'{CUDA_HOME}/include',
'deep_gemm/include',
'third-party/cutlass/include',
'third-party/fmt/include',
]
build_libraries = ['cuda', 'cudart', 'nvrtc']
build_library_dirs = [
f'{CUDA_HOME}/lib64',
2025-07-28 10:58:23 +02:00
f'{CUDA_HOME}/lib64/stubs'
]
third_party_include_dirs = [
2025-02-27 10:50:20 +08:00
'third-party/cutlass/include/cute',
'third-party/cutlass/include/cutlass',
]
2025-02-25 22:52:41 +08:00
2025-02-26 17:48:57 +00:00
class CustomBuildPy(build_py):
2025-02-25 22:52:41 +08:00
def run(self):
2025-02-26 17:48:57 +00:00
# First, prepare the include directories
self.prepare_includes()
2025-02-27 10:50:20 +08:00
# Second, make clusters' cache setting default into `envs.py`
self.generate_default_envs()
# Finally, run the regular build
2025-02-26 17:48:57 +00:00
build_py.run(self)
2025-02-25 22:52:41 +08:00
def generate_default_envs(self):
code = '# Pre-installed environment variables\n'
code += 'persistent_envs = dict()\n'
for name in ('DG_JIT_CACHE_DIR', 'DG_JIT_PRINT_COMPILER_COMMAND', 'DG_JIT_CPP_STANDARD'):
code += f"persistent_envs['{name}'] = '{os.environ[name]}'\n" if name in os.environ else ''
with open(os.path.join(self.build_lib, 'deep_gemm', 'envs.py'), 'w') as f:
f.write(code)
2025-02-26 17:48:57 +00:00
def prepare_includes(self):
# Create temporary build directory instead of modifying package directory
2025-02-27 10:50:20 +08:00
build_include_dir = os.path.join(self.build_lib, 'deep_gemm/include')
2025-02-26 17:48:57 +00:00
os.makedirs(build_include_dir, exist_ok=True)
# Copy third-party includes to the build directory
for d in third_party_include_dirs:
2025-02-27 10:50:20 +08:00
dirname = d.split('/')[-1]
2025-02-26 17:48:57 +00:00
src_dir = os.path.join(current_dir, d)
dst_dir = os.path.join(build_include_dir, dirname)
# Remove existing directory if it exists
if os.path.exists(dst_dir):
shutil.rmtree(dst_dir)
# Copy the directory
2025-02-25 22:52:41 +08:00
shutil.copytree(src_dir, dst_dir)
2025-02-27 10:50:20 +08:00
if __name__ == '__main__':
2025-02-25 22:52:41 +08:00
# noinspection PyBroadException
try:
2025-02-27 10:50:20 +08:00
cmd = ['git', 'rev-parse', '--short', 'HEAD']
revision = '+' + subprocess.check_output(cmd).decode('ascii').rstrip()
2025-02-25 22:52:41 +08:00
except:
2025-02-27 10:50:20 +08:00
revision = ''
2025-02-25 22:52:41 +08:00
# noinspection PyTypeChecker
2025-02-25 22:52:41 +08:00
setuptools.setup(
2025-02-27 10:50:20 +08:00
name='deep_gemm',
version='2.0.0' + revision,
packages=find_packages('.'),
2025-02-26 17:48:57 +00:00
package_data={
2025-02-27 10:50:20 +08:00
'deep_gemm': [
'include/deep_gemm/**/*',
2025-02-27 10:50:20 +08:00
'include/cute/**/*',
'include/cutlass/**/*',
2025-02-26 17:48:57 +00:00
]
},
ext_modules=[
CUDAExtension(name='deep_gemm_cpp',
2025-07-29 16:14:46 +08:00
sources=sources,
include_dirs=build_include_dirs,
libraries=build_libraries,
library_dirs=build_library_dirs,
extra_compile_args=cxx_flags)
],
zip_safe=False,
2025-02-25 22:52:41 +08:00
cmdclass={
2025-02-27 10:50:20 +08:00
'build_py': CustomBuildPy,
2025-02-26 17:48:57 +00:00
},
2025-02-25 22:52:41 +08:00
)