Skip to content

Commit 0b752e8

Browse files
committed
Build optimized library with CMake
1 parent e3ee6d9 commit 0b752e8

File tree

12 files changed

+383
-5
lines changed

12 files changed

+383
-5
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,6 @@
5959
path = third-party/lm-evaluation-harness
6060
url = https://github.com/EleutherAI/lm-evaluation-harness
6161
branch = v0.4.1
62+
[submodule "third-party/sleef"]
63+
path = third-party/sleef
64+
url = [email protected]:shibatch/sleef.git

CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,24 @@ if(NOT EXECUTORCH_ENABLE_LOGGING)
7777
add_definitions(-DET_LOG_ENABLED=0)
7878
endif()
7979

80+
# Configure log level. Must be one of debug, info, error, fatal.
81+
set(EXECUTORCH_LOG_LEVEL "Info" CACHE STRING
82+
"Build with the given ET_MIN_LOG_LEVEL value")
83+
string(TOLOWER "${EXECUTORCH_LOG_LEVEL}" LOG_LEVEL_LOWER)
84+
if(LOG_LEVEL_LOWER STREQUAL "debug")
85+
add_definitions(-DET_MIN_LOG_LEVEL=Debug)
86+
elseif(LOG_LEVEL_LOWER STREQUAL "info")
87+
add_definitions(-DET_MIN_LOG_LEVEL=Info)
88+
elseif(LOG_LEVEL_LOWER STREQUAL "error")
89+
add_definitions(-DET_MIN_LOG_LEVEL=Error)
90+
elseif(LOG_LEVEL_LOWER STREQUAL "fatal")
91+
add_definitions(-DET_MIN_LOG_LEVEL=Fatal)
92+
else()
93+
message(SEND_ERROR
94+
"Unknown log level \"${EXECUTORCH_LOG_LEVEL}\"." +
95+
"Expected one of Debug, Info, Error, or Fatal.")
96+
endif()
97+
8098
option(EXECUTORCH_ENABLE_PROGRAM_VERIFICATION
8199
"Build with ET_ENABLE_PROGRAM_VERIFICATION"
82100
${_default_release_disabled_options})
@@ -291,6 +309,7 @@ endif()
291309
# operators necessary for the models that will run.
292310
#
293311
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/kernels/portable)
312+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/kernels/optimized)
294313

295314
#
296315
# gflags: Commandline flag host library.

build/cmake_deps.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,22 @@ deps = [
4545
"executorch",
4646
]
4747

48+
[targets.optimized_kernels]
49+
buck_targets = [
50+
"//kernels/optimized:generated_lib",
51+
]
52+
filters = [
53+
".cpp$",
54+
]
55+
excludes = [
56+
# Exclude the codegen templates, which are picked up because the buck target
57+
# is the generated_lib and not the unwrapped set of kernels.
58+
"^codegen/templates",
59+
]
60+
deps = [
61+
"executorch",
62+
]
63+
4864
[targets.quantized_kernels]
4965
buck_targets = [
5066
"//kernels/quantized:generated_lib",

kernels/optimized/CMakeLists.txt

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
# Kernel library for optimized kernels. Please this file formatted by running:
8+
# ~~~
9+
# cmake-format --first-comment-is-literal=True CMakeLists.txt
10+
# ~~~
11+
12+
cmake_minimum_required(VERSION 3.19)
13+
14+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
15+
if(NOT CMAKE_CXX_STANDARD)
16+
set(CMAKE_CXX_STANDARD 17)
17+
endif()
18+
19+
if(NOT PYTHON_EXECUTABLE)
20+
set(PYTHON_EXECUTABLE python3)
21+
endif()
22+
# Source root directory for executorch.
23+
if(NOT EXECUTORCH_ROOT)
24+
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..)
25+
endif()
26+
# Source root directory for pytorch.
27+
if(NOT TORCH_ROOT)
28+
set(TORCH_ROOT ${EXECUTORCH_ROOT}/third-party/pytorch)
29+
endif()
30+
31+
set(_common_compile_options -Wno-deprecated-declarations)
32+
33+
# Set architecture-dependent flags.
34+
set(_arch_compile_flags "")
35+
# TODO
36+
37+
include(${EXECUTORCH_ROOT}/build/Utils.cmake)
38+
include(${EXECUTORCH_ROOT}/build/Codegen.cmake)
39+
40+
# Generate C++ bindings to register kernels into both PyTorch (for AOT) and
41+
# Executorch (for runtime). Here select all ops in optimized.yaml
42+
set(_yaml "${CMAKE_CURRENT_LIST_DIR}/optimized.yaml")
43+
gen_selected_ops("${_yaml}" "" "")
44+
45+
generate_bindings_for_kernels(${CMAKE_CURRENT_SOURCE_DIR}/optimized.yaml "")
46+
message("Generated files ${gen_command_sources}")
47+
48+
list(TRANSFORM _optimized_kernels__srcs PREPEND "${EXECUTORCH_ROOT}/")
49+
add_library(optimized_kernels ${_optimized_kernels__srcs})
50+
target_link_libraries(optimized_kernels PRIVATE executorch)
51+
target_compile_options(optimized_kernels PUBLIC ${_common_compile_options})
52+
# Build a library for _optimized_kernels_srcs
53+
#
54+
# optimized_ops_lib: Register optimized ops kernels into Executorch runtime
55+
gen_operators_lib("optimized_ops_lib" optimized_kernels executorch)

