Skip to content

Commit

Permalink
Tutorial for running TVM on Arm(R) Cortex(R)-M55 CPU and Ethos(TM)-U5…
Browse files Browse the repository at this point in the history
…5 NPU

- Moved tutorials/micro/cortex_m_ethosu.py to gallery/how_to/work_with_microtvm/micro_ethosu.py

Change-Id: Ib554df6649b7313d4414187d5334ec5b03f35f33
  • Loading branch information
grant-arm committed Oct 21, 2021
1 parent 1099f5c commit c9f5fe1
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 57 deletions.
6 changes: 4 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,11 @@ def git_describe_version(original_version):
"bring_your_own_datatypes.py",
],
"micro": [
"micro_autotune.py",
"micro_reference_vm.py",
"micro_tflite.py",
"cortex_m_ethosu.py",
],
"micro_ethosu.py",
],
}


Expand Down Expand Up @@ -355,6 +356,7 @@ def force_gc(gallery_conf, fname):
"gallery_dirs": gallery_dirs,
"subsection_order": subsection_order,
"filename_pattern": os.environ.get("TVM_TUTORIAL_EXEC_PATTERN", ".py"),
"find_mayavi_figures": False,
"download_all_examples": False,
"min_reported_time": 60,
"expected_failing_examples": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,20 @@
# .. code-block:: text
#
# usage: tvmc [-h] [-v] [--version] {tune,compile,run} ...
#
#
# TVM compiler driver
#
#
# optional arguments:
# -h, --help show this help message and exit
# -v, --verbose increase verbosity
# --version print the version and exit
#
#
# commands:
# {tune,compile,run}
# tune auto-tune a model
# compile compile a model.
# run run a compiled module
#
#
# TVMC - TVM driver command-line interface
#

Expand Down Expand Up @@ -238,8 +238,8 @@
# 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.
Expand All @@ -257,21 +257,21 @@
# 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")
Expand All @@ -284,8 +284,8 @@
# output_data,
# "./include",
# )
#
#
#
#
# if __name__ == "__main__":
# create_headers(sys.argv[1])
#
Expand All @@ -312,27 +312,27 @@
# import os
# import pathlib
# import sys
#
#
#
#
# def create_labels_header(labels_file, section, output_path):
# """
# This function generates a header file containing the ImageNet labels as an array of strings
# """
# labels_path = pathlib.Path(labels_file).resolve()
# file_path = pathlib.Path(f"{output_path}/labels.h").resolve()
#
#
# with open(labels_path) as f:
# labels = f.readlines()
#
#
# with open(file_path, "w") as header_file:
# header_file.write(f'char* labels[] __attribute__((section("{section}"), aligned(16))) = {{')
#
#
# for _, label in enumerate(labels):
# header_file.write(f'"{label.rstrip()}",')
#
#
# header_file.write("};\n")
#
#
#
#
# if __name__ == "__main__":
# create_labels_header(sys.argv[1], "ethosu_scratch", "./include")
#
Expand Down Expand Up @@ -360,25 +360,25 @@
#
# #include <stdio.h>
# #include <tvm_runtime.h>
#
#
# #include "ethosu_mod.h"
# #include "uart.h"
#
#
# // Header files generated by convert_image.py and convert_labels.py
# #include "inputs.h"
# #include "labels.h"
# #include "outputs.h"
#
#
# int abs(int v) { return v * ((v > 0) - (v < 0)); }
#
#
# int main(int argc, char** argv) {
# uart_init();
# printf("Starting Demo\n");
# EthosuInit();
#
#
# printf("Allocating memory\n");
# StackMemoryManager_Init(&app_workspace, g_aot_memory, WORKSPACE_SIZE);
#
#
# printf("Running inference\n");
# struct tvmgen_default_outputs outputs = {
# .output = output,
Expand All @@ -387,7 +387,7 @@
# .input = input,
# };
# tvmgen_default_run(&inputs, &outputs);
#
#
# // Calculate index of max value
# uint8_t max_value = 0;
# int32_t max_index = -1;
Expand All @@ -398,7 +398,7 @@
# }
# }
# printf("The image has been classified as '%s'\n", labels[max_index]);
#
#
# // The FVP will shut down when it receives "EXITTHESIM" on the UART
# printf("EXITTHESIM\n");
# while (1 == 1)
Expand Down Expand Up @@ -463,10 +463,10 @@
#
# #ifndef TVM_RUNTIME_CRT_CONFIG_H_
# #define TVM_RUNTIME_CRT_CONFIG_H_
#
#
# /*! Log level of the CRT runtime */
# #define TVM_CRT_LOG_LEVEL TVM_CRT_LOG_LEVEL_DEBUG
#
#
# #endif // TVM_RUNTIME_CRT_CONFIG_H_
#
#
Expand All @@ -479,39 +479,39 @@
# #include <stdlib.h>
# #include <tvm/runtime/c_runtime_api.h>
# #include <tvm/runtime/crt/stack_allocator.h>
#
#
# #ifdef __cplusplus
# extern "C" {
# #endif
#
#
# #define WORKSPACE_SIZE (16384 * 1024)
# __attribute__((section("ethosu_scratch"))) static uint8_t g_aot_memory[WORKSPACE_SIZE];
#
#
# tvm_workspace_t app_workspace;
#
#
# void __attribute__((noreturn)) TVMPlatformAbort(tvm_crt_error_t error_code) {
# printf("TVMPlatformAbort: %d\n", error_code);
# printf("EXITTHESIM\n");
# exit(-1);
# }
#
#
# tvm_crt_error_t TVMPlatformMemoryAllocate(size_t num_bytes, DLDevice dev, void** out_ptr) {
# return StackMemoryManager_Allocate(&app_workspace, num_bytes, out_ptr);
# }
#
#
# tvm_crt_error_t TVMPlatformMemoryFree(void* ptr, DLDevice dev) {
# return StackMemoryManager_Free(&app_workspace, ptr);
# }
#
#
# void TVMLogf(const char* msg, ...) {
# va_list args;
# va_start(args, msg);
# vfprintf(stdout, msg, args);
# va_end(args);
# }
#
#
# TVM_DLL int TVMFuncRegisterGlobal(const char* name, TVMFunctionHandle f, int override) { return 0; }
#
#
# #ifdef __cplusplus
# }
# #endif
Expand Down Expand Up @@ -841,7 +841,7 @@
# # 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
Expand All @@ -867,67 +867,67 @@
# -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
#

Expand Down

0 comments on commit c9f5fe1

Please sign in to comment.