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