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_v2_0.h"
18 #include "test/unittest/common/v2_0/mock_idevice.h"
19
20 OH_NN_ReturnCode OHOS::HDI::Nnrt::V2_0::MockIPreparedModel::m_ExpectRetCode = OH_NN_OPERATION_FORBIDDEN;
21
22 namespace OHOS {
23 namespace NeuralNetworkRuntime {
24
AllocateBuffer(size_t length)25 void* HDIDeviceV2_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::V2_0::MockIPreparedModel::m_ExpectRetCode == OH_NN_INVALID_PARAMETER) {
39 OHOS::HDI::Nnrt::V2_0::MockIPreparedModel::m_ExpectRetCode = OH_NN_OPERATION_FORBIDDEN;
40 return nullptr;
41 }
42 return buffer;
43 }
44
ReleaseBuffer(const void * buffer)45 OH_NN_ReturnCode HDIDeviceV2_0::ReleaseBuffer(const void* buffer)
46 {
47 if (buffer == nullptr) {
48 LOGE("alloct buffer failed");
49 return OH_NN_FAILED;
50 }
51 free(const_cast<void *>(buffer));
52 buffer = nullptr;
53 return OH_NN_SUCCESS;
54 }
55
GetInputDimRanges(std::vector<std::vector<uint32_t>> & minInputDims,std::vector<std::vector<uint32_t>> & maxInputDims)56 OH_NN_ReturnCode HDIPreparedModelV2_0::GetInputDimRanges(std::vector<std::vector<uint32_t>>& minInputDims,
57 std::vector<std::vector<uint32_t>>& maxInputDims)
58 {
59 minInputDims = {{0, 0}, {0, 0}};
60 maxInputDims = {{100, 100}, {100, 100}};
61
62 return OH_NN_SUCCESS;
63 }
64
Run(const std::vector<IOTensor> & inputs,const std::vector<IOTensor> & outputs,std::vector<std::vector<int32_t>> & outputsDims,std::vector<bool> & isOutputBufferEnough)65 OH_NN_ReturnCode HDIPreparedModelV2_0::Run(const std::vector<IOTensor>& inputs, const std::vector<IOTensor>& outputs,
66 std::vector<std::vector<int32_t>>& outputsDims, std::vector<bool>& isOutputBufferEnough)
67 {
68 if (inputs.empty() || outputs.empty()) {
69 return OH_NN_INVALID_PARAMETER;
70 }
71
72 if (OHOS::HDI::Nnrt::V2_0::MockIPreparedModel::m_ExpectRetCode == OH_NN_FAILED) {
73 OHOS::HDI::Nnrt::V2_0::MockIPreparedModel::m_ExpectRetCode = OH_NN_OPERATION_FORBIDDEN;
74 return OH_NN_INVALID_PARAMETER;
75 }
76
77 isOutputBufferEnough.emplace_back(true);
78 outputsDims.emplace_back(outputs[0].dimensions);
79
80 return OH_NN_SUCCESS;
81 }
82 } // namespace NeuralNetworkRuntime
83 } // namespace OHOS