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 "compilation.h"
17 #include "hdi_device_v1_0.h"
18 #include "test/unittest/common/v1_0/mock_idevice.h"
19
20 OH_NN_ReturnCode OHOS::HDI::Nnrt::V1_0::MockIPreparedModel::m_ExpectRetCode = OH_NN_OPERATION_FORBIDDEN;
21
22 namespace OHOS {
23 namespace NeuralNetworkRuntime {
24
AllocateBuffer(size_t length)25 void* HDIDeviceV1_0::AllocateBuffer(size_t length)
26 {
27 if (length == 0) {
28 LOGE("The length param is invalid, length=0");
29 return nullptr;
30 }
31
32 void* buffer = malloc(length);
33 if (buffer == nullptr) {
34 LOGE("alloct buffer failed");
35 return nullptr;
36 }
37
38 if (OHOS::HDI::Nnrt::V1_0::MockIPreparedModel::m_ExpectRetCode == OH_NN_INVALID_PARAMETER) {
39 OHOS::HDI::Nnrt::V1_0::MockIPreparedModel::m_ExpectRetCode = OH_NN_OPERATION_FORBIDDEN;
40 free(buffer);
41 return nullptr;
42 }
43 return buffer;
44 }
45
ReleaseBuffer(const void * buffer)46 OH_NN_ReturnCode HDIDeviceV1_0::ReleaseBuffer(const void* buffer)
47 {
48 if (buffer == nullptr) {
49 LOGE("alloct buffer failed");
50 return OH_NN_FAILED;
51 }
52 free(const_cast<void *>(buffer));
53 buffer = nullptr;
54 return OH_NN_SUCCESS;
55 }
56
Run(const std::vector<IOTensor> & inputs,const std::vector<IOTensor> & outputs,std::vector<std::vector<int32_t>> & outputsDims,std::vector<bool> & isOutputBufferEnough)57 OH_NN_ReturnCode HDIPreparedModelV1_0::Run(const std::vector<IOTensor>& inputs, const std::vector<IOTensor>& outputs,
58 std::vector<std::vector<int32_t>>& outputsDims, std::vector<bool>& isOutputBufferEnough)
59 {
60 if (inputs.empty() || outputs.empty()) {
61 return OH_NN_INVALID_PARAMETER;
62 }
63
64 if (OHOS::HDI::Nnrt::V1_0::MockIPreparedModel::m_ExpectRetCode == OH_NN_FAILED) {
65 OHOS::HDI::Nnrt::V1_0::MockIPreparedModel::m_ExpectRetCode = OH_NN_OPERATION_FORBIDDEN;
66 return OH_NN_INVALID_PARAMETER;
67 }
68
69 isOutputBufferEnough.emplace_back(true);
70 outputsDims.emplace_back(outputs[0].dimensions);
71
72 return OH_NN_SUCCESS;
73 }
74 } // namespace NeuralNetworkRuntime
75 } // namespace OHOS