Skip to content

Add example script to run on cpu #611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions examples/arm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
## ExecuTorch on ARM Cortex-M55 + Ethos-U55

This dir contains scripts to help you prepare setup needed to run a PyTorch
model on a ARM Corstone-300 platform via ExecuTorch. Corstone-300 platform
contains the Cortex-M55 CPU and Ethos-U55 NPU.

We will start from a PyTorch model in python, export it, convert it to a `.pte`
file - A binary format adopted by ExecuTorch. Then we will take the `.pte`
model file and embed that with a baremetal application executor_runner. We will
then take the executor_runner file, which contains not only the `.pte` file but
also necessary software component to run standalone on a baremetal system.
Lastly, we will run the executor_runner binary on a Corstone-300 FVP Simulator
platform.

### Example workflow

There are two main scripts, setup.sh and run.sh. Each takes one optional,
positional argument. It is a path to a scratch dir to download and generate
build artifacts. If supplied, the same argument must be supplied to both the scripts.

run.sh also takes a second optional positional arg to specify a buck2 command.

To run these scripts. On a Linux system, in a terminal, with a working internet connection,
```
# Step [1] - setup necessary tools
$ ./setup.sh

# Step [2] - build + run ExecuTorch and executor_runner baremetal application
# suited for Corstone300 to run a simple PyTorch model.
$ ./run.sh
```
105 changes: 105 additions & 0 deletions examples/arm/ethos-u-setup/arm-none-eabi-gcc.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#
# Copyright (c) 2020-2022 Arm Limited. All rights reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the License); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an AS IS BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Copied this file from core_platform/cmake/toolchain/arm-non-eabi-gcc.cmake
# And modified to align better with cs300 platform

set(TARGET_CPU "cortex-m55" CACHE STRING "Target CPU")
string(TOLOWER ${TARGET_CPU} CMAKE_SYSTEM_PROCESSOR)

set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_C_COMPILER "arm-none-eabi-gcc")
set(CMAKE_CXX_COMPILER "arm-none-eabi-g++")
set(CMAKE_ASM_COMPILER "arm-none-eabi-gcc")
set(CMAKE_LINKER "arm-none-eabi-ld")

set(CMAKE_EXECUTABLE_SUFFIX ".elf")
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

# Select C/C++ version
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 14)

set(GCC_CPU ${CMAKE_SYSTEM_PROCESSOR})
string(REPLACE "cortex-m85" "cortex-m55" GCC_CPU ${GCC_CPU})

# Compile options
add_compile_options(
-mcpu=${GCC_CPU}
-mthumb
"$<$<CONFIG:DEBUG>:-gdwarf-3>"
"$<$<COMPILE_LANGUAGE:CXX>:-fno-unwind-tables;-fno-rtti;-fno-exceptions>"
-fdata-sections
-ffunction-sections)

# Compile defines
add_compile_definitions(
"$<$<NOT:$<CONFIG:DEBUG>>:NDEBUG>")

# Link options
add_link_options(
-mcpu=${GCC_CPU}
-mthumb
--specs=nosys.specs)

# Set floating point unit
if(CMAKE_SYSTEM_PROCESSOR MATCHES "\\+fp")
set(FLOAT hard)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "\\+nofp")
set(FLOAT soft)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "cortex-m33(\\+|$)" OR
CMAKE_SYSTEM_PROCESSOR MATCHES "cortex-m55(\\+|$)" OR
CMAKE_SYSTEM_PROCESSOR MATCHES "cortex-m85(\\+|$)")
set(FLOAT hard)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "cortex-m4(\\+|$)" OR
CMAKE_SYSTEM_PROCESSOR MATCHES "cortex-m7(\\+|$)")
set(FLOAT hard)
set(FPU_CONFIG "fpv4-sp-d16")
add_compile_options(-mfpu=${FPU_CONFIG})
add_link_options(-mfpu=${FPU_CONFIG})
else()
set(FLOAT soft)
endif()

if(FLOAT)
add_compile_options(-mfloat-abi=${FLOAT})
add_link_options(-mfloat-abi=${FLOAT})
endif()

add_link_options(LINKER:--nmagic,--gc-sections)

