Skip to content

Commit b54fb42

Browse files
committed
patches and utils for setting up and running baremetal executor_runner for cs300
Differential Revision: https://internalfb.com/D49899371 fbshipit-source-id: 42cf5c8ad445b0405f9c23e3654b6a32fbc61522 Add example script to run on cpu Summary: An example script to build and run executor_runner baremetal version on ARM M-class CPUs ``` I executorch:runner.cpp:63] Model PTE file loaded. Size: 960 bytes. I executorch:runner.cpp:69] Model buffer loaded, has 1 methods I executorch:runner.cpp:77] Running method forward I executorch:runner.cpp:94] Setting up planned buffer 0, size 32. I executorch:runner.cpp:109] Method loaded. I executorch:runner.cpp:111] Preparing inputs... I executorch:runner.cpp:113] Input prepared. I executorch:runner.cpp:115] Starting the model execution... I executorch:runner.cpp:120] Model executed successfully. I executorch:runner.cpp:124] 1 outputs: Output[0][0]: 0.500000 Output[0][1]: 0.500000 Output[0][2]: 0.500000 Output[0][3]: 0.500000 ``` Differential Revision: D49900956 fbshipit-source-id: e30571f95abd5311f3ab649fcb1d154b4f5c3310
1 parent af36c52 commit b54fb42

10 files changed

+932
-0
lines changed

