Skip to content

Commit 0f76731

Browse files
author
Github Executorch
committed
Use c10 version of half/bfloat16 in executorch
Pull Request resolved: #7040 Pull Request resolved: pytorch/pytorch#144111 Accomplished by importing relevant files from c10 into executorch/runtime/core/portable_type/c10, and then using `using` in the top-level ExecuTorch headers. This approach should keep the ExecuTorch build hermetic for embedded use cases. In the future, we should add a CI job to ensure the c10 files stay identical to the PyTorch ones. ghstack-source-id: 265190624 @exported-using-ghexport Differential Revision: [D66106969](https://our.internmc.facebook.com/intern/diff/D66106969/)
1 parent 832f855 commit 0f76731

File tree

29 files changed

+2629
-1365
lines changed

29 files changed

+2629
-1365
lines changed

.lintrunner.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ exclude_patterns = [
7878
# File contains @generated
7979
'extension/llm/custom_ops/spinquant/fast_hadamard_transform_special.h',
8080
'extension/llm/custom_ops/spinquant/test/fast_hadamard_transform_special_unstrided_cpu.h',
81+
# Want to be able to keep c10 in sync with PyTorch core.
82+
'runtime/core/portable_type/c10/**',
8183
]
8284
command = [
8385
'python',
@@ -261,6 +263,8 @@ exclude_patterns = [
261263
'extension/**',
262264
'kernels/optimized/**',
263265
'runtime/core/exec_aten/**',
266+
# Want to be able to keep c10 in sync with PyTorch core.
267+
'runtime/core/portable_type/c10/**',
264268
'runtime/executor/tensor_parser_aten.cpp',
265269
'scripts/**',
266270
'test/**',

CMakeLists.txt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ if(NOT "${_repo_dir_name}" STREQUAL "executorch")
366366
"fix for this restriction."
367367
)
368368
endif()
369-
set(_common_include_directories ${CMAKE_CURRENT_SOURCE_DIR}/..)
369+
set(_common_include_directories ${CMAKE_CURRENT_SOURCE_DIR}/.. ${CMAKE_CURRENT_SOURCE_DIR}/runtime/core/portable_type)
370370

371371
#
372372
# The `_<target>_srcs` lists are defined by including ${EXECUTORCH_SRCS_FILE}.
@@ -549,6 +549,7 @@ endif()
549549
target_include_directories(
550550
executorch_core PUBLIC ${_common_include_directories}
551551
)
552+
target_compile_definitions(executorch_core PUBLIC C10_USING_CUSTOM_GENERATED_MACROS)
552553
target_compile_options(executorch_core PUBLIC ${_common_compile_options})
553554
if(MAX_KERNEL_NUM)
554555
target_compile_definitions(
@@ -569,6 +570,7 @@ if(EXECUTORCH_BUILD_PYBIND AND APPLE)
569570
target_include_directories(
570571
executorch_core_shared PUBLIC ${_common_include_directories}
571572
)
573+
target_compile_definitions(executorch_core_shared PUBLIC C10_USING_CUSTOM_GENERATED_MACROS)
572574
target_compile_options(
573575
executorch_core_shared PUBLIC ${_common_compile_options}
574576
)
@@ -589,6 +591,7 @@ endif()
589591
add_library(executorch ${_executorch__srcs})
590592
target_link_libraries(executorch PRIVATE executorch_core)
591593
target_include_directories(executorch PUBLIC ${_common_include_directories})
594+
target_compile_definitions(executorch PUBLIC C10_USING_CUSTOM_GENERATED_MACROS)
592595
target_compile_options(executorch PUBLIC ${_common_compile_options})
593596
target_link_options_shared_lib(executorch)
594597

@@ -622,6 +625,12 @@ endif()
622625

623626
# Install `executorch` library as well as `executorch-config.cmake` under
624627
# ${CMAKE_INSTALL_PREFIX}/
628+
install(DIRECTORY runtime/core/ DESTINATION include/executorch/runtime/core FILES_MATCHING PATTERN "*.h")
629+
install(DIRECTORY runtime/kernel/ DESTINATION include/executorch/runtime/kernel FILES_MATCHING PATTERN "*.h")
630+
install(DIRECTORY runtime/platform/ DESTINATION include/executorch/runtime/platform FILES_MATCHING PATTERN "*.h")
631+
install(DIRECTORY extension/kernel_util/ DESTINATION include/executorch/extension/kernel_util FILES_MATCHING PATTERN "*.h")
632+
install(DIRECTORY extension/tensor/ DESTINATION include/executorch/extension/tensor FILES_MATCHING PATTERN "*.h")
633+
install(DIRECTORY extension/threadpool/ DESTINATION include/executorch/extension/threadpool FILES_MATCHING PATTERN "*.h")
625634
install(
626635
TARGETS executorch executorch_core
627636
DESTINATION lib
@@ -780,6 +789,8 @@ if(EXECUTORCH_BUILD_PYBIND)
780789
target_include_directories(
781790
util PUBLIC ${_common_include_directories} ${TORCH_INCLUDE_DIRS}
782791
)
792+
target_compile_definitions(util PUBLIC C10_USING_CUSTOM_GENERATED_MACROS)
793+
783794
target_compile_options(util PUBLIC ${_pybind_compile_options})
784795
target_link_libraries(util PRIVATE torch c10 executorch extension_tensor)
785796

backends/apple/coreml/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ target_include_directories(
134134
coremldelegate PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/runtime/util
135135
)
136136
target_include_directories(coremldelegate PRIVATE ${EXECUTORCH_ROOT}/..)
137+
target_include_directories(coremldelegate PRIVATE ${EXECUTORCH_ROOT}/runtime/core/portable_type)
138+
target_compile_definitions(coremldelegate PRIVATE C10_USING_CUSTOM_GENERATED_MACROS)
137139
target_link_libraries(coremldelegate PRIVATE executorch_core)
138140

139141
if(EXECUTORCH_BUILD_DEVTOOLS)

backends/apple/coreml/runtime/workspace/executorchcoreml.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,7 @@
830830
GCC_OPTIMIZATION_LEVEL = 0;
831831
GCC_PREPROCESSOR_DEFINITIONS = (
832832
"DEBUG=1",
833+
"C10_USING_CUSTOM_GENERATED_MACROS",
833834
"$(inherited)",
834835
);
835836
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
@@ -911,6 +912,7 @@
911912
DEVELOPMENT_TEAM = "";
912913
GCC_PREPROCESSOR_DEFINITIONS = (
913914
"DEBUG=1",
915+
"C10_USING_CUSTOM_GENERATED_MACROS",
914916
"ET_EVENT_TRACER_ENABLED=1",
915917
"$(inherited)",
916918
);
@@ -920,6 +922,7 @@
920922
"$(SRCROOT)/../kvstore",
921923
"$(SRCROOT)/../inmemoryfs",
922924
"$(SRCROOT)/../include",
925+
"$(SRCROOT)/../include/executorch/runtime/core/portable_type",
923926
"$(SRCROOT)/../sdk",
924927
"$(SRCROOT)/../util",
925928
"$(SRCROOT)/../../third-party/nlohmann_json/single_include",
@@ -951,6 +954,7 @@
951954
"$(SRCROOT)/../kvstore",
952955
"$(SRCROOT)/../inmemoryfs",
953956
"$(SRCROOT)/../include",
957+
"$(SRCROOT)/../include/executorch/runtime/core/portable_type",
954958
"$(SRCROOT)/../sdk",
955959
"$(SRCROOT)/../util",
956960
"$(SRCROOT)/../../third-party/nlohmann_json/single_include",

backends/arm/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ endif()
1414

1515
include(${EXECUTORCH_ROOT}/build/Utils.cmake)
1616

17-
set(_common_include_directories ${EXECUTORCH_ROOT}/..)
17+
set(_common_include_directories ${EXECUTORCH_ROOT}/.. ${EXECUTORCH_ROOT}/runtime/core/portable_type)
18+
add_compile_definitions(C10_USING_CUSTOM_GENERATED_MACROS)
1819

1920
# Third-party folder and Ethos-U driver inclued
2021
set(THIRD_PARTY_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/third-party")

backends/qualcomm/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ add_custom_command(
5454
)
5555

5656
add_compile_options("-Wall" "-Werror" "-Wno-sign-compare")
57+
add_compile_definitions(C10_USING_CUSTOM_GENERATED_MACROS)
5758

5859
# GNU emit wanring for ignored attributes Unfortunately, we use [[maybe_unused]]
5960
# which can be ignored by GNU. So we make it a warning, not an error in GNU.
@@ -73,6 +74,7 @@ endif()
7374
include_directories(
7475
BEFORE ${_common_include_directories} ${QNN_SDK_ROOT}/include/QNN
7576
${EXECUTORCH_SOURCE_DIR}/third-party/flatbuffers/include
77+
${EXECUTORCH_SOURCE_DIR}/runtime/core/portable_type
7678
)
7779

7880
set(_qnn_schema__srcs

backends/xnnpack/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ target_include_directories(
119119
target_compile_options(xnnpack_backend PUBLIC ${_common_compile_options})
120120
target_link_options_shared_lib(xnnpack_backend)
121121

122-
list(APPEND xnn_executor_runner_libs xnnpack_backend)
122+
list(APPEND xnn_executor_runner_libs xnnpack_backend executorch)
123123

124124
# ios can only build library but not binary
125125
if(NOT CMAKE_TOOLCHAIN_FILE MATCHES ".*(iOS|ios\.toolchain)\.cmake$")

build/build_apple_frameworks.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,20 @@ check_command "$BUCK2"
200200
//extension/tensor: \
201201
| rsync -av --files-from=- "$SOURCE_ROOT_DIR" "$HEADERS_PATH/executorch"
202202

203+
# HACK: XCFrameworks don't appear to support exporting any build
204+
# options, but we need the following:
205+
# - runtime/core/portable/type/c10 reachable with `#include <c10/...>`
206+
# - exported -DC10_USING_CUSTOM_GENERATED_MACROS compiler flag
207+
# So, just patch our generated framework to do that.
208+
sed -i '' '1i\
209+
#define C10_USING_CUSTOM_GENERATED_MACROS
210+
' $HEADERS_PATH/executorch/runtime/core/portable_type/c10/macros/Macros.h
211+
sed -i '' '1i\
212+
#define C10_USING_CUSTOM_GENERATED_MACROS
213+
' $HEADERS_PATH/executorch/runtime/core/portable_type/c10/macros/Export.h
214+
ln -s $HEADERS_PATH/executorch/runtime/core/portable_type/c10 "$HEADERS_PATH/"
215+
216+
203217
cp "$SOURCE_ROOT_DIR/extension/apple/ExecuTorch/Exported/"*.h "$HEADERS_PATH/executorch"
204218
cp "$SOURCE_ROOT_DIR/extension/apple/ExecuTorch/Exported/"*.modulemap "$HEADERS_PATH"
205219

build/executorch-config.cmake

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,21 @@
2626

2727
cmake_minimum_required(VERSION 3.19)
2828

29-
set(_root "${CMAKE_CURRENT_LIST_DIR}/../..")
29+
set(_root "${CMAKE_CURRENT_LIST_DIR}/../../..")
3030
set(required_lib_list executorch executorch_core portable_kernels)
3131
set(EXECUTORCH_LIBRARIES)
32-
set(EXECUTORCH_INCLUDE_DIRS ${_root})
32+
set(EXECUTORCH_INCLUDE_DIRS ${_root}/include ${_root}/include/executorch/runtime/core/portable_type ${_root}/lib)
3333
foreach(lib ${required_lib_list})
3434
set(lib_var "LIB_${lib}")
3535
add_library(${lib} STATIC IMPORTED)
3636
find_library(
3737
${lib_var} ${lib}
38-
HINTS "${_root}"
38+
HINTS "${_root}/lib"
3939
CMAKE_FIND_ROOT_PATH_BOTH
4040
)
4141
set_target_properties(${lib} PROPERTIES IMPORTED_LOCATION "${${lib_var}}")
42-
target_include_directories(${lib} INTERFACE ${_root})
42+
target_compile_definitions(${lib} INTERFACE C10_USING_CUSTOM_GENERATED_MACROS)
43+
target_include_directories(${lib} INTERFACE ${_root}/include ${_root}/include/executorch/runtime/core/portable_type ${_root}/lib)
4344
list(APPEND EXECUTORCH_LIBRARIES ${lib})
4445
endforeach()
4546

@@ -93,7 +94,7 @@ foreach(lib ${lib_list})
9394
set(lib_var "LIB_${lib}")
9495
find_library(
9596
${lib_var} ${lib}
96-
HINTS "${_root}"
97+
HINTS "${_root}/lib"
9798
CMAKE_FIND_ROOT_PATH_BOTH
9899
)
99100
if(NOT ${lib_var})
@@ -109,7 +110,7 @@ foreach(lib ${lib_list})
109110
add_library(${lib} STATIC IMPORTED)
110111
endif()
111112
set_target_properties(${lib} PROPERTIES IMPORTED_LOCATION "${${lib_var}}")
112-
target_include_directories(${lib} INTERFACE ${_root})
113+
target_include_directories(${lib} INTERFACE ${_root}/include ${_root}/include/executorch/runtime/core/portable_type ${_root}/lib)
113114
list(APPEND EXECUTORCH_LIBRARIES ${lib})
114115
endif()
115116
endforeach()

examples/apple/coreml/executor_runner/coreml_executor_runner.xcodeproj/project.pbxproj

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@
214214
GCC_OPTIMIZATION_LEVEL = 0;
215215
GCC_PREPROCESSOR_DEFINITIONS = (
216216
"DEBUG=1",
217+
"C10_USING_CUSTOM_GENERATED_MACROS",
217218
"$(inherited)",
218219
);
219220
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
@@ -271,6 +272,10 @@
271272
ENABLE_USER_SCRIPT_SANDBOXING = YES;
272273
GCC_C_LANGUAGE_STANDARD = gnu17;
273274
GCC_NO_COMMON_BLOCKS = YES;
275+
GCC_PREPROCESSOR_DEFINITIONS = (
276+
"C10_USING_CUSTOM_GENERATED_MACROS",
277+
"$(inherited)",
278+
);
274279
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
275280
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
276281
GCC_WARN_UNDECLARED_SELECTOR = YES;
@@ -291,7 +296,10 @@
291296
CODE_SIGN_STYLE = Automatic;
292297
DEVELOPMENT_TEAM = "";
293298
ENABLE_HARDENED_RUNTIME = YES;
294-
HEADER_SEARCH_PATHS = "$(SRCROOT)/include";
299+
HEADER_SEARCH_PATHS = (
300+
"$(SRCROOT)/include",
301+
"$(SRCROOT)/include/executorch/runtime/core/portable_type",
302+
);
295303
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
296304
LIBRARY_SEARCH_PATHS = (
297305
"$(SRCROOT)/libraries",
@@ -310,7 +318,10 @@
310318
CODE_SIGN_STYLE = Automatic;
311319
DEVELOPMENT_TEAM = "";
312320
ENABLE_HARDENED_RUNTIME = YES;
313-
HEADER_SEARCH_PATHS = "$(SRCROOT)/include";
321+
HEADER_SEARCH_PATHS = (
322+
"$(SRCROOT)/include",
323+
"$(SRCROOT)/include/executorch/runtime/core/portable_type",
324+
);
314325
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
315326
LIBRARY_SEARCH_PATHS = (
316327
"$(SRCROOT)/libraries",

examples/arm/executor_runner/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,9 @@ target_link_options( arm_executor_runner PUBLIC LINKER:-Map=arm_executor_runner.
382382

383383
# ET headers and generated headers includes
384384
target_include_directories(
385-
arm_executor_runner PRIVATE ${ET_INCLUDE_PATH} ${CMAKE_CURRENT_BINARY_DIR}
385+
arm_executor_runner PRIVATE ${ET_INCLUDE_PATH} ${ET_DIR_PATH}/runtime/core/portable_type ${CMAKE_CURRENT_BINARY_DIR}
386386
)
387+
target_compile_definitions(arm_executor_runner PRIVATE C10_USING_CUSTOM_GENERATED_MACROS)
387388

388389
if(SEMIHOSTING)
389390
target_compile_definitions(arm_executor_runner PUBLIC SEMIHOSTING)

0 commit comments

Comments
 (0)