Skip to content

Commit 4d3aad8

Browse files
committed
flat-tensor cmake
1 parent 71b8b75 commit 4d3aad8

13 files changed

+207
-7
lines changed

CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ option(EXECUTORCH_BUILD_EXTENSION_DATA_LOADER "Build the Data Loader extension"
182182
OFF
183183
)
184184

185+
option(EXECUTORCH_BUILD_EXTENSION_FLAT_TENSOR "Build the Flat Tensor extension"
186+
OFF
187+
)
188+
185189
option(EXECUTORCH_BUILD_EXTENSION_MODULE "Build the Module extension" OFF)
186190

187191
option(EXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL "Build the Runner Util extension"
@@ -240,6 +244,9 @@ cmake_dependent_option(
240244
"NOT EXECUTORCH_BUILD_ARM_BAREMETAL" OFF
241245
)
242246

247+
if(EXECUTORCH_BUILD_EXTENSION_FLAT_TENSOR)
248+
set(EXECUTORCH_BUILF_EXTENSION_DATA_LOADER ON)
249+
endif()
243250

244251
if(EXECUTORCH_BUILD_EXTENSION_TRAINING)
245252
set(EXECUTORCH_BUILD_EXTENSION_TENSOR ON)
@@ -694,6 +701,11 @@ if(EXECUTORCH_BUILD_EXTENSION_DATA_LOADER)
694701
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/data_loader)
695702
endif()
696703

704+
if(EXECUTORCH_BUILD_EXTENSION_FLAT_TENSOR)
705+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/flat_tensor)
706+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/flat_tensor/serialize)
707+
endif()
708+
697709
if(EXECUTORCH_BUILD_EXTENSION_MODULE)
698710
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/module)
699711
endif()

build/Utils.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ function(executorch_print_configuration_summary)
6767
message(STATUS " EXECUTORCH_BUILD_EXTENSION_DATA_LOADER : "
6868
"${EXECUTORCH_BUILD_EXTENSION_DATA_LOADER}"
6969
)
70+
message(STATUS " EXECUTORCH_BUILD_EXTENSION_FLAT_TENSOR : "
71+
"${EXECUTORCH_BUILD_EXTENSION_FLAT_TENSOR}"
72+
)
7073
message(STATUS " EXECUTORCH_BUILD_EXTENSION_MODULE : "
7174
"${EXECUTORCH_BUILD_EXTENSION_MODULE}"
7275
)

build/cmake_deps.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,27 @@ deps = [
148148
"executorch",
149149
]
150150

151+
[targets.extension_flat_tensor_schema]
152+
buck_targets = [
153+
"//extension/flat_tensor/serialize:generated_headers",
154+
]
155+
filters = [
156+
".fbs$",
157+
]
158+
159+
[targets.extension_flat_tensor]
160+
buck_targets = [
161+
"//extension/flat_tensor:flat_tensor_data_map",
162+
]
163+
filters = [
164+
".cpp$",
165+
]
166+
deps = [
167+
"extension_flat_tensor_schema",
168+
"executorch_core",
169+
"executorch",
170+
]
171+
151172
[targets.extension_module]
152173
buck_targets = [
153174
"//extension/module:module",

extension/flat_tensor/CMakeLists.txt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
# Please this file formatted by running:
8+
# ~~~
9+
# cmake-format -i CMakeLists.txt
10+
# ~~~
11+
12+
cmake_minimum_required(VERSION 3.19)
13+
14+
# Source root directory for executorch.
15+
if(NOT EXECUTORCH_ROOT)
16+
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..)
17+
endif()
18+
19+
list(TRANSFORM _extension_flat_tensor__srcs PREPEND "${EXECUTORCH_ROOT}/")
20+
add_library(extension_flat_tensor ${_extension_flat_tensor__srcs})
21+
target_link_libraries(extension_flat_tensor executorch extension_data_loader)
22+
target_include_directories(extension_flat_tensor PUBLIC
23+
${EXECUTORCH_ROOT}/..
24+
"${CMAKE_BINARY_DIR}/extension/flat_tensor/include"
25+
"${EXECUTORCH_ROOT}/third-party/flatbuffers/include"
26+
${_common_include_directories})
27+
target_compile_options(extension_flat_tensor PUBLIC ${_common_compile_options})
28+
29+
# Install libraries
30+
install(
31+
TARGETS extension_flat_tensor
32+
DESTINATION lib
33+
INCLUDES
34+
DESTINATION ${_common_include_directories}
35+
)
36+
37+
if(BUILD_TESTING)
38+
add_subdirectory(test)
39+
endif()

extension/flat_tensor/flat_tensor_data_map.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
#include <executorch/extension/flat_tensor/flat_tensor_data_map.h>
1010