# Compilation warnings
add_compile_options(
# -Wall
# -Wextra
# -Wcast-align
# -Wdouble-promotion
# -Wformat
# -Wmissing-field-initializers
# -Wnull-dereference
# -Wredundant-decls
# -Wshadow
# -Wswitch
# -Wswitch-default
# -Wunused
# -Wno-redundant-decls
# -Wno-psabi
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
From 48a99f4b00e504c13cd74ca44a5ce7128f719cba Mon Sep 17 00:00:00 2001
From: Digant Desai <[email protected]>
Date: Tue, 3 Oct 2023 21:20:21 -0700
Subject: [PATCH 1/6] [Executorch] Add README

---
applications/executorch_tests/README.md | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
create mode 100644 applications/executorch_tests/README.md

diff --git a/applications/executorch_tests/README.md b/applications/executorch_tests/README.md
new file mode 100644
index 0000000..f2dfb05
--- /dev/null
+++ b/applications/executorch_tests/README.md
@@ -0,0 +1,16 @@
+## ExecuTorch
+A unified ML software stack within the PyTorch platform for edge devices. It
+defines new compiler entry points as well as a state-of-art runtime.
+
+Home: https://github.com/pytorch/executorch/
+
+### executor_runner
+
+This test is a simple wrapper around ExecuTorch runtime, capable of running
+`.pte` model files compatible with ExecuTorch.
+
+If configured correctly with `ET_*` CMake variables pointing to the ExecuTorch
+project build, then this test bin executes `model.pte.h` file converted from
+`model.pte` using `pte_to_header.py`, from the ExecuTorch project root dir,
+containing an ExecuTorch compatible PyTorch model on the Costrone 300 FVP using
+ExecuTorch runtime.
--
2.39.3

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
From 3359f94fc57ac76b3f5995d4453975251b56ae71 Mon Sep 17 00:00:00 2001
From: Digant Desai <[email protected]>
Date: Thu, 28 Sep 2023 18:05:03 -0700
Subject: [PATCH 2/6] [Executorch][local-patch] regress cmake version from 3.21
--> 3.20

---
targets/corstone-300/CMakeLists.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/targets/corstone-300/CMakeLists.txt b/targets/corstone-300/CMakeLists.txt
index 62205bb..7dda8a1 100644
--- a/targets/corstone-300/CMakeLists.txt
+++ b/targets/corstone-300/CMakeLists.txt
@@ -42,7 +42,7 @@ set(MEMORY_ARENA "dram" CACHE STRING "Memory config for arena")
# Project
#############################################################################

-cmake_minimum_required(VERSION 3.21)
+cmake_minimum_required(VERSION 3.20)

project(ethos-u-corstone-300 VERSION 0.0.1)

--
2.39.3

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
From b13de10ad4920da069d44efb99eceb86f6169a32 Mon Sep 17 00:00:00 2001
From: Digant Desai <[email protected]>
Date: Thu, 28 Sep 2023 18:05:30 -0700
Subject: [PATCH 3/6] [Executorch][local-patch] Disable warnings to reduce
verbosity

---
cmake/toolchain/arm-none-eabi-gcc.cmake | 28 ++++++++++++-------------
1 file changed, 14 insertions(+), 14 deletions(-)

diff --git a/cmake/toolchain/arm-none-eabi-gcc.cmake b/cmake/toolchain/arm-none-eabi-gcc.cmake
index 093005e..0e6a2ed 100644
--- a/cmake/toolchain/arm-none-eabi-gcc.cmake
+++ b/cmake/toolchain/arm-none-eabi-gcc.cmake
@@ -85,21 +85,21 @@ add_link_options(LINKER:--nmagic,--gc-sections)

# Compilation warnings
add_compile_options(
- -Wall
- -Wextra
+ # -Wall
+ # -Wextra

- -Wcast-align
- -Wdouble-promotion
- -Wformat
- -Wmissing-field-initializers
- -Wnull-dereference
- -Wredundant-decls
- -Wshadow
- -Wswitch
- -Wswitch-default
- -Wunused
+ # -Wcast-align
+ # -Wdouble-promotion
+ # -Wformat
+ # -Wmissing-field-initializers
+ # -Wnull-dereference
+ # -Wredundant-decls
+ # -Wshadow
+ # -Wswitch
+ # -Wswitch-default
+ # -Wunused

- -Wno-redundant-decls
+ # -Wno-redundant-decls

- -Wno-psabi
+ # -Wno-psabi
)
--
2.39.3

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
From 5423ef7ec31e4260ec79f2f6e60deddc1640f3e4 Mon Sep 17 00:00:00 2001
From: Digant Desai <[email protected]>
Date: Mon, 2 Oct 2023 20:39:39 -0700
Subject: [PATCH 4/6] [Executorch][local-patch] New phdr for .data section

---
targets/corstone-300/platform.ld | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/targets/corstone-300/platform.ld b/targets/corstone-300/platform.ld
index 8d77329..8de77c4 100644
--- a/targets/corstone-300/platform.ld
+++ b/targets/corstone-300/platform.ld
@@ -94,6 +94,7 @@ PHDRS
{
rom_exec PT_LOAD;
rom_dram PT_LOAD;
+ data PT_LOAD; /* HACK: New prog header for .data (and friends) going in DTCM */
null PT_NULL;
}

@@ -247,7 +248,7 @@ SECTIONS
/* All data end */
__data_end__ = .;

- } > DTCM :rom_exec
+ } > DTCM :data

