Difference between revisions of "AI:How to run larger models on STM32H747I-DISCO"

[checked revision] [quality revision]
m
m
 

This article describes how to allocate more internal Flash memory to the Cortex M7 of the STM32H747 Discovery board.

The STM32H747 microcontroller is a dual-core Cortex M7 + Cortex M4 with 2 Mbytes of internal Flash memory. By default, half of the Flash memory is allocated to the Cortex M7 and the other half to the Cortex M4. When using STM32Cube.AI to generate STM32 code from a pre-trained neural network, model weights are stored by default in internal Flash. To run models exceeding 1 Mbyte in weight, allocation of more Flash memory to the Cortex M7 is required (or on the contrary to the Cortex M4 depending on the selected core).

This article shows how to modify the internal Flash memory allocation. The tutorial shows how to allocate 1984 Kbytes of Flash memory to the Cortex M7 and the rest of the Flash memory (64 Kbytes) to the Cortex M4.

Info white.png Information
  • The STM32Cube.AI (X-CUBE-AI) software is intended for the generation of optimized C code for STM32 and neural network inference. It is delivered under the Mix Ultimate Liberty+OSS+3rd-party V1 software license agreement (SLA0048).

1 Prerequisites[edit]

1.1 Hardware[edit]

1.2 Software[edit]

2 Modify the boot addresses[edit]

