2021-10-06 04:16:12 +08:00
|
|
|
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
# See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
# Also available under a BSD-style license. See LICENSE.
|
|
|
|
|
|
|
|
# Script for generating the torch-mlir wheel.
|
|
|
|
# ```
|
|
|
|
# $ python setup.py bdist_wheel
|
|
|
|
# ```
|
|
|
|
#
|
|
|
|
# It is recommended to build with Ninja and ccache. To do so, set environment
|
|
|
|
# variables by prefixing to above invocations:
|
|
|
|
# ```
|
|
|
|
# CMAKE_GENERATOR=Ninja CMAKE_C_COMPILER_LAUNCHER=ccache CMAKE_CXX_COMPILER_LAUNCHER=ccache
|
|
|
|
# ```
|
|
|
|
#
|
|
|
|
# On CIs, it is often advantageous to re-use/control the CMake build directory.
|
|
|
|
# This can be set with the TORCH_MLIR_CMAKE_BUILD_DIR env var.
|
2021-10-06 07:26:26 +08:00
|
|
|
# Additionally, the TORCH_MLIR_CMAKE_BUILD_DIR_ALREADY_BUILT env var will
|
|
|
|
# prevent this script from attempting to build the directory, and will simply
|
|
|
|
# use the (presumed already built) directory as-is.
|
2021-10-06 04:16:12 +08:00
|
|
|
#
|
2022-03-31 01:51:52 +08:00
|
|
|
# The package version can be set with the TORCH_MLIR_PYTHON_PACKAGE_VERSION
|
|
|
|
# environment variable. For example, this can be "20220330.357" for a snapshot
|
|
|
|
# release on 2022-03-30 with build number 357.
|
|
|
|
#
|
2021-10-06 04:16:12 +08:00
|
|
|
# Implementation notes:
|
|
|
|
# The contents of the wheel is just the contents of the `python_packages`
|
|
|
|
# directory that our CMake build produces. We go through quite a bit of effort
|
|
|
|
# on the CMake side to organize that directory already, so we avoid duplicating
|
|
|
|
# that here, and just package up its contents.
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
import sys
|
2022-04-21 17:19:12 +08:00
|
|
|
import sysconfig
|
2021-10-06 04:16:12 +08:00
|
|
|
|
|
|
|
from distutils.command.build import build as _build
|
2022-04-21 17:19:12 +08:00
|
|
|
from distutils.sysconfig import get_python_inc
|
2021-10-06 07:26:26 +08:00
|
|
|
from setuptools import setup, Extension
|
2021-10-06 04:16:12 +08:00
|
|
|
from setuptools.command.build_ext import build_ext
|
|
|
|
from setuptools.command.build_py import build_py
|
|
|
|
|
2022-04-21 07:00:09 +08:00
|
|
|
|
2022-03-31 01:51:52 +08:00
|
|
|
PACKAGE_VERSION = os.environ.get("TORCH_MLIR_PYTHON_PACKAGE_VERSION") or "0.0.1"
|
2021-10-06 04:16:12 +08:00
|
|
|
|
2022-09-20 22:16:04 +08:00
|
|
|
# If true, enable LTC build by default
|
2022-10-12 01:29:07 +08:00
|
|
|
TORCH_MLIR_ENABLE_LTC_DEFAULT = True
|
2023-02-14 04:21:06 +08:00
|
|
|
TORCH_MLIR_ENABLE_ONLY_MLIR_PYTHON_BINDINGS = int(os.environ.get('TORCH_MLIR_ENABLE_ONLY_MLIR_PYTHON_BINDINGS', False))
|
|
|
|
if not TORCH_MLIR_ENABLE_ONLY_MLIR_PYTHON_BINDINGS:
|
|
|
|
import torch
|
2022-09-20 22:16:04 +08:00
|
|
|
|
2021-10-06 04:16:12 +08:00
|
|
|
# Build phase discovery is unreliable. Just tell it what phases to run.
|
|
|
|
class CustomBuild(_build):
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
self.run_command("build_py")
|
|
|
|
self.run_command("build_ext")
|
|
|
|
self.run_command("build_scripts")
|
|
|
|
|
|
|
|
class CMakeBuild(build_py):
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
target_dir = self.build_lib
|
|
|
|
cmake_build_dir = os.getenv("TORCH_MLIR_CMAKE_BUILD_DIR")
|
|
|
|
if not cmake_build_dir:
|
2022-03-30 04:49:35 +08:00
|
|
|
cmake_build_dir = os.path.abspath(
|
|
|
|
os.path.join(target_dir, "..", "cmake_build"))
|
2022-04-21 17:19:12 +08:00
|
|
|
python_package_dir = os.path.join(cmake_build_dir,
|
|
|
|
"tools", "torch-mlir", "python_packages",
|
|
|
|
"torch_mlir")
|
2021-10-06 07:26:26 +08:00
|
|
|
if not os.getenv("TORCH_MLIR_CMAKE_BUILD_DIR_ALREADY_BUILT"):
|
|
|
|
src_dir = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
llvm_dir = os.path.join(
|
2022-03-30 04:49:35 +08:00
|
|
|
src_dir, "externals", "llvm-project", "llvm")
|
2022-09-20 22:16:04 +08:00
|
|
|
|
|
|
|
enable_ltc = int(os.environ.get('TORCH_MLIR_ENABLE_LTC', TORCH_MLIR_ENABLE_LTC_DEFAULT))
|
|
|
|
|
2021-10-06 07:26:26 +08:00
|
|
|
cmake_args = [
|
|
|
|
f"-DCMAKE_BUILD_TYPE=Release",
|
|
|
|
f"-DPython3_EXECUTABLE={sys.executable}",
|
2023-01-02 23:53:04 +08:00
|
|
|
f"-DPython3_FIND_VIRTUALENV=ONLY",
|
2021-10-06 07:26:26 +08:00
|
|
|
f"-DLLVM_TARGETS_TO_BUILD=host",
|
|
|
|
f"-DMLIR_ENABLE_BINDINGS_PYTHON=ON",
|
|
|
|
f"-DLLVM_ENABLE_PROJECTS=mlir",
|
2022-09-19 07:50:16 +08:00
|
|
|
f"-DLLVM_ENABLE_ZSTD=OFF",
|
2022-02-03 07:01:38 +08:00
|
|
|
f"-DLLVM_EXTERNAL_PROJECTS=torch-mlir;torch-mlir-dialects",
|
2021-10-06 07:26:26 +08:00
|
|
|
f"-DLLVM_EXTERNAL_TORCH_MLIR_SOURCE_DIR={src_dir}",
|
2022-03-30 04:49:35 +08:00
|
|
|
f"-DLLVM_EXTERNAL_TORCH_MLIR_DIALECTS_SOURCE_DIR={src_dir}/externals/llvm-external-projects/torch-mlir-dialects",
|
2021-10-22 12:18:08 +08:00
|
|
|
# Optimization options for building wheels.
|
|
|
|
f"-DCMAKE_VISIBILITY_INLINES_HIDDEN=ON",
|
|
|
|
f"-DCMAKE_C_VISIBILITY_PRESET=hidden",
|
|
|
|
f"-DCMAKE_CXX_VISIBILITY_PRESET=hidden",
|
2022-09-20 22:16:04 +08:00
|
|
|
f"-DTORCH_MLIR_ENABLE_LTC={'ON' if enable_ltc else 'OFF'}",
|
2023-02-14 04:21:06 +08:00
|
|
|
f"-DTORCH_MLIR_ENABLE_ONLY_MLIR_PYTHON_BINDINGS={'ON' if TORCH_MLIR_ENABLE_ONLY_MLIR_PYTHON_BINDINGS else 'OFF'}",
|
2021-10-06 07:26:26 +08:00
|
|
|
]
|
2022-08-16 21:30:22 +08:00
|
|
|
|
2021-10-06 07:26:26 +08:00
|
|
|
os.makedirs(cmake_build_dir, exist_ok=True)
|
|
|
|
cmake_cache_file = os.path.join(cmake_build_dir, "CMakeCache.txt")
|
|
|
|
if os.path.exists(cmake_cache_file):
|
|
|
|
os.remove(cmake_cache_file)
|
2022-04-21 17:19:12 +08:00
|
|
|
# NOTE: With repeated builds for different Python versions, the
|
|
|
|
# prior version binaries will continue to accumulate. IREE uses
|
|
|
|
# a separate install step and cleans the install directory to
|
|
|
|
# keep this from happening. That is the most robust. Here we just
|
|
|
|
# delete the directory where we build native extensions to keep
|
|
|
|
# this from happening but still take advantage of most of the
|
|
|
|
# build cache.
|
|
|
|
mlir_libs_dir = os.path.join(python_package_dir, "torch_mlir", "_mlir_libs")
|
|
|
|
if os.path.exists(mlir_libs_dir):
|
|
|
|
print(f"Removing _mlir_mlibs dir to force rebuild: {mlir_libs_dir}")
|
|
|
|
shutil.rmtree(mlir_libs_dir)
|
|
|
|
else:
|
|
|
|
print(f"Not removing _mlir_libs dir (does not exist): {mlir_libs_dir}")
|
|
|
|
|
2021-10-06 07:26:26 +08:00
|
|
|
subprocess.check_call(["cmake", llvm_dir] +
|
|
|
|
cmake_args, cwd=cmake_build_dir)
|
|
|
|
subprocess.check_call(["cmake",
|
|
|
|
"--build", ".",
|
CI: prepare CI for ccache updates for MSVC/Windows (#2120)
This patch, by itself, doesn't fix caching on Windows, but once a new
release of ccache is available, caching for Windows builds should start
working again (validated by building ccache from source and using it
with LLVM builds).
Ccache rejects caching when either the `/Zi` or `/ZI` flags are used
during compilation on Windows, since these flags tell the compiler to
embed debug information in a PDB file (separate from the object file
produced by the compiler). In particular, our CI builds add the `/Zi`
flag, making ccache mark these compiler invocations as uncacheable.
But what caused our CI to add debug flags, especially when we specified
`-DCMAKE_BUILD_TYPE=Release`? On Windows, unless we specify the
`--config Release` flag during the CMake build step, CMake assumes a
debug build. So all this while, we had been producing debug builds of
torch-mlir for every PR! No doubt it took so long to build the Windows
binaries.
The reason for having to specify the configuration during the _build_
step (as opposed to the _configure_ step) of CMake on Windows is that
CMake's Visual Studio generators will produce _both_ Release and Debug
profiles during the CMake configure step (thus requiring a build-time
value that tells CMake whether to build in Release or Debug mode).
Luckily, on Linux and macOS, the `--config` flag seems to be simply
ignored, instead of causing build errors.
Strangely, based on cursory tests, it seems like on Windows we need to
specify the Relase configuration as both `-DCMAKE_BUILD_TYPE=Release` as
well as `--config Release`. Dropping either made my build switch to a
Debug configuration.
Additionally, there is a bug in ccache v4.8 (although this is addressed
in trunk) that causes ccache to reject caching if the compiler
invocation includes any flag that starts with `/Z`, including /`Zc`,
which is added by LLVM's HandleLLVMOptions.cmake and which isn't related
to debug info or PDB files. The next release of ccache should include
the fix, which is to reject caching only for `/Zi` and `/ZI` flags and
not all flags that start with `/Z`.
As a side note, debugging this problem was possible because of ccache's
log file, which is enabled by: `ccache --set-config="log_file=log.txt"`.
2023-05-13 01:45:01 +08:00
|
|
|
"--config", "Release",
|
2021-10-06 07:26:26 +08:00
|
|
|
"--target", "TorchMLIRPythonModules"],
|
|
|
|
cwd=cmake_build_dir)
|
2022-02-22 23:54:05 +08:00
|
|
|
|
|
|
|
if os.path.exists(target_dir):
|
|
|
|
shutil.rmtree(target_dir, ignore_errors=False, onerror=None)
|
|
|
|
|
2021-10-06 04:16:12 +08:00
|
|
|
shutil.copytree(python_package_dir,
|
|
|
|
target_dir,
|
2022-02-22 23:54:05 +08:00
|
|
|
symlinks=False)
|
2021-10-06 04:16:12 +08:00
|
|
|
|
|
|
|
|
2021-10-06 07:26:26 +08:00
|
|
|
class CMakeExtension(Extension):
|
|
|
|
|
|
|
|
def __init__(self, name, sourcedir=""):
|
|
|
|
Extension.__init__(self, name, sources=[])
|
|
|
|
self.sourcedir = os.path.abspath(sourcedir)
|
|
|
|
|
|
|
|
|
2021-10-06 04:16:12 +08:00
|
|
|
class NoopBuildExtension(build_ext):
|
|
|
|
|
|
|
|
def build_extension(self, ext):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2022-12-07 23:01:55 +08:00
|
|
|
with open("README.md", "r", encoding="utf-8") as fh:
|
|
|
|
long_description = fh.read()
|
|
|
|
|
|
|
|
|
2021-10-06 04:16:12 +08:00
|
|
|
setup(
|
2023-03-07 14:20:06 +08:00
|
|
|
name="torch-mlir" if not TORCH_MLIR_ENABLE_ONLY_MLIR_PYTHON_BINDINGS else "torch-mlir-core",
|
2022-03-31 01:51:52 +08:00
|
|
|
version=f"{PACKAGE_VERSION}",
|
2021-10-06 04:16:12 +08:00
|
|
|
author="Sean Silva",
|
|
|
|
author_email="silvasean@google.com",
|
|
|
|
description="First-class interop between PyTorch and MLIR",
|
2022-12-07 23:01:55 +08:00
|
|
|
long_description=long_description,
|
|
|
|
long_description_content_type="text/markdown",
|
2021-10-06 04:16:12 +08:00
|
|
|
include_package_data=True,
|
|
|
|
cmdclass={
|
|
|
|
"build": CustomBuild,
|
|
|
|
"built_ext": NoopBuildExtension,
|
|
|
|
"build_py": CMakeBuild,
|
|
|
|
},
|
2021-10-06 07:26:26 +08:00
|
|
|
ext_modules=[
|
|
|
|
CMakeExtension("torch_mlir._mlir_libs._jit_ir_importer"),
|
2023-03-06 02:23:43 +08:00
|
|
|
] if not TORCH_MLIR_ENABLE_ONLY_MLIR_PYTHON_BINDINGS else [CMakeExtension("torch_mlir._mlir_libs._torchMlir")],
|
2023-02-14 04:21:06 +08:00
|
|
|
install_requires=["numpy", ] + (
|
|
|
|
[f"torch=={torch.__version__}".split("+", 1)[0], ] if not TORCH_MLIR_ENABLE_ONLY_MLIR_PYTHON_BINDINGS else []),
|
2021-10-06 04:16:12 +08:00
|
|
|
zip_safe=False,
|
|
|
|
)
|