Skip to content

Commit d70c52b

Browse files
GregoryComerfacebook-github-bot
authored andcommitted
Refactor aot_util build (#2631)
Summary: Refactor aot_util build to be independent of runtime build, clean up build scripts. The intent of this change is to make the aot_util build independent of the runtime build, as well as to clean up the build scripts and setup.py logic. This change also makes aot_util build by default from setup.py, whereas it previously was tied to the EXECUTORCH_BUILD_PYBIND flag (which requires a full runtime build). Bypassing known failures in external CI. bypass-github-export-checks bypass-github-executorch-ci-checks Pull Request resolved: #2631 Test Plan: pip uninstall executorch BUCK=buck2-et2 ./install_requirements.sh Confirmed that libaot_util.so is present under site-packages/executorch/extension/aot_util/. Added logging when loading aot_util lib to log on successful invocation. Exported Stories110M and confirmed aotlib was loaded successfully. python -m examples.models.llama2.export_llama -c /mnt/c/Users/gregory/Downloads/Stories110M.pt -p /mnt/c/Users/gregory/Downloads/Stories110M_params.json -kv -qmode 8da4w -X -d fp32 Reviewed By: mcr229 Differential Revision: D55292709 Pulled By: GregoryComer fbshipit-source-id: 7f38cdfb0bcb326897a67c1fe99557ca3dc58a77
1 parent 67a7d20 commit d70c52b

File tree

7 files changed

+87
-30
lines changed

7 files changed

+87
-30
lines changed

CMakeLists.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ option(EXECUTORCH_BUILD_ARM_BAREMETAL
140140

141141
option(EXECUTORCH_BUILD_COREML "Build the Core ML backend" OFF)
142142

143-
option(EXECUTORCH_BUILD_EXTENSION_AOT_UTIL "Build the AOT Util extension" OFF)
143+
option(EXECUTORCH_BUILD_EXTENSION_AOT_UTIL "Build the AOT util library" OFF)
144144

145145
option(EXECUTORCH_BUILD_EXTENSION_DATA_LOADER "Build the Data Loader extension"
146146
OFF)
@@ -362,6 +362,10 @@ if(EXECUTORCH_BUILD_EXECUTOR_RUNNER)
362362
target_compile_options(executor_runner PUBLIC ${_common_compile_options})
363363
endif()
364364

365+
if(EXECUTORCH_BUILD_EXTENSION_AOT_UTIL)
366+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/aot_util)
367+
endif()
368+
365369
# Add googletest if any test targets should be built
366370
if(EXECUTORCH_BUILD_GTESTS)
367371
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/third-party/googletest)
@@ -390,10 +394,6 @@ if(EXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL)
390394
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/runner_util)
391395
endif()
392396

393-
if(EXECUTORCH_BUILD_EXTENSION_AOT_UTIL)
394-
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/aot_util)
395-
endif()
396-
397397
if(EXECUTORCH_BUILD_XNNPACK)
398398
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/xnnpack)
399399
endif()
Submodule serialization_lib updated from 187af0d to bd8c529

