Skip to content

Commit 6961f0a

Browse files
Re-organize project structure to separate PyTorch dependencies from core project. (#2542)
This is a first step towards the structure we discussed here: https://gist.github.com/stellaraccident/931b068aaf7fa56f34069426740ebf20 There are two primary goals: 1. Separate the core project (C++ dialects and conversions) from the hard PyTorch dependencies. We move all such things into projects/pt1 as a starting point since they are presently entangled with PT1-era APIs. Additional work can be done to disentangle components from that (specifically LTC is identified as likely ultimately living in a `projects/ltc`). 2. Create space for native PyTorch2 Dynamo-based infra to be upstreamed without needing to co-exist with the original TorchScript path. Very little changes in this path with respect to build layering or options. These can be updated in a followup without commingling directory structure changes. This also takes steps toward a couple of other layering enhancements: * Removes the llvm-external-projects/torch-mlir-dialects sub-project, collapsing it into the main tree. * Audits and fixes up the core C++ build to account for issues found while moving things. This is just an opportunistic pass through but roughly ~halves the number of build actions for the project from the high 4000's to the low 2000's. It deviates from the discussed plan by having a `projects/` tree instead of `compat/`. As I was thinking about it, this will better accommodate the follow-on code movement. Once things are roughly in place and the CI passing, followups will focus on more in-situ fixes and cleanups.
1 parent 536e45c commit 6961f0a

File tree

316 files changed

+249
-372
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

316 files changed

+249
-372
lines changed

.github/workflows/buildAndTest.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,8 @@ jobs:
125125
-DCMAKE_OSX_ARCHITECTURES=arm64 \
126126
-DLLVM_ENABLE_ASSERTIONS=ON \
127127
-DLLVM_ENABLE_PROJECTS=mlir \
128-
-DLLVM_EXTERNAL_PROJECTS="torch-mlir;torch-mlir-dialects" \
128+
-DLLVM_EXTERNAL_PROJECTS="torch-mlir" \
129129
-DLLVM_EXTERNAL_TORCH_MLIR_SOURCE_DIR="$GITHUB_WORKSPACE" \
130-
-DLLVM_EXTERNAL_TORCH_MLIR_DIALECTS_SOURCE_DIR="${GITHUB_WORKSPACE}/externals/llvm-external-projects/torch-mlir-dialects" \
131130
-DLLVM_TARGETS_TO_BUILD=AArch64 \
132131
-DLLVM_USE_HOST_TOOLS=ON \
133132
-DLLVM_ENABLE_ZSTD=OFF \

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ __pycache__
2626
bazel-*
2727

2828
# Autogenerated files
29-
/python/torch_mlir/csrc/base_lazy_backend/generated
29+
/projects/pt1/python/torch_mlir/csrc/base_lazy_backend/generated
3030

3131
#Docker builds
3232
build_oot/

CMakeLists.txt

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#-------------------------------------------------------------------------------
2+
# Project setup and globals
3+
#-------------------------------------------------------------------------------
4+
15
cmake_minimum_required(VERSION 3.12)
26

37
if(POLICY CMP0068)
@@ -17,52 +21,48 @@ if(POLICY CMP0116)
1721
cmake_policy(SET CMP0116 OLD)
1822
endif()
1923

20-
#-------------------------------------------------------------------------------
21-
# Project setup and globals
22-
#-------------------------------------------------------------------------------
23-
2424
project(torch-mlir LANGUAGES CXX C)
2525
set(CMAKE_C_STANDARD 11)
2626
set(CMAKE_CXX_STANDARD 17)
2727

28-
macro(torch_mlir_add_llvm_external_project name identifier location)
29-
message(STATUS "Adding LLVM external project ${name} (${identifier}) -> ${location}")
30-
if(NOT EXISTS "${location}/CMakeLists.txt")
31-
message(FATAL_ERROR "External project location ${location} is not valid")
32-
endif()
33-
list(APPEND LLVM_EXTERNAL_PROJECTS ${name})
34-
list(REMOVE_DUPLICATES LLVM_EXTERNAL_PROJECTS)
35-
set(LLVM_EXTERNAL_${identifier}_SOURCE_DIR ${location} CACHE STRING "" FORCE)
36-
set(LLVM_EXTERNAL_PROJECTS ${LLVM_EXTERNAL_PROJECTS} CACHE STRING "" FORCE)
37-
endmacro()
28+
#-------------------------------------------------------------------------------
29+
# Project options
30+
#-------------------------------------------------------------------------------
31+
32+
option(TORCH_MLIR_ENABLE_REFBACKEND "Enable reference backend" ON)
33+
if(TORCH_MLIR_ENABLE_REFBACKEND)
34+
add_definitions(-DTORCH_MLIR_ENABLE_REFBACKEND)
35+
endif()
3836

3937
option(TORCH_MLIR_ENABLE_STABLEHLO "Add stablehlo dialect" ON)
4038
if(TORCH_MLIR_ENABLE_STABLEHLO)
4139
add_definitions(-DTORCH_MLIR_ENABLE_STABLEHLO)
4240
endif()
4341

42+
option(TORCH_MLIR_OUT_OF_TREE_BUILD "Specifies an out of tree build" OFF)
43+
44+
# PT1 options.
45+
option(TORCH_MLIR_ENABLE_PROJECT_PT1 "Enables the PyTorch1 project under projects/pt1" OFF)
46+
# TODO: Rename/scope these. They use historic names for now to ease migration
47+
# burden.
4448
option(TORCH_MLIR_ENABLE_JIT_IR_IMPORTER "Enables JIT IR Importer" ON)
4549
option(TORCH_MLIR_ENABLE_LTC "Enables LTC backend" OFF)
4650
option(TORCH_MLIR_ENABLE_ONLY_MLIR_PYTHON_BINDINGS "Build Torch dialect MLIR Python bindings but neither JIT IR Importer nor LTC backend" OFF)
4751
if(TORCH_MLIR_ENABLE_ONLY_MLIR_PYTHON_BINDINGS)
4852
set(TORCH_MLIR_ENABLE_JIT_IR_IMPORTER OFF)
4953
set(TORCH_MLIR_ENABLE_LTC OFF)
5054
endif()
51-
52-
if(TORCH_MLIR_ENABLE_LTC)
53-
set(ENV{TORCH_MLIR_ENABLE_LTC} 1)
54-
message(STATUS "LTC Backend build is enabled")
55-
else()
56-
set(ENV{TORCH_MLIR_ENABLE_LTC} 0)
57-
message(STATUS "LTC Backend build is disabled")
55+
# Force enable the PT1 project if either the JIT_IR_IMPORTER or LTC is enabled.
56+
if(NOT TORCH_MLIR_ENABLE_PROJECT_PT1)
57+
if(TORCH_MLIR_ENABLE_JIT_IR_IMPORTER OR TORCH_MLIR_ENABLE_LTC)
58+
message(STATUS "Enabling projects/pt1 because features requiring it are enabled")
59+
set(TORCH_MLIR_ENABLE_PROJECT_PT1 ON)
60+
endif()
5861
endif()
5962

60-
torch_mlir_add_llvm_external_project(
61-
torch-mlir-dialects
62-
TORCH_MLIR_DIALECTS
63-
${CMAKE_CURRENT_SOURCE_DIR}/externals/llvm-external-projects/torch-mlir-dialects)
64-
65-
option(TORCH_MLIR_OUT_OF_TREE_BUILD "Specifies an out of tree build" OFF)
63+
#-------------------------------------------------------------------------------
64+
# Configure out-of-tree vs in-tree build
65+
#-------------------------------------------------------------------------------
6666

6767
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR OR TORCH_MLIR_OUT_OF_TREE_BUILD)
6868
message(STATUS "Torch-MLIR out-of-tree build.")
@@ -99,11 +99,10 @@ if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR OR TORCH_MLIR_OUT_OF_TREE_
9999

