Skip to content

Commit

Permalink
add setup.py.
Browse files Browse the repository at this point in the history
  • Loading branch information
lcy-seso committed Dec 27, 2024
1 parent 87b1f6d commit 720cc64
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 3 deletions.
25 changes: 25 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,28 @@
*.ptx
*.cubin
*.fatbin

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
8 changes: 7 additions & 1 deletion cmake/generic.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ set(CMAKE_BUILD_TYPE Release)

set(CMAKE_CXX_STANDARD
20
CACHE STRING "The C++ standard whoese features are requested." FORCE)
CACHE STRING "The C++ standard whose features are requested." FORCE)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_CUDA_STANDARD
Expand Down Expand Up @@ -48,6 +48,12 @@ set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} -std=c++20)
set(CUDA_NVCC_FLAGS_DEBUG ${CUDA_NVCC_FLAGS_DEBUG} -std=c++20 -O0)
set(CUDA_NVCC_FLAGS_RELEASE ${CUDA_NVCC_FLAGS_RELEASE} -std=c++20 -O3)

if(${CUDA_VERSION_MAJOR} VERSION_GREATER_EQUAL "11")
add_definitions("-DENABLE_BF16")
message(STATUS "CUDA_VERSION ${CUDA_VERSION_MAJOR}.${CUDA_VERSION_MINOR} "
"is greater or equal than 11.0, enable -DENABLE_BF16 flag.")
endif()

message(STATUS "tilefusion: CUDA detected: " ${CUDA_VERSION})
message(STATUS "tilefusion: CUDA nvcc is: " ${CUDA_NVCC_EXECUTABLE})
message(STATUS "tilefusion: CUDA toolkit directory: " ${CUDA_TOOLKIT_ROOT_DIR})
Expand Down
15 changes: 15 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,26 @@ classifiers = [
"Operating System :: OS Independent",
"Topic :: Software Development :: Libraries",
]
# NOTE: setuptools's `install_requires` can overwritten in
# `pyproject.toml`'s `dependencies` field.
# Make sure to keep this field in sync with what is in `requirements.txt`.
dependencies = [
"torch",
]

[project.urls]
Homepage = "https://github.com/microsoft/TileFusion"
Issues = "https://github.com/microsoft/TileFusion/issues"

[build-system]
requires = [
"cmake",
"packaging",
"setuptools>=49.4.0",
"wheel",
]
build-backend = "setuptools.build_meta"

[tool.ruff]
line-length = 80
exclude = [
Expand Down
1 change: 1 addition & 0 deletions pytilefusion/__version__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = '0.0.0'
5 changes: 5 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cmake
packaging
setuptools>=49.4.0
torch
wheel
152 changes: 152 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
# --------------------------------------------------------------------------

import os
import subprocess
from pathlib import Path

from setuptools import Command, Extension, find_packages, setup
from setuptools.command.build_ext import build_ext

cur_path = Path(__file__).parent


def get_requirements():
"""Get Python package dependencies from requirements.txt."""
with open(cur_path / "requirements.txt") as f:
requirements = f.read().strip().split("\n")
requirements = [req for req in requirements if "https" not in req]
return requirements


class CMakeExtension(Extension):
""" specify the root folder of the CMake projects"""

def __init__(self, name, cmake_lists_dir=".", **kwargs):
Extension.__init__(self, name, sources=[], **kwargs)
self.cmake_lists_dir = os.path.abspath(cmake_lists_dir)


class CMakeBuildExt(build_ext):
"""launches the CMake build."""

def build_extension(self, ext: CMakeExtension) -> None:
# Ensure that CMake is present and working
try:
subprocess.check_output(["cmake", "--version"])
except OSError:
raise RuntimeError("Cannot find CMake executable") from None

debug = int(
os.environ.get("DEBUG", 0)
) if self.debug is None else self.debug
cfg = "Debug" if debug else "Release"

parallel_level = os.environ.get("CMAKE_BUILD_PARALLEL_LEVEL", None)
if parallel_level is not None:
self.parallel = int(parallel_level)
else:
self.parallel = os.cpu_count()

for ext in self.extensions:
extdir = os.path.abspath(
os.path.dirname(self.get_ext_fullpath(ext.name))
)

cmake_args = [
"-DCMAKE_BUILD_TYPE=%s" % cfg,
"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}".format(
cfg.upper(), extdir
), "-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_{}={}".format(
cfg.upper(), self.build_temp
)
]

# Adding CMake arguments set as environment variable
if "CMAKE_ARGS" in os.environ:
cmake_args += [
item for item in os.environ["CMAKE_ARGS"].split(" ") if item
]

if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)

build_args = []
build_args += ["--config", cfg]
# Set CMAKE_BUILD_PARALLEL_LEVEL to control the parallel build level
# across all generators.
if (
"CMAKE_BUILD_PARALLEL_LEVEL" not in os.environ and
hasattr(self, "parallel") and self.parallel
):
build_args += [f"-j{self.parallel}"]

build_temp = Path(self.build_temp) / ext.name
if not build_temp.exists():
build_temp.mkdir(parents=True)

# Config
subprocess.check_call(["cmake", ext.cmake_lists_dir] + cmake_args,
cwd=self.build_temp)

# Build
subprocess.check_call(["cmake", "--build", "."] + build_args,
cwd=self.build_temp)


class clean(Command):
user_options = []

def initialize_options(self):
pass

def finalize_options(self):
pass

def run(self):
import glob
import re
import shutil

with open(".gitignore") as f:
ignores = f.read()
pat = re.compile(r"^#( BEGIN NOT-CLEAN-FILES )?")
for wildcard in filter(None, ignores.split("\n")):
match = pat.match(wildcard)
if match:
if match.group(1):
# Marker is found and stop reading .gitignore.
break
# Ignore lines which begin with '#'.
else:
# Don't remove absolute paths from the system
wildcard = wildcard.lstrip("./")

for filename in glob.glob(wildcard):
print(f"cleaning '{filename}'")
try:
os.remove(filename)
except OSError:
shutil.rmtree(filename, ignore_errors=True)


description = ("TileFusion: A Python wrapper for tilefusion C++ library.")

with open(os.path.join("pytilefusion", '__version__.py')) as f:
exec(f.read())

setup(
name="tilefusion",
python_requires=">=3.10",
packages=find_packages(exclude=[""]),
install_requires=get_requirements(),
version=__version__, # noqa F821
description=description,
ext_modules=[CMakeExtension("tilefusion")],
cmdclass={
"build_ext": CMakeBuildExt,
"clean": clean,
},
)
12 changes: 10 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,15 @@ set_target_properties(
CUDA_SEPARABLE_COMPILATION ON)

target_compile_options(
${TARGET} PUBLIC $<$<COMPILE_LANGUAGE:CUDA>: -Werror,-Wall -rdc=true
-std=c++20 -fconcepts -fpermissive>)
${TARGET}
PUBLIC $<$<COMPILE_LANGUAGE:CUDA>:
-Werror,-Wall
-rdc=true
-std=c++20
-fconcepts
-fpermissive
--use_fast_math
--generate-line-info
>)
target_compile_features(${TARGET} PUBLIC cxx_std_20 cuda_std_20)
target_link_libraries(${TARGET} "${TORCH_LIBRARIES}")

0 comments on commit 720cc64

Please sign in to comment.