KPU寄存器配置说明
芯片厂家没有给出寄存器手册,我们从kpu.c, kpu.h, Model Compiler中分析各寄存器定义。KPU的寄存器配置写在 kpu_layer_argument_t 结构体中,我们取standalone demo中的kpu demo中的gencode.c来分析.(https://github.com/kendryte/kendryte-standalone-demo/blob/master/kpu/gencode_output.c)
//层参数列表,共16层kpu_layer_argument_t la[] __attribute__((aligned(128))) = {
// 第0层{
.kernel_offset.data = {
.coef_row_offset = 0, //固定为0
.coef_column_offset = 0 //固定为0
},
.image_addr.data = { //图像输入输出地址,一个在前,一个在后,下一层运算的时候翻过来,可以避免拷贝工作。
.image_dst_addr = (uint64_t)0x6980, //图像输出地址,int((0 if idx & 1 else (img_ram_size - img_output_size)) / 64)
.image_src_addr = (uint64_t)0x0 //图像加载地址
},
.kernel_calc_type_cfg.data = {
.load_act = 1, //使能激活函数,必须使能(硬件设计如此),不使能则输出全为0
.active_addr = 0, //激活参数加载首地址,在kpu_task_init里初始化为激活折线表
.row_switch_addr = 0x5, //图像宽占用的单元数,一个单元64Byte. ceil(width/64)=ceil(320/64)=5
.channel_switch_addr = 0x4b0, //单通道占用的单元数. row_switch_addr*height=5*240=1200=0x4b0
.coef_size = 0, //固定为0
.coef_group = 1 //一次可以计算的组数,因为一个单元64字节,
//所以宽度>32,设置为1;宽度17~32,设置为2;宽度<=16,设置为4
},
.interrupt_enabe.data = {
.depth_wise_layer = 0, //常规卷积层,设置为0
.ram_flag = 0, //固定为0
.int_en = 0, //失能中断
.full_add = 0 //固定为0
},
.dma_parameter.data = { //DMA传输参数
.dma_total_byte = 307199, //该层输出16通道,即 19200*16=308200
.send_data_out = 0, //使能输出数据
.channel_byte_num = 19199 //输出单通道的字节数,因为后面是2x2 pooling, 所以大小为160*120=19200
},
.conv_value.data = { //卷积参数,y = (x*arg_x)>>shr_x
.arg_x = 0x809179, //24bit 乘法参数
.arg_w = 0x0,
.shr_x = 8, //4bit 移位参数
.shr_w = 0
},
.conv_value2.data = { //arg_add = kernel_size * kernel_size * bw_div_sw * bx_div_sx =3x3x?x?
.arg_add = 0
},
.write_back_cfg.data = { //写回配置
.wb_row_switch_addr = 0x3, //ceil(160/64)=3
.wb_channel_switch_addr = 0x168, //120*3=360=0x168
.wb_group = 1 //输入行宽>32,设置为1
},
.image_size.data = { //输入320*240,输出160*120
.o_col_high = 0x77,
.i_col_high = 0xef,
.i_row_wid = 0x13f,
.o_row_wid = 0x9f
},
.kernel_pool_type_cfg.data = {
.bypass_conv = 0, //硬件不能跳过卷积,固定为0
.pad_value = 0x0, //边界填充0
.load_para = 1, //硬件不能跳过归一化,固定为1
.pad_type = 0, //使用填充值
.kernel_type = 1, //3x3设置为1, 1x1设置为0
.pool_type = 1, //池化类型,步长为2的2x2 max pooling
.dma_burst_size = 15, //dma突发传送大小,16字节;脚本中固定为16
.bwsx_base_addr = 0, //批归一化首地址,在kpu_task_init中初始化
.first_stride = 0 //图像高度不超过255;图像高度最大为512。
},
.image_channel_num.data = {
.o_ch_num_coef = 0xf, //一次性参数加载可计算的通道数,16通道。4K/单通道卷积核数
//o_ch_num_coef = math.floor(weight_buffer_size / o_ch_weights_size_pad)
.i_ch_num = 0x2, //输入通道,3通道 RGB
.o_ch_num = 0xf //输出通道,16通道
},
.kernel_load_cfg.data = {
.load_time = 0, //卷积加载次数,不超过72KB,只加载一次
.para_size = 864, //卷积参数大小864字节,864=3(RGB)*9(3x3)*2*16
.para_start_addr = 0, //起始地址
.load_coor = 1 //允许加载卷积参数
}
},
//第0层参数结束……
};
上表中还有些结构体内容没有填充,是在KPU初始化函数中填充:```kpu_task_t* kpu_task_init(kpu_task_t* task){
la[0].kernel_pool_type_cfg.data.bwsx_base_addr = (uint64_t)&bwsx_base_addr_0; //初始化批归一化表
la[0].kernel_calc_type_cfg.data.active_addr = (uint64_t)&active_addr_0; //初始化激活表
la[0].kernel_load_cfg.data.para_start_addr = (uint64_t)¶_start_addr_0; //初始化参数加载
…… //共16层参数,逐层计算
task->layers = la;
task->layers_length = sizeof(la)/sizeof(la[0]); //16层
task->eight_bit_mode = 0; //16bit模式
task->output_scale = 0.12349300010531557; //输出的缩放,偏置
task->output_bias = -13.528212547302246;
return task;
}```
|