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 <gmock/gmock.h>
17 #include <gtest/gtest.h>
18
19 #include "common/utils.h"
20 #include "frameworks/native/inner_model.h"
21 #include "frameworks/native/hdi_device.h"
22 #include "frameworks/native/device_manager.h"
23 #include "frameworks/native/ops/div_builder.h"
24 #include "mock_idevice.h"
25
26 namespace OHOS {
27 namespace NeuralNetworkRuntime {
28 // Mock the palce where the devicemanager GetDevice is called in inner_model build function.
GetDevice(size_t deviceId) const29 std::shared_ptr<Device> DeviceManager::GetDevice(size_t deviceId) const
30 {
31 sptr<OHOS::HDI::Nnrt::V1_0::INnrtDevice> idevice =
32 sptr<OHOS::HDI::Nnrt::V1_0::MockIDevice>(new (std::nothrow) OHOS::HDI::Nnrt::V1_0::MockIDevice());
33
34 if (idevice == nullptr) {
35 LOGE("DeviceManager mock GetDevice failed, error happened when new sptr");
36 return nullptr;
37 } else {
38 std::shared_ptr<Device> device = CreateSharedPtr<HDIDevice>(idevice);
39 if (device == nullptr) {
40 LOGE("DeviceManager mock GetDevice failed, device is nullptr");
41 return nullptr;
42 }
43
44 if (deviceId == 0) {
45 return nullptr;
46 } else {
47 return device;
48 }
49 }
50 }
51
52 // Mock the palce where the operator GetPrimitive is called in inner_model build function.
GetPrimitive()53 Ops::LiteGraphPrimitvePtr Ops::DivBuilder::GetPrimitive()
54 {
55 Ops::LiteGraphPrimitvePtr primitive = {nullptr, DestroyLiteGraphTensor};
56 return primitive;
57 }
58
59 // Mock the palce where the device GetSupportedOperation is called in inner_model build function.
GetSupportedOperation(std::shared_ptr<const mindspore::lite::LiteGraph> model,std::vector<bool> & supportedOperations)60 OH_NN_ReturnCode HDIDevice::GetSupportedOperation(std::shared_ptr<const mindspore::lite::LiteGraph> model,
61 std::vector<bool>& supportedOperations)
62 {
63 supportedOperations = {true, true, true};
64
65 if (model->name_ == "Loaded_NNR_Model") {
66 return OH_NN_UNAVALIDABLE_DEVICE;
67 } else {
68 return OH_NN_SUCCESS;
69 }
70 }
71 } // NeuralNetworkRuntime
72 } // OHOS
73