examples/arm/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TODO: Add a pointer to the Tutorial page
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#
2+
# Copyright (c) 2020-2022 Arm Limited. All rights reserved.
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the License); you may
7+
# not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an AS IS BASIS, WITHOUT
14+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
# Copied this file from core_platform/cmake/toolchain/arm-non-eabi-gcc.cmake
20+
# And modified to align better with cs300 platform
21+
22+
set(TARGET_CPU "cortex-m55" CACHE STRING "Target CPU")
23+
string(TOLOWER ${TARGET_CPU} CMAKE_SYSTEM_PROCESSOR)
24+
25+
set(CMAKE_SYSTEM_NAME Generic)
26+
set(CMAKE_C_COMPILER "arm-none-eabi-gcc")
27+
set(CMAKE_CXX_COMPILER "arm-none-eabi-g++")
28+
set(CMAKE_ASM_COMPILER "arm-none-eabi-gcc")
29+
set(CMAKE_LINKER "arm-none-eabi-ld")
30+
31+
set(CMAKE_EXECUTABLE_SUFFIX ".elf")
32+
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
33+
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
34+
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
35+
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
36+
37+
# Select C/C++ version
38+
set(CMAKE_C_STANDARD 11)
39+
set(CMAKE_CXX_STANDARD 14)
40+
41+
set(GCC_CPU ${CMAKE_SYSTEM_PROCESSOR})
42+
string(REPLACE "cortex-m85" "cortex-m55" GCC_CPU ${GCC_CPU})
43+
44+
# Compile options
45+
add_compile_options(
46+
-mcpu=${GCC_CPU}
47+
-mthumb
48+
"$<$<CONFIG:DEBUG>:-gdwarf-3>"
49+
"$<$<COMPILE_LANGUAGE:CXX>:-fno-unwind-tables;-fno-rtti;-fno-exceptions>"
50+
-fdata-sections
51+
-ffunction-sections)
52+
53+
# Compile defines
54+
add_compile_definitions(
55+
"$<$<NOT:$<CONFIG:DEBUG>>:NDEBUG>")
56+
57+
# Link options
58+
add_link_options(
59+
-mcpu=${GCC_CPU}
60+
-mthumb
61+
--specs=nosys.specs)
62+
63+
# Set floating point unit
64+
if(CMAKE_SYSTEM_PROCESSOR MATCHES "\\+fp")
65+
set(FLOAT hard)
66+
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "\\+nofp")
67+
set(FLOAT soft)
68+
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "cortex-m33(\\+|$)" OR
69+
CMAKE_SYSTEM_PROCESSOR MATCHES "cortex-m55(\\+|$)" OR
70+
CMAKE_SYSTEM_PROCESSOR MATCHES "cortex-m85(\\+|$)")
71+
set(FLOAT hard)
72+
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "cortex-m4(\\+|$)" OR
73+
CMAKE_SYSTEM_PROCESSOR MATCHES "cortex-m7(\\+|$)")
74+
set(FLOAT hard)
75+
set(FPU_CONFIG "fpv4-sp-d16")
76+
add_compile_options(-mfpu=${FPU_CONFIG})
77+
add_link_options(-mfpu=${FPU_CONFIG})
78+
else()
79+
set(FLOAT soft)
80+
endif()
81+
82+
if (FLOAT)
83+
add_compile_options(-mfloat-abi=${FLOAT})
84+
add_link_options(-mfloat-abi=${FLOAT})
85+
endif()
86+
87+
add_link_options(LINKER:--nmagic,--gc-sections)
88+
89+
# Compilation warnings
90+
add_compile_options(
91+
# -Wall
92+
# -Wextra
93+
# -Wcast-align
94+
# -Wdouble-promotion
95+
# -Wformat
96+
# -Wmissing-field-initializers
97+
# -Wnull-dereference
98+
# -Wredundant-decls
99+
# -Wshadow
100+
# -Wswitch
101+
# -Wswitch-default
102+
# -Wunused
103+
# -Wno-redundant-decls
104+
# -Wno-psabi
105+
)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
From 48a99f4b00e504c13cd74ca44a5ce7128f719cba Mon Sep 17 00:00:00 2001
2+
From: Digant Desai <[email protected]>
3+
Date: Tue, 3 Oct 2023 21:20:21 -0700
4+
Subject: [PATCH 1/6] [Executorch] Add README
5+
6+
---
7+
applications/executorch_tests/README.md | 16 ++++++++++++++++
8+
1 file changed, 16 insertions(+)
9+
create mode 100644 applications/executorch_tests/README.md
10+
11+
diff --git a/applications/executorch_tests/README.md b/applications/executorch_tests/README.md
12+
new file mode 100644
13+
index 0000000..f2dfb05
14+
--- /dev/null
15+
+++ b/applications/executorch_tests/README.md
16+
@@ -0,0 +1,16 @@
17+
+## ExecuTorch
18+
+A unified ML software stack within the PyTorch platform for edge devices. It
19+
+defines new compiler entry points as well as a state-of-art runtime.
20+
+
21+
+Home: https://github.com/pytorch/executorch/
22+
+
23+
+### executor_runner
24+
+
25+
+This test is a simple wrapper around ExecuTorch runtime, capable of running
26+
+`.pte` model files compatible with ExecuTorch.
27+
+
28+
+If configured correctly with `ET_*` CMake variables pointing to the ExecuTorch
29+
+project build, then this test bin executes `model.pte.h` file converted from
30+
+`model.pte` using `pte_to_header.py`, from the ExecuTorch project root dir,
31+
+containing an ExecuTorch compatible PyTorch model on the Costrone 300 FVP using
32+
+ExecuTorch runtime.
33+
--
34+
2.39.3
35+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
From 3359f94fc57ac76b3f5995d4453975251b56ae71 Mon Sep 17 00:00:00 2001
2+
From: Digant Desai <[email protected]>
3+
Date: Thu, 28 Sep 2023 18:05:03 -0700
4+
Subject: [PATCH 2/6] [Executorch][local-patch] regress cmake version from 3.21
5+
--> 3.20
6+
7+
---
8+
targets/corstone-300/CMakeLists.txt | 2 +-
9+
1 file changed, 1 insertion(+), 1 deletion(-)
10+
11+
diff --git a/targets/corstone-300/CMakeLists.txt b/targets/corstone-300/CMakeLists.txt
12+
index 62205bb..7dda8a1 100644
13+
--- a/targets/corstone-300/CMakeLists.txt
14+
+++ b/targets/corstone-300/CMakeLists.txt
15+
@@ -42,7 +42,7 @@ set(MEMORY_ARENA "dram" CACHE STRING "Memory config for arena")
16+
# Project
17+
#############################################################################
18+
19+
-cmake_minimum_required(VERSION 3.21)
20+
+cmake_minimum_required(VERSION 3.20)
21+
22+
project(ethos-u-corstone-300 VERSION 0.0.1)
23+
24+
--
25+
2.39.3
26+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
From b13de10ad4920da069d44efb99eceb86f6169a32 Mon Sep 17 00:00:00 2001
2+
From: Digant Desai <[email protected]>
3+
Date: Thu, 28 Sep 2023 18:05:30 -0700
4+
Subject: [PATCH 3/6] [Executorch][local-patch] Disable warnings to reduce
5+
verbosity
6+
7+
---
8+
cmake/toolchain/arm-none-eabi-gcc.cmake | 28 ++++++++++++-------------
9+
1 file changed, 14 insertions(+), 14 deletions(-)
10+
11+
diff --git a/cmake/toolchain/arm-none-eabi-gcc.cmake b/cmake/toolchain/arm-none-eabi-gcc.cmake
12+
index 093005e..0e6a2ed 100644
13+
--- a/cmake/toolchain/arm-none-eabi-gcc.cmake
14+
+++ b/cmake/toolchain/arm-none-eabi-gcc.cmake
15+
@@ -85,21 +85,21 @@ add_link_options(LINKER:--nmagic,--gc-sections)
16+
17+
# Compilation warnings
18+
add_compile_options(
19+
- -Wall
20+
- -Wextra
21+
+ # -Wall
22+
+ # -Wextra
23+
24+
- -Wcast-align
25+
- -Wdouble-promotion
26+
- -Wformat
27+
- -Wmissing-field-initializers
28+
- -Wnull-dereference
29+
- -Wredundant-decls
30+
- -Wshadow
31+
- -Wswitch
32+
- -Wswitch-default
33+
- -Wunused
34+
+ # -Wcast-align
35+
+ # -Wdouble-promotion
36+
+ # -Wformat
37+
+ # -Wmissing-field-initializers
38+
+ # -Wnull-dereference
39+
+ # -Wredundant-decls
40+
+ # -Wshadow
41+
+ # -Wswitch
42+
+ # -Wswitch-default
43+
+ # -Wunused
44+
45+
- -Wno-redundant-decls
46+
+ # -Wno-redundant-decls
47+
48+
- -Wno-psabi
49+
+ # -Wno-psabi
50+
)
51+
--
52+
2.39.3
53+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
From 5423ef7ec31e4260ec79f2f6e60deddc1640f3e4 Mon Sep 17 00:00:00 2001
2+
From: Digant Desai <[email protected]>
3+
Date: Mon, 2 Oct 2023 20:39:39 -0700
4+
Subject: [PATCH 4/6] [Executorch][local-patch] New phdr for .data section
5+
6+
---
7+
targets/corstone-300/platform.ld | 3 ++-
8+
1 file changed, 2 insertions(+), 1 deletion(-)
9+
10+
diff --git a/targets/corstone-300/platform.ld b/targets/corstone-300/platform.ld
11+
index 8d77329..8de77c4 100644
12+
--- a/targets/corstone-300/platform.ld
13+
+++ b/targets/corstone-300/platform.ld
14+
@@ -94,6 +94,7 @@ PHDRS
15+
{
16+
rom_exec PT_LOAD;
17+
rom_dram PT_LOAD;
18+
+ data PT_LOAD; /* HACK: New prog header for .data (and friends) going in DTCM */
19+
null PT_NULL;
20+
}
21+
22+
@@ -247,7 +248,7 @@ SECTIONS
23+
/* All data end */
24+
__data_end__ = .;
25+
26+
- } > DTCM :rom_exec
27+
+ } > DTCM :data
28+
29+
.sram.bss :
30+
{
31+
--
32+
2.39.3
33+
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
From dcf2e249d7f96f521e19c556d7529757aa94a0f5 Mon Sep 17 00:00:00 2001
2+
From: Digant Desai <[email protected]>
3+
Date: Tue, 3 Oct 2023 21:20:07 -0700
4+
Subject: [PATCH 5/6] [Executorch] Add pte to header script
5+
6+
---
7+
.../executorch_tests/pte_to_header.py | 65 +++++++++++++++++++
8+
1 file changed, 65 insertions(+)
9+
create mode 100644 applications/executorch_tests/pte_to_header.py
10+
11+
diff --git a/applications/executorch_tests/pte_to_header.py b/applications/executorch_tests/pte_to_header.py
12+
new file mode 100644
13+
index 0000000..37d88aa
14+
--- /dev/null
15+
+++ b/applications/executorch_tests/pte_to_header.py
16+
@@ -0,0 +1,65 @@
17+
+# Copyright (c) Meta Platforms, Inc. and affiliates.
18+
+# All rights reserved.
19+
+#
20+
+# This source code is licensed under the BSD-style license found in the
21+
+# LICENSE file in the root directory of this source tree.
22+
+
23+
+import binascii
24+
+import os
25+
+from argparse import ArgumentParser, ArgumentTypeError
26+
+
27+
+# Also see: https://git.mlplatform.org/ml/ethos-u/ml-embedded-evaluation-kit.git/tree/scripts/py/gen_model_cpp.py
28+
+
29+
+bytes_per_line = 32
30+
+hex_digits_per_line = bytes_per_line * 2
31+
+
32+
+
33+
+def input_file_path(path):
34+
+ if os.path.exists(path):
35+
+ return path
36+
+ else:
37+
+ raise ArgumentTypeError(f"input filepath:{path} does not exist")
38+
+
39+
+
40+
+parser = ArgumentParser()
41+
+parser.add_argument(
42+
+ "--pte",
43+
+ help="ExecuTorch .pte model file",
44+
+ type=input_file_path,
45+
+ required=True,
46+
+)
47+
+parser.add_argument(
48+
+ "--outdir",
49+
+ help="Output dir for model_pte.h",
50+
+ type=str,
51+
+ required=False,
52+
+ default=".",
53+
+)
54+
+parser.add_argument(
55+
+ "--section",
56+
+ help="Section attribute for the data array",
57+
+ type=str,
58+
+ required=False,
59+
+ default=".sram.data",
60+
+)
61+
+args = parser.parse_args()
62+
+outfile = os.path.join(args.outdir, "model_pte.h")
63+
+attr = f'__attribute__((section("{args.section}"), aligned(16))) char '
64+
+
65+
+with open(args.pte, "rb") as fr, open(
66+
+ outfile, "w"
67+
+) as fw:
68+
+ data = fr.read()
69+
+ hexstream = binascii.hexlify(data).decode("utf-8")
70+
+ hexstring = attr + "model_pte[] = {"
71+
+
72+
+ for i in range(0, len(hexstream), 2):
73+
+ if 0 == (i % hex_digits_per_line):
74+
+ hexstring += "\n"
75+
+ hexstring += "0x" + hexstream[i : i + 2] + ", "
76+
+
77+
+ hexstring += "};\n"
78+
+ fw.write(hexstring)
79+
+ print(
80+
+ f"Input: {args.pte} with {len(data)} bytes. Output: {outfile} with {len(hexstring)} bytes. Section: {args.section}."
81+
+ )
82+
--
83+
2.39.3
84+

0 commit comments

Comments
 (0)