• 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 #ifndef MOCK_IDEVICE_H
16 #define MOCK_IDEVICE_H
17 
18 #include <iostream>
19 #include <hdi_support.h>
20 #include <string_ex.h>
21 #include <hdf_base.h>
22 #include <unordered_map>
23 
24 #include <gtest/gtest.h>
25 #include <gmock/gmock.h>
26 #include "mindir_lite_graph.h"
27 #include "mindir.h"
28 
29 #include "securec.h"
30 #include "refbase.h"
31 #include "common/log.h"
32 #include "ashmem.h"
33 
34 #include <v2_0/nnrt_types.h>
35 #include <v2_0/innrt_device.h>
36 #include <v2_0/iprepared_model.h>
37 
38 namespace OHOS {
39 namespace HDI {
40 namespace Nnrt {
41 namespace V2_0 {
42 
43 class MockIDevice : public INnrtDevice {
44 public:
45     int32_t GetSupportedOperation(const Model& model, std::vector<bool>& ops) override;
46 
47     int32_t IsFloat16PrecisionSupported(bool& isSupported) override;
48 
49     int32_t IsPerformanceModeSupported(bool& isSupported) override;
50 
51     int32_t IsPrioritySupported(bool& isSupported) override;
52 
53     int32_t IsDynamicInputSupported(bool& isSupported) override;
54 
55     int32_t IsModelCacheSupported(bool& isSupported) override;
56 
57     int32_t AllocateBuffer(uint32_t length, SharedBuffer &buffer) override;
58 
59     int32_t ReleaseBuffer(const SharedBuffer &buffer) override;
60 
61     int32_t GetDeviceName(std::string& name) override;
62 
63     int32_t GetVendorName(std::string& name) override;
64 
65     int32_t GetDeviceType(DeviceType& deviceType) override;
66 
67     int32_t GetDeviceStatus(DeviceStatus& status) override;
68 
69     int32_t GetVersion(uint32_t &majorVersion, uint32_t &minorVersion) override;
70 
71     int32_t PrepareModel(const Model& model, const ModelConfig& config, sptr<IPreparedModel>& preparedModel) override;
72 
73     int32_t PrepareOfflineModel(const std::vector<SharedBuffer>& offlineModels, const ModelConfig& config,
74         sptr<OHOS::HDI::Nnrt::V2_0::IPreparedModel>& preparedModel) override;
75 
76     int32_t PrepareModelFromModelCache(const std::vector<SharedBuffer>& modelCache, const ModelConfig& config,
77          sptr<IPreparedModel>& preparedModel) override;
78 
79     int32_t MemoryCopy(float* data, uint32_t length);
80 
81     void SetFP16Supported(bool isSupported);
82 
83     void SetPerformanceSupported(bool isSupported);
84 
85     void SetPrioritySupported(bool isSupported);
86 
87     void SetModelCacheSupported(bool isSupported);
88 
89     void SetOperationsSupported(std::vector<bool> isSupported);
90 
91     void SetDynamicInputSupported(bool isSupported);
92 
93     static MockIDevice *GetInstance();
94 
95     MockIDevice();
96     virtual ~MockIDevice();
97 
98 private:
99     std::unordered_map<int, sptr<Ashmem>> m_ashmems;
100     int m_bufferFd;
101     bool m_fp16 = true;
102     bool m_performance = true;
103     bool m_priority = true;
104     bool m_cache = true;
105     bool m_dynamic = true;
106     std::vector<bool> m_operations{true};
107     std::mutex m_mtx;
108 };
109 
110 class MockIPreparedModel : public IPreparedModel {
111 public:
112     int32_t ExportModelCache(std::vector<SharedBuffer>& modelCache) override;
113     int32_t Run(const std::vector<IOTensor>& inputs, const std::vector<IOTensor>& outputs,
114     std::vector<std::vector<int32_t>>& outputsDims) override;
115     int32_t GetInputDimRanges(std::vector<std::vector<uint32_t>>& minInputDims,
116                               std::vector<std::vector<uint32_t>>& maxInputDims) override;
117     int32_t GetVersion(uint32_t &majorVersion, uint32_t &minorVersion) override;
118     MockIPreparedModel() = default;
119     virtual ~MockIPreparedModel();
120 private:
121     std::unordered_map<int, sptr<Ashmem>> m_ashmems;
122 };
123 
124 } // namespace V2_0
125 } // namespace Nnrt
126 } // namespace HDI
127 } // namespace OHOS
128 #endif // MOCK_IDEVICE_H
129