Skip to content

Commit 0c26dc0

Browse files
[Cadence] Enabled x86 executor flow with numerical verification
Differential Revision: D60424030 Pull Request resolved: #4453
1 parent 9b2bfb6 commit 0c26dc0

File tree

12 files changed

+1148
-7
lines changed

12 files changed

+1148
-7
lines changed

backends/cadence/CMakeLists.txt

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,64 @@ if(NOT EXECUTORCH_ROOT)
2020
endif()
2121

2222
include(${EXECUTORCH_ROOT}/build/Utils.cmake)
23+
include(${EXECUTORCH_ROOT}/build/Codegen.cmake)
2324

2425
# Let files say "include <executorch/path/to/header.h>".
2526
set(_common_include_directories ${EXECUTORCH_ROOT}/..)
2627
set(TARGET_DIR reference)
2728

2829
if(EXECUTORCH_NNLIB_OPT)
2930
set(TARGET_DIR hifi)
30-
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${TARGET_DIR}/third-party/nnlib)
31+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${TARGET_DIR}/third-party/nnlib)
3132
endif()
33+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
34+
35+
# Source root directory for executorch.
36+
if(NOT EXECUTORCH_ROOT)
37+
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..)
38+
endif()
39+
40+
if(NOT PYTHON_EXECUTABLE)
41+
resolve_python_executable()
42+
endif()
43+
44+
set(_common_compile_options -Wno-deprecated-declarations -fPIC)
45+
46+
# Find prebuilt libraries. executorch package should contain portable_ops_lib,
47+
# etdump, bundled_program.
48+
find_package(executorch CONFIG REQUIRED)
49+
target_link_options_shared_lib(executorch)
50+
target_link_options_shared_lib(portable_ops_lib)
51+
52+
target_include_directories(executorch INTERFACE ${_common_include_directories})
53+
54+
find_package(
55+
gflags REQUIRED PATHS ${CMAKE_CURRENT_BINARY_DIR}/../../third-party
56+
)
57+
58+
add_executable(cadence_runner cadence_runner/cadence_runner.cpp)
59+
target_compile_options(executorch INTERFACE -DET_EVENT_TRACER_ENABLED)
3260

3361
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${TARGET_DIR}/operators)
3462
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${TARGET_DIR}/kernels)
63+
64+
target_include_directories(
65+
etdump INTERFACE ${CMAKE_CURRENT_BINARY_DIR}/../../sdk/include
66+
${EXECUTORCH_ROOT}/third-party/flatcc/include
67+
)
68+
69+
target_include_directories(
70+
cadence_runner PUBLIC ${ROOT_DIR}/.. ${CMAKE_BINARY_DIR}
71+
${_common_include_directories}
72+
)
73+
74+
target_link_libraries(
75+
cadence_runner
76+
executorch
77+
gflags
78+
etdump
79+
extension_data_loader
80+
bundled_program
81+
cadence_ops_lib
82+
flatccrt
83+
)

backends/cadence/aot/export_example.py

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,21 @@
77
# Example script for exporting simple models to flatbuffer
88

99
import logging
10+
import tempfile
1011

1112
from executorch.backends.cadence.aot.ops_registrations import * # noqa
12-
1313
import os
1414
from typing import Any, Tuple
1515

1616
from executorch.backends.cadence.aot.compiler import (
17+
convert_pt2,
1718
export_to_cadence,
1819
export_to_edge,
1920
quantize_pt2,
2021
)
22+
from executorch.backends.cadence.aot.quantizer.quantizer import CadenceQuantizer
23+
from executorch.backends.cadence.runtime import runtime
24+
from executorch.backends.cadence.runtime.executor import BundledProgramManager
2125
from executorch.exir import ExecutorchProgramManager
2226
from torch import nn
2327

