Skip to content

Commit d8e07bd

Browse files
authored
Add .ptd support to portable executor runner (#14833)
This pull request enhances the `executor_runner` example by adding support for loading and using `.ptd` (portable tensor data) files. This enables the runner to ingest pre-serialized tensor data, improving flexibility for model input handling. The changes include updates to both build configuration and the main runner logic. **Support for .ptd file loading and usage:** * Added a new command-line flag `data_path` to specify the path to a `.ptd` data file in `executor_runner.cpp` and integrated logic to load this file and parse its contents using `FlatTensorDataMap`. [[1]](diffhunk://#diff-179a73518cca7aa859d17ae188553f0eb0bee3ba5d2a99d8c636fae0bb39f759R54) [[2]](diffhunk://#diff-179a73518cca7aa859d17ae188553f0eb0bee3ba5d2a99d8c636fae0bb39f759R177-R204) * Updated the runner to pass the loaded tensor data map to the model method loader, allowing methods to access pre-loaded input data. **Build and dependency updates:** * Included `flat_tensor_data_map` as a dependency in both the Bazel build targets and CMake build configuration to ensure the new functionality is available during compilation. [[1]](diffhunk://#diff-d613fef537c6c97cf343cfcde252e980f7673c21aad54b40a2315aa44c284a8cR22) [[2]](diffhunk://#diff-d613fef537c6c97cf343cfcde252e980f7673c21aad54b40a2315aa44c284a8cR42) [[3]](diffhunk://#diff-1e7de1ae2d059d21e1dd75d5812d5a34b0222cef273b7c3a2af62eb747f9d20aR1024-R1026) * Added the necessary header include for `flat_tensor_data_map` in `executor_runner.cpp` and updated the relevant namespace usage. [[1]](diffhunk://#diff-179a73518cca7aa859d17ae188553f0eb0bee3ba5d2a99d8c636fae0bb39f759R29) [[2]](diffhunk://#diff-179a73518cca7aa859d17ae188553f0eb0bee3ba5d2a99d8c636fae0bb39f759R77) ## Test Plan: Tested with .pte and .ptd for CUDA backend: ``` python -m executorch.examples.cuda.scripts.export --model_name linear --output_dir ./ ``` Make sure we have `linear.pte` and `aoti_cuda_blob.ptd`. Build executor runner with the following options: ``` cmake -DCMAKE_BUILD_TYPE=Debug -DEXECUTORCH_BUILD_CUDA=ON -DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON -DEXECUTORCH_BUILD_EXTENSION_FLAT_TENSOR=ON -DEXECUTORCH_BUILD_EXTENSION_TENSOR=ON -S. -Bcmake-out ``` Then do: ``` cmake --build cmake-out -j8 ``` Then we can run: ``` cmake-out/executor_runner --model_path linear.pte --ptd_path aoti_cuda_blob.ptd I 00:00:00.000594 executorch:executor_runner.cpp:189] PTD file aoti_cuda_blob.ptd is loaded. I 00:00:00.000671 executorch:executor_runner.cpp:199] PTD data map created with 1 keys. I 00:00:00.000749 executorch:executor_runner.cpp:249] Model file linear.pte is loaded. I 00:00:00.000758 executorch:executor_runner.cpp:258] Using method forward I 00:00:00.000770 executorch:executor_runner.cpp:309] Setting up planned buffer 0, size 96. I 00:00:00.002908 executorch:cuda_backend.cpp:140] Writing 394624 bytes to /tmp/linear_so_blob844427.so I 00:00:00.324783 executorch:cuda_backend.cpp:174] container_handle = 0x26a71b0 I 00:00:00.324867 executorch:executor_runner.cpp:337] Method loaded. I 00:00:00.325796 executorch:cuda_backend.cpp:249] Inputs copied to GPU I 00:00:00.325829 executorch:cuda_backend.cpp:278] Outputs created on GPU E 00:00:00.326623 executorch:memory.cpp:286] Cannot delete null tensor I 00:00:00.326678 executorch:executor_runner.cpp:374] Model executed successfully 1 time(s) in 1.777041 ms. I 00:00:00.326691 executorch:executor_runner.cpp:383] 1 outputs: OutputX 0: tensor(sizes=[3, 3], [-0.199237, 0.550725, 0.0830356, -0.199237, 0.550725, 0.0830356, -0.199237, 0.550725, 0.0830356]) E 00:00:00.328474 executorch:memory.cpp:299] Didn't find tensor 0x699a3d0 ```
1 parent 1b8d380 commit d8e07bd

File tree

12 files changed

+81
-18
lines changed

12 files changed

+81
-18
lines changed

.ci/scripts/test_model.sh

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,25 @@ prepare_artifacts_upload() {
4848
fi
4949
}
5050

51+
5152
build_cmake_executor_runner() {
5253
local backend_string_select="${1:-}"
5354
echo "Building executor_runner"
5455
rm -rf ${CMAKE_OUTPUT_DIR}
5556
mkdir ${CMAKE_OUTPUT_DIR}
57+
# Common options:
58+
COMMON="-DPYTHON_EXECUTABLE=$PYTHON_EXECUTABLE"
5659
if [[ "$backend_string_select" == "XNNPACK" ]]; then
5760
echo "Backend $backend_string_select selected"
58-
(cd ${CMAKE_OUTPUT_DIR} \
59-
&& cmake -DCMAKE_BUILD_TYPE=Release \
61+
cmake -DCMAKE_BUILD_TYPE=Release \
6062
-DEXECUTORCH_BUILD_XNNPACK=ON \
61-
-DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" ..)
63+
${COMMON} \
64+
-B${CMAKE_OUTPUT_DIR} .
6265
cmake --build ${CMAKE_OUTPUT_DIR} -j4
6366
else
6467
cmake -DCMAKE_BUILD_TYPE=Debug \
6568
-DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=ON \
66-
-DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \
69+
${COMMON} \
6770
-B${CMAKE_OUTPUT_DIR} .
6871
cmake --build ${CMAKE_OUTPUT_DIR} -j4 --config Debug
6972
fi

.ci/scripts/utils.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,15 @@ build_executorch_runner_cmake() {
125125
clean_executorch_install_folders
126126
mkdir "${CMAKE_OUTPUT_DIR}"
127127

128-
pushd "${CMAKE_OUTPUT_DIR}" || return
129128
if [[ $1 == "Debug" ]]; then
130129
CXXFLAGS="-fsanitize=address,undefined"
131130
else
132131
CXXFLAGS=""
133132
fi
134-
CXXFLAGS="$CXXFLAGS" retry cmake -DPYTHON_EXECUTABLE="${PYTHON_EXECUTABLE}" -DCMAKE_BUILD_TYPE="${1:-Release}" ..
135-
popd || return
133+
CXXFLAGS="$CXXFLAGS" retry cmake \
134+
-DPYTHON_EXECUTABLE="${PYTHON_EXECUTABLE}" \
135+
-DCMAKE_BUILD_TYPE="${1:-Release}" \
136+
-B${CMAKE_OUTPUT_DIR} .
136137

137138
if [ "$(uname)" == "Darwin" ]; then
138139
CMAKE_JOBS=$(( $(sysctl -n hw.ncpu) - 1 ))

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,10 @@ if(EXECUTORCH_BUILD_EXECUTOR_RUNNER)
10211021
extension_runner_util gflags executorch_backends
10221022
)
10231023

1024+
if(EXECUTORCH_BUILD_EXTENSION_FLAT_TENSOR)
1025+
list(APPEND _executor_runner_libs extension_flat_tensor)
1026+
endif()
1027+
10241028
if(EXECUTORCH_BUILD_KERNELS_OPTIMIZED)
10251029
list(APPEND _executor_runner_libs optimized_native_cpu_ops_lib)
10261030
elseif(EXECUTORCH_BUILD_CADENCE)

examples/portable/custom_ops/CMakeLists.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,14 @@ list(TRANSFORM _executor_runner__srcs PREPEND "${EXECUTORCH_ROOT}/")
117117

118118
add_executable(custom_ops_executor_runner ${_executor_runner__srcs})
119119
target_link_libraries(
120-
custom_ops_executor_runner custom_ops_lib executorch extension_evalue_util
121-
extension_runner_util gflags
120+
custom_ops_executor_runner
121+
custom_ops_lib
122+
executorch
123+
extension_evalue_util
124+
extension_runner_util
125+
gflags
126+
extension_data_loader
127+
extension_flat_tensor
122128
)
123129
target_compile_options(
124130
custom_ops_executor_runner PUBLIC ${_common_compile_options}

examples/portable/executor_runner/executor_runner.cpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <executorch/extension/data_loader/file_data_loader.h>
2828
#include <executorch/extension/evalue_util/print_evalue.h>
29+
#include <executorch/extension/flat_tensor/flat_tensor_data_map.h>
2930
#include <executorch/extension/runner_util/inputs.h>
3031
#include <executorch/runtime/core/event_tracer.h>
3132
#include <executorch/runtime/executor/method.h>
@@ -50,6 +51,7 @@ DEFINE_string(
5051
model_path,
5152
"model.pte",
5253
"Model serialized in flatbuffer format.");
54+
DEFINE_string(data_path, "", "Path to data file.");
5355
DEFINE_string(inputs, "", "Comma-separated list of input files");
5456
DEFINE_string(
5557
output_file,
@@ -72,6 +74,7 @@ DEFINE_int32(
7274
using executorch::aten::ScalarType;
7375
using executorch::aten::Tensor;
7476
using executorch::extension::FileDataLoader;
77+
using executorch::extension::FlatTensorDataMap;
7578
using executorch::runtime::Error;
7679
using executorch::runtime::EValue;
7780
using executorch::runtime::EventTracer;
@@ -171,6 +174,34 @@ int main(int argc, char** argv) {
171174
"FileDataLoader::from() failed: 0x%" PRIx32,
172175
(uint32_t)loader.error());
173176

177+
// Load .ptd file if provided
178+
std::unique_ptr<FileDataLoader> ptd_loader;
179+
std::unique_ptr<FlatTensorDataMap> ptd_data_map;
180+
if (!FLAGS_data_path.empty()) {
181+
const char* data_path = FLAGS_data_path.c_str();
182+
Result<FileDataLoader> ptd_loader_result = FileDataLoader::from(data_path);
183+
ET_CHECK_MSG(
184+
ptd_loader_result.ok(),
185+
"FileDataLoader::from() failed for PTD file: 0x%" PRIx32,
186+
(uint32_t)ptd_loader_result.error());
187+
ptd_loader =
188+
std::make_unique<FileDataLoader>(std::move(ptd_loader_result.get()));
189+
ET_LOG(Info, "PTD file %s is loaded.", data_path);
190+
191+
Result<FlatTensorDataMap> ptd_data_map_result =
192+
FlatTensorDataMap::load(ptd_loader.get());
193+
ET_CHECK_MSG(
194+
ptd_data_map_result.ok(),
195+
"FlatTensorDataMap::load() failed for PTD file: 0x%" PRIx32,
196+
(uint32_t)ptd_data_map_result.error());
197+
ptd_data_map = std::make_unique<FlatTensorDataMap>(
198+
std::move(ptd_data_map_result.get()));
199+
ET_LOG(
200+
Info,
201+
"PTD data map created with %" PRIu64 " keys.",
202+
static_cast<uint64_t>(ptd_data_map->get_num_keys().get()));
203+
}
204+
174205
std::vector<std::string> inputs_storage;
175206
std::vector<std::pair<char*, size_t>> input_buffers;
176207

@@ -294,7 +325,10 @@ int main(int argc, char** argv) {
294325
//
295326
EventTraceManager tracer;
296327
Result<Method> method = program->load_method(
297-
method_name, &memory_manager, tracer.get_event_tracer());
328+
method_name,
329+
&memory_manager,
330+
tracer.get_event_tracer(),
331+
ptd_data_map.get());
298332
ET_CHECK_MSG(
299333
method.ok(),
300334
"Loading of method %s failed with status 0x%" PRIx32,

examples/portable/executor_runner/targets.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def define_common_targets():
1919
"//executorch/devtools/etdump:etdump_flatcc",
2020
"//executorch/extension/data_loader:file_data_loader",
2121
"//executorch/extension/evalue_util:print_evalue",
22+
"//executorch/extension/flat_tensor:flat_tensor_data_map",
2223
"//executorch/extension/runner_util:inputs",
2324
],
2425
external_deps = [
@@ -38,6 +39,7 @@ def define_common_targets():
3839
"//executorch/runtime/executor:program",
3940
"//executorch/extension/data_loader:file_data_loader",
4041
"//executorch/extension/evalue_util:print_evalue",
42+
"//executorch/extension/flat_tensor:flat_tensor_data_map",
4143
"//executorch/extension/runner_util:inputs",
4244
"//executorch/extension/threadpool:cpuinfo_utils",
4345
"//executorch/extension/threadpool:threadpool",

examples/selective_build/advanced/CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,12 @@ if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
139139
endif()
140140
target_link_libraries(
141141
selective_build_test
142-
PRIVATE executorch_core extension_evalue_util extension_runner_util
143-
gflags::gflags ${selected_kernel_target}
142+
PRIVATE executorch_core
143+
extension_evalue_util
144+
extension_runner_util
145+
gflags::gflags
146+
extension_flat_tensor
147+
extension_data_loader
148+
${selected_kernel_target}
144149
)
145150
target_compile_options(selective_build_test PUBLIC ${_common_compile_options})

examples/selective_build/basic/CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,12 @@ if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
7171
endif()
7272
target_link_libraries(
7373
selective_build_test
74-
PRIVATE executorch_core extension_evalue_util extension_runner_util
75-
gflags::gflags executorch_kernels
74+
PRIVATE executorch_core
75+
extension_evalue_util
76+
extension_runner_util
77+
gflags::gflags
78+
executorch_kernels
79+
extension_data_loader
80+
extension_flat_tensor
7681
)
7782
target_compile_options(selective_build_test PUBLIC ${_common_compile_options})

extension/flat_tensor/flat_tensor_data_map.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Result<const flat_tensor_flatbuffer::NamedData*> get_named_data(
5555
if (named_data == nullptr) {
5656
return Error::NotFound;
5757
}
58-
for (int i = 0; i < named_data->size(); i++) {
58+
for (flatbuffers::uoffset_t i = 0; i < named_data->size(); ++i) {
5959
if (key.size() == named_data->Get(i)->key()->size() &&
6060
std::strncmp(
6161
named_data->Get(i)->key()->c_str(),

extension/flat_tensor/serialize/flat_tensor_header.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
#include <executorch/runtime/core/error.h>
1515
#include <executorch/runtime/core/result.h>
1616

17+
#if defined(__clang__)
1718
#pragma clang diagnostic ignored "-Wdeprecated"
19+
#endif
1820

1921
namespace executorch {
2022
using runtime::Error;

0 commit comments

Comments
 (0)