|
| 1 | +/************************************************************************************************** |
| 2 | + * |
| 3 | + * Copyright (c) 2019-2023 Axera Semiconductor (Ningbo) Co., Ltd. All Rights Reserved. |
| 4 | + * |
| 5 | + * This source file is the property of Axera Semiconductor (Ningbo) Co., Ltd. and |
| 6 | + * may not be copied or distributed in any isomorphic form without the prior |
| 7 | + * written consent of Axera Semiconductor (Ningbo) Co., Ltd. |
| 8 | + * |
| 9 | + **************************************************************************************************/ |
| 10 | +#include "EngineWrapper.hpp" |
| 11 | +#include "utils/io.hpp" |
| 12 | + |
| 13 | +#include <cstdlib> |
| 14 | + |
| 15 | + |
| 16 | +static const char *strAlgoModelType[AX_ENGINE_MODEL_TYPE_BUTT] = {"HalfOCM", "FullOCM"}; |
| 17 | + |
| 18 | + |
| 19 | +/// @brief npu type |
| 20 | +typedef enum axNPU_TYPE_E { |
| 21 | + AX_NPU_DEFAULT = 0, /* running under default NPU according to system */ |
| 22 | + AX_STD_VNPU_1 = (1 << 0), /* running under STD VNPU1 */ |
| 23 | + AX_STD_VNPU_2 = (1 << 1), /* running under STD VNPU2 */ |
| 24 | + AX_STD_VNPU_3 = (1 << 2), /* running under STD VNPU3 */ |
| 25 | + AX_BL_VNPU_1 = (1 << 3), /* running under BIG-LITTLE VNPU1 */ |
| 26 | + AX_BL_VNPU_2 = (1 << 4) /* running under BIG-LITTLE VNPU2 */ |
| 27 | +} AX_NPU_TYPE_E; |
| 28 | + |
| 29 | + |
| 30 | +static AX_S32 CheckModelVNpu(const std::string &strModel, const AX_ENGINE_MODEL_TYPE_T &eModelType, const AX_S32 &nNpuType, AX_U32 &nNpuSet) { |
| 31 | + AX_ENGINE_NPU_ATTR_T stNpuAttr; |
| 32 | + memset(&stNpuAttr, 0x00, sizeof(stNpuAttr)); |
| 33 | + |
| 34 | + auto ret = AX_ENGINE_GetVNPUAttr(&stNpuAttr); |
| 35 | + if (ret == 0) { |
| 36 | + // VNPU DISABLE |
| 37 | + if (stNpuAttr.eHardMode == AX_ENGINE_VIRTUAL_NPU_DISABLE) { |
| 38 | + nNpuSet = 0x01; // NON-VNPU (0b111) |
| 39 | + // ALOGN("%s will run under VNPU-DISABLE [%s]", strModel.c_str(), strAlgoModelType[eModelType]); |
| 40 | + } |
| 41 | + // STD VNPU |
| 42 | + else if (stNpuAttr.eHardMode == AX_ENGINE_VIRTUAL_NPU_ENABLE) { |
| 43 | + // full ocm model was no allowned |
| 44 | + if (eModelType == AX_ENGINE_MODEL_TYPE1) { |
| 45 | + // printf("%s model type%d: [%s], no allow run under STD VNPU", strModel.c_str(), eModelType, strAlgoModelType[eModelType]); |
| 46 | + return -1; |
| 47 | + } |
| 48 | + |
| 49 | + // default STD VNPU2 |
| 50 | + if (nNpuType == 0) { |
| 51 | + nNpuSet = 0x02; // VNPU2 (0b010) |
| 52 | + // printf("%s will run under default STD-VNPU2 [%s]", strModel.c_str(), strAlgoModelType[eModelType]); |
| 53 | + } |
| 54 | + else { |
| 55 | + if (nNpuType & AX_STD_VNPU_1) { |
| 56 | + nNpuSet |= 0x01; // VNPU1 (0b001) |
| 57 | + // printf("%s will run under STD-VNPU1 [%s]", strModel.c_str(), strAlgoModelType[eModelType]); |
| 58 | + } |
| 59 | + if (nNpuType & AX_STD_VNPU_2) { |
| 60 | + nNpuSet |= 0x02; // VNPU2 (0b010) |
| 61 | + // printf("%s will run under STD-VNPU2 [%s]", strModel.c_str(), strAlgoModelType[eModelType]); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + else { |
| 67 | + printf("AX_ENGINE_GetVNPUAttr fail ret = %x", ret); |
| 68 | + } |
| 69 | + |
| 70 | + return ret; |
| 71 | +} |
| 72 | + |
| 73 | +int EngineWrapper::Init(const char* strModelPath, uint32_t nNpuType) |
| 74 | +{ |
| 75 | + AX_S32 ret = 0; |
| 76 | + |
| 77 | + // 1. load model |
| 78 | + AX_BOOL bLoadModelUseCmm = AX_FALSE; |
| 79 | + AX_CHAR *pModelBufferVirAddr = nullptr; |
| 80 | + AX_U64 u64ModelBufferPhyAddr = 0; |
| 81 | + AX_U32 nModelBufferSize = 0; |
| 82 | + |
| 83 | + std::vector<char> model_buffer; |
| 84 | + |
| 85 | + if (bLoadModelUseCmm) { |
| 86 | + if (!utils::read_file(strModelPath, (AX_VOID **)&pModelBufferVirAddr, u64ModelBufferPhyAddr, nModelBufferSize)) { |
| 87 | + printf("ALGO read model(%s) fail\n", strModelPath); |
| 88 | + return -1; |
| 89 | + } |
| 90 | + } |
| 91 | + else { |
| 92 | + if (!utils::read_file(strModelPath, model_buffer)) { |
| 93 | + printf("ALGO read model(%s) fail\n", strModelPath); |
| 94 | + return -1; |
| 95 | + } |
| 96 | + |
| 97 | + pModelBufferVirAddr = model_buffer.data(); |
| 98 | + nModelBufferSize = model_buffer.size(); |
| 99 | + } |
| 100 | + |
| 101 | + auto freeModelBuffer = [&]() { |
| 102 | + if (bLoadModelUseCmm) { |
| 103 | + if (u64ModelBufferPhyAddr != 0) { |
| 104 | + AX_SYS_MemFree(u64ModelBufferPhyAddr, &pModelBufferVirAddr); |
| 105 | + } |
| 106 | + } |
| 107 | + else { |
| 108 | + std::vector<char>().swap(model_buffer); |
| 109 | + } |
| 110 | + return; |
| 111 | + }; |
| 112 | + |
| 113 | + // 1.1 Get Model Type |
| 114 | + AX_ENGINE_MODEL_TYPE_T eModelType = AX_ENGINE_MODEL_TYPE0; |
| 115 | + ret = AX_ENGINE_GetModelType(pModelBufferVirAddr, nModelBufferSize, &eModelType); |
| 116 | + if (0 != ret || eModelType >= AX_ENGINE_MODEL_TYPE_BUTT) { |
| 117 | + printf("%s AX_ENGINE_GetModelType fail ret=%x, eModelType=%d\n", strModelPath, eModelType); |
| 118 | + freeModelBuffer(); |
| 119 | + return -1; |
| 120 | + } |
| 121 | + |
| 122 | + // 1.2 Check VNPU |
| 123 | + AX_ENGINE_NPU_SET_T nNpuSet = 0; |
| 124 | + ret = CheckModelVNpu(strModelPath, eModelType, nNpuType, nNpuSet); |
| 125 | + if (0 != ret) { |
| 126 | + printf("ALGO CheckModelVNpu fail\n"); |
| 127 | + freeModelBuffer(); |
| 128 | + return -1; |
| 129 | + } |
| 130 | + |
| 131 | + // 2. create handle |
| 132 | + AX_ENGINE_HANDLE handle = nullptr; |
| 133 | + ret = AX_ENGINE_CreateHandle(&handle, pModelBufferVirAddr, nModelBufferSize); |
| 134 | + auto deinit_handle = [&handle]() { |
| 135 | + if (handle) { |
| 136 | + AX_ENGINE_DestroyHandle(handle); |
| 137 | + } |
| 138 | + return -1; |
| 139 | + }; |
| 140 | + |
| 141 | + freeModelBuffer(); |
| 142 | + |
| 143 | + if (0 != ret || !handle) { |
| 144 | + printf("ALGO Create model(%s) handle fail\n", strModelPath); |
| 145 | + |
| 146 | + return deinit_handle(); |
| 147 | + } |
| 148 | + |
| 149 | + // 3. create context |
| 150 | + ret = AX_ENGINE_CreateContext(handle); |
| 151 | + if (0 != ret) { |
| 152 | + return deinit_handle(); |
| 153 | + } |
| 154 | + |
| 155 | + // 4. set io |
| 156 | + m_io_info = nullptr; |
| 157 | + ret = AX_ENGINE_GetIOInfo(handle, &m_io_info); |
| 158 | + if (0 != ret) { |
| 159 | + return deinit_handle(); |
| 160 | + } |
| 161 | + m_input_num = m_io_info->nInputSize; |
| 162 | + m_output_num = m_io_info->nOutputSize; |
| 163 | + |
| 164 | + // 4.1 query io |
| 165 | +// AX_IMG_FORMAT_E eDtype; |
| 166 | +// ret = utils::query_model_input_size(m_io_info, m_input_size, eDtype);//FIXME. |
| 167 | +// if (0 != ret) { |
| 168 | +// printf("model(%s) query model input size fail\n", strModelPath.c_str()); |
| 169 | +// return deinit_handle(); |
| 170 | +// } |
| 171 | + |
| 172 | +// if (!(eDtype == AX_FORMAT_YUV420_SEMIPLANAR || eDtype == AX_FORMAT_YUV420_SEMIPLANAR_VU || |
| 173 | +// eDtype == AX_FORMAT_RGB888 || eDtype == AX_FORMAT_BGR888)) { |
| 174 | +// printf("model(%s) data type is: 0x%02X, unsupport\n", strModelPath, eDtype); |
| 175 | +// return deinit_handle(); |
| 176 | +// } |
| 177 | + |
| 178 | + // 4.2 brief io |
| 179 | +#ifdef __DEBUG__ |
| 180 | + printf("brief_io_info\n"); |
| 181 | + utils::brief_io_info(strModelPath, m_io_info); |
| 182 | +#endif |
| 183 | + |
| 184 | + //5. Config VNPU |
| 185 | + // printf("model(%s) nNpuSet: 0x%08X\n", strModelPath.c_str(), nNpuSet); |
| 186 | + // will do nothing for using create handle v2 api |
| 187 | + |
| 188 | + // 6. prepare io |
| 189 | + // AX_U32 nIoDepth = (stCtx.vecOutputBufferFlag.size() == 0) ? 1 : stCtx.vecOutputBufferFlag.size(); |
| 190 | + ret = utils::prepare_io(strModelPath, m_io_info, m_io, utils::IO_BUFFER_STRATEGY_CACHED); |
| 191 | + if (0 != ret) { |
| 192 | + printf("prepare io failed!\n"); |
| 193 | + utils::free_io(m_io); |
| 194 | + return deinit_handle(); |
| 195 | + } |
| 196 | + |
| 197 | + m_handle = handle; |
| 198 | + m_hasInit = true; |
| 199 | + |
| 200 | + return 0; |
| 201 | +} |
| 202 | + |
| 203 | +int EngineWrapper::SetInput(void* pInput, int index) { |
| 204 | + return utils::push_io_input(pInput, index, m_io); |
| 205 | +} |
| 206 | + |
| 207 | +int EngineWrapper::Run() |
| 208 | +{ |
| 209 | + if (!m_hasInit) |
| 210 | + return -1; |
| 211 | + |
| 212 | + // 7.3 run & benchmark |
| 213 | + auto ret = AX_ENGINE_RunSync(m_handle, &m_io); |
| 214 | + if (0 != ret) { |
| 215 | + printf("AX_ENGINE_RunSync failed. ret=0x%x\n", ret); |
| 216 | + return ret; |
| 217 | + } |
| 218 | + |
| 219 | + return 0; |
| 220 | +} |
| 221 | + |
| 222 | +int EngineWrapper::GetOutput(void* pOutput, int index) { |
| 223 | + return utils::push_io_output(pOutput, index, m_io); |
| 224 | +} |
| 225 | + |
| 226 | +int EngineWrapper::GetInputSize(int index) { |
| 227 | + return m_io.pInputs[index].nSize; |
| 228 | +} |
| 229 | + |
| 230 | +int EngineWrapper::GetOutputSize(int index) { |
| 231 | + return m_io.pOutputs[index].nSize; |
| 232 | +} |
| 233 | + |
| 234 | +void* EngineWrapper::GetOutputPtr(int index) { |
| 235 | + utils::cache_io_flush(&m_io.pOutputs[index]); |
| 236 | + return m_io.pOutputs[index].pVirAddr; |
| 237 | +} |
| 238 | + |
| 239 | +int EngineWrapper::Release() |
| 240 | +{ |
| 241 | + if (m_handle) { |
| 242 | + utils::free_io(m_io); |
| 243 | + AX_ENGINE_DestroyHandle(m_handle); |
| 244 | + m_handle = nullptr; |
| 245 | + } |
| 246 | + return 0; |
| 247 | +} |
0 commit comments