kernels/optimized/cpu/targets.bzl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime")
2-
load("@fbsource//xplat/executorch/kernels/optimized:op_registration_util.bzl", "define_op_target", "op_target")
2+
load("@fbsource//xplat/executorch/kernels/optimized:op_registration_util.bzl", "define_op_target", "op_target", "is_op_disabled")
33

44
_OPTIMIZED_ATEN_OPS = (
55
op_target(
@@ -81,11 +81,13 @@ def define_common_targets():
8181
TARGETS and BUCK files that call this function.
8282
"""
8383

84+
enabled_ops = [op for op in _OPTIMIZED_ATEN_OPS if not is_op_disabled(op["name"])]
85+
8486
# Define build targets for all operators registered in the tables above.
85-
for op in _OPTIMIZED_ATEN_OPS:
87+
for op in enabled_ops:
8688
define_op_target(**op)
8789

88-
aten_op_targets = [":{}".format(op["name"]) for op in _OPTIMIZED_ATEN_OPS]
90+
aten_op_targets = [":{}".format(op["name"]) for op in enabled_ops]
8991
all_op_targets = aten_op_targets
9092

9193
runtime.cxx_library(

kernels/optimized/op_registration_util.bzl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
load("@fbsource//tools/build_defs:selects.bzl", "selects")
21
load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime")
2+
load("@fbsource//xplat/executorch/build:selects.bzl", "selects")
33
load(
44
"@fbsource//xplat/executorch/kernels/optimized:lib_defs.bzl",
55
"get_vec_android_preprocessor_flags",
@@ -124,3 +124,7 @@ def define_op_target(name, deps):
124124
name = name,
125125
deps = deps,
126126
)
127+
128+
def is_op_disabled(name):
129+
# All ops are enabled for internal builds.
130+
return False

shim/third-party/sleef/TARGETS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
runtime.cxx_library(
2+
name = "sleef_arm",
3+
srcs = [],
4+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DEVSERVER_PLATFORM_REGEX = "UNUSED"

shim/xplat/executorch/build/env_interface.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def _remove_platform_specific_args(kwargs):
117117
"""
118118
keys = []
119119
for key in kwargs:
120-
if key.endswith("_platform_preprocessor_flags") or key.endswith("_platform_deps"):
120+
if key.endswith("_platform_preprocessor_flags") or key.endswith("_platform_deps") or key.startswith("fbobjc"):
121121
keys.append(key)
122122
for key in keys:
123123
kwargs.pop(key)
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
load("@fbsource//tools/build_defs:default_platform_defs.bzl", "DEVSERVER_PLATFORM_REGEX")
2+
load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime")
3+
4+
# Because vec exists as a collection of header files, compile and preprocessor
5+
# flags applied to the vec target do not have any effect, since no compilation
6+
# actually occurs for the target.
7+
#
8+
# Targets using the vec library must therefore call the get_vec_*_flags
9+
# functions in order to declare the required compiler flags needed in order to
10+
# access CPU vector intrinsics.
11+
12+
def get_vec_android_preprocessor_flags():
13+
preprocessor_flags = [
14+
(
15+
"^android-arm64.*$",
16+
[
17+
"-DET_BUILD_ARM_VEC256_WITH_SLEEF",
18+
],
19+
),
20+
]
21+
return preprocessor_flags
22+
23+
def get_vec_cxx_preprocessor_flags():
24+
preprocessor_flags = [
25+
(
26+
DEVSERVER_PLATFORM_REGEX,
27+
[
28+
"-DCPU_CAPABILITY_AVX2",
29+
],
30+
),
31+
]
32+
return preprocessor_flags
33+
34+
def get_vec_fbcode_preprocessor_flags():
35+
preprocessor_flags = [
36+
"-DCPU_CAPABILITY_AVX2",
37+
]
38+
return preprocessor_flags
39+
40+
# Currently, having a dependency on fbsource//third-party/sleef:sleef may cause
41+
# duplicate symbol errors when linking fbcode targets in opt mode that also
42+
# depend on ATen. This is because ATen accesses sleef via the third-party folder
43+
# in caffe2 (caffe2/third-party//sleef:sleef).
44+
# TODO(ssjia): Enable -DCPU_CAPABILITY_AVX2 in fbcode, which requires sleef.
45+
def define_libs():
46+
runtime.cxx_library(
47+
name = "libvec",
48+
srcs = [],
49+
exported_headers = native.glob([
50+
"vec/**/*.h",
51+
]),
52+
header_namespace = "executorch/kernels/optimized",
53+
visibility = [
54+
"//executorch/...",
55+
"@EXECUTORCH_CLIENTS",
56+
],
57+
cxx_platform_deps = select({
58+
"DEFAULT": [
59+
(
60+
DEVSERVER_PLATFORM_REGEX,
61+
[
62+
"fbsource//third-party/sleef:sleef",
63+
],
64+
),
65+
],
66+
"ovr_config//cpu:arm64": [
67+
(
68+
DEVSERVER_PLATFORM_REGEX,
69+
[
70+
"fbsource//third-party/sleef:sleef_arm",
71+
],
72+
),
73+
],
74+
}),
75+
fbandroid_platform_deps = [
76+
(
77+
"^android-arm64.*$",
78+
[
79+
"fbsource//third-party/sleef:sleef_arm",
80+
],
81+
),
82+
],
83+
)
84+
85+
runtime.cxx_library(
86+
name = "libutils",
87+
srcs = [],
88+
exported_headers = native.glob([
89+
"utils/**/*.h",
90+
]),
91+
header_namespace = "executorch/kernels/optimized",
92+
visibility = [
93+
"//executorch/...",
94+
"@EXECUTORCH_CLIENTS",
95+
],
96+
exported_deps = [
97+
# Needed to access the __ET_INLINE macro
98+
"//executorch/runtime/platform:compiler",
99+
],
100+
)
101+
102+
runtime.cxx_library(
103+
name = "libblas",
104+
srcs = native.glob([
105+
"blas/**/*.cpp",
106+
]),
107+
exported_headers = native.glob([
108+
"blas/**/*.h",
109+
]),
110+
header_namespace = "executorch/kernels/optimized",
111+
visibility = [
112+
"//executorch/...",
113+
"@EXECUTORCH_CLIENTS",
114+
],
115+
fbandroid_platform_preprocessor_flags = [
116+
(
117+
"^android-arm64.*$",
118+
[
119+
"-DET_BUILD_WITH_BLAS",
120+
],
121+
),
122+
],
123+
fbandroid_platform_deps = [
124+
(
125+
"^android-arm64.*$",
126+
[
127+
"fbsource//third-party/openblas:openblas",
128+
],
129+
),
130+
],
131+
fbobjc_exported_preprocessor_flags = [
132+
"-DET_BUILD_WITH_BLAS",
133+
"-DET_BUILD_FOR_APPLE",
134+
],
135+
fbobjc_frameworks = [
136+
"Accelerate",
137+
],
138+
exported_deps = [
139+
"//executorch/kernels/optimized:libutils",
140+
"//executorch/runtime/core/exec_aten:lib",
141+
],
142+
)

0 commit comments

Comments
 (0)