1 /* 2 * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef SAMPLE_COMM_NNIE_H 16 #define SAMPLE_COMM_NNIE_H 17 18 #include <sys/time.h> 19 #include "hi_comm_svp.h" 20 #include "hi_nnie.h" 21 #include "mpi_nnie.h" 22 #include "sample_comm_ive.h" 23 24 #ifdef __cplusplus 25 #if __cplusplus 26 extern "C" { 27 #endif 28 #endif /* __cplusplus */ 29 30 /* 16Byte align */ 31 #define SAMPLE_SVP_NNIE_ALIGN_16 16 32 #define SAMPLE_SVP_NNIE_ALIGN16(u32Num) \ 33 (((u32Num) + (SAMPLE_SVP_NNIE_ALIGN_16 - 1)) / (SAMPLE_SVP_NNIE_ALIGN_16) * (SAMPLE_SVP_NNIE_ALIGN_16)) 34 /* 32Byte align */ 35 #define SAMPLE_SVP_NNIE_ALIGN_32 32 36 #define SAMPLE_SVP_NNIE_ALIGN32(u32Num) \ 37 (((u32Num) + (SAMPLE_SVP_NNIE_ALIGN_32 - 1)) / (SAMPLE_SVP_NNIE_ALIGN_32) * (SAMPLE_SVP_NNIE_ALIGN_32)) 38 39 #define SAMPLE_SVP_NNIE_CONVERT_64BIT_ADDR(Type, Addr) (Type *)(HI_UINTPTR_T)(Addr) 40 #define SAMPLE_SVP_NNIE_CONVERT_PTR_TO_ADDR(Type, Addr) ((Type)(HI_UINTPTR_T)(Addr)) 41 42 #define SAMPLE_SVP_COORDI_NUM 4 /* num of coordinates */ 43 #define SAMPLE_SVP_PROPOSAL_WIDTH 6 /* the width of each proposal array */ 44 #define SAMPLE_SVP_QUANT_BASE 4096 /* the basic quantity */ 45 #define SAMPLE_SVP_NNIE_MAX_SOFTWARE_MEM_NUM 4 46 #define SAMPLE_SVP_NNIE_SSD_REPORT_NODE_NUM 12 47 #define SAMPLE_SVP_NNIE_SSD_PRIORBOX_NUM 6 48 #define SAMPLE_SVP_NNIE_SSD_SOFTMAX_NUM 6 49 #define SAMPLE_SVP_NNIE_SSD_ASPECT_RATIO_NUM 6 50 #define SAMPLE_SVP_NNIE_YOLOV1_WIDTH_GRID_NUM 7 51 #define SAMPLE_SVP_NNIE_YOLOV1_HEIGHT_GRID_NUM 7 52 #define SAMPLE_SVP_NNIE_SEGMENT_NUM 2 53 #define SAMPLE_SVP_NNIE_EACH_SEG_STEP_ADDR_NUM 2 54 #define SAMPLE_SVP_NNIE_MAX_CLASS_NUM 30 55 #define SAMPLE_SVP_NNIE_MAX_ROI_NUM_OF_CLASS 50 56 #define SAMPLE_SVP_NNIE_REPORT_NAME_LENGTH 64 57 #define SAMPLE_SVP_NNIE_MAX_MEM 0xFFFFFFFF 58 59 typedef struct hiSAMPLE_SVP_NNIE_MODEL_S { 60 SVP_NNIE_MODEL_S stModel; 61 SVP_MEM_INFO_S stModelBuf; // store Model file 62 } SAMPLE_SVP_NNIE_MODEL_S; 63 64 /* each seg input and output memory */ 65 typedef struct hiSAMPLE_SVP_NNIE_SEG_DATA_S { 66 SVP_SRC_BLOB_S astSrc[SVP_NNIE_MAX_INPUT_NUM]; 67 SVP_DST_BLOB_S astDst[SVP_NNIE_MAX_OUTPUT_NUM]; 68 } SAMPLE_SVP_NNIE_SEG_DATA_S; 69 70 /* each seg input and output data memory size */ 71 typedef struct hiSAMPLE_SVP_NNIE_BLOB_SIZE_S { 72 HI_U32 au32SrcSize[SVP_NNIE_MAX_INPUT_NUM]; 73 HI_U32 au32DstSize[SVP_NNIE_MAX_OUTPUT_NUM]; 74 } SAMPLE_SVP_NNIE_BLOB_SIZE_S; 75 76 /* NNIE Execution parameters */ 77 typedef struct hiSAMPLE_SVP_NNIE_PARAM_S { 78 SVP_NNIE_MODEL_S *pstModel; 79 HI_U32 u32TmpBufSize; 80 HI_U32 au32TaskBufSize[SVP_NNIE_MAX_NET_SEG_NUM]; 81 SVP_MEM_INFO_S stTaskBuf; 82 SVP_MEM_INFO_S stTmpBuf; 83 SVP_MEM_INFO_S stStepBuf; // store Lstm step info 84 SAMPLE_SVP_NNIE_SEG_DATA_S astSegData[SVP_NNIE_MAX_NET_SEG_NUM]; // each seg's input and output blob 85 SVP_NNIE_FORWARD_CTRL_S astForwardCtrl[SVP_NNIE_MAX_NET_SEG_NUM]; 86 SVP_NNIE_FORWARD_WITHBBOX_CTRL_S astForwardWithBboxCtrl[SVP_NNIE_MAX_NET_SEG_NUM]; 87 } SAMPLE_SVP_NNIE_PARAM_S; 88 89 /* NNIE input or output data index */ 90 typedef struct hiSAMPLE_SVP_NNIE_DATA_INDEX_S { 91 HI_U32 u32SegIdx; 92 HI_U32 u32NodeIdx; 93 } SAMPLE_SVP_NNIE_DATA_INDEX_S; 94 95 /* this struct is used to indicate the input data from which seg's input or report node */ 96 typedef SAMPLE_SVP_NNIE_DATA_INDEX_S SAMPLE_SVP_NNIE_INPUT_DATA_INDEX_S; 97 /* this struct is used to indicate which seg will be executed */ 98 typedef SAMPLE_SVP_NNIE_DATA_INDEX_S SAMPLE_SVP_NNIE_PROCESS_SEG_INDEX_S; 99 100 typedef enum hiSAMPLE_SVP_NNIE_NET_TYPE_E { 101 SAMPLE_SVP_NNIE_ALEXNET_FASTER_RCNN = 0x0, /* FasterRcnn Alexnet */ 102 SAMPLE_SVP_NNIE_VGG16_FASTER_RCNN = 0x1, /* FasterRcnn Vgg16 */ 103 SAMPLE_SVP_NNIE_PVANET_FASTER_RCNN = 0x2, /* pavenet fasterRcnn */ 104 SAMPLE_SVP_NNIE_NET_TYPE_BUTT 105 } SAMPLE_SVP_NNIE_NET_TYPE_E; 106 107 /* NNIE configuration parameter */ 108 typedef struct hiSAMPLE_SVP_NNIE_CFG_S { 109 const HI_CHAR *pszPic; 110 HI_U32 u32MaxInputNum; 111 HI_U32 u32MaxRoiNum; 112 // virtual addr of LSTM's or RNN's step buffer 113 HI_U64 au64StepVirAddr[SAMPLE_SVP_NNIE_EACH_SEG_STEP_ADDR_NUM * SVP_NNIE_MAX_NET_SEG_NUM]; 114 SVP_NNIE_ID_E aenNnieCoreId[SVP_NNIE_MAX_NET_SEG_NUM]; 115 } SAMPLE_SVP_NNIE_CFG_S; 116 117 /* CNN GetTopN parameter */ 118 typedef struct hiSAMPLE_SVP_NNIE_CNN_SOFTWARE_PARAM_S { 119 HI_U32 u32TopN; 120 SVP_DST_BLOB_S stGetTopN; 121 SVP_MEM_INFO_S stAssistBuf; 122 } SAMPLE_SVP_NNIE_CNN_SOFTWARE_PARAM_S; 123 124 /* FasterRcnn software parameter */ 125 typedef struct hiSAMPLE_SVP_NNIE_FASTERRCNN_SOFTWARE_PARAM_S { 126 HI_U32 au32Scales[9]; 127 HI_U32 au32Ratios[9]; 128 HI_U32 au32ConvHeight[2]; 129 HI_U32 au32ConvWidth[2]; 130 HI_U32 au32ConvChannel[2]; 131 HI_U32 u32ConvStride; 132 HI_U32 u32NumRatioAnchors; 133 HI_U32 u32NumScaleAnchors; 134 HI_U32 u32OriImHeight; 135 HI_U32 u32OriImWidth; 136 HI_U32 u32MinSize; 137 HI_U32 u32SpatialScale; 138 HI_U32 u32NmsThresh; 139 HI_U32 u32FilterThresh; 140 HI_U32 u32NumBeforeNms; 141 HI_U32 u32MaxRoiNum; 142 HI_U32 u32ClassNum; 143 HI_U32 au32ConfThresh[21]; 144 HI_U32 u32ValidNmsThresh; 145 HI_S32 *aps32Conv[2]; 146 SVP_MEM_INFO_S stRpnTmpBuf; 147 SVP_DST_BLOB_S stRpnBbox; 148 SVP_DST_BLOB_S stClassRoiNum; 149 SVP_DST_BLOB_S stDstRoi; 150 SVP_DST_BLOB_S stDstScore; 151 SVP_MEM_INFO_S stGetResultTmpBuf; 152 HI_CHAR *apcRpnDataLayerName[2]; 153 } SAMPLE_SVP_NNIE_FASTERRCNN_SOFTWARE_PARAM_S; 154 155 /* Array rect info */ 156 typedef struct hiSAMPLE_SVP_NNIE_RECT_ARRAY_S { 157 HI_U32 u32ClsNum; 158 HI_U32 u32TotalNum; 159 HI_U32 au32RoiNum[SAMPLE_SVP_NNIE_MAX_CLASS_NUM]; 160 SAMPLE_IVE_RECT_S astRect[SAMPLE_SVP_NNIE_MAX_CLASS_NUM][SAMPLE_SVP_NNIE_MAX_ROI_NUM_OF_CLASS]; 161 } SAMPLE_SVP_NNIE_RECT_ARRAY_S; 162 163 /* RFCN software parameter */ 164 typedef struct hiSAMPLE_SVP_NNIE_RFCN_SOFTWARE_PARAM_S { 165 HI_U32 au32Scales[9]; 166 HI_U32 au32Ratios[9]; 167 HI_U32 au32ConvHeight[2]; 168 HI_U32 au32ConvWidth[2]; 169 HI_U32 au32ConvChannel[2]; 170 HI_U32 u32ConvStride; 171 HI_U32 u32NumRatioAnchors; 172 HI_U32 u32NumScaleAnchors; 173 HI_U32 u32OriImHeight; 174 HI_U32 u32OriImWidth; 175 HI_U32 u32MinSize; 176 HI_U32 u32SpatialScale; 177 HI_U32 u32NmsThresh; 178 HI_U32 u32FilterThresh; 179 HI_U32 u32NumBeforeNms; 180 HI_U32 u32MaxRoiNum; 181 HI_U32 u32ClassNum; 182 HI_U32 au32ConfThresh[21]; 183 HI_U32 u32ValidNmsThresh; 184 HI_S32 *aps32Conv[2]; 185 HI_FLOAT af32ScoreThr[SAMPLE_SVP_NNIE_MAX_CLASS_NUM]; 186 SVP_MEM_INFO_S stRpnTmpBuf; 187 SVP_DST_BLOB_S stRpnBbox; 188 SVP_DST_BLOB_S stClassRoiNum; 189 SVP_DST_BLOB_S stDstRoi; 190 SVP_DST_BLOB_S stDstScore; 191 SVP_MEM_INFO_S stGetResultTmpBuf; 192 SAMPLE_SVP_NNIE_RECT_ARRAY_S stRect; 193 HI_CHAR *apcRpnDataLayerName[2]; 194 } SAMPLE_SVP_NNIE_RFCN_SOFTWARE_PARAM_S; 195 196 /* SSD software parameter */ 197 typedef struct hiSAMPLE_SVP_NNIE_SSD_SOFTWARE_PARAM_S { 198 /* ----------------- Model Parameters --------------- */ 199 HI_U32 au32ConvHeight[12]; 200 HI_U32 au32ConvWidth[12]; 201 HI_U32 au32ConvChannel[12]; 202 /* ----------------- PriorBox Parameters --------------- */ 203 HI_U32 au32PriorBoxWidth[6]; 204 HI_U32 au32PriorBoxHeight[6]; 205 HI_FLOAT af32PriorBoxMinSize[6][1]; 206 HI_FLOAT af32PriorBoxMaxSize[6][1]; 207 HI_U32 u32MinSizeNum; 208 HI_U32 u32MaxSizeNum; 209 HI_U32 u32OriImHeight; 210 HI_U32 u32OriImWidth; 211 HI_U32 au32InputAspectRatioNum[6]; 212 HI_FLOAT af32PriorBoxAspectRatio[6][2]; 213 HI_FLOAT af32PriorBoxStepWidth[6]; 214 HI_FLOAT af32PriorBoxStepHeight[6]; 215 HI_FLOAT f32Offset; 216 HI_BOOL bFlip; 217 HI_BOOL bClip; 218 HI_S32 as32PriorBoxVar[4]; 219 /* ----------------- Softmax Parameters --------------- */ 220 HI_U32 au32SoftMaxInChn[6]; 221 HI_U32 u32SoftMaxInHeight; 222 HI_U32 u32ConcatNum; 223 HI_U32 u32SoftMaxOutWidth; 224 HI_U32 u32SoftMaxOutHeight; 225 HI_U32 u32SoftMaxOutChn; 226 /* ----------------- DetectionOut Parameters --------------- */ 227 HI_U32 u32ClassNum; 228 HI_U32 u32TopK; 229 HI_U32 u32KeepTopK; 230 HI_U32 u32NmsThresh; 231 HI_U32 u32ConfThresh; 232 HI_U32 au32DetectInputChn[6]; 233 HI_U32 au32ConvStride[6]; 234 SVP_MEM_INFO_S stPriorBoxTmpBuf; 235 SVP_MEM_INFO_S stSoftMaxTmpBuf; 236 SVP_DST_BLOB_S stClassRoiNum; 237 SVP_DST_BLOB_S stDstRoi; 238 SVP_DST_BLOB_S stDstScore; 239 SVP_MEM_INFO_S stGetResultTmpBuf; 240 } SAMPLE_SVP_NNIE_SSD_SOFTWARE_PARAM_S; 241 242 /* Yolov1 software parameter */ 243 typedef struct hiSAMPLE_SVP_NNIE_YOLOV1_SOFTWARE_PARAM_S { 244 HI_U32 u32OriImHeight; 245 HI_U32 u32OriImWidth; 246 HI_U32 u32BboxNumEachGrid; 247 HI_U32 u32ClassNum; 248 HI_U32 u32GridNumHeight; 249 HI_U32 u32GridNumWidth; 250 HI_U32 u32NmsThresh; 251 HI_U32 u32ConfThresh; 252 SVP_MEM_INFO_S stGetResultTmpBuf; 253 SVP_DST_BLOB_S stClassRoiNum; 254 SVP_DST_BLOB_S stDstRoi; 255 SVP_DST_BLOB_S stDstScore; 256 } SAMPLE_SVP_NNIE_YOLOV1_SOFTWARE_PARAM_S; 257 258 /* Yolov2 software parameter */ 259 typedef struct hiSAMPLE_SVP_NNIE_YOLOV2_SOFTWARE_PARAM_S { 260 HI_U32 u32OriImHeight; 261 HI_U32 u32OriImWidth; 262 HI_U32 u32BboxNumEachGrid; 263 HI_U32 u32ClassNum; 264 HI_U32 u32GridNumHeight; 265 HI_U32 u32GridNumWidth; 266 HI_U32 u32NmsThresh; 267 HI_U32 u32ConfThresh; 268 HI_U32 u32MaxRoiNum; 269 HI_FLOAT af32Bias[10]; 270 SVP_MEM_INFO_S stGetResultTmpBuf; 271 SVP_DST_BLOB_S stClassRoiNum; 272 SVP_DST_BLOB_S stDstRoi; 273 SVP_DST_BLOB_S stDstScore; 274 } SAMPLE_SVP_NNIE_YOLOV2_SOFTWARE_PARAM_S; 275 276 /* Yolov3 software parameter */ 277 typedef struct hiSAMPLE_SVP_NNIE_YOLOV3_SOFTWARE_PARAM_S { 278 HI_U32 u32OriImHeight; 279 HI_U32 u32OriImWidth; 280 HI_U32 u32BboxNumEachGrid; 281 HI_U32 u32ClassNum; 282 HI_U32 au32GridNumHeight[3]; 283 HI_U32 au32GridNumWidth[3]; 284 HI_U32 u32NmsThresh; 285 HI_U32 u32ConfThresh; 286 HI_U32 u32MaxRoiNum; 287 HI_FLOAT af32Bias[3][6]; 288 SVP_MEM_INFO_S stGetResultTmpBuf; 289 SVP_DST_BLOB_S stClassRoiNum; 290 SVP_DST_BLOB_S stDstRoi; 291 SVP_DST_BLOB_S stDstScore; 292 } SAMPLE_SVP_NNIE_YOLOV3_SOFTWARE_PARAM_S; 293 294 HI_S32 SAMPLE_COMM_SVP_NNIE_ParamDeinit(SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam); 295 296 HI_S32 SAMPLE_COMM_SVP_NNIE_ParamInit(SAMPLE_SVP_NNIE_CFG_S *pstNnieCfg, SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam); 297 298 HI_S32 SAMPLE_COMM_SVP_NNIE_UnloadModel(SAMPLE_SVP_NNIE_MODEL_S *pstNnieModel); 299 300 HI_S32 SAMPLE_COMM_SVP_NNIE_LoadModel(const HI_CHAR *pszModelFile, SAMPLE_SVP_NNIE_MODEL_S *pstNnieModel); 301 302 HI_S32 SAMPLE_COMM_SVP_NNIE_FillRect(VIDEO_FRAME_INFO_S *pstFrmInfo, SAMPLE_SVP_NNIE_RECT_ARRAY_S *pstRect, 303 HI_U32 u32Color); 304 305 #ifdef __cplusplus 306 #if __cplusplus 307 } 308 #endif 309 #endif /* __cplusplus */ 310 311 #endif /* __SAMPLE_COMM_NNIE_H__ */ 312