100100
# Don't try to compile the python extensions at the moment. We need
101101
# to import lots of dependencies from AddMLIRPython to make this work.
102-
set(MLIR_ENABLE_BINDINGS_PYTHON 1)
102+
set(MLIR_ENABLE_BINDINGS_PYTHON ON)
103103

104-
set(TORCH-MLIR_BUILT_STANDALONE 1)
104+
set(TORCH-MLIR_BUILT_STANDALONE ON)
105105
set(BACKEND_PACKAGE_STRING "LLVM ${LLVM_PACKAGE_VERSION}")
106-
add_subdirectory(externals/llvm-external-projects/torch-mlir-dialects)
107106
else()
108107
message(STATUS "Torch-MLIR in-tree build.")
109108
# In-tree build with LLVM_EXTERNAL_PROJECTS=torch-mlir
@@ -169,24 +168,14 @@ add_subdirectory(lib)
169168
add_subdirectory(tools)
170169

171170
add_custom_target(check-torch-mlir-all)
172-
add_dependencies(check-torch-mlir-all
173-
check-torch-mlir
174-
check-torch-mlir-dialects
175-
check-torch-mlir-capi
176-
)
171+
add_dependencies(check-torch-mlir-all check-torch-mlir)
177172

178173
if(MLIR_ENABLE_BINDINGS_PYTHON)
179174
# If parent projects want to configure where to place the python packages,
180175
# respect that.
181176
if(NOT TORCH_MLIR_PYTHON_PACKAGES_DIR)
182177
set(TORCH_MLIR_PYTHON_PACKAGES_DIR "${CMAKE_CURRENT_BINARY_DIR}/python_packages")
183178
endif()
184-
add_dependencies(check-torch-mlir-all
185-
check-torch-mlir-python
186-
)
187-
add_subdirectory(python)
188-
else()
189-
add_custom_target(TorchMLIRPythonModules)
190179
endif()
191180

