surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | 93e4898 | 2018-09-05 13:05:09 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 4 | // |
| 5 | #pragma once |
| 6 | |
| 7 | #ifndef LOG_TAG |
| 8 | #define LOG_TAG "ArmnnDriverTests" |
| 9 | #endif // LOG_TAG |
| 10 | |
| 11 | #include "../ArmnnDriver.hpp" |
| 12 | #include <iosfwd> |
Nikhil Raj | 7760582 | 2018-09-03 11:25:56 +0100 | [diff] [blame] | 13 | #include <boost/test/unit_test.hpp> |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 14 | |
| 15 | namespace android |
| 16 | { |
| 17 | namespace hardware |
| 18 | { |
| 19 | namespace neuralnetworks |
| 20 | { |
| 21 | namespace V1_0 |
| 22 | { |
| 23 | |
| 24 | std::ostream& operator<<(std::ostream& os, ErrorStatus stat); |
| 25 | |
| 26 | } // namespace android::hardware::neuralnetworks::V1_0 |
| 27 | } // namespace android::hardware::neuralnetworks |
| 28 | } // namespace android::hardware |
| 29 | } // namespace android |
| 30 | |
| 31 | namespace driverTestHelpers |
| 32 | { |
| 33 | |
Matteo Martincigh | 8b287c2 | 2018-09-07 09:25:10 +0100 | [diff] [blame] | 34 | std::ostream& operator<<(std::ostream& os, V1_0::ErrorStatus stat); |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 35 | |
| 36 | struct ExecutionCallback : public IExecutionCallback |
| 37 | { |
| 38 | ExecutionCallback() : mNotified(false) {} |
| 39 | Return<void> notify(ErrorStatus status) override; |
| 40 | /// wait until the callback has notified us that it is done |
| 41 | Return<void> wait(); |
| 42 | |
| 43 | private: |
| 44 | // use a mutex and a condition variable to wait for asynchronous callbacks |
| 45 | std::mutex mMutex; |
| 46 | std::condition_variable mCondition; |
| 47 | // and a flag, in case we are notified before the wait call |
| 48 | bool mNotified; |
| 49 | }; |
| 50 | |
| 51 | class PreparedModelCallback : public IPreparedModelCallback |
| 52 | { |
| 53 | public: |
| 54 | PreparedModelCallback() |
| 55 | : m_ErrorStatus(ErrorStatus::NONE) |
| 56 | , m_PreparedModel() |
| 57 | { } |
| 58 | ~PreparedModelCallback() override { } |
| 59 | |
| 60 | Return<void> notify(ErrorStatus status, |
| 61 | const android::sp<IPreparedModel>& preparedModel) override; |
| 62 | ErrorStatus GetErrorStatus() { return m_ErrorStatus; } |
| 63 | android::sp<IPreparedModel> GetPreparedModel() { return m_PreparedModel; } |
| 64 | |
| 65 | private: |
| 66 | ErrorStatus m_ErrorStatus; |
| 67 | android::sp<IPreparedModel> m_PreparedModel; |
| 68 | }; |
| 69 | |
| 70 | hidl_memory allocateSharedMemory(int64_t size); |
| 71 | |
| 72 | android::sp<IMemory> AddPoolAndGetData(uint32_t size, Request& request); |
| 73 | |
| 74 | void AddPoolAndSetData(uint32_t size, Request& request, const float* data); |
| 75 | |
Nikhil Raj | 7760582 | 2018-09-03 11:25:56 +0100 | [diff] [blame] | 76 | template<typename HalModel> |
| 77 | void AddOperand(HalModel& model, const Operand& op) |
| 78 | { |
| 79 | model.operands.resize(model.operands.size() + 1); |
| 80 | model.operands[model.operands.size() - 1] = op; |
| 81 | } |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 82 | |
Nikhil Raj | 7760582 | 2018-09-03 11:25:56 +0100 | [diff] [blame] | 83 | template<typename HalModel> |
| 84 | void AddIntOperand(HalModel& model, int32_t value) |
| 85 | { |
| 86 | DataLocation location = {}; |
| 87 | location.offset = model.operandValues.size(); |
| 88 | location.length = sizeof(int32_t); |
| 89 | |
| 90 | Operand op = {}; |
| 91 | op.type = OperandType::INT32; |
| 92 | op.dimensions = hidl_vec<uint32_t>{}; |
| 93 | op.lifetime = OperandLifeTime::CONSTANT_COPY; |
| 94 | op.location = location; |
| 95 | |
| 96 | model.operandValues.resize(model.operandValues.size() + location.length); |
| 97 | *reinterpret_cast<int32_t*>(&model.operandValues[location.offset]) = value; |
| 98 | |
| 99 | AddOperand<HalModel>(model, op); |
| 100 | } |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 101 | |
| 102 | template<typename T> |
| 103 | OperandType TypeToOperandType(); |
| 104 | |
| 105 | template<> |
| 106 | OperandType TypeToOperandType<float>(); |
| 107 | |
| 108 | template<> |
| 109 | OperandType TypeToOperandType<int32_t>(); |
| 110 | |
Nikhil Raj | 7760582 | 2018-09-03 11:25:56 +0100 | [diff] [blame] | 111 | template<typename HalModel, typename T> |
| 112 | void AddTensorOperand(HalModel& model, |
Matteo Martincigh | c743412 | 2018-11-14 12:27:04 +0000 | [diff] [blame] | 113 | const hidl_vec<uint32_t>& dimensions, |
| 114 | const T* values, |
Kevin May | f29a2c5 | 2019-03-14 11:56:32 +0000 | [diff] [blame] | 115 | OperandType operandType = OperandType::TENSOR_FLOAT32, |
| 116 | OperandLifeTime operandLifeTime = OperandLifeTime::CONSTANT_COPY) |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 117 | { |
| 118 | uint32_t totalElements = 1; |
| 119 | for (uint32_t dim : dimensions) |
| 120 | { |
| 121 | totalElements *= dim; |
| 122 | } |
| 123 | |
| 124 | DataLocation location = {}; |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 125 | location.length = totalElements * sizeof(T); |
| 126 | |
Kevin May | f29a2c5 | 2019-03-14 11:56:32 +0000 | [diff] [blame] | 127 | if(operandLifeTime == OperandLifeTime::CONSTANT_COPY) |
| 128 | { |
| 129 | location.offset = model.operandValues.size(); |
| 130 | } |
| 131 | |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 132 | Operand op = {}; |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 133 | op.type = operandType; |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 134 | op.dimensions = dimensions; |
| 135 | op.lifetime = OperandLifeTime::CONSTANT_COPY; |
| 136 | op.location = location; |
| 137 | |
| 138 | model.operandValues.resize(model.operandValues.size() + location.length); |
| 139 | for (uint32_t i = 0; i < totalElements; i++) |
| 140 | { |
| 141 | *(reinterpret_cast<T*>(&model.operandValues[location.offset]) + i) = values[i]; |
| 142 | } |
| 143 | |
Nikhil Raj | 7760582 | 2018-09-03 11:25:56 +0100 | [diff] [blame] | 144 | AddOperand<HalModel>(model, op); |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 145 | } |
| 146 | |
Matteo Martincigh | c743412 | 2018-11-14 12:27:04 +0000 | [diff] [blame] | 147 | template<typename HalModel, typename T> |
| 148 | void AddTensorOperand(HalModel& model, |
| 149 | const hidl_vec<uint32_t>& dimensions, |
| 150 | const std::vector<T>& values, |
Kevin May | f29a2c5 | 2019-03-14 11:56:32 +0000 | [diff] [blame] | 151 | OperandType operandType = OperandType::TENSOR_FLOAT32, |
| 152 | OperandLifeTime operandLifeTime = OperandLifeTime::CONSTANT_COPY) |
Matteo Martincigh | c743412 | 2018-11-14 12:27:04 +0000 | [diff] [blame] | 153 | { |
Kevin May | f29a2c5 | 2019-03-14 11:56:32 +0000 | [diff] [blame] | 154 | AddTensorOperand<HalModel, T>(model, dimensions, values.data(), operandType, operandLifeTime); |
Matteo Martincigh | c743412 | 2018-11-14 12:27:04 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Nikhil Raj | 7760582 | 2018-09-03 11:25:56 +0100 | [diff] [blame] | 157 | template<typename HalModel> |
| 158 | void AddInputOperand(HalModel& model, |
Matteo Martincigh | c743412 | 2018-11-14 12:27:04 +0000 | [diff] [blame] | 159 | const hidl_vec<uint32_t>& dimensions, |
Nikhil Raj | 7760582 | 2018-09-03 11:25:56 +0100 | [diff] [blame] | 160 | OperandType operandType = OperandType::TENSOR_FLOAT32) |
| 161 | { |
| 162 | Operand op = {}; |
| 163 | op.type = operandType; |
Matteo Martincigh | 8d50f8f | 2018-10-25 15:39:33 +0100 | [diff] [blame] | 164 | op.scale = operandType == OperandType::TENSOR_QUANT8_ASYMM ? 1.f / 255.f : 0.f; |
Nikhil Raj | 7760582 | 2018-09-03 11:25:56 +0100 | [diff] [blame] | 165 | op.dimensions = dimensions; |
| 166 | op.lifetime = OperandLifeTime::MODEL_INPUT; |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 167 | |
Nikhil Raj | 7760582 | 2018-09-03 11:25:56 +0100 | [diff] [blame] | 168 | AddOperand<HalModel>(model, op); |
| 169 | |
| 170 | model.inputIndexes.resize(model.inputIndexes.size() + 1); |
| 171 | model.inputIndexes[model.inputIndexes.size() - 1] = model.operands.size() - 1; |
| 172 | } |
| 173 | |
| 174 | template<typename HalModel> |
| 175 | void AddOutputOperand(HalModel& model, |
Matteo Martincigh | c743412 | 2018-11-14 12:27:04 +0000 | [diff] [blame] | 176 | const hidl_vec<uint32_t>& dimensions, |
Nikhil Raj | 7760582 | 2018-09-03 11:25:56 +0100 | [diff] [blame] | 177 | OperandType operandType = OperandType::TENSOR_FLOAT32) |
| 178 | { |
| 179 | Operand op = {}; |
| 180 | op.type = operandType; |
| 181 | op.scale = operandType == OperandType::TENSOR_QUANT8_ASYMM ? 1.f / 255.f : 0.f; |
| 182 | op.dimensions = dimensions; |
| 183 | op.lifetime = OperandLifeTime::MODEL_OUTPUT; |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 184 | |
Nikhil Raj | 7760582 | 2018-09-03 11:25:56 +0100 | [diff] [blame] | 185 | AddOperand<HalModel>(model, op); |
| 186 | |
| 187 | model.outputIndexes.resize(model.outputIndexes.size() + 1); |
| 188 | model.outputIndexes[model.outputIndexes.size() - 1] = model.operands.size() - 1; |
| 189 | } |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 190 | |
Matteo Martincigh | 8b287c2 | 2018-09-07 09:25:10 +0100 | [diff] [blame] | 191 | android::sp<IPreparedModel> PrepareModelWithStatus(const V1_0::Model& model, |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 192 | armnn_driver::ArmnnDriver& driver, |
Nikhil Raj | 7760582 | 2018-09-03 11:25:56 +0100 | [diff] [blame] | 193 | ErrorStatus& prepareStatus, |
| 194 | ErrorStatus expectedStatus = ErrorStatus::NONE); |
| 195 | |
Matteo Martincigh | 8b287c2 | 2018-09-07 09:25:10 +0100 | [diff] [blame] | 196 | #ifdef ARMNN_ANDROID_NN_V1_1 |
Nikhil Raj | 7760582 | 2018-09-03 11:25:56 +0100 | [diff] [blame] | 197 | |
Matteo Martincigh | 8b287c2 | 2018-09-07 09:25:10 +0100 | [diff] [blame] | 198 | android::sp<IPreparedModel> PrepareModelWithStatus(const V1_1::Model& model, |
Nikhil Raj | 7760582 | 2018-09-03 11:25:56 +0100 | [diff] [blame] | 199 | armnn_driver::ArmnnDriver& driver, |
| 200 | ErrorStatus& prepareStatus, |
| 201 | ErrorStatus expectedStatus = ErrorStatus::NONE); |
| 202 | |
| 203 | #endif |
| 204 | |
| 205 | template<typename HalModel> |
| 206 | android::sp<IPreparedModel> PrepareModel(const HalModel& model, |
| 207 | armnn_driver::ArmnnDriver& driver) |
| 208 | { |
| 209 | ErrorStatus prepareStatus = ErrorStatus::NONE; |
| 210 | return PrepareModelWithStatus(model, driver, prepareStatus); |
| 211 | } |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 212 | |
| 213 | ErrorStatus Execute(android::sp<IPreparedModel> preparedModel, |
| 214 | const Request& request, |
Nikhil Raj | 7760582 | 2018-09-03 11:25:56 +0100 | [diff] [blame] | 215 | ErrorStatus expectedStatus = ErrorStatus::NONE); |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 216 | |
| 217 | android::sp<ExecutionCallback> ExecuteNoWait(android::sp<IPreparedModel> preparedModel, |
| 218 | const Request& request); |
| 219 | |
| 220 | } // namespace driverTestHelpers |