MATLAB Coder: How do I build the Arm CMSIS-NN library for Deep Learning C code generation and deployment?

15 visualizzazioni (ultimi 30 giorni)
I see a few deep learning networks supported for code generation using MATLAB Coder:
I'm looking to generate code from my deep learning network to run on Arm Cortex-M CPUs using MATLAB Coder and the Arm CMSIS-NN library. What are the steps to do this?

Risposta accettata

Bill Chou
Bill Chou il 29 Giu 2022
To generate and run C code for deep neural networks on Cortex-M hardware, you must have the CMSIS-NN library. See the post below for instructions on building the library:

Più risposte (1)

Bill Chou
Bill Chou il 14 Mar 2022
Modificato: Bill Chou il 29 Giu 2022
This is an old answer that has since been updated, and is kept here for reference only. Please see this post for up-to-date instructions.
To generate and run C code for deep neural networks on Cortex-M hardware, you must have the CMSIS-NN (Cortex Microcontroller Software Interface Standard - Neural Networks) library. This following describes the build steps for CMSIS-NN on Windows and Linux platforms that use a cross-compiler toolchain.
Requirements
To build the CMSIS-NN static library, you must first create a makefile. Copy the following code snippet into a file and save it as Makefile.mk. This sample makefile builds the CMSIS-NN library for the Cortex-M7 core. To build the library for other targets, modify the CFLAGS variable as appropriate for your target hardware.
STATIC_LIB_DIR=./lib
STATIC_LIB_FILE_NAME = libcmsisnn.a
CFLAGS= -fPIC -c -mcpu=cortex-m7 -Ofast -DNDEBUG
INC = -I../Core/Include -I../DSP/PrivateInclude -I../DSP/Include -I../NN/Include
rwildcard=$(foreach d,$(wildcard $(1:=/*)),$(call rwildcard,$d,$2) $(filter $(subst *,%,$2),$d))
SOURCE_FILES = $(call rwildcard,Source,*.c)
OBJECT_FILES = $(patsubst %.c,%.o,$(wildcard $(SOURCE_FILES)))
$(STATIC_LIB_FILE_NAME): $(OBJECT_FILES)
ar -r -o $(STATIC_LIB_DIR)/$@ $^
#Compiling every *.c to *.o
%.o: %.c dirmake
arm-none-eabi-gcc -c $(INC) $(CFLAGS) -o $@ $<
dirmake:
@mkdir -p $(STATIC_LIB_DIR)
clean:
rm -f $(OBJECT_FILES) $(STATIC_LIB_DIR)/$(STATIC_LIB_FILE_NAME)
Note: Make sure that Makefile was written properly, using the above code, and following makefile indentation rules.
We have validated the CMSIS-NN library build process below using:
  • Windows platform: GNU Arm Embedded Toolchain version 10.3.1
  • Linux platform: GNU Arm Embedded Toolchain version 8.3.0
Linux Platform
1. Install the open-source GNU Arm Embedded Toolchain provided by ARM (See https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm for more information).
2. Download the source code for CMSIS version 5.7.0 (available at https://github.com/ARM-software/CMSIS_5/archive/refs/tags/5.7.0.zip).
3. Unzip the source code to a folder and follow these steps to build and generate the static library:
  • Open a Linux terminal.
  • Change directory to the CMSIS-NN source folder by running this command. Here, <CMSIS Root folder> points to the extracted CMSIS folder.
cd <CMSIS Root folder>/CMSIS/NN
  • Copy the Makefile.mk file that you created to the current directory.
  • Run the makefile by using the make command.
make -f Makefile.mk
  • Running the makefile creates the static library libcmsisnn.a in the ./lib folder.
4. Configure the MATLAB environment to generate code that uses the CMSIS-NN library:
  • At /usr/local, create a folder named cmsisnn.
  • Copy the header files located at <CMSIS Root folder>/CMSIS/DSP/Include and <CMSIS Root folder>/CMSIS/NN/Include to the location /usr/local/cmsisnn/include.
  • Copy the generated static library located at <CMSIS Root folder>/CMSIS/NN/Include to the location /usr/local/cmsisnn/lib.
  • At the MATLAB command window, set the MATLAB environment variable CMSISNN_PATH to the location of the CMSIS-NN install folder.
setenv('CMSISNN_PATH’, ‘/usr/local/cmsisnn’)
Windows Platform
1. Install open-source GNU Arm Embedded Toolchain provided by ARM (available at https://developer.arm.com/-/media/Files/downloads/gnu-rm/10.3-2021.10/gcc-arm-none-eabi-10.3-2021.10-win32.exe).
2. Download and install the 'build system(make.exe)' and 'archiving(ar.exe)' tools. We have tested this step by using Cygwin (available at https://www.cygwin.com/) to run the makefile.
3. Download the source code for CMSIS version 5.7.0 (available at https://github.com/ARM-software/CMSIS_5/archive/refs/tags/5.7.0.zip).
4. Unzip the source code to a folder, and follow these steps to build and generate the static library:
  • Open a Windows command prompt.
  • Change directory to the CMSIS-NN source folder by running this command. Here, <CMSIS Root folder> points to the extracted CMSIS folder.
cd <CMSIS Root folder>\CMSIS\NN
  • Copy the Makefile.mk file that you created to the current directory.
  • Run the makefile by using the make command.
make -f Makefile.mk
  • Running the makefile creates the static library libcmsisnn.a in the .\lib folder.
5. Configure the MATLAB environment to generate code that uses the CMSIS-NN library:
  • At C:\Program Files, create a folder named cmsisnn.
  • Copy the header files located at <CMSIS Root folder>\CMSIS\DSP\Include and <CMSIS Root folder>\CMSIS\NN\Include to the location C:\Program Files\cmsisnn\include.
  • Copy the generated static library located at <CMSIS Root folder>\CMSIS\NN\Include to the location C:\Program Files\cmsisnn\lib.
  • At the MATLAB command window, set the MATLAB environment variable CMSISNN_PATH to the location of the CMSIS-NN install folder.
setenv('CMSISNN_PATH’, ‘C:\Program Files\cmsisnn’)

Prodotti


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by