192181
add_subdirectory(test)
@@ -237,3 +226,11 @@ if (TORCH_MLIR_ENABLE_STABLEHLO)
237226
EXCLUDE_FROM_ALL)
238227
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/externals/stablehlo)
239228
endif()
229+
230+
#-------------------------------------------------------------------------------
231+
# Sub-projects
232+
#-------------------------------------------------------------------------------
233+
234+
if(TORCH_MLIR_ENABLE_PROJECT_PT1)
235+
add_subdirectory(projects/pt1)
236+
endif()

build_tools/autogen_ltc_backend.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
TORCH_INCLUDE_DIR = TORCH_DIR
3030
TORCHGEN_DIR = Path(torchgen.__path__[0]).resolve()
3131
TORCH_MLIR_DIR = Path(__file__).resolve().parent.parent
32-
32+
TORCH_MLIR_PT1_DIR = TORCH_MLIR_DIR / "projects" / "pt1"
3333

3434
def reindent(text, prefix=""):
3535
return indent(dedent(text), prefix)
@@ -114,12 +114,12 @@ def __init__(self, binary_dir):
114114
self.binary_dir = Path(binary_dir)
115115
assert self.binary_dir.is_dir(), f"Binary directory not found: {self.binary_dir}"
116116
self.source_yaml = self.binary_dir.joinpath("generated_native_functions.yaml")
117-
self.backend_path = TORCH_MLIR_DIR.joinpath(
117+
self.backend_path = TORCH_MLIR_PT1_DIR.joinpath(
118118
"python", "torch_mlir", "csrc", "base_lazy_backend"
119119
)
120-
assert self.backend_path.is_dir()
120+
assert self.backend_path.is_dir(), f"Backend path not found: {self.backend_path}"
121121
self.generated_path = self.binary_dir.joinpath(
122-
"python", "torch_mlir", "csrc", "base_lazy_backend", "generated"
122+
"projects", "pt1", "python", "torch_mlir", "csrc", "base_lazy_backend", "generated"
123123
)
124124
self.generated_path.mkdir(parents=True, exist_ok=True)
125125

build_tools/build_standalone.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ cmake -GNinja -B"$build_dir" "$llvm_project_dir/llvm" \
1919
-DCMAKE_BUILD_TYPE=Release \
2020
-DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache \
2121
-DLLVM_ENABLE_PROJECTS=mlir \
22-
-DLLVM_EXTERNAL_PROJECTS="torch-mlir;torch-mlir-dialects" \
22+
-DLLVM_EXTERNAL_PROJECTS="torch-mlir" \
2323
-DLLVM_EXTERNAL_TORCH_MLIR_SOURCE_DIR="$project_dir" \
24-
-DLLVM_EXTERNAL_TORCH_MLIR_DIALECTS_SOURCE_DIR="${project_dir}"/externals/llvm-external-projects/torch-mlir-dialects \
2524
-DMLIR_ENABLE_BINDINGS_PYTHON=ON \
2625
-DLLVM_ENABLE_ASSERTIONS=ON \
2726
-DLLVM_TARGETS_TO_BUILD=host

build_tools/python_deploy/build_linux_packages.sh

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,8 @@ function build_in_tree() {
234234
-DCMAKE_C_COMPILER_LAUNCHER=ccache \
235235
-DCMAKE_CXX_COMPILER_LAUNCHER=ccache \
236236
-DLLVM_ENABLE_PROJECTS=mlir \
237-
-DLLVM_EXTERNAL_PROJECTS="torch-mlir;torch-mlir-dialects" \
237+
-DLLVM_EXTERNAL_PROJECTS="torch-mlir" \
238238
-DLLVM_EXTERNAL_TORCH_MLIR_SOURCE_DIR="/main_checkout/torch-mlir" \
239-
-DLLVM_EXTERNAL_TORCH_MLIR_DIALECTS_SOURCE_DIR="/main_checkout/torch-mlir/externals/llvm-external-projects/torch-mlir-dialects" \
240239
-DLLVM_TARGETS_TO_BUILD=host \
241240
-DMLIR_ENABLE_BINDINGS_PYTHON=ON \
242241
-DTORCH_MLIR_ENABLE_LTC=${enable_ltc} \
@@ -246,7 +245,7 @@ function build_in_tree() {
246245
-DTM_PYTORCH_INSTALL_WITHOUT_REBUILD=${TM_PYTORCH_INSTALL_WITHOUT_REBUILD} \
247246
-DPython3_EXECUTABLE="$(which python3)" \
248247
/main_checkout/torch-mlir/externals/llvm-project/llvm
249-
cmake --build /main_checkout/torch-mlir/build
248+
cmake --build /main_checkout/torch-mlir/build --target tools/torch-mlir/all
250249
ccache -s
251250
}
252251

@@ -288,7 +287,7 @@ function test_in_tree() {
288287
cmake --build /main_checkout/torch-mlir/build --target check-torch-mlir-all
289288

290289
cd /main_checkout/torch-mlir/
291-
export PYTHONPATH="/main_checkout/torch-mlir/build/tools/torch-mlir/python_packages/torch_mlir"
290+
export PYTHONPATH="/main_checkout/torch-mlir/build/tools/torch-mlir/python_packages/torch_mlir:/main_checkout/torch-mlir/projects/pt1"
292291

293292
case $torch_version in
294293
nightly)

build_tools/python_deploy/build_windows_ci.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ cmake -GNinja -Bbuild \
1111
-DLLVM_TARGETS_TO_BUILD=host \
1212
-DMLIR_ENABLE_BINDINGS_PYTHON=ON \
1313
-DPython3_FIND_VIRTUALENV=ONLY \
14-
-DLLVM_EXTERNAL_PROJECTS="torch-mlir;torch-mlir-dialects" \
14+
-DLLVM_EXTERNAL_PROJECTS="torch-mlir" \
1515
-DLLVM_EXTERNAL_TORCH_MLIR_SOURCE_DIR="$PWD" \
16-
-DLLVM_EXTERNAL_TORCH_MLIR_DIALECTS_SOURCE_DIR="$PWD/externals/llvm-external-projects/torch-mlir-dialects" \
1716
-DPython3_EXECUTABLE="$(which python)" \
1817
$GITHUB_WORKSPACE/externals/llvm-project/llvm
1918

docs/development.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ cmake -GNinja -Bbuild \
3535
-DCMAKE_BUILD_TYPE=Release \
3636
-DPython3_FIND_VIRTUALENV=ONLY \
3737
-DLLVM_ENABLE_PROJECTS=mlir \
38-
-DLLVM_EXTERNAL_PROJECTS="torch-mlir;torch-mlir-dialects" \
38+
-DLLVM_EXTERNAL_PROJECTS="torch-mlir" \
3939
-DLLVM_EXTERNAL_TORCH_MLIR_SOURCE_DIR="$PWD" \
40-
-DLLVM_EXTERNAL_TORCH_MLIR_DIALECTS_SOURCE_DIR="$PWD"/externals/llvm-external-projects/torch-mlir-dialects \
4140
-DMLIR_ENABLE_BINDINGS_PYTHON=ON \
4241
-DLLVM_TARGETS_TO_BUILD=host \
4342
externals/llvm-project/llvm
@@ -360,6 +359,7 @@ Torch-MLIR has two types of tests:
360359
Alternatively, you can run the tests via Python directly:
361360

362361
```shell
362+
cd projects/pt1
363363
python -m e2e_testing.main -f 'AtenEmbeddingBag'
364364
```
365365

@@ -374,7 +374,7 @@ ninja check-torch-mlir-all
374374
This can be broken down into
375375

376376
```
377-
ninja check-torch-mlir check-torch-mlir-dialects check-torch-mlir-python
377+
ninja check-torch-mlir check-torch-mlir-python
378378
```
379379

380380
To run more fine-grained tests, you can do, for `check-torch-mlir`:

externals/llvm-external-projects/torch-mlir-dialects/CMakeLists.txt

Lines changed: 0 additions & 64 deletions
This file was deleted.

externals/llvm-external-projects/torch-mlir-dialects/README.md

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)