11+
#include <executorch/extension/flat_tensor/serialize/flat_tensor_generated.h>
1112
#include <executorch/extension/flat_tensor/serialize/flat_tensor_header.h>
12-
#include <executorch/extension/flat_tensor/serialize/schema_generated.h>
1313

1414
#include <executorch/runtime/core/error.h>
1515
#include <executorch/runtime/core/exec_aten/util/tensor_util.h>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
# Flatbuffer schema header lib. Please this file formatted by running:
8+
# ~~~
9+
# cmake-format -i CMakeLists.txt
10+
# ~~~
11+
12+
if(NOT FLATC_EXECUTABLE)
13+
set(FLATC_EXECUTABLE flatc)
14+
endif()
15+
16+
# The include directory that will contain the generated schema headers.
17+
set(_flat_tensor_schema__include_dir "${CMAKE_BINARY_DIR}/extension/flat_tensor/include")
18+
set(_flat_tensor_schema__output_dir "${_flat_tensor_schema__include_dir}/executorch/extension/flat_tensor/serialize")
19+
# Source root directory for executorch.
20+
if(NOT EXECUTORCH_ROOT)
21+
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/..)
22+
endif()
23+
24+
function(generate_flat_tensor_schema _schema_srcs _schema_name)
25+
set(_schema_outputs)
26+
foreach(fbs_file ${_schema_srcs})
27+
string(REGEX REPLACE "[.]fbs$" "_generated.h" generated "${fbs_file}")
28+
list(APPEND _schema_outputs
29+
"${_flat_tensor_schema__output_dir}/${generated}"
30+
)
31+
endforeach()
32+
33+
# Generate the headers from the .fbs files.
34+
add_custom_command(
35+
OUTPUT ${_schema_outputs}
36+
COMMAND
37+
${FLATC_EXECUTABLE} --cpp --cpp-std c++11 --gen-mutable --scoped-enums -o
38+
"${_flat_tensor_schema__output_dir}" ${_schema_srcs}
39+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
40+
DEPENDS ${FLATC_EXECUTABLE} ${_schema_srcs}
41+
COMMENT "Generating ${_schema_name} headers"
42+
VERBATIM
43+
)
44+
45+
add_library(${_schema_name} INTERFACE ${_schema_outputs})
46+
set_target_properties(${_schema_name} PROPERTIES LINKER_LANGUAGE CXX)
47+
48+
# exir lets users set the alignment of tensor data embedded in the flatbuffer,
49+
# and some users need an alignment larger than the default, which is typically
50+
# 32.
51+
target_compile_definitions(
52+
${_schema_name} INTERFACE FLATBUFFERS_MAX_ALIGNMENT=1024
53+
)
54+
55+
target_include_directories(
56+
${_schema_name}
57+
INTERFACE ${_flat_tensor_schema__include_dir}
58+
${EXECUTORCH_ROOT}/third-party/flatbuffers/include
59+
)
60+
endfunction()
61+
62+
# Generate common schema
63+
set(scalar_type_schema_srcs scalar_type.fbs)
64+
generate_flat_tensor_schema("${scalar_type_schema_srcs}" "scalar_type_schema")
65+
66+
# For the other schemas
67+
set(flat_tensor_schema_srcs flat_tensor.fbs)
68+
generate_flat_tensor_schema("${flat_tensor_schema_srcs}" "flat_tensor_schema")
69+
add_dependencies(flat_tensor_schema scalar_type_schema)

extension/flat_tensor/serialize/serialize.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
#include <executorch/extension/flat_tensor/serialize/serialize.h>
1010

11+
#include <executorch/extension/flat_tensor/serialize/flat_tensor_generated.h>
1112
#include <executorch/extension/flat_tensor/serialize/flat_tensor_header.h>
1213
#include <executorch/extension/flat_tensor/serialize/scalar_type_generated.h>
13-
#include <executorch/extension/flat_tensor/serialize/schema_generated.h>
1414

1515
#include <fstream>
1616
#include <string>