The Cortex M7 boots by default at address 0x08000000, whereas the Cortex M4 boots at address 0x08100000. This should be modified as follows:

  1. connect the board to the PC by a USB cable using the USB STLink connector
  2. Open the STM32CubeProgrammer tool and connect to the board
  3. Click on OB (Option Bytes) to modify the BOOT_CM4_ADD0
  4. Modify the CM4 boot address bytes to 0x81F0000 address instead of 0x8100000 (simply modify the value from 0x810 to 8x1F

The initial configuration is as shown below:

STM32H747 default Option Bytes

The final configuration shall be as shown below:

STM32H747 modified Option Bytes

2.1 Generate the code with STM32Cube.AI[edit]

Generate the project for your model as usual with STM32Cube.AI. Do not use the one click "Validation on target" feature from the UI, as it would take the default memory allocation. Instead use the Project Manager. Be sure to select the "Device Application" in the menu when selecting the X-CUBE-AI software package, for instance the Validation application as below:

STM32CubeMX X-CUBE-AI software pack selector

Then use the Generate Project feature (be sure to enter a name for your project as well as selecting your IDE in the Project Manager table).

2.2 Modify the linker scripts[edit]

Once the project has been generated, the linker script reflects the new memory allocation. The modification for STM32Cube IDE linker scripts is detailed below: Cortex M7 Flash linker script stm32h747xi_flash_cm7.ld

This snippet is provided AS IS, and by taking it, you agree to be bound to the license terms that can be found here for the component: Linker Scripts.
/* Memories definition */
MEMORY
{
  RAM    (xrw)   : ORIGIN = 0x24000000, LENGTH =  512K    /* Memory is divided. Actual start is 0x24000000 and actual length is 512K */
  FLASH   (rx)   : ORIGIN = 0x08000000, LENGTH = 1984K    /* Memory is divided. Actual start is 0x08000000 and actual length is 1984K */
 

Cortex M7 RAM linker script stm32h747xi_ram_cm7.ld :

This snippet is provided AS IS, and by taking it, you agree to be bound to the license terms that can be found here for the component: Linker Scripts.
/* Memories definition */
MEMORY
{
  RAM    (xrw)   : ORIGIN = 0x24000000, LENGTH =  512K    /* Memory is divided. Actual start is 0x24000000 and actual length is 512K */
  FLASH   (rx)   : ORIGIN = 0x08000000, LENGTH = 1984K    /* Memory is divided. Actual start is 0x8000000 and actual length is 1984K */
.

Cortex M4 Flash linker script stm32h747xi_flash_cm4.ld:

This snippet is provided AS IS, and by taking it, you agree to be bound to the license terms that can be found here for the component: Linker Scripts.
/* Specify the memory areas */
MEMORY
{
FLASH (rx)     : ORIGIN = 0x081F0000, LENGTH = 64K
RAM (xrw)      : ORIGIN = 0x10000000, LENGTH = 288K
}

You can then build the application and load it to the STM32 target using the IDE or STM32CubeProgrammer.

2.3 Run the validation on a target from STM32Cube.AI[edit]

Once the validation application has been uploaded in the STM32 target with the board connected to the PC through the STLink USB connector, the firmware starts, and waits for commands from the PC. On the configuration panel of STM32Cube.AI, you can then launch the "Validation on target". Click on the corresponding button:

STM32Cube.AI model configuration

The following pop-up window opens. Select the COM port manually or let the plugin do it automatically. Do not enable the "Automatic compilation and download" option otherwise the firmware is replaced by the default one without the new Flash allocation":

STM32Cube.AI validation on target once FW loaded

You can then click on "OK" to run the validation on target as usual.


This article describes how to allocate more internal Flash memory to the Cortex M7 of the STM32H747 Discovery board.

The STM32H747 microcontroller is a dual-core Cortex M7 + Cortex M4 with 2 Mbytes of internal Flash memory.
By default, half of the Flash memory is allocated to the Cortex M7 and the other half to the Cortex M4.
When using STM32Cube.AI to generate STM32 code from a pre-trained neural network, model weights are stored by default in internal Flash. To run models exceeding 1 Mbyte in weight, allocation of more Flash memory to the Cortex M7 is required (or on the contrary to the Cortex M4 depending on the selected core).

This article shows how to modify the internal Flash memory allocation. The tutorial shows how to allocate 1984 Kbytes of Flash memory to the Cortex M7 and the rest of the Flash memory (64 Kbytes) to the Cortex M4.

{{Info| 
* The STM32Cube.AI ([https://www.st.com/en/product/x-cube-ai X-CUBE-AI]) software is intended for the generation of optimized C code for STM32 and neural network inference. It is delivered under the Mix Ultimate Liberty+OSS+3rd-party V1 software license agreement ([https://st.com/SLA0048 SLA0048]). }}

= Prerequisites =

== Hardware ==

* [https://www.st.com/en/evaluation-tools/stm32h747i-disco.html STM32H747I-DISCO] 
* A Micro-USB to USB cables

== Software ==

* [https://www.st.com/en/development-tools/stm32cubeprog.html STM32CubeProgrammer] 
* [https://www.st.com/en/development-tools/stm32cubeide.html STM32Cube IDE]

= Modify the boot addresses =

The Cortex M7 boots by default at address 0x08000000, whereas the Cortex M4 boots at address 0x08100000. 
This should be modified as follows:
# connect the board to the PC by a USB cable using the USB STLink connector
# Open the STM32CubeProgrammer tool and connect to the board
# Click on OB (Option Bytes) to modify the BOOT_CM4_ADD0 
# Modify the CM4 boot address bytes to 0x81F0000 address instead of 0x8100000 (simply modify the value from 0x810 to 8x1F
The initial configuration is as shown below:<div class="res-img">

[[File:H747_OB_Init.png|center|alt=STM32H747 default Option Bytes|STM32H747 default Option Bytes]]</div>

The final configuration shall be as shown below:<div class="res-img">

[[File:H747_OB_Final.png|center|alt=STM32H747 modified Option Bytes|STM32H747 modified Option Bytes]]</div>


== Generate the code with STM32Cube.AI ==

Generate the project for your model as usual with STM32Cube.AI.
Do not use the one click "Validation on target" feature from the UI, as it would take the default memory allocation.
Instead use the Project Manager.
Be sure to select the "{{Highlight|Device Application}}" in the menu when selecting the X-CUBE-AI software package, for instance the Validation application as below:<div class="res-img">

[[File:CubeAI_SoftwarePacks_Selector.png|center|alt=STM32CubeMX X-CUBE-AI software pack selector|STM32CubeMX X-CUBE-AI software pack selector]]</div>

Then use the Generate Project feature (be sure to enter a name for your project as well as selecting your IDE in the Project Manager table). 

== Modify the linker scripts ==
Once the project has been generated, the linker script reflects the new memory allocation.
The modification for STM32Cube IDE linker scripts is detailed below:
Cortex M7 Flash linker script stm32h747xi_flash_cm7.ld
{{Snippet | category=AI | component=Linker Scripts | snippet=<source lang="c" highlight="5">

/* Memories definition */
MEMORY
{
  RAM    (xrw)   : ORIGIN = 0x24000000, LENGTH =  512K    /* Memory is divided. Actual start is 0x24000000 and actual length is 512K */
  FLASH   (rx)   : ORIGIN = 0x08000000, LENGTH = 1984K    /* Memory is divided. Actual start is 0x08000000 and actual length is 1984K */
 …</source>

}}

Cortex M7 RAM linker script stm32h747xi_ram_cm7.ld :
{{Snippet | category=AI | component=Linker Scripts | snippet=<source lang="c" highlight="5">

/* Memories definition */
MEMORY
{
  RAM    (xrw)   : ORIGIN = 0x24000000, LENGTH =  512K    /* Memory is divided. Actual start is 0x24000000 and actual length is 512K */
  FLASH   (rx)   : ORIGIN = 0x08000000, LENGTH = 1984K    /* Memory is divided. Actual start is 0x8000000 and actual length is 1984K */
….</source>

}}
Cortex M4 Flash linker script stm32h747xi_flash_cm4.ld:
{{Snippet | category=AI | component=Linker Scripts | snippet=<source lang="c" highlight="4">

/* Specify the memory areas */
MEMORY
{
FLASH (rx)     : ORIGIN = 0x081F0000, LENGTH = 64K
RAM (xrw)      : ORIGIN = 0x10000000, LENGTH = 288K
}</source>

}}

You can then build the application and load it to the STM32 target using the IDE or STM32CubeProgrammer.

== Run the validation on a target from STM32Cube.AI ==

Once the validation application has been uploaded in the STM32 target with the board connected to the PC through the STLink USB connector, the firmware starts, and waits for commands from the PC.
On the configuration panel of STM32Cube.AI, you can then launch the "Validation on target". Click on the corresponding button:<div class="res-img">

[[File:CubeAI_ConfigModel.png|center|alt=STM32Cube.AI model configuration|STM32Cube.AI model configuration]]</div>

The following pop-up window opens. Select the COM port manually or let the plugin do it automatically. {{Highlight|Do not enable the "Automatic compilation and download" option}} otherwise the firmware is replaced by the default one without the new Flash allocation":<div class="res-img">

[[File:CubeAI_ValidationOnTarget.png|center|alt=STM32Cube.AI validation on target once FW loaded|STM32Cube.AI validation on target once FW loaded]]</div>

You can then click on "OK" to run the validation on target as usual.
<noinclude>
[[Category:STM32Cube.AI | 30]]{{PublicationRequestId | 20965 | 2021-09-01 }}	</noinclude>
Line 104: Line 104:
   
 
<noinclude>
 
<noinclude>
  +
[[Category:STM32Cube.AI | 30]]
 
{{PublicationRequestId | 20965 | 2021-09-01 }}
 
{{PublicationRequestId | 20965 | 2021-09-01 }}
 
</noinclude>
 
</noinclude>