• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "hdi_prepared_model.h"
17 
18 #include "common/log.h"
19 #include "memory_manager.h"
20 #include "transform.h"
21 
22 namespace OHOS {
23 namespace NeuralNetworkRuntime {
HDIPreparedModel(OHOS::sptr<V1_0::IPreparedModel> hdiPreparedModel)24 HDIPreparedModel::HDIPreparedModel(OHOS::sptr<V1_0::IPreparedModel> hdiPreparedModel)
25     : m_hdiPreparedModel(hdiPreparedModel)
26 {
27     hdiPreparedModel->GetVersion(m_hdiVersion.first, m_hdiVersion.second);
28 }
29 
ExportModelCache(std::vector<ModelBuffer> & modelCache)30 OH_NN_ReturnCode HDIPreparedModel::ExportModelCache(std::vector<ModelBuffer>& modelCache)
31 {
32     if (!modelCache.empty()) {
33         LOGE("The vector of modelCache should be empty. size=%zu", modelCache.size());
34         return OH_NN_INVALID_PARAMETER;
35     }
36 
37     std::vector<V1_0::SharedBuffer> iBuffers;
38     auto ret = m_hdiPreparedModel->ExportModelCache(iBuffers);
39     if (ret != HDF_SUCCESS) {
40         LOGE("Export model cache failed. ErrorCode=%d", ret);
41         return OH_NN_UNAVALIDABLE_DEVICE;
42     }
43 
44     auto memManager = MemoryManager::GetInstance();
45     for (size_t i = 0; i < iBuffers.size(); i++) {
46         auto addr = memManager->MapMemory(iBuffers[i].fd, iBuffers[i].bufferSize);
47         if (addr == nullptr) {
48             LOGE("Export the %zuth model cache failed, cannot not map fd to address.", i + 1);
49             return OH_NN_MEMORY_ERROR;
50         }
51         ModelBuffer modelbuffer {addr, iBuffers[i].bufferSize};
52         modelCache.emplace_back(modelbuffer);
53     }
54 
55     return OH_NN_SUCCESS;
56 }
57 
Run(const std::vector<IOTensor> & inputs,const std::vector<IOTensor> & outputs,std::vector<std::vector<int32_t>> & outputsDims,std::vector<bool> & isOutputBufferEnough)58 OH_NN_ReturnCode HDIPreparedModel::Run(const std::vector<IOTensor>& inputs, const std::vector<IOTensor>& outputs,
59     std::vector<std::vector<int32_t>>& outputsDims, std::vector<bool>& isOutputBufferEnough)
60 {
61     V1_0::IOTensor iTensor;
62     std::vector<V1_0::IOTensor> iInputTensors;
63     for (auto& input: inputs) {
64         iTensor = NNToHDI::TransIOTensor(input);
65         if (iTensor.data.fd == INVALID_FD) {
66             LOGE("Transform inputs tensor failed, cannot find data file descriptor.");
67             return OH_NN_INVALID_PARAMETER;
68         }
69         iInputTensors.emplace_back(iTensor);
70     }
71 
72     std::vector<V1_0::IOTensor> iOutputTensors;
73     for (auto& output: outputs) {
74         iTensor = NNToHDI::TransIOTensor(output);
75         if (iTensor.data.fd == INVALID_FD) {
76             LOGE("Transform outputs tensor failed, cannot find data file descriptor.");
77             return OH_NN_INVALID_PARAMETER;
78         }
79         iOutputTensors.emplace_back(iTensor);
80     }
81 
82     auto ret = m_hdiPreparedModel->Run(iInputTensors, iOutputTensors, outputsDims, isOutputBufferEnough);
83     if (ret != HDF_SUCCESS || outputsDims.empty()) {
84         LOGE("Run model failed. ErrorCode=%d", ret);
85         return OH_NN_UNAVALIDABLE_DEVICE;
86     }
87 
88     return OH_NN_SUCCESS;
89 }
90 } // namespace NeuralNetworkRuntime
91 } // OHOS