Lev Proleev | c185e88 | 2020-12-15 19:25:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ANDROID_HARDWARE_NEURALNETWORKS_AIDL_UTILS_H |
| 18 | #define ANDROID_HARDWARE_NEURALNETWORKS_AIDL_UTILS_H |
| 19 | |
| 20 | #include <android-base/logging.h> |
| 21 | #include <android/hardware_buffer.h> |
| 22 | #include <gtest/gtest.h> |
| 23 | |
| 24 | #include <algorithm> |
| 25 | #include <iosfwd> |
| 26 | #include <string> |
| 27 | #include <utility> |
| 28 | #include <vector> |
| 29 | |
| 30 | #include <aidl/android/hardware/neuralnetworks/IDevice.h> |
| 31 | #include <aidl/android/hardware/neuralnetworks/Memory.h> |
| 32 | #include <aidl/android/hardware/neuralnetworks/Operand.h> |
| 33 | #include <aidl/android/hardware/neuralnetworks/OperandType.h> |
| 34 | #include <aidl/android/hardware/neuralnetworks/Priority.h> |
| 35 | #include <aidl/android/hardware/neuralnetworks/Request.h> |
| 36 | |
| 37 | #include <TestHarness.h> |
| 38 | #include <nnapi/SharedMemory.h> |
| 39 | |
| 40 | namespace aidl::android::hardware::neuralnetworks { |
| 41 | |
| 42 | namespace nn = ::android::nn; |
| 43 | |
| 44 | inline constexpr Priority kDefaultPriority = Priority::MEDIUM; |
| 45 | |
| 46 | inline constexpr Timing kNoTiming = {.timeOnDevice = -1, .timeInDriver = -1}; |
| 47 | inline constexpr int64_t kNoDeadline = -1; |
| 48 | inline constexpr int64_t kOmittedTimeoutDuration = -1; |
| 49 | inline constexpr int64_t kNoDuration = -1; |
| 50 | inline const std::vector<uint8_t> kEmptyCacheToken(IDevice::BYTE_SIZE_OF_CACHE_TOKEN); |
| 51 | |
| 52 | // Returns the amount of space needed to store a value of the specified type. |
| 53 | // |
| 54 | // Aborts if the specified type is an extension type or OEM type. |
| 55 | uint32_t sizeOfData(OperandType type); |
| 56 | |
| 57 | // Returns the amount of space needed to store a value of the dimensions and |
| 58 | // type of this operand. For a non-extension, non-OEM tensor with unspecified |
| 59 | // rank or at least one unspecified dimension, returns zero. |
| 60 | // |
| 61 | // Aborts if the specified type is an extension type or OEM type. |
| 62 | uint32_t sizeOfData(const Operand& operand); |
| 63 | |
| 64 | // Convenience class to manage the lifetime of memory resources. |
| 65 | class TestMemoryBase { |
| 66 | DISALLOW_COPY_AND_ASSIGN(TestMemoryBase); |
| 67 | |
| 68 | public: |
| 69 | TestMemoryBase() = default; |
| 70 | virtual ~TestMemoryBase() = default; |
| 71 | uint8_t* getPointer() const { return mPtr; } |
| 72 | const Memory* getAidlMemory() const { return &mAidlMemory; } |
| 73 | |
| 74 | protected: |
| 75 | uint8_t* mPtr = nullptr; |
| 76 | Memory mAidlMemory; |
| 77 | bool mIsValid = false; |
| 78 | }; |
| 79 | |
| 80 | class TestAshmem : public TestMemoryBase { |
| 81 | public: |
| 82 | static std::unique_ptr<TestAshmem> create(uint32_t size); |
| 83 | |
| 84 | // Prefer TestAshmem::create. |
| 85 | // The constructor calls initialize, which constructs the memory resources. This is a workaround |
| 86 | // that gtest macros cannot be used directly in a constructor. |
| 87 | TestAshmem(uint32_t size) { initialize(size); } |
| 88 | |
| 89 | private: |
| 90 | void initialize(uint32_t size); |
| 91 | nn::Mapping mMappedMemory; |
| 92 | }; |
| 93 | |
| 94 | class TestBlobAHWB : public TestMemoryBase { |
| 95 | public: |
| 96 | static std::unique_ptr<TestBlobAHWB> create(uint32_t size); |
| 97 | |
| 98 | // Prefer TestBlobAHWB::create. |
| 99 | // The constructor calls initialize, which constructs the memory resources. This is a |
| 100 | // workaround that gtest macros cannot be used directly in a constructor. |
| 101 | TestBlobAHWB(uint32_t size) { initialize(size); } |
| 102 | ~TestBlobAHWB(); |
| 103 | |
| 104 | private: |
| 105 | void initialize(uint32_t size); |
| 106 | AHardwareBuffer* mAhwb = nullptr; |
| 107 | nn::Mapping mMapping; |
| 108 | }; |
| 109 | |
| 110 | enum class MemoryType { ASHMEM, BLOB_AHWB, DEVICE }; |
| 111 | |
| 112 | // Manages the lifetime of memory resources used in an execution. |
| 113 | class ExecutionContext { |
| 114 | DISALLOW_COPY_AND_ASSIGN(ExecutionContext); |
| 115 | |
| 116 | public: |
| 117 | static constexpr uint32_t kInputPoolIndex = 0; |
| 118 | static constexpr uint32_t kOutputPoolIndex = 1; |
| 119 | |
| 120 | ExecutionContext() = default; |
| 121 | |
| 122 | // Create HIDL Request from the TestModel struct. |
| 123 | Request createRequest(const test_helper::TestModel& testModel, |
| 124 | MemoryType memoryType = MemoryType::ASHMEM); |
| 125 | |
| 126 | // After execution, copy out output results from the output memory pool. |
| 127 | std::vector<test_helper::TestBuffer> getOutputBuffers(const Request& request) const; |
| 128 | |
| 129 | private: |
| 130 | std::unique_ptr<TestMemoryBase> mInputMemory, mOutputMemory; |
| 131 | }; |
| 132 | |
| 133 | template <typename Type> |
| 134 | using Named = std::pair<std::string, Type>; |
| 135 | |
| 136 | template <typename Type> |
| 137 | const std::string& getName(const Named<Type>& namedData) { |
| 138 | return namedData.first; |
| 139 | } |
| 140 | |
| 141 | template <typename Type> |
| 142 | const Type& getData(const Named<Type>& namedData) { |
| 143 | return namedData.second; |
| 144 | } |
| 145 | |
| 146 | std::string gtestCompliantName(std::string name); |
| 147 | |
| 148 | // pretty-print values for error messages |
| 149 | ::std::ostream& operator<<(::std::ostream& os, ErrorStatus errorStatus); |
| 150 | |
| 151 | } // namespace aidl::android::hardware::neuralnetworks |
| 152 | |
| 153 | #endif // ANDROID_HARDWARE_NEURALNETWORKS_AIDL_UTILS_H |