277 lines
7.1 KiB
C
277 lines
7.1 KiB
C
/*
|
||
* @Author: xun & lixiao
|
||
* @Date: 2024-04-03 16:02:39
|
||
* @LastEditors: xun
|
||
* @LastEditTime: 2025-03-05 15:13:58
|
||
* @Description:
|
||
* Copyright (c) 2024 by CCYH, All Rights Reserved.
|
||
*/
|
||
#ifndef _NN_API_H_
|
||
#define _NN_API_H_
|
||
|
||
#ifdef __cplusplus
|
||
extern "C" {
|
||
#endif
|
||
|
||
#include <stdint.h>
|
||
#include "pnna_lite.h"
|
||
|
||
/*
|
||
* define this macro definition, between the cpu、dsp and pnna, zero copy of data can be achieved.
|
||
*/
|
||
// #define USE_CMEM_ALLOC_PNNA_BUFFER
|
||
|
||
#ifdef USE_CMEM_ALLOC_PNNA_BUFFER
|
||
#include <ti/cmem.h>
|
||
#endif
|
||
|
||
void TimeBegin(int id);
|
||
void TimeEnd(int id);
|
||
uint64_t TimeGet(int id);
|
||
|
||
#ifndef LINUX
|
||
|
||
extern uint64_t t_overhead;
|
||
|
||
extern uint32_t *network_address; //NBG
|
||
extern uint32_t *input_dat0_address; //input_0.dat
|
||
extern uint32_t *input_dat1_address; //input_1.dat
|
||
extern uint32_t my_network_size; //yolov4-tiny NBG
|
||
extern uint32_t my_input_size; //yolov4-tiny input_0.dat
|
||
|
||
void init_timer();
|
||
|
||
#endif
|
||
|
||
|
||
/**
|
||
* @brief 网络推理上下文,用于存放nbg相关信息
|
||
*
|
||
*/
|
||
typedef struct _nn_context {
|
||
/* nn information. */
|
||
char * nbg_name;
|
||
int input_count;
|
||
char ** input_names;
|
||
int output_count;
|
||
char ** output_names;
|
||
int * input_size;
|
||
int * output_size;
|
||
|
||
/* PNNA lite buffer objects. */
|
||
pnna_network network;
|
||
pnna_buffer * input_buffers;
|
||
pnna_buffer * output_buffers;
|
||
|
||
/* Use for CMEM alloc buffer. */
|
||
void ** input_buffs;
|
||
void ** output_buffs;
|
||
long * input_buffs_phys;
|
||
long * output_buffs_phys;
|
||
|
||
/* nn 推理时间相关参数 */
|
||
uint32_t loop_count;
|
||
uint32_t infer_cycle;
|
||
uint32_t infer_time;
|
||
uint64_t total_infer_cycle;
|
||
uint64_t total_infer_time;
|
||
} nn_context;
|
||
|
||
|
||
/**
|
||
* @brief 量化相关参数,输入、输出量化参数不相同,
|
||
* 有几个输出就有几个不同的输出量化参数
|
||
*
|
||
*/
|
||
typedef struct _quantization_params
|
||
{
|
||
unsigned int num_of_dims;
|
||
unsigned int sizes[6];
|
||
int data_format;
|
||
int quant_format;
|
||
union {
|
||
struct {
|
||
int fixed_point_pos;
|
||
} dfp;
|
||
|
||
struct {
|
||
float scale;
|
||
int zeroPoint;
|
||
} affine;
|
||
} quant_data;
|
||
|
||
int memory_type;
|
||
} quantization_params;
|
||
|
||
|
||
/**
|
||
* @brief 打开 PNNA 设备
|
||
*
|
||
* @return int 处理后的状态码,0表示成功打开设备
|
||
*/
|
||
int pnna_open();
|
||
|
||
/**
|
||
* @brief 关闭 PNNA 设备
|
||
*
|
||
* @return int 处理后的状态码,0表示成功关闭设备
|
||
*/
|
||
int pnna_close();
|
||
|
||
|
||
/**
|
||
* @brief 初始化网络,为context上下文结构体中的变量分配空间
|
||
*
|
||
* @param OUT context 网络推理上下文
|
||
* @param IN nbg_name NB文件名
|
||
* @param IN time_out 网络推理超时时间
|
||
* @return int 处理后的状态码,0表示成功,其他表示失败
|
||
*/
|
||
int nn_init(nn_context **context, const char *nbg_name,\
|
||
unsigned int time_out);
|
||
|
||
/**
|
||
* @brief 对已经量化后的buffer进行推理
|
||
*
|
||
* @param IN context 网络推理上下文
|
||
* @param IN input_buffer 已量化后的buffer
|
||
* @param OUT output_buffer 存放结果
|
||
* @return int 处理后的状态码,0表示成功,其他表示失败
|
||
*/
|
||
int nn_infer(nn_context *context, void **input_buffer, void **output_buffer);
|
||
|
||
/**
|
||
* @brief 获取网络输入的量化参数
|
||
*
|
||
* @param IN context 网络推理上下文
|
||
* @param IN index 网络输入的下标
|
||
* @param OUT params 网络输入端量化参数
|
||
* @return int 处理后的状态码,0表示成功,其他表示失败
|
||
*/
|
||
int get_input_param(nn_context *context, \
|
||
int index, quantization_params *params);
|
||
|
||
/**
|
||
* @brief 获取网络输出的量化参数
|
||
*
|
||
* @param IN context 网络推理上下文
|
||
* @param IN index 网络输出的下标
|
||
* @param OUT param 网络输出端量化参数
|
||
* @return int 处理后的状态码,0表示成功,其他表示失败
|
||
*/
|
||
int get_output_param(nn_context *context, \
|
||
int index, quantization_params *param);
|
||
|
||
|
||
/**
|
||
* @brief 获取网络输入和输出的量化参数
|
||
*
|
||
* @param IN context 网络推理上下文
|
||
* @param OUT input_param 网络输入端量化参数
|
||
* @param OUT output_param 网络输出端量化参数
|
||
* @return int 处理后的状态码,0表示成功,其他表示失败
|
||
*/
|
||
int get_quantize_params(nn_context *context, \
|
||
quantization_params *input_param,\
|
||
quantization_params *output_param);
|
||
|
||
|
||
/**
|
||
* @brief 获取网络每个输出的大小
|
||
*
|
||
* @param IN context 网络推理上下文
|
||
* @param IN type 输出数据的类型
|
||
* @param OUT size 每个输出的大小
|
||
* @return int 处理后的状态码,0表示成功,其他表示失败
|
||
*/
|
||
int get_output_sizes(nn_context *context, uint8_t type, int *size);
|
||
|
||
|
||
/**
|
||
* @brief 获取网络上下文中每个输出的大小,包含步长
|
||
*
|
||
* @param IN context 网络推理上下文
|
||
* @param OUT size 每个输出的大小
|
||
* @return int 处理后的状态码,0表示成功,其他表示失败
|
||
*/
|
||
int get_context_output_buffer_size(nn_context *context, int *size);
|
||
|
||
|
||
/**
|
||
* @brief 通过驱动,获取推理时间和周期数(非应用层函数)
|
||
*
|
||
* @param OUT context 结果存放在context结构体的变量中
|
||
* @param IN count 固定值:1
|
||
* @return int 处理后的状态码,0表示成功,其他表示失败
|
||
*/
|
||
int get_infer_time(nn_context *context, int count);
|
||
|
||
|
||
/**
|
||
* @brief 将数据进行量化操作
|
||
*
|
||
* @param IN context 网络推理上下文
|
||
* @param IN input_data 待量化的数据
|
||
* @param OUT output_data 量化后的结果
|
||
* @return int 处理后的状态码,0表示成功,其他表示失败
|
||
*/
|
||
int nn_quantize(nn_context *context, float **input_data, void **output_data);
|
||
|
||
|
||
/**
|
||
* @brief 将数据进行反量化操作
|
||
*
|
||
* @param IN context 网络推理上下文
|
||
* @param IN input_data 待反量化的数据
|
||
* @param OUT output_data 反量化后的结果
|
||
* @return int 处理后的状态码,0表示成功,其他表示失败
|
||
*/
|
||
int nn_dequantize(nn_context *context, void **input_data, float **output_data);
|
||
|
||
|
||
/**
|
||
* @brief 从量化参数集中获取后处理分类个数
|
||
*
|
||
* @param IN params 量化参数数据集
|
||
* @return int 返回得到的后处理分类个数
|
||
*/
|
||
int get_yolo_classes_from_params(quantization_params *params);
|
||
|
||
|
||
/**
|
||
* @brief 从网络推理上下文中获取后处理分类个数
|
||
*
|
||
* @param IN context 网络推理上下文
|
||
* @return int 返回得到的后处理分类个数
|
||
*/
|
||
int get_yolo_classes_from_context(nn_context *context);
|
||
|
||
/**
|
||
* @brief 释放初始化上下文时,malloc出的内存空间
|
||
*
|
||
* @param IN context 网络推理上下文
|
||
*/
|
||
int nn_destroy(nn_context *context);
|
||
|
||
/**
|
||
* @brief 仅进行推理
|
||
*
|
||
* @param IN context 网络推理上下文
|
||
* @param IN input_buffer 已量化后的buffer
|
||
*/
|
||
int nn_infer_only(nn_context *context, void **input_buffer);
|
||
|
||
/**
|
||
* @brief 等待推理完成
|
||
*
|
||
* @param IN context 网络推理上下文
|
||
* @param OUT output_buffer 存放结果
|
||
*/
|
||
int nn_wait_done(nn_context *context, void **output_buffer);
|
||
|
||
#ifdef __cplusplus
|
||
}
|
||
#endif
|
||
|
||
#endif // !_NN_API_H_
|