• 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_set>
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 "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     void SetFP16Supported(bool isSupported);
80 
81     void SetPerformanceSupported(bool isSupported);
82 
83     void SetPrioritySupported(bool isSupported);
84 
85     void SetModelCacheSupported(bool isSupported);
86 
87     void SetOperationsSupported(std::vector<bool> isSupported);
88 
89     void SetDynamicInputSupported(bool isSupported);
90 
91     static MockIDevice *GetInstance();
92 
93     MockIDevice();
94     virtual ~MockIDevice();
95 
96 private:
97     std::unordered_set<int> m_fds;
98     int m_bufferFd;
99     bool m_fp16 = true;
100     bool m_performance = true;
101     bool m_priority = true;
102     bool m_cache = true;
103     bool m_dynamic = true;
104     std::vector<bool> m_operations{true};
105     std::mutex m_mtx;
106 };
107 
108 class MockIPreparedModel : public IPreparedModel {
109 public:
110     int32_t ExportModelCache(std::vector<SharedBuffer>& modelCache) override;
111     int32_t Run(const std::vector<IOTensor>& inputs, const std::vector<IOTensor>& outputs,
112     std::vector<std::vector<int32_t>>& outputsDims) override;
113     int32_t GetInputDimRanges(std::vector<std::vector<uint32_t>>& minInputDims,
114                               std::vector<std::vector<uint32_t>>& maxInputDims) override;
115     int32_t GetVersion(uint32_t &majorVersion, uint32_t &minorVersion) override;
116     MockIPreparedModel() = default;
117     virtual ~MockIPreparedModel();
118 private:
119     std::unordered_set<int> m_fds;
120 };
121 
122 } // namespace V2_0
123 } // namespace Nnrt
124 } // namespace HDI
125 } // namespace OHOS
126 #endif // MOCK_IDEVICE_H
127