@@ -44,23 +48,50 @@ def _save_pte_program(
4448
logging.error(f"Error while saving to {filename}: {e}")
4549

4650

51+
def _save_bpte_program(
52+
buffer: bytes,
53+
model_name: str,
54+
output_dir: str = "",
55+
) -> None:
56+
if model_name.endswith(".bpte"):
57+
filename = model_name
58+
else:
59+
filename = os.path.join(output_dir, f"{model_name}.bpte")
60+
try:
61+
with open(filename, "wb") as f:
62+
f.write(buffer)
63+
logging.info(f"Saved exported program to {filename}")
64+
except Exception as e:
65+
logging.error(f"Error while saving to {output_dir}: {e}")
66+
67+
4768
def export_model(
4869
model: nn.Module,
4970
example_inputs: Tuple[Any, ...],
5071
file_name: str = "CadenceDemoModel",
5172
):
73+
# create work directory for outputs and model binary
74+
working_dir = tempfile.mkdtemp(dir="/tmp")
75+
logging.debug(f"Created work directory {working_dir}")
76+
77+
# convert the model (also called in quantize_pt2)
78+
converted_model = convert_pt2(model, example_inputs, CadenceQuantizer())
79+
80+
# Get reference outputs from quantized_model
81+
ref_outputs = converted_model(*example_inputs)
82+
5283
# Quantize the model
5384
quantized_model = quantize_pt2(model, example_inputs)
5485

55-
# Get edge program
86+
# Get edge program (also called in export_to_cadence)
5687
edge_prog_manager = export_to_edge(quantized_model, example_inputs)
5788

5889
# Get edge program after Cadence specific passes
5990
cadence_prog_manager = export_to_cadence(quantized_model, example_inputs)
6091

61-
exec_prog = cadence_prog_manager.to_executorch()
92+
exec_prog: ExecutorchProgramManager = cadence_prog_manager.to_executorch()
6293

63-
logging.info("Final exported graph:")
94+
logging.info("Final exported graph:\n")
6495
exec_prog.exported_program().graph_module.graph.print_tabular()
6596

6697
# Print some information to terminal
@@ -69,5 +100,28 @@ def export_model(
69100
cadence_prog_manager.exported_program().graph_module,
70101
)
71102

72-
# Save the program as (default name is CadenceDemoModel.pte)
73-
_save_pte_program(exec_prog, file_name)
103+
forward_test_data = BundledProgramManager.bundled_program_test_data_gen(
104+
method="forward", inputs=example_inputs, expected_outputs=ref_outputs
105+
)
106+
bundled_program_manager = BundledProgramManager([forward_test_data])
107+
buffer = bundled_program_manager._serialize(
108+
exec_prog,
109+
bundled_program_manager.get_method_test_suites(),
110+
forward_test_data,
111+
)
112+
# Save the program as pte (default name is CadenceDemoModel.pte)
113+
_save_pte_program(exec_prog, file_name, working_dir)
114+
# Save the program as btpe (default name is CadenceDemoModel.bpte)
115+
_save_bpte_program(buffer, file_name, working_dir)
116+
117+
logging.debug(
118+
f"Executorch bundled program buffer saved to {file_name} is {len(buffer)} total bytes"
119+
)
120+
121+
# TODO: move to test infra
122+
runtime.run_and_compare(
123+
executorch_prog=exec_prog,
124+
inputs=example_inputs,
125+
ref_outputs=ref_outputs,
126+
working_dir=working_dir,
127+
)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/bin/bash
2+
# Copyright (c) Meta Platforms, Inc. and affiliates.
3+
# All rights reserved.
4+
#
5+
# This source code is licensed under the BSD-style license found in the
6+
# LICENSE file in the root directory of this source tree.
7+
8+
# Builds cadence_runner and prints its path.
9+
10+
set -euo pipefail
11+
12+
SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
13+
readonly SCRIPT_DIR
14+
15+
readonly EXECUTORCH_ROOT="${SCRIPT_DIR}/../.."
16+
17+
# Allow overriding the number of build jobs. Default to 9.
18+
export CMAKE_BUILD_PARALLEL_LEVEL="${CMAKE_BUILD_PARALLEL_LEVEL:-9}"
19+
20+
main() {
21+
cd "${EXECUTORCH_ROOT}"
22+
23+
rm -rf cmake-out
24+
cmake -DCMAKE_INSTALL_PREFIX=cmake-out \
25+
-DCMAKE_BUILD_TYPE=Release \
26+
-DEXECUTORCH_BUILD_SDK=ON \
27+
-DEXECUTORCH_ENABLE_EVENT_TRACER=ON \
28+
-DPYTHON_EXECUTABLE=python3 \
29+
-DEXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL=ON \
30+
-DEXECUTORCH_BUILD_HOST_TARGETS=ON \
31+
-DEXECUTORCH_BUILD_EXECUTOR_RUNNER=OFF \
32+
-DEXECUTORCH_BUILD_PTHREADPOOL=OFF \
33+
-DEXECUTORCH_BUILD_CPUINFO=OFF \
34+
-DEXECUTORCH_ENABLE_LOGGING=ON \
35+
-Bcmake-out .
36+
cmake --build cmake-out --target install --config Release
37+
38+
local example_dir=backends/cadence
39+
local build_dir="cmake-out/${example_dir}"
40+
local cmake_prefix_path="${PWD}/cmake-out/lib/cmake/ExecuTorch;${PWD}/cmake-out/third-party/gflags"
41+
rm -rf ${build_dir}
42+
cmake -DCMAKE_PREFIX_PATH="${cmake_prefix_path}" \
43+
-DCMAKE_BUILD_TYPE=Release \
44+
-B"${build_dir}" \
45+
"${example_dir}"
46+
cmake --build "${build_dir}" --config Release
47+
48+
local runner="${PWD}/${build_dir}/cadence_runner"
49+
if [[ ! -f "${runner}" ]]; then
50+
echo "ERROR: Failed to build ${build_dir}/cadence_runner" >&2
51+
exit 1
52+
else
53+
echo "Built ${build_dir}/cadence_runner"
54+
fi
55+
}
56+
57+
main "$@"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Any targets that should be shared between fbcode and xplat must be defined in
2+
# targets.bzl. This file can contain fbcode-only targets.
3+
4+
load(":targets.bzl", "define_common_targets")
5+
6+
oncall("odai_jarvis")
7+
8+
define_common_targets()

0 commit comments

Comments
 (0)