1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 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 16 #ifndef NEURAL_NETWORK_RUNTIME_INNER_H 17 #define NEURAL_NETWORK_RUNTIME_INNER_H 18 19 #include "interfaces/kits/c/neural_network_runtime/neural_network_runtime_type.h" 20 21 #ifdef __cplusplus 22 extern "C" { 23 #endif 24 25 /** 26 * @brief 定义Tensor信息结构体。包含名字,数据类型,维度信息,格式信息。 27 * 28 * @since 10 29 * @version 1.1 30 */ 31 typedef struct OH_NN_TensorInfo { 32 char name[128]; 33 OH_NN_DataType dataType; 34 uint32_t dimensionCount; 35 const int32_t *dimensions; 36 OH_NN_Format format; 37 } OH_NN_TensorInfo; 38 39 /** 40 * @brief 定义扩展字段结构体。 41 * 42 * @since 10 43 * @version 1.1 44 */ 45 typedef struct OH_NN_Extension { 46 char name[128]; 47 char *value; 48 size_t valueSize; 49 } OH_NN_Extension; 50 51 /** 52 * @brief 直接加载LiteGraph,完成模型搭建。 53 * 54 * 调用{@link OH_NNModel_Construct}创建模型实例后,直接调用本方法加载LiteGraph。加载LiteGraph后,只能调用 55 * {@link OH_NNCompilation_Construct}创建模型编译器,或者调用{@link OH_NNModel_Destroy}销毁模型实例。\n 56 * 57 * 不允许本方法与{@link OH_NNModel_AddTensor}、{@link OH_NNModel_AddOperation}、 58 * {@link OH_NNModel_SetTensorData}和{@link OH_NNModel_SpecifyInputsAndOutputs} 59 * 等构图接口混用,否则返回{@link OH_NN_OPERATION_FORBIDDEN}错误。\n 60 * 61 * 如果本方法调用成功,返回{@link OH_NN_SUCCESS},liteGraph将由NNRt管理,调用者无需释放,避免造成二次释放; 62 * 如果方法返回其他错误码,则NNRt不会持有liteGraph,此时需要调用者主动释放内存。\n 63 * 64 * 65 * 本接口不作为Neural Network Runtime接口对外开放。\n 66 * 67 * @param model 指向{@link OH_NNModel}实例的指针。 68 * @param liteGraph 指向LiteGraph的指针。 69 * @return 函数执行的结果状态,执行成功返回OH_NN_SUCCESS,失败返回具体错误码,参考{@link OH_NN_ReturnCode}。 70 * @throw std::bad_alloc 本方法可能在转换原始指针到智能指针的过程中,抛出std::bad_alloc异常,此时liteGraph将被 71 * 主动释放。 72 * @since 9 73 * @version 1.0 74 */ 75 OH_NN_ReturnCode OH_NNModel_BuildFromLiteGraph(OH_NNModel *model, const void *liteGraph, 76 const OH_NN_Extension *extensions, size_t extensionSize); 77 78 /** 79 * @brief 设置MetaGraph的输入输出信息。 80 * 81 * 调用{@link OH_NNModel_Construct}创建模型实例后,直接调用本方法设置MetaGraph的输入输出信息。然后调用{@link OH_NNModel_BuildFromMetaGraph} 82 * 加载MetaGraph,完成模型搭建。\n 83 * 84 * 不允许本方法与{@link OH_NNModel_AddTensor}、和{@link OH_NNModel_SpecifyInputsAndOutputs} 85 * 等构图接口混用,否则返回{@link OH_NN_OPERATION_FORBIDDEN}错误。\n 86 * 87 * 如果本方法调用成功,返回{@link OH_NN_SUCCESS}。\n 88 * 89 * 90 * 本接口不作为Neural Network Runtime接口对外开放。\n 91 * 92 * @param model 指向{@link OH_NNModel}实例的指针。 93 * @param inputsInfo 指向{@link OH_NN_TensorInfo}数组的指针,代表传入的输入Tensor信息。 94 * @param inputSize 代表inputsInfo数组大小。 95 * @param outputsInfo 指向{@link OH_NN_TensorInfo}数组的指针,代表传入的输出Tensor信息。 96 * @param outputSize 代表outputsInfo数组大小。 97 * @return 函数执行的结果状态,执行成功返回OH_NN_SUCCESS,失败返回具体错误码,参考{@link OH_NN_ReturnCode}。 98 * @since 10 99 * @version 1.1 100 */ 101 OH_NN_ReturnCode OH_NNModel_SetInputsAndOutputsInfo(OH_NNModel *model, const OH_NN_TensorInfo *inputsInfo, 102 size_t inputSize, const OH_NN_TensorInfo *outputsInfo, size_t outputSize); 103 104 /** 105 * @brief 直接加载MetaGraph,完成模型搭建。 106 * 107 * 调用{@link OH_NNModel_SetInputsAndOutputsInfo}设置好MetaGraph输入输出信息后,直接调用本方法加载MetaGraph。加载MetaGraph后,只能调用 108 * {@link OH_NNCompilation_Construct}创建模型编译器,或者调用{@link OH_NNModel_Destroy}销毁模型实例。\n 109 * 110 * 不允许本方法与{@link OH_NNModel_AddTensor}、{@link OH_NNModel_AddOperation}、 111 * {@link OH_NNModel_SetTensorData}、{@link OH_NNModel_SpecifyInputsAndOutputs}和{@link OH_NNModel_Finish} 112 * 等构图接口混用,否则返回{@link OH_NN_OPERATION_FORBIDDEN}错误。\n 113 * 114 * 如果本方法调用成功,返回{@link OH_NN_SUCCESS}。\n 115 * 116 * 117 * 本接口不作为Neural Network Runtime接口对外开放。\n 118 * 119 * @param model 指向{@link OH_NNModel}实例的指针。 120 * @param metaGraph 指向MetaGraph的指针。 121 * @param extensions 指向{@ OH_NN_Extension}数组的指针,代表传入的扩展字段。 122 * 例如,传递量化信息时,指定Extension.name为"QuantBuffer",将量化Buffer赋给value和valueSize。 123 * @param extensionSize 代表extensions数组大小。 124 * @return 函数执行的结果状态,执行成功返回OH_NN_SUCCESS,失败返回具体错误码,参考{@link OH_NN_ReturnCode}。 125 * @since 10 126 * @version 1.1 127 */ 128 OH_NN_ReturnCode OH_NNModel_BuildFromMetaGraph(OH_NNModel *model, const void *metaGraph, 129 const OH_NN_Extension *extensions, size_t extensionSize); 130 131 /** 132 * @brief 判断cache文件是否存在。 133 * 134 * 本接口不作为Neural Network Runtime接口对外开放。\n 135 * 136 * @param cacheDir cache文件所在文件夹。 137 * @param modelName 模型名字,与构图时传入的一致。 138 * @return bool值,true表示cache文件存在,false表示cache文件不存在。 139 * @since 11 140 * @version 1.0 141 */ 142 bool OH_NNModel_HasCache(const char *cacheDir, const char *modelName, uint32_t version); 143 144 /** 145 * @brief 获取NNRt device信息。 146 * 147 * 本接口不作为Neural Network Runtime接口对外开放。\n 148 * 149 * @param nnrtDevice 传出device参数。 150 * @return 函数执行的结果状态,执行成功返回OH_NN_SUCCESS,失败返回具体错误码,参考{@link OH_NN_ReturnCode}。 151 * @since 11 152 * @version 1.0 153 */ 154 OH_NN_ReturnCode OH_NN_GetDeviceID(char *nnrtDevice, size_t len); 155 156 #ifdef __cplusplus 157 } 158 #endif // __cpluscplus 159 #endif // NEURAL_NETWORK_RUNTIME_INNER_H 160