.sram.bss :
{
--
2.39.3

Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
From dcf2e249d7f96f521e19c556d7529757aa94a0f5 Mon Sep 17 00:00:00 2001
From: Digant Desai <[email protected]>
Date: Tue, 3 Oct 2023 21:20:07 -0700
Subject: [PATCH 5/6] [Executorch] Add pte to header script

---
.../executorch_tests/pte_to_header.py | 65 +++++++++++++++++++
1 file changed, 65 insertions(+)
create mode 100644 applications/executorch_tests/pte_to_header.py

diff --git a/applications/executorch_tests/pte_to_header.py b/applications/executorch_tests/pte_to_header.py
new file mode 100644
index 0000000..37d88aa
--- /dev/null
+++ b/applications/executorch_tests/pte_to_header.py
@@ -0,0 +1,65 @@
+# Copyright (c) Meta Platforms, Inc. and affiliates.
+# All rights reserved.
+#
+# This source code is licensed under the BSD-style license found in the
+# LICENSE file in the root directory of this source tree.
+
+import binascii
+import os
+from argparse import ArgumentParser, ArgumentTypeError
+
+# Also see: https://git.mlplatform.org/ml/ethos-u/ml-embedded-evaluation-kit.git/tree/scripts/py/gen_model_cpp.py
+
+bytes_per_line = 32
+hex_digits_per_line = bytes_per_line * 2
+
+
+def input_file_path(path):
+ if os.path.exists(path):
+ return path
+ else:
+ raise ArgumentTypeError(f"input filepath:{path} does not exist")
+
+
+parser = ArgumentParser()
+parser.add_argument(
+ "--pte",
+ help="ExecuTorch .pte model file",
+ type=input_file_path,
+ required=True,
+)
+parser.add_argument(
+ "--outdir",
+ help="Output dir for model_pte.h",
+ type=str,
+ required=False,
+ default=".",
+)
+parser.add_argument(
+ "--section",
+ help="Section attribute for the data array",
+ type=str,
+ required=False,
+ default=".sram.data",
+)
+args = parser.parse_args()
+outfile = os.path.join(args.outdir, "model_pte.h")
+attr = f'__attribute__((section("{args.section}"), aligned(16))) char '
+
+with open(args.pte, "rb") as fr, open(
+ outfile, "w"
+) as fw:
+ data = fr.read()
+ hexstream = binascii.hexlify(data).decode("utf-8")
+ hexstring = attr + "model_pte[] = {"
+
+ for i in range(0, len(hexstream), 2):
+ if 0 == (i % hex_digits_per_line):
+ hexstring += "\n"
+ hexstring += "0x" + hexstream[i : i + 2] + ", "
+
+ hexstring += "};\n"
+ fw.write(hexstring)
+ print(
+ f"Input: {args.pte} with {len(data)} bytes. Output: {outfile} with {len(hexstring)} bytes. Section: {args.section}."
+ )
--
2.39.3

Loading