diff --git a/frameworks/native/neural_network_core/cpp_type.h b/frameworks/native/neural_network_core/cpp_type.h index 7007244d397071c6d08488f3ea2cbecb24309c61..a5d0bbc1f9bbf739efe92782c1c1f22c8c986047 100644 --- a/frameworks/native/neural_network_core/cpp_type.h +++ b/frameworks/native/neural_network_core/cpp_type.h @@ -41,19 +41,28 @@ enum class TuningStrategy { ON_CLOUD_TUNING }; -struct ModelConfig { - bool enableFloat16; - OH_NN_PerformanceMode mode; - OH_NN_Priority priority; +struct Buffer { + void* data; + size_t length; +}; + +struct ExtensionConfig { + Buffer quantBuffer; + std::string modelName; std::string isProfiling; - std::string cachePath; std::map opLayout; TuningStrategy tuningStrategy{TuningStrategy::OFF}; + // inputDims and dynamicDims are used in hiai adapter + std::vector> inputDims; + std::vector> dynamicDims; }; -struct Buffer { - void* data; - size_t length; +struct ModelConfig { + bool enableFloat16; + OH_NN_PerformanceMode mode; + OH_NN_Priority priority; + std::string cachePath; + ExtensionConfig extensionConfig; }; struct QuantParam { diff --git a/frameworks/native/neural_network_runtime/device.h b/frameworks/native/neural_network_runtime/device.h index 015e9e5d0380eff8e1a4515c12eb538b6ab9039b..853c102b0ce41b7e977558c1d5380fe1c1203cb0 100644 --- a/frameworks/native/neural_network_runtime/device.h +++ b/frameworks/native/neural_network_runtime/device.h @@ -49,11 +49,9 @@ public: virtual OH_NN_ReturnCode IsModelCacheSupported(bool& isSupported) = 0; virtual OH_NN_ReturnCode PrepareModel(std::shared_ptr model, - const Buffer& quantBuffer, const ModelConfig& config, std::shared_ptr& preparedModel) = 0; virtual OH_NN_ReturnCode PrepareModel(const void* metaGraph, - const Buffer& quantBuffer, const ModelConfig& config, std::shared_ptr& preparedModel) = 0; virtual OH_NN_ReturnCode PrepareModelFromModelCache(const std::vector& modelCache, diff --git a/frameworks/native/neural_network_runtime/hdi_device_v1_0.cpp b/frameworks/native/neural_network_runtime/hdi_device_v1_0.cpp index 5fb12c0a790f4c7ccc29662aa94cdeeeb1fdd2ce..5482c2da8121a0122e70d7f36eaa1eab444a7337 100644 --- a/frameworks/native/neural_network_runtime/hdi_device_v1_0.cpp +++ b/frameworks/native/neural_network_runtime/hdi_device_v1_0.cpp @@ -238,7 +238,7 @@ OH_NN_ReturnCode HDIDeviceV1_0::IsModelCacheSupported(bool& isSupported) } OH_NN_ReturnCode HDIDeviceV1_0::PrepareModel(std::shared_ptr model, - const Buffer& quantBuffer, const ModelConfig& config, std::shared_ptr& preparedModel) + const ModelConfig& config, std::shared_ptr& preparedModel) { if (model == nullptr) { LOGE("Model is nullptr, cannot prepare model."); @@ -292,7 +292,6 @@ OH_NN_ReturnCode HDIDeviceV1_0::PrepareModel(std::shared_ptr& preparedModel) { diff --git a/frameworks/native/neural_network_runtime/hdi_device_v1_0.h b/frameworks/native/neural_network_runtime/hdi_device_v1_0.h index fb0f2aeb41f0782ef1b0d7f36720c3642a53e16c..cc78f11e4fead24b4dc8323f998de7b8fb598020 100644 --- a/frameworks/native/neural_network_runtime/hdi_device_v1_0.h +++ b/frameworks/native/neural_network_runtime/hdi_device_v1_0.h @@ -45,11 +45,9 @@ public: OH_NN_ReturnCode IsModelCacheSupported(bool& isSupported) override; OH_NN_ReturnCode PrepareModel(std::shared_ptr model, - const Buffer& quantBuffer, const ModelConfig& config, std::shared_ptr& preparedModel) override; OH_NN_ReturnCode PrepareModel(const void* metaGraph, - const Buffer& quantBuffer, const ModelConfig& config, std::shared_ptr& preparedModel) override; OH_NN_ReturnCode PrepareModelFromModelCache(const std::vector& modelCache, diff --git a/frameworks/native/neural_network_runtime/hdi_device_v2_0.cpp b/frameworks/native/neural_network_runtime/hdi_device_v2_0.cpp index 3d5bd6578b2f805a45e738b64d1ac5b031d63e85..18755a3d6d84a3517a4ab4c6085a297a04d28952 100644 --- a/frameworks/native/neural_network_runtime/hdi_device_v2_0.cpp +++ b/frameworks/native/neural_network_runtime/hdi_device_v2_0.cpp @@ -277,7 +277,7 @@ OH_NN_ReturnCode HDIDeviceV2_0::IsModelCacheSupported(bool& isSupported) } OH_NN_ReturnCode HDIDeviceV2_0::PrepareModel(std::shared_ptr model, - const Buffer& quantBuffer, const ModelConfig& config, std::shared_ptr& preparedModel) + const ModelConfig& config, std::shared_ptr& preparedModel) { if (model == nullptr) { LOGE("Model is nullptr, cannot prepare model."); @@ -329,7 +329,6 @@ OH_NN_ReturnCode HDIDeviceV2_0::PrepareModel(std::shared_ptr& preparedModel) { diff --git a/frameworks/native/neural_network_runtime/hdi_device_v2_0.h b/frameworks/native/neural_network_runtime/hdi_device_v2_0.h index 2c3aab5e0b7f940feb8fe8e2c82d3879e94687f2..f9ec9d739b3dc3732d0cd3f1b1d4f0bdae5ca2eb 100644 --- a/frameworks/native/neural_network_runtime/hdi_device_v2_0.h +++ b/frameworks/native/neural_network_runtime/hdi_device_v2_0.h @@ -45,11 +45,9 @@ public: OH_NN_ReturnCode IsModelCacheSupported(bool& isSupported) override; OH_NN_ReturnCode PrepareModel(std::shared_ptr model, - const Buffer& quantBuffer, const ModelConfig& config, std::shared_ptr& preparedModel) override; OH_NN_ReturnCode PrepareModel(const void* metaGraph, - const Buffer& quantBuffer, const ModelConfig& config, std::shared_ptr& preparedModel) override; OH_NN_ReturnCode PrepareModelFromModelCache(const std::vector& modelCache, diff --git a/frameworks/native/neural_network_runtime/hdi_device_v2_1.cpp b/frameworks/native/neural_network_runtime/hdi_device_v2_1.cpp index 0d8024647ef619a3696791e1aae78352ed52977d..30f35e9757b96083178a8f08c8877a59c344b4ea 100644 --- a/frameworks/native/neural_network_runtime/hdi_device_v2_1.cpp +++ b/frameworks/native/neural_network_runtime/hdi_device_v2_1.cpp @@ -277,7 +277,7 @@ OH_NN_ReturnCode HDIDeviceV2_1::IsModelCacheSupported(bool& isSupported) } OH_NN_ReturnCode HDIDeviceV2_1::PrepareModel(std::shared_ptr model, - const Buffer& quantBuffer, const ModelConfig& config, std::shared_ptr& preparedModel) + const ModelConfig& config, std::shared_ptr& preparedModel) { if (model == nullptr) { LOGE("Model is nullptr, cannot prepare model."); @@ -329,7 +329,6 @@ OH_NN_ReturnCode HDIDeviceV2_1::PrepareModel(std::shared_ptr& preparedModel) { diff --git a/frameworks/native/neural_network_runtime/hdi_device_v2_1.h b/frameworks/native/neural_network_runtime/hdi_device_v2_1.h index 06016b9e334d89e62d79a3c0e96c177b90c6ee31..0cde9a7d0c4e865a125f0358a8df276d7a8bdad8 100644 --- a/frameworks/native/neural_network_runtime/hdi_device_v2_1.h +++ b/frameworks/native/neural_network_runtime/hdi_device_v2_1.h @@ -45,11 +45,9 @@ public: OH_NN_ReturnCode IsModelCacheSupported(bool& isSupported) override; OH_NN_ReturnCode PrepareModel(std::shared_ptr model, - const Buffer& quantBuffer, const ModelConfig& config, std::shared_ptr& preparedModel) override; OH_NN_ReturnCode PrepareModel(const void* metaGraph, - const Buffer& quantBuffer, const ModelConfig& config, std::shared_ptr& preparedModel) override; OH_NN_ReturnCode PrepareModelFromModelCache(const std::vector& modelCache, diff --git a/frameworks/native/neural_network_runtime/inner_model.cpp b/frameworks/native/neural_network_runtime/inner_model.cpp index 15f51e234ea35a83db63e8e0f3f9424f6b3a2378..6ad34df241f65a098e5d69dac8097431db1c54f9 100644 --- a/frameworks/native/neural_network_runtime/inner_model.cpp +++ b/frameworks/native/neural_network_runtime/inner_model.cpp @@ -109,8 +109,8 @@ bool InnerModel::IsBuild() const return ((m_liteGraph != nullptr) || (m_metaGraph != nullptr)); } -OH_NN_ReturnCode InnerModel::BuildFromLiteGraph(const MSLITE::LiteGraph* liteGraph, const Buffer& quantBuffer, - const std::string& modelName, const std::string& isProfiling, std::map& opLayouts) +OH_NN_ReturnCode InnerModel::BuildFromLiteGraph(const MSLITE::LiteGraph* liteGraph, + const ExtensionConfig& extensionConfig) { NNRT_TRACE_NAME("Build model from lite graph"); if (liteGraph == nullptr) { @@ -145,17 +145,12 @@ OH_NN_ReturnCode InnerModel::BuildFromLiteGraph(const MSLITE::LiteGraph* liteGra m_liteGraph.reset(const_cast(liteGraph), LiteGraphDeleter()); m_liteGraph->name_ = LOADED_NNR_MODEL; - m_quantBuffer = quantBuffer; - m_modelName = modelName; - m_isProfiling = isProfiling; - m_opLayouts = opLayouts; + m_extensionConfig = extensionConfig; return OH_NN_SUCCESS; } -OH_NN_ReturnCode InnerModel::BuildFromMetaGraph( - const void* metaGraph, const Buffer& quantBuffer, const std::string& modelName, const std::string& isProfiling, - std::map& opLayouts) +OH_NN_ReturnCode InnerModel::BuildFromMetaGraph(const void* metaGraph, const ExtensionConfig& extensionConfig) { NNRT_TRACE_NAME("Build model from meta graph"); if (metaGraph == nullptr) { @@ -174,10 +169,7 @@ OH_NN_ReturnCode InnerModel::BuildFromMetaGraph( } m_metaGraph = const_cast(metaGraph); - m_quantBuffer = quantBuffer; - m_modelName = modelName; - m_isProfiling = isProfiling; - m_opLayouts = opLayouts; + m_extensionConfig = extensionConfig; return OH_NN_SUCCESS; } @@ -761,34 +753,9 @@ void* InnerModel::GetMetaGraph() const return m_metaGraph; } -Buffer InnerModel::GetQuantBuffer() const +ExtensionConfig InnerModel::GetExtensionConfig() const { - return m_quantBuffer; -} - -std::string InnerModel::GetModelName() const -{ - return m_modelName; -} - -std::string InnerModel::GetProfiling() const -{ - return m_isProfiling; -} - -std::map InnerModel::GetOpLayouts() const -{ - return m_opLayouts; -} - -TuningStrategy InnerModel::GetTuningStrategy() const -{ - return m_tuningStrategy; -} - -void InnerModel::SetTuningStrategy(const TuningStrategy tuningStrategy) -{ - m_tuningStrategy = tuningStrategy; + return m_extensionConfig; } } // namespace NeuralNetworkRuntime } // namespace OHOS diff --git a/frameworks/native/neural_network_runtime/inner_model.h b/frameworks/native/neural_network_runtime/inner_model.h index a5c3fb068b18ce5baa95a0758b0b8c80e90c487e..066ab7bb50f32e5250a77837420674420943871a 100644 --- a/frameworks/native/neural_network_runtime/inner_model.h +++ b/frameworks/native/neural_network_runtime/inner_model.h @@ -32,12 +32,9 @@ public: InnerModel(); bool IsBuild() const; - OH_NN_ReturnCode BuildFromLiteGraph(const mindspore::lite::LiteGraph* liteGraph, const Buffer& quantBuffer, - const std::string& modelName, const std::string& isProfiling, - std::map& opLayouts); - OH_NN_ReturnCode BuildFromMetaGraph(const void* metaGraph, const Buffer& quantBuffer, - const std::string& modelName, const std::string& isProfiling, - std::map& opLayouts); + OH_NN_ReturnCode BuildFromLiteGraph(const mindspore::lite::LiteGraph* liteGraph, + const ExtensionConfig& extensionConfig); + OH_NN_ReturnCode BuildFromMetaGraph(const void* metaGraph, const ExtensionConfig& extensionConfig); OH_NN_ReturnCode AddTensor(const OH_NN_Tensor& nnTensor); OH_NN_ReturnCode AddTensorDesc(const NN_TensorDesc* nnTensorDesc); OH_NN_ReturnCode SetTensorQuantParam(uint32_t index, const NN_QuantParam* quantParam); @@ -59,12 +56,7 @@ public: std::vector, OH_NN_TensorType>> GetOutputTensorDescs() const; std::shared_ptr GetLiteGraphs() const; void* GetMetaGraph() const; - Buffer GetQuantBuffer() const; - std::string GetModelName() const; - std::string GetProfiling() const; - std::map GetOpLayouts() const; - TuningStrategy GetTuningStrategy() const; - void SetTuningStrategy(const TuningStrategy tuningStrategy); + ExtensionConfig GetExtensionConfig() const; private: void AddTensorsToLiteGraph(std::unordered_map& modelIDToGraphID); @@ -84,11 +76,7 @@ private: std::vector> m_outputTensors; // Used to pass output tensors to compilation. std::shared_ptr m_liteGraph {nullptr}; void* m_metaGraph {nullptr}; - Buffer m_quantBuffer = {nullptr, 0}; - std::string m_modelName; - std::string m_isProfiling; - std::map m_opLayouts; - TuningStrategy m_tuningStrategy{TuningStrategy::OFF}; + ExtensionConfig m_extensionConfig; }; } // namespace NeuralNetworkRuntime } // namespace OHOS diff --git a/frameworks/native/neural_network_runtime/neural_network_runtime.cpp b/frameworks/native/neural_network_runtime/neural_network_runtime.cpp index f6402f61165e2dfe016e824f14e1d7fb6d538db9..4b7aac83b0dbed5695a45fa6c0a514bcc8036227 100644 --- a/frameworks/native/neural_network_runtime/neural_network_runtime.cpp +++ b/frameworks/native/neural_network_runtime/neural_network_runtime.cpp @@ -27,6 +27,13 @@ using namespace OHOS::NeuralNetworkRuntime; #define NNRT_API __attribute__((visibility("default"))) +const std::string EXTENSION_KEY_QUANT_BUFFER = "QuantBuffer"; +const std::string EXTENSION_KEY_MODEL_NAME = "ModelName"; +const std::string EXTENSION_KEY_IS_PROFILING = "isProfiling"; +const std::string EXTENSION_KEY_OP_LAYOUT = "opLayout"; +const std::string EXTENSION_KEY_INPUT_DIMS = "InputDims"; +const std::string EXTENSION_KEY_DYNAMIC_DIMS = "DynamicDims"; + NNRT_API NN_QuantParam *OH_NNQuantParam_Create() { auto* quantParamImpl = new (std::nothrow) QuantParams(); @@ -294,6 +301,167 @@ NNRT_API OH_NN_ReturnCode OH_NNModel_Finish(OH_NNModel *model) return innerModel->Build(); } +OH_NN_ReturnCode ParseInputDimsFromExtensions(char* data, size_t dataSize, const mindspore::lite::LiteGraph* liteGraph, + ExtensionConfig& extensionConfig, size_t& dynamicCount) +{ + extensionConfig.inputDims.clear(); + int32_t* dimsValue = reinterpret_cast(data); + size_t allDimsSize = dataSize / sizeof(int32_t); + + size_t inputCount = liteGraph->input_indices_.size(); // LiteGraph输入个数 + size_t allTensorSize = liteGraph->all_tensors_.size(); // LiteGraph所有tensor个数 + size_t inputDimSize = 0; // 存放每个输入的维度 + std::vector inputDim; + size_t dataIndex = 0; + for (size_t i = 0; i < inputCount; ++i) { + inputDim.clear(); + if (liteGraph->input_indices_[i] >= allTensorSize) { + LOGE("ParseInputDimsFromExtensions failed, indice of input %u is out of range.", + liteGraph->input_indices_[i]); + extensionConfig.inputDims.clear(); + return OH_NN_INVALID_PARAMETER; + } + //获取当前输入的维度 + mindspore::lite::TensorPtr tensor = liteGraph->all_tensors_[liteGraph->input_indices_[i]]; + auto tensorDims = mindspore::lite::MindIR_Tensor_GetDims(tensor); + inputDimSize = tensorDims.size(); + if (allDimsSize < inputDimSize) { + LOGE("ParseInputDimsFromExtensions failed, dataSize is invalid."); + extensionConfig.inputDims.clear(); + return OH_NN_INVALID_PARAMETER; + } + // 读取extensor中当前输入的dim值 + for (size_t j = 0; j < inputDimSize; ++j) { + inputDim.emplace_back(dimsValue[dataIndex]); + if (dimsValue[dataIndex] == -1) { + ++dynamicCount; + } + ++dataIndex; + } + extensionConfig.inputDims.emplace_back(inputDim); + allDimsSize -= inputDimSize; + } + // allDimsSize应和模型一致,遍历完后,allDimsSize等于0 + if (allDimsSize != 0) { + LOGE("ParseInputDimsFromExtensions failed, allDimsSize is not equal to liteGraph."); + extensionConfig.inputDims.clear(); + return OH_NN_INVALID_PARAMETER; + } + return OH_NN_SUCCESS; +} + +OH_NN_ReturnCode ParseDynamicDimsFromExtensions( + const std::unordered_map>>& extensionMap, + const mindspore::lite::LiteGraph* liteGraph, ExtensionConfig& extensionConfig) +{ + const std::vector>& inputDims = extensionMap.at(EXTENSION_KEY_INPUT_DIMS); + if (inputDims.empty()) { + LOGE("ParseDynamicDimsFromExtensions failed, input dims is empty."); + return OH_NN_INVALID_PARAMETER; + } + auto dynamicDims = extensionMap.at(EXTENSION_KEY_DYNAMIC_DIMS); + if (dynamicDims.empty()) { + LOGE("ParseDynamicDimsFromExtensions failed, dynamic dims is empty."); + return OH_NN_INVALID_PARAMETER; + } + if (inputDims[0].first == nullptr || inputDims[0].second == 0 || + dynamicDims[0].first == nullptr || dynamicDims[0].second == 0) { + LOGE("ParseDynamicDimsFromExtensions failed, data or dataSize is invalid."); + return OH_NN_INVALID_PARAMETER; + } + + size_t dynamicCount = 0; + auto returnCode = ParseInputDimsFromExtensions( + inputDims[0].first, inputDims[0].second, liteGraph, extensionConfig, dynamicCount); + if (returnCode != OH_NN_SUCCESS) { + LOGE("ParseDynamicDimsFromExtensions failed, failed to get input dims from extensions."); + return returnCode; + } + if (dynamicCount == 0) { + LOGE("ParseDynamicDimsFromExtensions failed, dynamic count is 0."); + extensionConfig.inputDims.clear(); + return OH_NN_INVALID_PARAMETER; + } + + extensionConfig.dynamicDims.clear(); + int32_t* dynamicDimsValue = reinterpret_cast(dynamicDims[0].first); + size_t dynamicDimsSize = dynamicDims[0].second / sizeof(int32_t); + if ((dynamicDimsSize % dynamicCount) != 0) { + LOGE("ParseDynamicDimsFromExtensions failed, dynamic dataSize is invalid."); + extensionConfig.inputDims.clear(); + return OH_NN_INVALID_PARAMETER; + } + size_t dynamicSize = dynamicDimsSize / dynamicCount; + std::vector dynamicDim; + size_t dataIndex = 0; + for (size_t i = 0; i < dynamicSize; ++i) { + dynamicDim.clear(); + for (size_t j = 0; j < dynamicCount; ++j) { + dynamicDim.emplace_back(dynamicDimsValue[dataIndex]); + ++dataIndex; + } + extensionConfig.dynamicDims.emplace_back(dynamicDim); + } + + return OH_NN_SUCCESS; +} + +OH_NN_ReturnCode ParseExtensionConfigs( + const std::unordered_map>>& extensionMap, + const mindspore::lite::LiteGraph* pLiteGraph, ExtensionConfig& extensionConfig) +{ + extensionConfig.tuningStrategy = TuningStrategy::ON_DEVICE_PREPROCESS_TUNING; + if (extensionMap.find(EXTENSION_KEY_QUANT_BUFFER) != extensionMap.end()) { + const std::vector>& value = extensionMap.at(EXTENSION_KEY_QUANT_BUFFER); + if (value.empty()) { + LOGE("ParseExtensionConfigs failed, get empty quant buffer value."); + return OH_NN_INVALID_PARAMETER; + } + extensionConfig.quantBuffer.data = value[0].first; + extensionConfig.quantBuffer.length = value[0].second; + } + if (extensionMap.find(EXTENSION_KEY_MODEL_NAME) != extensionMap.end()) { + const std::vector>& value = extensionMap.at(EXTENSION_KEY_MODEL_NAME); + if (value.empty()) { + LOGE("ParseExtensionConfigs failed, get empty model name value."); + return OH_NN_INVALID_PARAMETER; + } + extensionConfig.modelName.assign(value[0].first, value[0].first + value[0].second); + } + if (extensionMap.find(EXTENSION_KEY_IS_PROFILING) != extensionMap.end()) { + const std::vector>& value = extensionMap.at(EXTENSION_KEY_IS_PROFILING); + if (value.empty()) { + LOGE("ParseExtensionConfigs failed, get empty isProfiling value."); + return OH_NN_INVALID_PARAMETER; + } + extensionConfig.isProfiling.assign(value[0].first, value[0].first + value[0].second); + } + if (extensionMap.find(EXTENSION_KEY_OP_LAYOUT) != extensionMap.end()) { + const std::vector>& value = extensionMap.at(EXTENSION_KEY_OP_LAYOUT); + if (value.empty()) { + LOGE("ParseExtensionConfigs failed, get empty op layout value."); + return OH_NN_INVALID_PARAMETER; + } + std::string ops; + for (auto singleValue : value) { + ops.assign(singleValue.first, singleValue.first + singleValue.second); + extensionConfig.opLayout.insert({ops, "hiai::ExecuteDevice::CPU"}); + LOGI("ParseExtensionConfigs opLayout:%{public}s.", ops.c_str()); + } + } + if (extensionMap.find(EXTENSION_KEY_INPUT_DIMS) != extensionMap.end() && + extensionMap.find(EXTENSION_KEY_DYNAMIC_DIMS) != extensionMap.end()) { + auto returnCode = ParseDynamicDimsFromExtensions(extensionMap, pLiteGraph, extensionConfig); + if (returnCode != OH_NN_SUCCESS) { + LOGE("ParseExtensionConfigs failed, parse dynamic dims from extensions failed."); + return returnCode; + } + extensionConfig.tuningStrategy = TuningStrategy::OFF; // 分档shape不支持fftl + } + + return OH_NN_SUCCESS; +} + NNRT_API OH_NN_ReturnCode OH_NNModel_BuildFromLiteGraph(OH_NNModel *model, const void *liteGraph, const OH_NN_Extension *extensions, size_t extensionSize) { @@ -307,36 +475,29 @@ NNRT_API OH_NN_ReturnCode OH_NNModel_BuildFromLiteGraph(OH_NNModel *model, const return OH_NN_INVALID_PARAMETER; } - Buffer buffer; - std::string modelName; - std::string isProfiling; - std::string opLayout; - std::map opLayouts; + auto *pLiteGraph = reinterpret_cast(liteGraph); + ExtensionConfig extensionConfig; + std::unordered_map>> extensionMap; for (size_t i = 0; i < extensionSize; ++i) { std::string name = extensions[i].name; - if (name == "QuantBuffer") { - buffer.data = extensions[i].value; - buffer.length = extensions[i].valueSize; - } else if (name == "ModelName") { - modelName.assign(extensions[i].value, extensions[i].value + extensions[i].valueSize); - } else if (name == "Profiling") { - isProfiling.assign(extensions[i].value, extensions[i].value + extensions[i].valueSize); - LOGI("OH_NNModel_BuildFromLiteGraph isProfiling enable."); - } else if (name == "opLayout") { - opLayout.assign(extensions[i].value, extensions[i].value + extensions[i].valueSize); - opLayouts.insert({opLayout, "hiai::ExecuteDevice::CPU"}); - LOGI("OH_NNModel_BuildFromLiteGraph opLayout:%{public}s.", opLayout.c_str()); + if (extensionMap.find(name) == extensionMap.end()) { + extensionMap.insert({name, {{extensions[i].value, extensions[i].valueSize}}}); + } else { + extensionMap[name].push_back({extensions[i].value, extensions[i].valueSize}); } } + auto returnCode = ParseExtensionConfigs(extensionMap, pLiteGraph, extensionConfig); + if (returnCode != OH_NN_SUCCESS) { + LOGE("OH_NNModel_BuildFromLiteGraph failed, parse extension configs failed."); + return returnCode; + } - auto *pLiteGraph = static_cast(liteGraph); InnerModel *innerModel = reinterpret_cast(model); - innerModel->SetTuningStrategy(TuningStrategy::ON_DEVICE_PREPROCESS_TUNING); // Once the innerModel built from the liteGraph successfully, the innerModel // owns the liteGraph, in which case, the invoker should not delete // the liteGraph actively. Otherwise, the invoker still has the ownership. - return innerModel->BuildFromLiteGraph(pLiteGraph, buffer, modelName, isProfiling, opLayouts); + return innerModel->BuildFromLiteGraph(pLiteGraph, extensionConfig); } NNRT_API OH_NN_ReturnCode OH_NNModel_BuildFromMetaGraph(OH_NNModel *model, const void *metaGraph, @@ -352,30 +513,27 @@ NNRT_API OH_NN_ReturnCode OH_NNModel_BuildFromMetaGraph(OH_NNModel *model, const return OH_NN_INVALID_PARAMETER; } - Buffer buffer; - std::string modelName; - std::string isProfiling; - std::string opLayout; - std::map opLayouts; + ExtensionConfig extensionConfig; + std::string ops; for (size_t i = 0; i < extensionSize; ++i) { std::string name = extensions[i].name; if (name == "QuantBuffer") { - buffer.data = extensions[i].value; - buffer.length = extensions[i].valueSize; + extensionConfig.quantBuffer.data = extensions[i].value; + extensionConfig.quantBuffer.length = extensions[i].valueSize; } else if (name == "ModelName") { - modelName.assign(extensions[i].value, extensions[i].value + extensions[i].valueSize); + extensionConfig.modelName.assign(extensions[i].value, extensions[i].value + extensions[i].valueSize); } else if (name == "Profiling") { - isProfiling.assign(extensions[i].value, extensions[i].value + extensions[i].valueSize); + extensionConfig.isProfiling.assign(extensions[i].value, extensions[i].value + extensions[i].valueSize); LOGI("OH_NNModel_BuildFromMetaGraph isProfiling enable."); } else if (name == "opLayout") { - opLayout.assign(extensions[i].value, extensions[i].value + extensions[i].valueSize); - opLayouts.insert({opLayout, "hiai::ExecuteDevice::CPU"}); - LOGI("OH_NNModel_BuildFromMetaGraph opLayout:%{public}s.", opLayout.c_str()); + ops.assign(extensions[i].value, extensions[i].value + extensions[i].valueSize); + extensionConfig.opLayout.insert({ops, "hiai::ExecuteDevice::CPU"}); + LOGI("OH_NNModel_BuildFromMetaGraph opLayout:%{public}s.", ops.c_str()); } } InnerModel *innerModel = reinterpret_cast(model); - return innerModel->BuildFromMetaGraph(metaGraph, buffer, modelName, isProfiling, opLayouts); + return innerModel->BuildFromMetaGraph(metaGraph, extensionConfig); } NNRT_API OH_NN_ReturnCode OH_NNModel_SetInputsAndOutputsInfo(OH_NNModel *model, const OH_NN_TensorInfo *inputsInfo, diff --git a/frameworks/native/neural_network_runtime/nncompiler.cpp b/frameworks/native/neural_network_runtime/nncompiler.cpp index ad383f46d0e147cf486bd3f3d0b6156701945db3..45a9fca5a1f55b360d2f337e9f5e596fb3d325c2 100644 --- a/frameworks/native/neural_network_runtime/nncompiler.cpp +++ b/frameworks/native/neural_network_runtime/nncompiler.cpp @@ -126,11 +126,7 @@ NNCompiler::NNCompiler(const void* model, std::shared_ptr device, size_t m_inputTensorDescs = m_innerModel->GetInputTensorDescs(); m_outputTensorDescs = m_innerModel->GetOutputTensorDescs(); m_metaGraph = m_innerModel->GetMetaGraph(); - m_quantBuffer = m_innerModel->GetQuantBuffer(); - m_modelName = m_innerModel->GetModelName(); - m_isProfiling = m_innerModel->GetProfiling(); - m_opLayouts = m_innerModel->GetOpLayouts(); - m_tuningStrategy = m_innerModel->GetTuningStrategy(); + m_extensionConfig = m_innerModel->GetExtensionConfig(); } NNCompiler::~NNCompiler() @@ -376,12 +372,12 @@ OH_NN_ReturnCode NNCompiler::NormalBuild() } ModelConfig config {m_enableFp16, static_cast(m_performance), - static_cast(m_priority), m_isProfiling, m_cachePath, m_opLayouts, m_tuningStrategy}; + static_cast(m_priority), m_cachePath, m_extensionConfig}; if (m_liteGraph != nullptr) { - ret = m_device->PrepareModel(m_liteGraph, m_quantBuffer, config, m_preparedModel); + ret = m_device->PrepareModel(m_liteGraph, config, m_preparedModel); } if (m_metaGraph != nullptr) { - ret = m_device->PrepareModel(m_metaGraph, m_quantBuffer, config, m_preparedModel); + ret = m_device->PrepareModel(m_metaGraph, config, m_preparedModel); } if (ret != OH_NN_SUCCESS) { LOGE("[NNCompiler] Build failed, fail to prepare model when normally building."); @@ -529,7 +525,7 @@ OH_NN_ReturnCode NNCompiler::SaveToCacheFile() const caches.emplace_back(outputTensorDescBuffer); tensorBuffers.emplace_back(outputTensorDescBuffer); - compiledCache.SetModelName(m_modelName); + compiledCache.SetModelName(m_extensionConfig.modelName); ret = compiledCache.Save(caches, m_cachePath, m_cacheVersion); if (ret != OH_NN_SUCCESS) { LOGE("[NNCompiler] SaveToCacheFile failed, error happened when saving model cache."); @@ -567,7 +563,7 @@ OH_NN_ReturnCode NNCompiler::RestoreFromCacheFile() } std::vector caches; - compiledCache.SetModelName(m_modelName); + compiledCache.SetModelName(m_extensionConfig.modelName); ret = compiledCache.Restore(m_cachePath, m_cacheVersion, caches); if (ret != OH_NN_SUCCESS) { LOGE("[NNCompiler] RestoreFromCacheFile failed, error happened when restoring model cache."); diff --git a/frameworks/native/neural_network_runtime/nncompiler.h b/frameworks/native/neural_network_runtime/nncompiler.h index 62b88ee672c80d449171b809306e6ebc6ea0db98..5a22574d0644c0e312664bac4aec8ea8b127ad72 100644 --- a/frameworks/native/neural_network_runtime/nncompiler.h +++ b/frameworks/native/neural_network_runtime/nncompiler.h @@ -80,16 +80,12 @@ private: OH_NN_Priority m_priority {OH_NN_PRIORITY_NONE}; OH_NN_PerformanceMode m_performance {OH_NN_PERFORMANCE_NONE}; std::shared_ptr m_preparedModel {nullptr}; - Buffer m_quantBuffer {nullptr, 0}; - std::string m_modelName; - std::string m_isProfiling; - std::map m_opLayouts; void* m_metaGraph {nullptr}; InnerModel* m_innerModel {nullptr}; std::shared_ptr m_liteGraph {nullptr}; std::vector, OH_NN_TensorType>> m_inputTensorDescs; std::vector, OH_NN_TensorType>> m_outputTensorDescs; - TuningStrategy m_tuningStrategy{TuningStrategy::OFF}; + ExtensionConfig m_extensionConfig; }; } // NeuralNetworkRuntime } // OHOS diff --git a/test/unittest/common/v1_0/compilation_mock_idevice.cpp b/test/unittest/common/v1_0/compilation_mock_idevice.cpp index a0217862f77432595a1e5a13e8d9f5871eb4a649..f99b30278fd395d17e73753d3cbcecf542dc7bd3 100644 --- a/test/unittest/common/v1_0/compilation_mock_idevice.cpp +++ b/test/unittest/common/v1_0/compilation_mock_idevice.cpp @@ -139,7 +139,7 @@ OH_NN_ReturnCode HDIDeviceV1_0::IsFloat16PrecisionSupported(bool& isSupported) } OH_NN_ReturnCode HDIDeviceV1_0::PrepareModel(std::shared_ptr model, - const Buffer& quantBuffer, const ModelConfig& config, std::shared_ptr& preparedModel) + const ModelConfig& config, std::shared_ptr& preparedModel) { if (model == nullptr) { LOGE("HDIDeviceV1_0 mock PrepareModel failed, the model is nullptr"); diff --git a/test/unittest/common/v2_0/compilation_mock_idevice.cpp b/test/unittest/common/v2_0/compilation_mock_idevice.cpp index 0d4bd04f09a1ef4fcc305582770dc00e9fa5e2b8..d9aea094835262f2aab93eec768f0bfac471c87c 100644 --- a/test/unittest/common/v2_0/compilation_mock_idevice.cpp +++ b/test/unittest/common/v2_0/compilation_mock_idevice.cpp @@ -139,7 +139,7 @@ OH_NN_ReturnCode HDIDeviceV2_0::IsFloat16PrecisionSupported(bool& isSupported) } OH_NN_ReturnCode HDIDeviceV2_0::PrepareModel(std::shared_ptr model, - const Buffer& quantBuffer, const ModelConfig& config, std::shared_ptr& preparedModel) + const ModelConfig& config, std::shared_ptr& preparedModel) { if (model == nullptr) { LOGE("HDIDeviceV2_0 mock PrepareModel failed, the model is nullptr"); diff --git a/test/unittest/components/v1_0/hdi_device/hdi_device_test.cpp b/test/unittest/components/v1_0/hdi_device/hdi_device_test.cpp index f21d4b0d816da1884a0aa1cf3d0cb04a7e9adc02..fa924948de96d63a259d93ab4153ea9bc23d546c 100644 --- a/test/unittest/components/v1_0/hdi_device/hdi_device_test.cpp +++ b/test/unittest/components/v1_0/hdi_device/hdi_device_test.cpp @@ -111,8 +111,7 @@ OH_NN_ReturnCode HDIDeviceTest::PrepareModel(int32_t allocBufferType, int32_t pr ::testing::Return(prepareType))); ModelConfig config; - Buffer quantBuffer; - OH_NN_ReturnCode result = hdiDevice->PrepareModel(model, quantBuffer, config, preparedModel); + OH_NN_ReturnCode result = hdiDevice->PrepareModel(model, config, preparedModel); return result; } @@ -600,9 +599,8 @@ HWTEST_F(HDIDeviceTest, hdidevice_preparemodel_002, TestSize.Level0) std::shared_ptr model = nullptr; ModelConfig config; - Buffer quantBuffer; std::shared_ptr preparedModel; - OH_NN_ReturnCode result = hdiDevice->PrepareModel(model, quantBuffer, config, preparedModel); + OH_NN_ReturnCode result = hdiDevice->PrepareModel(model, config, preparedModel); EXPECT_EQ(OH_NN_INVALID_PARAMETER, result); } diff --git a/test/unittest/components/v1_0/inner_model/inner_model_test.cpp b/test/unittest/components/v1_0/inner_model/inner_model_test.cpp index b4a4e36028d522660222260a04cfffaa7fcad524..4ed1e4f2dc25c9b619a643c4bf64ac35bd52de2d 100644 --- a/test/unittest/components/v1_0/inner_model/inner_model_test.cpp +++ b/test/unittest/components/v1_0/inner_model/inner_model_test.cpp @@ -109,14 +109,10 @@ HWTEST_F(InnerModelTest, inner_model_construct_nntensor_from_litegraph_001, Test SetLiteGraph(liteGraph); - Buffer buffer; - std::string modelName; - std::string isProfiling; - std::string opLayout; - std::map opLayouts; + ExtensionConfig extensionConfig; EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_innerModelTest - .BuildFromLiteGraph(liteGraph, buffer, modelName, isProfiling, opLayouts)); + .BuildFromLiteGraph(liteGraph, extensionConfig)); } /** @@ -132,14 +128,10 @@ HWTEST_F(InnerModelTest, inner_model_construct_nntensor_from_litegraph_002, Test SetLiteGraph(liteGraph); - Buffer buffer; - std::string modelName; - std::string isProfiling; - std::string opLayout; - std::map opLayouts; + ExtensionConfig extensionConfig; EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_innerModelTest - .BuildFromLiteGraph(liteGraph, buffer, modelName, isProfiling, opLayouts)); + .BuildFromLiteGraph(liteGraph, extensionConfig)); } /** @@ -154,13 +146,9 @@ HWTEST_F(InnerModelTest, inner_model_construct_nntensor_from_litegraph_003, Test SetLiteGraph(liteGraph); - Buffer buffer; - std::string modelName; - std::string isProfiling; - std::string opLayout; - std::map opLayouts; + ExtensionConfig extensionConfig; - EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.BuildFromLiteGraph(liteGraph, buffer, modelName, isProfiling, opLayouts)); + EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.BuildFromLiteGraph(liteGraph, extensionConfig)); } /** @@ -176,13 +164,9 @@ HWTEST_F(InnerModelTest, inner_model_construct_nntensor_from_litegraph_004, Test SetLiteGraph(liteGraph); - Buffer buffer; - std::string modelName; - std::string isProfiling; - std::string opLayout; - std::map opLayouts; + ExtensionConfig extensionConfig; - EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.BuildFromLiteGraph(liteGraph, buffer, modelName, isProfiling, opLayouts)); + EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.BuildFromLiteGraph(liteGraph, extensionConfig)); } /** @@ -198,14 +182,10 @@ HWTEST_F(InnerModelTest, inner_model_construct_nntensor_from_litegraph_005, Test SetLiteGraph(liteGraph); - Buffer buffer; - std::string modelName; - std::string isProfiling; - std::string opLayout; - std::map opLayouts; + ExtensionConfig extensionConfig; EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_innerModelTest - .BuildFromLiteGraph(liteGraph, buffer, modelName, isProfiling, opLayouts)); + .BuildFromLiteGraph(liteGraph, extensionConfig)); } /** @@ -222,30 +202,28 @@ HWTEST_F(InnerModelTest, inner_model_build_from_lite_graph_001, TestSize.Level1) }; OH_NN_Extension *extensions = &on_exit; size_t extensionSize = 1; - Buffer buffer; - std::string modelName; - std::string isProfiling; + + ExtensionConfig extensionConfig; std::string opLayout; - std::map opLayouts; for (size_t i = 0; i < extensionSize; ++i) { std::string name = extensions[i].name; if (name == "QuantBuffer") { - buffer.data = extensions[i].value; - buffer.length = extensions[i].valueSize; + extensionConfig.quantBuffer.data = extensions[i].value; + extensionConfig.quantBuffer.length = extensions[i].valueSize; } else if (name == "ModelName") { - modelName.assign(extensions[i].value, extensions[i].value + extensions[i].valueSize); + extensionConfig.modelName.assign(extensions[i].value, extensions[i].value + extensions[i].valueSize); } else if (name == "Profiling") { - isProfiling.assign(extensions[i].value, extensions[i].value + extensions[i].valueSize); + extensionConfig.isProfiling.assign(extensions[i].value, extensions[i].value + extensions[i].valueSize); LOGI("OH_NNModel_BuildFromLiteGraph isProfiling enable."); } else if (name == "opLayout") { opLayout.assign(extensions[i].value, extensions[i].value + extensions[i].valueSize); - opLayouts.insert({opLayout, "hiai::ExecuteDevice::CPU"}); + extensionConfig.opLayout.insert({opLayout, "hiai::ExecuteDevice::CPU"}); LOGI("OH_NNModel_BuildFromLiteGraph opLayout:%{public}s.", opLayout.c_str()); } } EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_innerModelTest - .BuildFromLiteGraph(nullptr, buffer, modelName, isProfiling, opLayouts)); + .BuildFromLiteGraph(nullptr, extensionConfig)); } /** @@ -259,16 +237,12 @@ HWTEST_F(InnerModelTest, inner_model_build_from_lite_graph_002, TestSize.Level1) EXPECT_NE(nullptr, liteGraph); SetLiteGraph(liteGraph); - - Buffer buffer; - std::string modelName; - std::string isProfiling; - std::string opLayout; - std::map opLayouts; - EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.BuildFromLiteGraph(liteGraph, buffer, modelName, isProfiling, opLayouts)); + ExtensionConfig extensionConfig; + + EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.BuildFromLiteGraph(liteGraph, extensionConfig)); EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, m_innerModelTest - .BuildFromLiteGraph(liteGraph, buffer, modelName, isProfiling, opLayouts)); + .BuildFromLiteGraph(liteGraph, extensionConfig)); } /** @@ -288,14 +262,10 @@ HWTEST_F(InnerModelTest, inner_model_build_from_lite_graph_003, TestSize.Level1) const OH_NN_Tensor& tensor = {OH_NN_INT8, 2, dimInput, nullptr, OH_NN_TENSOR}; EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.AddTensor(tensor)); - Buffer buffer; - std::string modelName; - std::string isProfiling; - std::string opLayout; - std::map opLayouts; + ExtensionConfig extensionConfig; EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, m_innerModelTest - .BuildFromLiteGraph(liteGraph, buffer, modelName, isProfiling, opLayouts)); + .BuildFromLiteGraph(liteGraph, extensionConfig)); } @@ -322,13 +292,9 @@ HWTEST_F(InnerModelTest, inner_model_add_tensor_002, TestSize.Level1) EXPECT_NE(nullptr, liteGraph); SetLiteGraph(liteGraph); - Buffer buffer; - std::string modelName; - std::string isProfiling; - std::string opLayout; - std::map opLayouts; + ExtensionConfig extensionConfig; - EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.BuildFromLiteGraph(liteGraph, buffer, modelName, isProfiling, opLayouts)); + EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.BuildFromLiteGraph(liteGraph, extensionConfig)); const int32_t dimInput[2] = {2, 2}; const OH_NN_Tensor& tensor = {OH_NN_INT8, 2, dimInput, nullptr, OH_NN_TENSOR}; @@ -422,13 +388,9 @@ HWTEST_F(InnerModelTest, inner_model_set_tensor_value_005, TestSize.Level1) EXPECT_NE(nullptr, liteGraph); SetLiteGraph(liteGraph); - Buffer buffer; - std::string modelName; - std::string isProfiling; - std::string opLayout; - std::map opLayouts; + ExtensionConfig extensionConfig; - EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.BuildFromLiteGraph(liteGraph, buffer, modelName, isProfiling, opLayouts)); + EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.BuildFromLiteGraph(liteGraph, extensionConfig)); EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, m_innerModelTest.SetTensorValue(index, static_cast(&activation), sizeof(int8_t))); @@ -509,13 +471,9 @@ HWTEST_F(InnerModelTest, inner_model_add_operation_002, TestSize.Level1) EXPECT_NE(nullptr, liteGraph); SetLiteGraph(liteGraph); - Buffer buffer; - std::string modelName; - std::string isProfiling; - std::string opLayout; - std::map opLayouts; + ExtensionConfig extensionConfig; - EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.BuildFromLiteGraph(liteGraph, buffer, modelName, isProfiling, opLayouts)); + EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.BuildFromLiteGraph(liteGraph, extensionConfig)); EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, m_innerModelTest.AddOperation(m_opType, m_params, m_inputs, m_outputs)); @@ -709,13 +667,9 @@ HWTEST_F(InnerModelTest, inner_model_specify_inputs_and_outputs_002, TestSize.Le EXPECT_NE(nullptr, liteGraph); SetLiteGraph(liteGraph); - Buffer buffer; - std::string modelName; - std::string isProfiling; - std::string opLayout; - std::map opLayouts; + ExtensionConfig extensionConfig; - EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.BuildFromLiteGraph(liteGraph, buffer, modelName, isProfiling, opLayouts)); + EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.BuildFromLiteGraph(liteGraph, extensionConfig)); EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, m_innerModelTest.SpecifyInputsAndOutputs(inputs, outputs)); } @@ -880,13 +834,9 @@ HWTEST_F(InnerModelTest, inner_model_get_supported_operation_002, TestSize.Level EXPECT_NE(nullptr, liteGraph); SetLiteGraph(liteGraph); - Buffer buffer; - std::string modelName; - std::string isProfiling; - std::string opLayout; - std::map opLayouts; + ExtensionConfig extensionConfig; - EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.BuildFromLiteGraph(liteGraph, buffer, modelName, isProfiling, opLayouts)); + EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.BuildFromLiteGraph(liteGraph, extensionConfig)); EXPECT_EQ(OH_NN_FAILED, m_innerModelTest.GetSupportedOperations(deviceID, &isSupported, opCount)); } diff --git a/test/unittest/components/v1_0/neural_network_runtime_test/neural_network_runtime_test.cpp b/test/unittest/components/v1_0/neural_network_runtime_test/neural_network_runtime_test.cpp index 26a74dbca0415e496ef2b7a93b6c2d311326fad8..0e47be67b287289988bafe66d0cccfcc4ed11d53 100644 --- a/test/unittest/components/v1_0/neural_network_runtime_test/neural_network_runtime_test.cpp +++ b/test/unittest/components/v1_0/neural_network_runtime_test/neural_network_runtime_test.cpp @@ -25,7 +25,7 @@ namespace OHOS { namespace NeuralNetworkRuntime { OH_NN_ReturnCode HDIDeviceV1_0::PrepareModel(std::shared_ptr model, - const Buffer& quantBuffer, const ModelConfig& config, std::shared_ptr& preparedModel) + const ModelConfig& config, std::shared_ptr& preparedModel) { if (model == nullptr) { return OH_NN_INVALID_PARAMETER; diff --git a/test/unittest/components/v2_0/hdi_device/hdi_device_test.cpp b/test/unittest/components/v2_0/hdi_device/hdi_device_test.cpp index 53bfbb0ceaf88e55cb5984d312756bca0794d00b..14c96b85dba43fa1f814117e747800abe832617e 100644 --- a/test/unittest/components/v2_0/hdi_device/hdi_device_test.cpp +++ b/test/unittest/components/v2_0/hdi_device/hdi_device_test.cpp @@ -140,8 +140,7 @@ OH_NN_ReturnCode HDIDeviceTest::PrepareModel(int32_t allocBufferType, int32_t pr ::testing::Return(prepareType))); ModelConfig config; - Buffer quantBuffer; - OH_NN_ReturnCode result = hdiDevice->PrepareModel(model, quantBuffer, config, preparedModel); + OH_NN_ReturnCode result = hdiDevice->PrepareModel(model, config, preparedModel); return result; } @@ -638,9 +637,8 @@ HWTEST_F(HDIDeviceTest, hdidevice_preparemodel_002, TestSize.Level0) std::shared_ptr model = nullptr; ModelConfig config; - Buffer quantBuffer; std::shared_ptr preparedModel; - OH_NN_ReturnCode result = hdiDevice->PrepareModel(model, quantBuffer, config, preparedModel); + OH_NN_ReturnCode result = hdiDevice->PrepareModel(model, config, preparedModel); EXPECT_EQ(OH_NN_INVALID_PARAMETER, result); } diff --git a/test/unittest/components/v2_0/inner_model/inner_model_test.cpp b/test/unittest/components/v2_0/inner_model/inner_model_test.cpp index 4a41c4c36205d0d7dd8c5580b1462af4d85f6161..da5a5d0670cf6989ba4909f84efb8cc6f7f393fd 100644 --- a/test/unittest/components/v2_0/inner_model/inner_model_test.cpp +++ b/test/unittest/components/v2_0/inner_model/inner_model_test.cpp @@ -109,14 +109,10 @@ HWTEST_F(InnerModelTest, inner_model_construct_nntensor_from_litegraph_001, Test SetLiteGraph(liteGraph); - Buffer buffer; - std::string modelName; - std::string isProfiling; - std::string opLayout; - std::map opLayouts; + ExtensionConfig extensionConfig; EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_innerModelTest - .BuildFromLiteGraph(liteGraph, buffer, modelName, isProfiling, opLayouts)); + .BuildFromLiteGraph(liteGraph, extensionConfig)); } /** @@ -132,14 +128,10 @@ HWTEST_F(InnerModelTest, inner_model_construct_nntensor_from_litegraph_002, Test SetLiteGraph(liteGraph); - Buffer buffer; - std::string modelName; - std::string isProfiling; - std::string opLayout; - std::map opLayouts; + ExtensionConfig extensionConfig; EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_innerModelTest - .BuildFromLiteGraph(liteGraph, buffer, modelName, isProfiling, opLayouts)); + .BuildFromLiteGraph(liteGraph, extensionConfig)); } /** @@ -154,13 +146,9 @@ HWTEST_F(InnerModelTest, inner_model_construct_nntensor_from_litegraph_003, Test SetLiteGraph(liteGraph); - Buffer buffer; - std::string modelName; - std::string isProfiling; - std::string opLayout; - std::map opLayouts; + ExtensionConfig extensionConfig; - EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.BuildFromLiteGraph(liteGraph, buffer, modelName, isProfiling, opLayouts)); + EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.BuildFromLiteGraph(liteGraph, extensionConfig)); } /** @@ -175,13 +163,10 @@ HWTEST_F(InnerModelTest, inner_model_construct_nntensor_from_litegraph_004, Test m_dimInput = {3, -3}; SetLiteGraph(liteGraph); - Buffer buffer; - std::string modelName; - std::string isProfiling; - std::string opLayout; - std::map opLayouts; - EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.BuildFromLiteGraph(liteGraph, buffer, modelName, isProfiling, opLayouts)); + ExtensionConfig extensionConfig; + + EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.BuildFromLiteGraph(liteGraph, extensionConfig)); } /** @@ -196,14 +181,11 @@ HWTEST_F(InnerModelTest, inner_model_construct_nntensor_from_litegraph_005, Test m_outputIndices = {6}; SetLiteGraph(liteGraph); - Buffer buffer; - std::string modelName; - std::string isProfiling; - std::string opLayout; - std::map opLayouts; + + ExtensionConfig extensionConfig; EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_innerModelTest - .BuildFromLiteGraph(liteGraph, buffer, modelName, isProfiling, opLayouts)); + .BuildFromLiteGraph(liteGraph, extensionConfig)); } /** @@ -220,30 +202,27 @@ HWTEST_F(InnerModelTest, inner_model_build_from_lite_graph_001, TestSize.Level1) }; OH_NN_Extension *extensions = &on_exit; size_t extensionSize = 1; - Buffer buffer; - std::string modelName; - std::string isProfiling; + ExtensionConfig extensionConfig; std::string opLayout; - std::map opLayouts; for (size_t i = 0; i < extensionSize; ++i) { std::string name = extensions[i].name; if (name == "QuantBuffer") { - buffer.data = extensions[i].value; - buffer.length = extensions[i].valueSize; + extensionConfig.quantBuffer.data = extensions[i].value; + extensionConfig.quantBuffer.length = extensions[i].valueSize; } else if (name == "ModelName") { - modelName.assign(extensions[i].value, extensions[i].value + extensions[i].valueSize); + extensionConfig.modelName.assign(extensions[i].value, extensions[i].value + extensions[i].valueSize); } else if (name == "Profiling") { - isProfiling.assign(extensions[i].value, extensions[i].value + extensions[i].valueSize); + extensionConfig.isProfiling.assign(extensions[i].value, extensions[i].value + extensions[i].valueSize); LOGI("OH_NNModel_BuildFromLiteGraph isProfiling enable."); } else if (name == "opLayout") { opLayout.assign(extensions[i].value, extensions[i].value + extensions[i].valueSize); - opLayouts.insert({opLayout, "hiai::ExecuteDevice::CPU"}); + extensionConfig.opLayout.insert({opLayout, "hiai::ExecuteDevice::CPU"}); LOGI("OH_NNModel_BuildFromLiteGraph opLayout:%{public}s.", opLayout.c_str()); } } EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_innerModelTest - .BuildFromLiteGraph(nullptr, buffer, modelName, isProfiling, opLayouts)); + .BuildFromLiteGraph(nullptr, extensionConfig)); } /** @@ -257,15 +236,11 @@ HWTEST_F(InnerModelTest, inner_model_build_from_lite_graph_002, TestSize.Level1) EXPECT_NE(nullptr, liteGraph); SetLiteGraph(liteGraph); - Buffer buffer; - std::string modelName; - std::string isProfiling; - std::string opLayout; - std::map opLayouts; + ExtensionConfig extensionConfig; - EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.BuildFromLiteGraph(liteGraph, buffer, modelName, isProfiling, opLayouts)); + EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.BuildFromLiteGraph(liteGraph, extensionConfig)); EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, m_innerModelTest - .BuildFromLiteGraph(liteGraph, buffer, modelName, isProfiling, opLayouts)); + .BuildFromLiteGraph(liteGraph, extensionConfig)); } /** @@ -284,14 +259,10 @@ HWTEST_F(InnerModelTest, inner_model_build_from_lite_graph_003, TestSize.Level1) const int32_t dimInput[2] = {2, 2}; const OH_NN_Tensor& tensor = {OH_NN_INT8, 2, dimInput, nullptr, OH_NN_TENSOR}; EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.AddTensor(tensor)); - Buffer buffer; - std::string modelName; - std::string isProfiling; - std::string opLayout; - std::map opLayouts; + ExtensionConfig extensionConfig; EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, m_innerModelTest - .BuildFromLiteGraph(liteGraph, buffer, modelName, isProfiling, opLayouts)); + .BuildFromLiteGraph(liteGraph, extensionConfig)); } @@ -318,13 +289,9 @@ HWTEST_F(InnerModelTest, inner_model_add_tensor_002, TestSize.Level1) EXPECT_NE(nullptr, liteGraph); SetLiteGraph(liteGraph); - Buffer buffer; - std::string modelName; - std::string isProfiling; - std::string opLayout; - std::map opLayouts; + ExtensionConfig extensionConfig; - EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.BuildFromLiteGraph(liteGraph, buffer, modelName, isProfiling, opLayouts)); + EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.BuildFromLiteGraph(liteGraph, extensionConfig)); const int32_t dimInput[2] = {2, 2}; const OH_NN_Tensor& tensor = {OH_NN_INT8, 2, dimInput, nullptr, OH_NN_TENSOR}; @@ -418,13 +385,8 @@ HWTEST_F(InnerModelTest, inner_model_set_tensor_value_005, TestSize.Level1) EXPECT_NE(nullptr, liteGraph); SetLiteGraph(liteGraph); - Buffer buffer; - std::string modelName; - std::string isProfiling; - std::string opLayout; - std::map opLayouts; - - EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.BuildFromLiteGraph(liteGraph, buffer, modelName, isProfiling, opLayouts)); + ExtensionConfig extensionConfig; + EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.BuildFromLiteGraph(liteGraph, extensionConfig)); EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, m_innerModelTest.SetTensorValue(index, static_cast(&activation), sizeof(int8_t))); @@ -505,13 +467,9 @@ HWTEST_F(InnerModelTest, inner_model_add_operation_002, TestSize.Level1) EXPECT_NE(nullptr, liteGraph); SetLiteGraph(liteGraph); - Buffer buffer; - std::string modelName; - std::string isProfiling; - std::string opLayout; - std::map opLayouts; + ExtensionConfig extensionConfig; - EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.BuildFromLiteGraph(liteGraph, buffer, modelName, isProfiling, opLayouts)); + EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.BuildFromLiteGraph(liteGraph, extensionConfig)); EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, m_innerModelTest.AddOperation(m_opType, m_params, m_inputs, m_outputs)); @@ -705,13 +663,9 @@ HWTEST_F(InnerModelTest, inner_model_specify_inputs_and_outputs_002, TestSize.Le EXPECT_NE(nullptr, liteGraph); SetLiteGraph(liteGraph); - Buffer buffer; - std::string modelName; - std::string isProfiling; - std::string opLayout; - std::map opLayouts; + ExtensionConfig extensionConfig; - EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.BuildFromLiteGraph(liteGraph, buffer, modelName, isProfiling, opLayouts)); + EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.BuildFromLiteGraph(liteGraph, extensionConfig)); EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, m_innerModelTest.SpecifyInputsAndOutputs(inputs, outputs)); } @@ -876,13 +830,9 @@ HWTEST_F(InnerModelTest, inner_model_get_supported_operation_002, TestSize.Level EXPECT_NE(nullptr, liteGraph); SetLiteGraph(liteGraph); - Buffer buffer; - std::string modelName; - std::string isProfiling; - std::string opLayout; - std::map opLayouts; + ExtensionConfig extensionConfig; - EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.BuildFromLiteGraph(liteGraph, buffer, modelName, isProfiling, opLayouts)); + EXPECT_EQ(OH_NN_SUCCESS, m_innerModelTest.BuildFromLiteGraph(liteGraph, extensionConfig)); EXPECT_EQ(OH_NN_FAILED, m_innerModelTest.GetSupportedOperations(deviceID, &isSupported, opCount)); } diff --git a/test/unittest/components/v2_0/neural_network_runtime_test/neural_network_runtime_test.cpp b/test/unittest/components/v2_0/neural_network_runtime_test/neural_network_runtime_test.cpp index 920b00e3d9e80efd55f19fea2d09abebf300b984..b5b9f63f06fbcf2d2c5a80e3de64ff494dc65898 100644 --- a/test/unittest/components/v2_0/neural_network_runtime_test/neural_network_runtime_test.cpp +++ b/test/unittest/components/v2_0/neural_network_runtime_test/neural_network_runtime_test.cpp @@ -25,7 +25,7 @@ namespace OHOS { namespace NeuralNetworkRuntime { OH_NN_ReturnCode HDIDeviceV2_0::PrepareModel(std::shared_ptr model, - const Buffer& quantBuffer, const ModelConfig& config, std::shared_ptr& preparedModel) + const ModelConfig& config, std::shared_ptr& preparedModel) { if (model == nullptr) { return OH_NN_INVALID_PARAMETER;