-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Arm(R) Cortex(R)-M55 CPU and Arm(R) Ethos(TM)-U55 NPU Demo App #8922
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you 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 | ||
| # | ||
| # http://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. | ||
|
|
||
| # Makefile to build demo | ||
|
|
||
| # Setup build environment | ||
| BUILD_DIR := build | ||
| STANDALONE_CRT_PATH := $(shell python3 -c "import tvm.micro; print(tvm.micro.get_standalone_crt_dir())") | ||
|
|
||
| ARM_CPU = ARMCM55 | ||
| ETHOSU_PATH = /opt/arm/ethosu | ||
| ETHOSU_DRIVER_PATH ?= ${ETHOSU_PATH}/core_driver | ||
| CMSIS_PATH ?= ${ETHOSU_PATH}/cmsis | ||
| ETHOSU_PLATFORM_PATH ?= ${ETHOSU_PATH}/core_platform | ||
| CORSTONE_300_PATH = ${ETHOSU_PLATFORM_PATH}/targets/corstone-300 | ||
| PKG_COMPILE_OPTS = -g -Wall -O2 -Wno-incompatible-pointer-types -Wno-format -mcpu=cortex-m55 -mthumb -mfloat-abi=hard -std=gnu99 | ||
| CMAKE = cmake | ||
| CC = arm-none-eabi-gcc | ||
| AR = arm-none-eabi-ar | ||
| RANLIB = arm-none-eabi-ranlib | ||
| PKG_CFLAGS = ${PKG_COMPILE_OPTS} \ | ||
| -I${STANDALONE_CRT_PATH}/include \ | ||
| -I${STANDALONE_CRT_PATH}/src/runtime/crt/include \ | ||
| -Iinclude \ | ||
| -I${CORSTONE_300_PATH} \ | ||
| -I${ETHOSU_PATH}/core_driver/include \ | ||
| -I${CMSIS_PATH}/Device/ARM/${ARM_CPU}/Include/ \ | ||
| -I${CMSIS_PATH}/CMSIS/Core/Include \ | ||
| -I$(abspath $(BUILD_DIR))/codegen/host/include \ | ||
| -DETHOSU_TEST_RUNNER_TOL=${ETHOSU_TEST_RUNNER_TOL} | ||
| DRIVER_CMAKE_FLAGS = -DCMAKE_TOOLCHAIN_FILE=$(abspath $(BUILD_DIR))/../arm-none-eabi-gcc.cmake \ | ||
| -DETHOSU_LOG_SEVERITY=debug \ | ||
| -DCMAKE_SYSTEM_PROCESSOR=cortex-m55 | ||
| PKG_LDFLAGS = -lm -specs=nosys.specs -static -T corstone300.ld | ||
|
|
||
| $(ifeq VERBOSE,1) | ||
| QUIET ?= | ||
| $(else) | ||
| QUIET ?= @ | ||
| $(endif) | ||
|
|
||
| CODEGEN_SRCS = $(wildcard $(abspath $(BUILD_DIR))/codegen/host/src/*.c) | ||
| CODEGEN_OBJS = $(subst .c,.o,$(CODEGEN_SRCS)) | ||
| CMSIS_STARTUP_SRCS = $(wildcard ${CMSIS_PATH}/Device/ARM/${ARM_CPU}/Source/*.c) | ||
| UART_SRCS = $(wildcard ${CORSTONE_300_PATH}/*.c) | ||
|
|
||
| demo: $(BUILD_DIR)/demo | ||
|
|
||
| $(BUILD_DIR)/stack_allocator.o: $(STANDALONE_CRT_PATH)/src/runtime/crt/memory/stack_allocator.c | ||
| $(QUIET)mkdir -p $(@D) | ||
| $(QUIET)$(CC) -c $(PKG_CFLAGS) -o $@ $^ | ||
|
|
||
| $(BUILD_DIR)/crt_backend_api.o: $(STANDALONE_CRT_PATH)/src/runtime/crt/common/crt_backend_api.c | ||
| $(QUIET)mkdir -p $(@D) | ||
| $(QUIET)$(CC) -c $(PKG_CFLAGS) -o $@ $^ | ||
|
|
||
| # Build generated code | ||
| $(BUILD_DIR)/libcodegen.a: $(CODEGEN_SRCS) | ||
| $(QUIET)cd $(abspath $(BUILD_DIR)/codegen/host/src) && $(CC) -c $(PKG_CFLAGS) $(CODEGEN_SRCS) | ||
| $(QUIET)$(AR) -cr $(abspath $(BUILD_DIR)/libcodegen.a) $(CODEGEN_OBJS) | ||
| $(QUIET)$(RANLIB) $(abspath $(BUILD_DIR)/libcodegen.a) | ||
|
|
||
| # Build CMSIS startup code | ||
| ${BUILD_DIR}/libcmsis_startup.a: $(CMSIS_STARTUP_SRCS) | ||
| $(QUIET)mkdir -p $(abspath $(BUILD_DIR)/libcmsis_startup) | ||
| $(QUIET)cd $(abspath $(BUILD_DIR)/libcmsis_startup) && $(CC) -c $(PKG_CFLAGS) -D${ARM_CPU} $^ | ||
| $(QUIET)$(AR) -cr $(abspath $(BUILD_DIR)/libcmsis_startup.a) $(abspath $(BUILD_DIR))/libcmsis_startup/*.o | ||
| $(QUIET)$(RANLIB) $(abspath $(BUILD_DIR)/libcmsis_startup.a) | ||
|
|
||
| # Build UART code | ||
| ${BUILD_DIR}/libuart.a: $(UART_SRCS) | ||
| $(QUIET)mkdir -p $(abspath $(BUILD_DIR)/libuart) | ||
| $(QUIET)cd $(abspath $(BUILD_DIR)/libuart) && $(CC) -c $(PKG_CFLAGS) $^ | ||
| $(QUIET)$(AR) -cr $(abspath $(BUILD_DIR)/libuart.a) $(abspath $(BUILD_DIR))/libuart/*.o | ||
| $(QUIET)$(RANLIB) $(abspath $(BUILD_DIR)/libuart.a) | ||
|
|
||
| # Build Arm(R) Ethos(TM)-U core driver | ||
| ${BUILD_DIR}/ethosu_core_driver/libethosu_core_driver.a: | ||
| $(QUIET)mkdir -p $(@D) | ||
| $(QUIET)cd $(ETHOSU_DRIVER_PATH) && $(CMAKE) -B $(abspath $(BUILD_DIR)/ethosu_core_driver) $(DRIVER_CMAKE_FLAGS) | ||
| $(QUIET)cd $(abspath $(BUILD_DIR)/ethosu_core_driver) && $(MAKE) | ||
|
|
||
| # Build demo application | ||
| $(BUILD_DIR)/demo: src/demo.c $(BUILD_DIR)/stack_allocator.o $(BUILD_DIR)/crt_backend_api.o ${BUILD_DIR}/libcodegen.a ${BUILD_DIR}/libcmsis_startup.a ${BUILD_DIR}/ethosu_core_driver/libethosu_core_driver.a ${BUILD_DIR}/libuart.a | ||
| $(QUIET)mkdir -p $(@D) | ||
| $(QUIET)$(CC) $(PKG_CFLAGS) -o $@ $^ $(PKG_LDFLAGS) | ||
|
|
||
| clean: | ||
| $(QUIET)rm -rf $(BUILD_DIR)/crt | ||
|
|
||
| cleanall: | ||
| $(QUIET)rm -rf $(BUILD_DIR) | ||
|
|
||
| .SUFFIXES: | ||
|
|
||
| .DEFAULT: demo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| <!--- Licensed to the Apache Software Foundation (ASF) under one --> | ||
| <!--- or more contributor license agreements. See the NOTICE file --> | ||
| <!--- distributed with this work for additional information --> | ||
| <!--- regarding copyright ownership. The ASF licenses this file --> | ||
| <!--- to you 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 --> | ||
|
|
||
| <!--- http://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. --> | ||
|
|
||
|
|
||
| Running TVM on bare metal Arm(R) Cortex(R)-M55 CPU and Ethos(TM)-U55 NPU | ||
| ======================================================================== | ||
|
|
||
| This folder contains an example of how to use TVM to run a model | ||
| on bare metal Cortex(R)-M55 CPU and Ethos(TM)-U55 NPU. | ||
|
|
||
| Prerequisites | ||
| ------------- | ||
| If the demo is run in the ci_cpu Docker container provided with TVM, then the following | ||
| software will already be installed. | ||
|
|
||
| If the demo is not run in the ci_cpu Docker container, then you will need the following: | ||
| - Software required to build the Ethos(TM)-U driver stack and run the demo (These can all be | ||
| installed by running tvm/docker/install/ubuntu_install_ethosu_driver_stack.sh.) | ||
| - [Fixed Virtual Platform (FVP) based on Arm(R) Corstone(TM)-300 software](https://developer.arm.com/tools-and-software/open-source-software/arm-platforms-software/arm-ecosystem-fvps) | ||
| - [cmake 3.19.5](https://github.com/Kitware/CMake/releases/) | ||
| - [GCC toolchain from Arm(R)](https://developer.arm.com/-/media/Files/downloads/gnu-rm/10-2020q4/gcc-arm-none-eabi-10-2020-q4-major-x86_64-linux.tar.bz2) | ||
| - [Arm(R) Ethos(TM)-U NPU driver stack](https://review.mlplatform.org) | ||
| - [CMSIS](https://github.com/ARM-software/CMSIS_5) | ||
| - The python libraries listed in the requirements.txt of this directory | ||
| - These can be installed by running the following from the current directory: | ||
| ```bash | ||
| pip install -r ./requirements.txt | ||
| ``` | ||
|
|
||
| You will also need TVM which can either be: | ||
| - Built from source (see [Install from Source](https://tvm.apache.org/docs/install/from_source.html)) | ||
| - Installed from TLCPack(see [TLCPack](https://tlcpack.ai/)) | ||
|
|
||
| You will need to update your PATH environment variable to include the path to cmake 3.19.5 and the FVP. | ||
| For example if you've installed these in ```/opt/arm``` , then you would do the following: | ||
| ```bash | ||
| export PATH=/opt/arm/FVP_Corstone_SSE-300_Ethos-U55/models/Linux64_GCC-6.4:/opt/arm/cmake/bin:$PATH | ||
| ``` | ||
|
|
||
| Running the demo application | ||
| ---------------------------- | ||
| Type the following command to run the demo application: | ||
|
|
||
| ```bash | ||
| ./run_demo.sh | ||
| ``` | ||
|
|
||
| If the Ethos(TM)-U driver and/or CMSIS have not been installed in /opt/arm/ethosu then | ||
| the locations for these can be specified as arguments to run_demo.sh, for example: | ||
|
|
||
| ```bash | ||
| ./run_demo.sh --ethosu_driver_path /home/tvm-user/ethosu/core_driver --cmsis_path /home/tvm-user/cmsis \ | ||
| --ethosu_platform_path /home/tvm-user/ethosu/core_platform | ||
| ``` | ||
|
|
||
| This will: | ||
| - Download a quantized mobilenet v1 model | ||
| - Use tvmc to compile the model for Cortex(R)-M55 CPU and Ethos(TM)-U55 NPU | ||
| - Download an image of a kitten to run the model on | ||
| - Create a C header file inputs.c containing the image data as a C array | ||
| - Create a C header file outputs.c containing a C array where the output of inference will be stored | ||
| - Build the Ethos(TM)-U55 core driver | ||
| - Build the demo application | ||
| - Run the demo application on a Fixed Virtual Platform (FVP) based on Arm(R) Corstone(TM)-300 software | ||
| - The application will display what the image has been classified as e.g. "The image has been classified as 'tabby'" | ||
|
|
||
| Using your own image | ||
| -------------------- | ||
| The create_image.py script takes a single argument on the command line which is the path of the | ||
| image to be converted into an array of bytes for consumption by the model. | ||
|
|
||
| The demo can be modified to use an image of your choice by changing the following lines in run_demo.sh | ||
|
|
||
| ```bash | ||
| curl -sS https://s3.amazonaws.com/model-server/inputs/kitten.jpg -o ./kitten.jpg | ||
| python3 ./convert_image.py ./kitten.jpg | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you 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 | ||
| # | ||
| # http://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. | ||
|
|
||
| if (__TOOLCHAIN_LOADED) | ||
| return() | ||
| endif() | ||
| set(__TOOLCHAIN_LOADED TRUE) | ||
|
|
||
| set(CMAKE_SYSTEM_NAME Generic) | ||
| set(CMAKE_C_COMPILER "arm-none-eabi-gcc") | ||
| set(CMAKE_CXX_COMPILER "arm-none-eabi-g++") | ||
| set(CMAKE_SYSTEM_PROCESSOR "cortex-m55" CACHE STRING "Select Arm(R) Cortex(R)-M architecture. (cortex-m0, cortex-m3, cortex-m33, cortex-m4, cortex-m55, cortex-m7, etc)") | ||
|
|
||
| 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) | ||
|
|
||
| set(CMAKE_C_STANDARD 99) | ||
| set(CMAKE_CXX_STANDARD 14) | ||
|
|
||
| # The system processor could for example be set to cortex-m33+nodsp+nofp. | ||
| set(__CPU_COMPILE_TARGET ${CMAKE_SYSTEM_PROCESSOR}) | ||
| string(REPLACE "+" ";" __CPU_FEATURES ${__CPU_COMPILE_TARGET}) | ||
| list(POP_FRONT __CPU_FEATURES CMAKE_SYSTEM_PROCESSOR) | ||
|
|
||
| string(FIND ${__CPU_COMPILE_TARGET} "+" __OFFSET) | ||
| if(__OFFSET GREATER_EQUAL 0) | ||
| string(SUBSTRING ${__CPU_COMPILE_TARGET} ${__OFFSET} -1 CPU_FEATURES) | ||
| endif() | ||
|
|
||
| # Add -mcpu to the compile options to override the -mcpu the CMake toolchain adds | ||
| add_compile_options(-mcpu=${__CPU_COMPILE_TARGET}) | ||
|
|
||
| # Set floating point unit | ||
| if("${__CPU_COMPILE_TARGET}" MATCHES "\\+fp") | ||
| set(FLOAT hard) | ||
| elseif("${__CPU_COMPILE_TARGET}" MATCHES "\\+nofp") | ||
| set(FLOAT soft) | ||
| elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "cortex-m33" OR | ||
| "${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "cortex-m55") | ||
| set(FLOAT hard) | ||
| else() | ||
| set(FLOAT soft) | ||
| endif() | ||
|
|
||
| add_compile_options(-mfloat-abi=${FLOAT}) | ||
| add_link_options(-mfloat-abi=${FLOAT}) | ||
|
|
||
| # Link target | ||
| add_link_options(-mcpu=${__CPU_COMPILE_TARGET}) | ||
| add_link_options(-Xlinker -Map=output.map) | ||
|
|
||
| # | ||
| # Compile options | ||
| # | ||
| set(cxx_flags "-fno-unwind-tables;-fno-rtti;-fno-exceptions") | ||
|
|
||
| add_compile_options("-Wall;-Wextra;-Wsign-compare;-Wunused;-Wswitch-default;\ | ||
| -Wdouble-promotion;-Wredundant-decls;-Wshadow;-Wnull-dereference;\ | ||
| -Wno-format-extra-args;-Wno-unused-function;-Wno-unused-label;\ | ||
| -Wno-missing-field-initializers;-Wno-return-type;-Wno-format;-Wno-int-conversion" | ||
| "$<$<COMPILE_LANGUAGE:CXX>:${cxx_flags}>" | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you 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 | ||
| # | ||
| # http://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. | ||
|
|
||
| import os | ||
| import pathlib | ||
| import re | ||
| import sys | ||
| from PIL import Image | ||
| import numpy as np | ||
|
|
||
|
|
||
| def create_header_file(name, section, tensor_name, tensor_data, output_path): | ||
| """ | ||
| This function generates a header file containing the data from the numpy array provided. | ||
| """ | ||
| file_path = pathlib.Path(f"{output_path}/" + name).resolve() | ||
| # Create header file with npy_data as a C array | ||
| raw_path = file_path.with_suffix(".h").resolve() | ||
| with open(raw_path, "w") as header_file: | ||
| header_file.write( | ||
| "#include <tvmgen_default.h>\n" | ||
| + f"const size_t {tensor_name}_len = {tensor_data.size};\n" | ||
| + f'uint8_t {tensor_name}[] __attribute__((section("{section}"), aligned(16))) = "' | ||
| ) | ||
| data_hexstr = tensor_data.tobytes().hex() | ||
| for i in range(0, len(data_hexstr), 2): | ||
| header_file.write(f"\\x{data_hexstr[i:i+2]}") | ||
| header_file.write('";\n\n') | ||
|
|
||
|
|
||
| def create_headers(image_name): | ||
| """ | ||
| This function generates C header files for the input and output arrays required to run inferences | ||
| """ | ||
| img_path = os.path.join("./", f"{image_name}") | ||
|
|
||
| # Resize image to 224x224 | ||
| resized_image = Image.open(img_path).resize((224, 224)) | ||
| img_data = np.asarray(resized_image).astype("float32") | ||
|
|
||
| # Convert input to NCHW | ||
| img_data = np.transpose(img_data, (2, 0, 1)) | ||
|
|
||
| # Create input header file | ||
| input_data = img_data.astype(np.uint8) | ||
| create_header_file("inputs", "ethosu_scratch", "input", input_data, "./include") | ||
| # Create output header file | ||
| output_data = np.zeros([1001], np.uint8) | ||
| create_header_file( | ||
| "outputs", | ||
| "output_data_sec", | ||
| "output", | ||
| output_data, | ||
| "./include", | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| create_headers(sys.argv[1]) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you need
+es here (see: https://github.com/apache/tvm/blob/main/python/tvm/micro/interface_api.py#L59-L66)