backends/xnnpack/operators/node_visitor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,8 @@ def define_tensor(
451451

452452
@staticmethod
453453
def find_aot_util_path() -> str:
454-
# Look for .so installed by wheel (OSS).
455-
rel_path = "executorch/extension/pybindings/libaot_util.so"
454+
# Look for .so installed by wheel (OSS). TODO(gjcomer) Improve this.
455+
rel_path = "executorch/extension/aot_util/libaot_util.so"
456456
for sys_path in sys.path:
457457
so_path = Path(sys_path) / rel_path
458458
if so_path.exists():

build/cmake_deps.toml

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@ filters = [
8888
# ---------------------------------- core end ----------------------------------
8989
# ---------------------------------- extension start ----------------------------------
9090

91+
[targets.extension_aot_util]
92+
buck_targets = [
93+
"//extension/aot_util:aot_util",
94+
]
95+
filters = [
96+
".cpp$",
97+
]
98+
deps = [
99+
"executorch",
100+
]
91101
[targets.extension_data_loader]
92102
buck_targets = [
93103
"//extension/data_loader:buffer_data_loader",
@@ -125,17 +135,6 @@ deps = [
125135
"executorch",
126136
]
127137

128-
[targets.extension_aot_util]
129-
buck_targets = [
130-
"//extension/aot_util:aot_util",
131-
]
132-
filters = [
133-
".cpp$",
134-
]
135-
deps = [
136-
"executorch",
137-
]
138-
139138
# ---------------------------------- extension end ----------------------------------
140139
# ---------------------------------- binary start ----------------------------------
141140

extension/aot_util/CMakeLists.txt

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,60 @@
99
# cmake-format --first-comment-is-literal=True CMakeLists.txt
1010
# ~~~
1111

12+
cmake_minimum_required(VERSION 3.19)
13+
project(aot_util)
14+
include(../../build/Utils.cmake)
15+
16+
if(NOT CMAKE_CXX_STANDARD)
17+
set(CMAKE_CXX_STANDARD 17)
18+
endif()
19+
20+
if(NOT CMAKE_BUILD_TYPE)
21+
set(CMAKE_BUILD_TYPE Debug)
22+
endif()
23+
24+
if(NOT EXECUTORCH_ROOT)
25+
set(EXECUTORCH_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../../")
26+
endif()
27+
28+
if(NOT BUCK2)
29+
set(BUCK2 buck2)
30+
endif()
31+
32+
if(NOT PYTHON_EXECUTABLE)
33+
set(PYTHON_EXECUTABLE python3)
34+
endif()
35+
36+
if(NOT EXECUTORCH_SRCS_FILE)
37+
# A file wasn't provided. Run a script to extract the source lists from the
38+
# buck2 build system and write them to a file we can include.
39+
#
40+
# NOTE: This will only happen once during cmake setup, so it will not re-run
41+
# if the buck2 targets change.
42+
message(STATUS "executorch: Generating source lists")
43+
set(EXECUTORCH_SRCS_FILE "${CMAKE_CURRENT_BINARY_DIR}/executorch_srcs.cmake")
44+
extract_sources(${EXECUTORCH_SRCS_FILE})
45+
endif()
46+
47+
# This file defines the `_<target>__srcs` variables used below.
48+
message(STATUS "executorch: Using sources file ${EXECUTORCH_SRCS_FILE}")
49+
include(${EXECUTORCH_SRCS_FILE})
50+
51+
52+
# Ahead-of-time (AOT) utility library. Contains native code used by the
53+
# AOT lowering and delegation logic. Note that this library should build
54+
# independently of the runtime code, and as such, should not have
55+
# dependencies on runtime targets.
1256
find_package(Torch CONFIG REQUIRED)
1357
find_library(TORCH_PYTHON_LIBRARY torch_python
1458
PATHS "${TORCH_INSTALL_PREFIX}/lib")
1559

16-
# Override compiler flags set in upper scope. ExecuTorch builds with
17-
# -fno-exceptions and -fno-rtti, but we need these for ATen.
18-
set(CMAKE_CXX_FLAGS_RELEASE "")
60+
# Override compiler flags set in upper scope when included from the top-level
61+
# CMakeLists. ExecuTorch builds with -fno-exceptions and -fno-rtti, but we
62+
# need these for ATen.
63+
unset(CMAKE_CXX_FLAGS_RELEASE)
1964

20-
#list(TRANSFORM _xnnpack_convert_to_qc4w__srcs PREPEND "${EXECUTORCH_ROOT}/")
21-
# add_library(aot_util ${_xnnpack_convert_to_qc4w__srcs})
22-
add_library(aot_util "convert_to_qc4w.cpp")
65+
list(TRANSFORM _extension_aot_util__srcs PREPEND "${EXECUTORCH_ROOT}/")
66+
add_library(aot_util ${_extension_aot_util__srcs})
2367
target_include_directories(aot_util PUBLIC ${TORCH_INCLUDE_DIRS})
2468
target_link_libraries(aot_util torch)

extension/aot_util/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# AOT Util
2+
3+
Ahead-of-time (AOT) utility library. Contains native code used by the AOT lowering and delegation logic. Note
4+
that this library should build independently of the runtime code, and as such, should not have dependencies
5+
on runtime targets.
6+
7+
This library is intended to be built and distributed as part of the Python pip package, such that it can be
8+
loaded by AOT Python code.
9+

setup.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,10 @@ def build_extension(self, ext: CMakeExtension) -> None:
9696
# Set Python_EXECUTABLE instead if you use PYBIND11_FINDPYTHON
9797
# EXAMPLE_VERSION_INFO shows you how to pass a value into the C++ code
9898
# from Python.
99-
buck = os.environ.get("BUCK", "buck2")
99+
buck = os.environ.get("BUCK", "")
100100
cmake_prefix_path = os.environ.get("CMAKE_PREFIX_PATH", get_python_lib())
101101
cmake_args = [
102102
"-DEXECUTORCH_BUILD_PYBIND=ON",
103-
"-DEXECUTORCH_BUILD_EXTENSION_AOT_UTIL=ON",
104103
"-DBUILD_SHARED_LIBS=ON", # For flatcc
105104
f"-DBUCK2={buck}",
106105
f"-DCMAKE_PREFIX_PATH={cmake_prefix_path}",
@@ -225,14 +224,20 @@ def run(self):
225224

226225

227226
cmdclass = {
227+
"build_ext": CMakeBuild,
228228
"install": CustomInstallCommand,
229229
"develop": CustomDevelopCommand,
230230
"egg_info": CustomEggInfoCommand,
231231
}
232-
ext_modules = None
232+
ext_modules = []
233+
234+
if os.environ.get("EXECUTORCH_BUILD_AOT_UTIL", "ON") == "ON":
235+
ext_modules.append(
236+
CMakeExtension("executorch.extension.aot_util.aot_util", "extension/aot_util")
237+
)
238+
233239
if os.environ.get("EXECUTORCH_BUILD_PYBIND", "OFF") == "ON":
234-
cmdclass["build_ext"] = CMakeBuild
235-
ext_modules = [CMakeExtension("executorch.extension.pybindings.portable_lib")]
240+
ext_modules.append(CMakeExtension("executorch.extension.pybindings.portable_lib"))
236241

237242
setup(
238243
package_dir={

0 commit comments

Comments
 (0)