2021-09-11 02:44:38 +08:00
|
|
|
include(AddMLIRPython)
|
|
|
|
|
2021-10-22 12:18:08 +08:00
|
|
|
# Disables generation of "version soname" (i.e. libFoo.so.<version>), which
|
|
|
|
# causes pure duplication as part of Python wheels.
|
|
|
|
set(CMAKE_PLATFORM_NO_VERSIONED_SONAME ON)
|
|
|
|
|
2021-09-21 04:55:36 +08:00
|
|
|
# The directory at which the Python import tree begins.
|
|
|
|
# See documentation for `declare_mlir_python_sources`'s ROOT_DIR
|
|
|
|
# argument.
|
|
|
|
set(TORCH_MLIR_PYTHON_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/torch_mlir")
|
|
|
|
|
2021-09-28 02:36:44 +08:00
|
|
|
|
2021-09-21 04:55:36 +08:00
|
|
|
# We vendor our own MLIR instance in the `torch_mlir` namespace.
|
|
|
|
add_compile_definitions("MLIR_PYTHON_PACKAGE_PREFIX=torch_mlir.")
|
|
|
|
|
2022-07-30 17:54:40 +08:00
|
|
|
################################################################################
|
|
|
|
# PyTorch
|
|
|
|
################################################################################
|
|
|
|
|
|
|
|
if (NOT TORCH_MLIR_USE_INSTALLED_PYTORCH)
|
|
|
|
# Source builds
|
2022-08-06 07:51:35 +08:00
|
|
|
set(ENV{TORCH_MLIR_SRC_PYTORCH_REPO} ${TORCH_MLIR_SRC_PYTORCH_REPO})
|
|
|
|
set(ENV{TORCH_MLIR_SRC_PYTORCH_BRANCH} ${TORCH_MLIR_SRC_PYTORCH_BRANCH})
|
2022-07-30 17:54:40 +08:00
|
|
|
set(ENV{MACOSX_DEPLOYMENT_TARGET} ${MACOSX_DEPLOYMENT_TARGET})
|
|
|
|
set(ENV{CMAKE_OSX_ARCHITECTURES} ${CMAKE_OSX_ARCHITECTURES})
|
|
|
|
set(ENV{CMAKE_C_COMPILER_LAUNCHER} ${CMAKE_C_COMPILER_LAUNCHER})
|
|
|
|
set(ENV{CMAKE_CXX_COMPILER_LAUNCHER} ${CMAKE_CXX_COMPILER_LAUNCHER})
|
|
|
|
execute_process(
|
|
|
|
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/../build_tools/build_libtorch.sh
|
|
|
|
RESULT_VARIABLE _result
|
|
|
|
)
|
|
|
|
if(_result)
|
|
|
|
message(FATAL_ERROR "Failed to run `build_libtorch.sh`")
|
|
|
|
endif()
|
|
|
|
set(TORCH_INSTALL_PREFIX "libtorch")
|
|
|
|
endif()
|
|
|
|
|
2021-09-11 02:44:38 +08:00
|
|
|
################################################################################
|
|
|
|
# Sources
|
|
|
|
################################################################################
|
|
|
|
|
|
|
|
declare_mlir_python_sources(TorchMLIRPythonSources)
|
|
|
|
declare_mlir_python_sources(TorchMLIRPythonExtensions)
|
|
|
|
|
2022-04-20 08:30:09 +08:00
|
|
|
declare_mlir_python_sources(TorchMLIRPythonSources.TopLevel
|
|
|
|
ROOT_DIR "${TORCH_MLIR_PYTHON_ROOT_DIR}"
|
|
|
|
ADD_TO_PARENT TorchMLIRPythonSources
|
|
|
|
SOURCES
|
|
|
|
__init__.py
|
|
|
|
compiler_utils.py
|
|
|
|
)
|
|
|
|
|
2021-09-11 02:44:38 +08:00
|
|
|
declare_mlir_python_sources(TorchMLIRPythonSources.Dialects
|
2021-09-21 04:55:36 +08:00
|
|
|
ROOT_DIR "${TORCH_MLIR_PYTHON_ROOT_DIR}"
|
2021-09-11 02:44:38 +08:00
|
|
|
ADD_TO_PARENT TorchMLIRPythonSources
|
|
|
|
)
|
|
|
|
|
|
|
|
declare_mlir_dialect_python_bindings(
|
|
|
|
ADD_TO_PARENT TorchMLIRPythonSources.Dialects
|
2021-09-21 04:55:36 +08:00
|
|
|
ROOT_DIR "${TORCH_MLIR_PYTHON_ROOT_DIR}"
|
2021-09-11 02:44:38 +08:00
|
|
|
TD_FILE dialects/TorchBinding.td
|
2021-09-21 04:55:36 +08:00
|
|
|
SOURCES dialects/torch/__init__.py
|
2021-09-11 02:44:38 +08:00
|
|
|
DIALECT_NAME torch
|
|
|
|
)
|
|
|
|
|
2022-09-21 00:59:54 +08:00
|
|
|
if(TORCH_MLIR_CUSTOM_OP_EXAMPLE)
|
|
|
|
declare_mlir_python_sources(TorchMLIRPythonSources.CustomOp
|
|
|
|
ROOT_DIR "${TORCH_MLIR_PYTHON_ROOT_DIR}"
|
|
|
|
ADD_TO_PARENT TorchMLIRPythonSources
|
|
|
|
SOURCES_GLOB
|
|
|
|
_torch_mlir_custom_op_example/__init__.py
|
|
|
|
)
|
|
|
|
endif()
|
|
|
|
|
2021-09-11 02:44:38 +08:00
|
|
|
################################################################################
|
|
|
|
# Extensions
|
|
|
|
################################################################################
|
|
|
|
|
|
|
|
declare_mlir_python_extension(TorchMLIRPythonExtensions.Main
|
|
|
|
MODULE_NAME _torchMlir
|
|
|
|
ADD_TO_PARENT TorchMLIRPythonExtensions
|
|
|
|
SOURCES
|
2021-10-28 08:04:12 +08:00
|
|
|
TorchMLIRModule.cpp
|
2021-09-11 02:44:38 +08:00
|
|
|
EMBED_CAPI_LINK_LIBS
|
|
|
|
TorchMLIRCAPI
|
|
|
|
PRIVATE_LINK_LIBS
|
|
|
|
LLVMSupport
|
|
|
|
)
|
|
|
|
|
2022-02-18 09:15:37 +08:00
|
|
|
################################################################################
|
|
|
|
# Lazy Tensor Core
|
|
|
|
################################################################################
|
|
|
|
|
2022-07-30 17:54:40 +08:00
|
|
|
if(TORCH_MLIR_ENABLE_LTC)
|
|
|
|
add_subdirectory(torch_mlir/csrc/base_lazy_backend)
|
|
|
|
endif()
|
2022-08-06 02:12:20 +08:00
|
|
|
# Reference backend has a separate check for TORCH_MLIR_ENABLE_LTC, since it
|
|
|
|
# generates a dummy Python library when disabled.
|
2022-07-13 03:56:52 +08:00
|
|
|
add_subdirectory(torch_mlir/csrc/reference_lazy_backend)
|
2022-02-18 09:15:37 +08:00
|
|
|
|
2021-09-21 04:55:36 +08:00
|
|
|
################################################################################
|
|
|
|
# Optionally handle JIT IR importer.
|
|
|
|
################################################################################
|
|
|
|
|
|
|
|
if(TORCH_MLIR_ENABLE_JIT_IR_IMPORTER)
|
|
|
|
add_subdirectory(torch_mlir/dialects/torch/importer/jit_ir)
|
2021-09-28 02:36:44 +08:00
|
|
|
add_subdirectory(torch_mlir_e2e_test)
|
2021-09-21 04:55:36 +08:00
|
|
|
endif()
|
|
|
|
|
2022-03-03 07:02:18 +08:00
|
|
|
################################################################################
|
|
|
|
# Eager mode
|
|
|
|
################################################################################
|
|
|
|
|
|
|
|
add_subdirectory(torch_mlir/eager_mode)
|
|
|
|
|
2022-06-14 05:51:30 +08:00
|
|
|
################################################################################
|
|
|
|
# Custom op example
|
|
|
|
# Required for running the update_torch_ods.sh and update_shape_lib.sh scripts.
|
|
|
|
################################################################################
|
|
|
|
|
2022-09-21 00:59:54 +08:00
|
|
|
if(TORCH_MLIR_CUSTOM_OP_EXAMPLE)
|
|
|
|
add_subdirectory(torch_mlir/_torch_mlir_custom_op_example)
|
|
|
|
endif()
|
2022-06-14 05:51:30 +08:00
|
|
|
|
2021-09-11 02:44:38 +08:00
|
|
|
################################################################################
|
|
|
|
# Generate packages and shared library
|
|
|
|
# Downstreams typically will not use these, but they are useful for local
|
|
|
|
# testing.
|
|
|
|
################################################################################
|
|
|
|
|
|
|
|
set(_source_components
|
2022-07-22 00:35:42 +08:00
|
|
|
# TODO: Core is now implicitly building/registering all dialects, increasing
|
|
|
|
# build burden by ~5x. Make it stop.
|
|
|
|
# TODO: Reduce dependencies. We need ExecutionEngine and a bunch of passes
|
|
|
|
# for the reference backend, but logically they can be separate. But seemingly
|
|
|
|
# the only way to handle that is to create a separate mlir python package
|
|
|
|
# tree, which seems excessive.
|
|
|
|
MLIRPythonSources
|
2021-09-23 00:55:09 +08:00
|
|
|
MLIRPythonExtension.Core
|
2022-07-22 00:35:42 +08:00
|
|
|
MLIRPythonExtension.RegisterEverything
|
2021-09-11 02:44:38 +08:00
|
|
|
TorchMLIRPythonSources
|
|
|
|
TorchMLIRPythonExtensions
|
|
|
|
)
|
|
|
|
|
|
|
|
add_mlir_python_common_capi_library(TorchMLIRAggregateCAPI
|
|
|
|
INSTALL_COMPONENT TorchMLIRPythonModules
|
2021-09-21 04:55:36 +08:00
|
|
|
INSTALL_DESTINATION python_packages/torch_mlir/torch_mlir/_mlir_libs
|
|
|
|
OUTPUT_DIRECTORY "${TORCH_MLIR_PYTHON_PACKAGES_DIR}/torch_mlir/torch_mlir/_mlir_libs"
|
2021-09-11 02:44:38 +08:00
|
|
|
RELATIVE_INSTALL_ROOT "../../../.."
|
|
|
|
DECLARED_SOURCES ${_source_components}
|
|
|
|
)
|
|
|
|
|
|
|
|
add_mlir_python_modules(TorchMLIRPythonModules
|
2021-09-21 04:55:36 +08:00
|
|
|
ROOT_PREFIX "${TORCH_MLIR_PYTHON_PACKAGES_DIR}/torch_mlir/torch_mlir"
|
|
|
|
INSTALL_PREFIX "python_packages/torch_mlir/torch_mlir"
|
2021-09-11 02:44:38 +08:00
|
|
|
DECLARED_SOURCES ${_source_components}
|
|
|
|
COMMON_CAPI_LINK_LIBS
|
|
|
|
TorchMLIRAggregateCAPI
|
|
|
|
)
|
2021-09-21 04:55:36 +08:00
|
|
|
|
|
|
|
# TODO: Find a cleaner way to do this.
|
|
|
|
# Can we build the JIT IR importer with `declare_mlir_python_extension`?
|
|
|
|
# Then it would "just work".
|
|
|
|
if(TORCH_MLIR_ENABLE_JIT_IR_IMPORTER)
|
|
|
|
add_dependencies(TorchMLIRPythonModules TorchMLIRJITIRImporter)
|
2022-08-20 03:25:00 +08:00
|
|
|
add_dependencies(TorchMLIRPythonModules TorchMLIRJITIRImporterPybind)
|
2021-09-28 02:36:44 +08:00
|
|
|
# Build the E2E Tests (which depend on the JIT IR importer now).
|
|
|
|
add_dependencies(TorchMLIRPythonModules TorchMLIRE2ETestPythonModules)
|
2021-09-21 04:55:36 +08:00
|
|
|
endif()
|
2021-09-28 02:36:44 +08:00
|
|
|
|
2022-07-30 17:54:40 +08:00
|
|
|
if(TORCH_MLIR_ENABLE_LTC)
|
|
|
|
# Add Torch-MLIR LTC backend as dependency
|
|
|
|
add_dependencies(TorchMLIRPythonModules torch_mlir_ltc_backend)
|
|
|
|
add_dependencies(TorchMLIRPythonModules reference_lazy_backend)
|
|
|
|
endif()
|
2022-02-18 09:15:37 +08:00
|
|
|
|
2022-09-21 00:59:54 +08:00
|
|
|
if(TORCH_MLIR_CUSTOM_OP_EXAMPLE)
|
|
|
|
add_dependencies(TorchMLIRPythonModules torch_mlir_custom_op_example)
|
|
|
|
endif()
|
|
|
|
|
2021-09-28 02:36:44 +08:00
|
|
|
add_subdirectory(test)
|