extension/flat_tensor/serialize/targets.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def define_common_targets():
88
"scalar_type.fbs",
99
],
1010
outs = {
11-
"schema_generated.h": ["flat_tensor_generated.h"],
11+
"flat_tensor_generated.h": ["flat_tensor_generated.h"],
1212
"scalar_type_generated.h": ["scalar_type_generated.h"]
1313
},
1414
cmd = " ".join([
@@ -29,7 +29,7 @@ def define_common_targets():
2929
"//executorch/...",
3030
],
3131
exported_headers = {
32-
"schema_generated.h": ":gen_schema[schema_generated.h]",
32+
"flat_tensor_generated.h": ":gen_schema[flat_tensor_generated.h]",
3333
"scalar_type_generated.h": ":gen_schema[scalar_type_generated.h]",
3434
},
3535
exported_external_deps = ["flatbuffers-api"],
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
# @generated by test/utils/generate_gtest_cmakelists.py
8+
#
9+
# This file should be formatted with
10+
# ~~~
11+
# cmake-format -i CMakeLists.txt
12+
# ~~~
13+
# It should also be cmake-lint clean.
14+
#
15+
16+
cmake_minimum_required(VERSION 3.19)
17+
18+
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
19+
20+
include(${EXECUTORCH_ROOT}/build/Test.cmake)
21+
22+
add_custom_command(
23+
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/ModuleLinearProgram.pte"
24+
"${CMAKE_CURRENT_BINARY_DIR}/_default_external_constant.ptd"
25+
26+
COMMAND
27+
python -m test.models.export_program --modules "ModuleLinear" --external-constants --outdir "${CMAKE_CURRENT_BINARY_DIR}" 2> /dev/null
28+
29+
WORKING_DIRECTORY ${EXECUTORCH_ROOT}
30+
)
31+
32+
add_custom_target(
33+
program_data_files
34+
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/ModuleLinearProgram.pte"
35+
"${CMAKE_CURRENT_BINARY_DIR}/_default_external_constant.ptd"
36+
)
37+
38+
set(test_env_
39+
"ET_MODULE_LINEAR_PROGRAM_PATH=${CMAKE_CURRENT_BINARY_DIR}/ModuleLinearProgram.pte"
40+
"ET_MODULE_LINEAR_DATA_PATH=${CMAKE_CURRENT_BINARY_DIR}/_default_external_constant.ptd"
41+
)
42+
43+
set(_test_srcs flat_tensor_data_map_test.cpp flat_tensor_header_test.cpp)
44+
45+
et_cxx_test(extension_flat_tensor_test SOURCES ${_test_srcs} EXTRA_LIBS extension_flat_tensor extension_data_loader)
46+
47+
add_dependencies(extension_flat_tensor_test program_data_files)
48+
set_property(TEST extension_flat_tensor_test PROPERTY ENVIRONMENT ${test_env_})

extension/flat_tensor/test/flat_tensor_data_map_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
#include <executorch/extension/data_loader/file_data_loader.h>
1010
#include <executorch/extension/flat_tensor/flat_tensor_data_map.h>
11+
#include <executorch/extension/flat_tensor/serialize/flat_tensor_generated.h>
1112
#include <executorch/extension/flat_tensor/serialize/flat_tensor_header.h>
12-
#include <executorch/extension/flat_tensor/serialize/schema_generated.h>
1313
#include <executorch/runtime/core/error.h>
1414
#include <executorch/runtime/core/result.h>
1515
#include <executorch/runtime/platform/runtime.h>
@@ -35,7 +35,7 @@ class FlatTensorDataMapTest : public ::testing::Test {
3535

3636
// Load data map. The eager linear model is defined at:
3737
// //executorch/test/models/linear_model.py
38-
const char* path = std::getenv("ET_MODULE_LINEAR_DATA");
38+
const char* path = std::getenv("ET_MODULE_LINEAR_DATA_PATH");
3939
Result<FileDataLoader> loader = FileDataLoader::from(path);
4040
ASSERT_EQ(loader.error(), Error::Ok);
4141

extension/flat_tensor/test/test_serialize.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
#include <executorch/extension/flat_tensor/serialize/serialize.h>
1010

11+
#include <executorch/extension/flat_tensor/serialize/flat_tensor_generated.h>
1112
#include <executorch/extension/flat_tensor/serialize/flat_tensor_header.h>
1213
#include <executorch/extension/flat_tensor/serialize/scalar_type_generated.h>
13-
#include <executorch/extension/flat_tensor/serialize/schema_generated.h>
1414

1515
#include <executorch/extension/tensor/tensor_ptr.h>
1616
#include <executorch/runtime/core/result.h>

test/run_oss_cpp_tests.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ build_executorch() {
3333
-DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=ON \
3434
-DEXECUTORCH_BUILD_KERNELS_QUANTIZED=ON \
3535
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
36+
-DEXECUTORCH_BUILD_EXTENSION_FLAT_TENSOR=ON \
3637
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
3738
-DEXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL=ON \
3839
-DEXECUTORCH_BUILD_EXTENSION_TENSOR=ON \

test/utils/OSSTestConfig.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
"../print_evalue.cpp"
1919
]
2020
},
21+
{
22+
"directory": "extension/flat_tensor/test",
23+
"sources": [
24+
"flat_tensor_data_map_test.cpp",
25+
"flat_tensor_header_test.cpp"
26+
]
27+
},
2128
{
2229
"directory": "extension/kernel_util/test",
2330
"sources": [

0 commit comments

Comments
 (0)