cancel
Showing results for 
Search instead for 
Did you mean: 

AI relocatable run-time API lets the MCU HardFault.

WHUAN.1
Associate II

Hi,

I'm testing running a relocatable binary model.

But when I use ai_rel_network_run(), I get into HardFault.

The detailed error information is as follows, with my current test code attached.

Does anyone know how to fix this please?

Thanks in advance.

0693W00000SwKsQQAV.png

/* USER CODE BEGIN PV */
float aiInData[AI_NETWORK_IN_1_SIZE];
float aiOutData[AI_NETWORK_OUT_1_SIZE];
const char* activities[AI_NETWORK_OUT_1_SIZE] = {
  "stationary", "walking", "running"
};
ai_i32 batch;
 
ai_handle net = AI_HANDLE_NULL;
ai_handle weights_addr;
ai_bool res;
ai_network_report net_info;
AI_ALIGNED(4) ai_u8 rt_ai_ram[2596*4];
AI_ALIGNED(4) ai_u8 act[1708*4];
/* USER CODE END PV */

Main:

int main(void)
{
  /* USER CODE BEGIN 1 */
 
  /* USER CODE END 1 */
 
  /* MCU Configuration--------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* USER CODE BEGIN Init */
  
  /* USER CODE END Init */
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* USER CODE BEGIN SysInit */
  
  /* USER CODE END SysInit */
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_CRC_Init();
  MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */
  ai_error err;
  ai_rel_network_info rt_info;
  const ai_handle bin_addr = ai_network_reloc_img_get();
  err = ai_rel_network_rt_get_info(bin_addr, &rt_info);
 
  //rt_ai_ram = (uint8_t*)malloc(rt_info.rt_ram_xip);
  err = ai_rel_network_load_and_create(bin_addr, rt_ai_ram, rt_info.rt_ram_xip,
                                       AI_RELOC_RT_LOAD_MODE_XIP, &net);
  //act = (uint8_t*)malloc(rt_info.acts_sz);
  if (rt_info.weights)
    weights_addr = rt_info.weights;
  else
    weights_addr = "";
  const ai_handle act_addr = act;
  res = ai_rel_network_init(net, weights_addr, act_addr);
  res = ai_rel_network_get_report(net, &net_info);
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  uint32_t write_index = 0;
  
  while (1)
  {
    aiInData[write_index + 0] = 0.03025;
    aiInData[write_index + 1] = 0.1865;
    aiInData[write_index + 2] = 0.205;
    write_index += 3;
 
    if (write_index == AI_NETWORK_IN_1_SIZE) {
      write_index = 0;
 
      printf("Running inference\r\n");
 
      AI_Run(aiInData, aiOutData);
 
      /* Output results */
      for (uint32_t i = 0; i < AI_NETWORK_OUT_1_SIZE; i++) {
        printf("%8.6f ", aiOutData[i]);
      }
      uint32_t class = argmax(aiOutData, AI_NETWORK_OUT_1_SIZE);
      printf(": %d - %s\r\n", (int) class, activities[class]);
      HAL_Delay(500);
    }
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

AI_Run():

static void AI_Run(float *pIn, float *pOut)
{
  ai_i32 batch;
  ai_error err;
 
  ai_buffer *ai_input = net_info.inputs;
  ai_buffer *ai_output = net_info.outputs;
  
  ai_input[0].data = AI_HANDLE_PTR(pIn);
  ai_output[0].data = AI_HANDLE_PTR(pOut);
 
  batch = ai_rel_network_run(net, ai_input, ai_output);
  if (batch != 1) {
    err = ai_rel_network_get_error(net);
  }
}

And I had already increased both heap and stack size, but the problem still exists.

0693W00000SwKyEQAV.png

1 REPLY 1
jean-michel.d
ST Employee

Hello WHUAN,

What is the version of X-CUBE-AI tools used?

Since 7.1, the multi-heap feature has introduced an API break to initialize the model.

ai_rel_network_init() expects now a table of pointer for the weights and also for the activations.

ai_handle net = AI_HANDLE_NULL;
ai_handle weights_addr;
ai_handle activations;
...
const ai_handle acts[] = { activations };
res = ai_rel_network_init(net, &weights_addr, acts))
...
}

br,

Jean-Michel