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 | |
Sadik Armagan | e6e54a8 | 2019-05-08 10:18:05 +0100 | [diff] [blame] | 36 | struct ExecutionCallback : public V1_0::IExecutionCallback |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 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 | |
Sadik Armagan | e6e54a8 | 2019-05-08 10:18:05 +0100 | [diff] [blame] | 51 | class PreparedModelCallback : public V1_0::IPreparedModelCallback |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 52 | { |
| 53 | public: |
| 54 | PreparedModelCallback() |
| 55 | : m_ErrorStatus(ErrorStatus::NONE) |
| 56 | , m_PreparedModel() |
| 57 | { } |
| 58 | ~PreparedModelCallback() override { } |
| 59 | |
| 60 | Return<void> notify(ErrorStatus status, |
Sadik Armagan | e6e54a8 | 2019-05-08 10:18:05 +0100 | [diff] [blame] | 61 | const android::sp<V1_0::IPreparedModel>& preparedModel) override; |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 62 | ErrorStatus GetErrorStatus() { return m_ErrorStatus; } |
Sadik Armagan | e6e54a8 | 2019-05-08 10:18:05 +0100 | [diff] [blame] | 63 | android::sp<V1_0::IPreparedModel> GetPreparedModel() { return m_PreparedModel; } |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 64 | |
| 65 | private: |
| 66 | ErrorStatus m_ErrorStatus; |
Sadik Armagan | e6e54a8 | 2019-05-08 10:18:05 +0100 | [diff] [blame] | 67 | android::sp<V1_0::IPreparedModel> m_PreparedModel; |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 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 | |
Aron Virginas-Tar | 44cfd84 | 2019-06-14 15:45:03 +0100 | [diff] [blame] | 76 | template<typename HalPolicy, |
| 77 | typename HalModel = typename HalPolicy::Model, |
| 78 | typename HalOperand = typename HalPolicy::Operand> |
| 79 | void AddOperand(HalModel& model, const HalOperand& op) |
Nikhil Raj | 7760582 | 2018-09-03 11:25:56 +0100 | [diff] [blame] | 80 | { |
| 81 | model.operands.resize(model.operands.size() + 1); |
| 82 | model.operands[model.operands.size() - 1] = op; |
| 83 | } |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 84 | |
Aron Virginas-Tar | 44cfd84 | 2019-06-14 15:45:03 +0100 | [diff] [blame] | 85 | template<typename HalPolicy, typename HalModel = typename HalPolicy::Model> |
Nikhil Raj | 7760582 | 2018-09-03 11:25:56 +0100 | [diff] [blame] | 86 | void AddIntOperand(HalModel& model, int32_t value) |
| 87 | { |
Aron Virginas-Tar | 44cfd84 | 2019-06-14 15:45:03 +0100 | [diff] [blame] | 88 | using HalOperand = typename HalPolicy::Operand; |
| 89 | using HalOperandType = typename HalPolicy::OperandType; |
| 90 | using HalOperandLifeTime = typename HalPolicy::OperandLifeTime; |
| 91 | |
Nikhil Raj | 7760582 | 2018-09-03 11:25:56 +0100 | [diff] [blame] | 92 | DataLocation location = {}; |
| 93 | location.offset = model.operandValues.size(); |
| 94 | location.length = sizeof(int32_t); |
| 95 | |
Aron Virginas-Tar | 44cfd84 | 2019-06-14 15:45:03 +0100 | [diff] [blame] | 96 | HalOperand op = {}; |
| 97 | op.type = HalOperandType::INT32; |
| 98 | op.dimensions = hidl_vec<uint32_t>{}; |
| 99 | op.lifetime = HalOperandLifeTime::CONSTANT_COPY; |
| 100 | op.location = location; |
Nikhil Raj | 7760582 | 2018-09-03 11:25:56 +0100 | [diff] [blame] | 101 | |
| 102 | model.operandValues.resize(model.operandValues.size() + location.length); |
| 103 | *reinterpret_cast<int32_t*>(&model.operandValues[location.offset]) = value; |
| 104 | |
Aron Virginas-Tar | 44cfd84 | 2019-06-14 15:45:03 +0100 | [diff] [blame] | 105 | AddOperand<HalPolicy>(model, op); |
| 106 | } |
| 107 | |
| 108 | template<typename HalPolicy, typename HalModel = typename HalPolicy::Model> |
| 109 | void AddBoolOperand(HalModel& model, bool value) |
| 110 | { |
| 111 | using HalOperand = typename HalPolicy::Operand; |
| 112 | using HalOperandType = typename HalPolicy::OperandType; |
| 113 | using HalOperandLifeTime = typename HalPolicy::OperandLifeTime; |
| 114 | |
| 115 | DataLocation location = {}; |
| 116 | location.offset = model.operandValues.size(); |
| 117 | location.length = sizeof(uint8_t); |
| 118 | |
| 119 | HalOperand op = {}; |
| 120 | op.type = HalOperandType::BOOL; |
| 121 | op.dimensions = hidl_vec<uint32_t>{}; |
| 122 | op.lifetime = HalOperandLifeTime::CONSTANT_COPY; |
| 123 | op.location = location; |
| 124 | |
| 125 | model.operandValues.resize(model.operandValues.size() + location.length); |
| 126 | *reinterpret_cast<uint8_t*>(&model.operandValues[location.offset]) = static_cast<uint8_t>(value); |
| 127 | |
Nikhil Raj | 7760582 | 2018-09-03 11:25:56 +0100 | [diff] [blame] | 128 | AddOperand<HalModel>(model, op); |
| 129 | } |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 130 | |
| 131 | template<typename T> |
| 132 | OperandType TypeToOperandType(); |
| 133 | |
| 134 | template<> |
| 135 | OperandType TypeToOperandType<float>(); |
| 136 | |
| 137 | template<> |
| 138 | OperandType TypeToOperandType<int32_t>(); |
| 139 | |
Aron Virginas-Tar | 44cfd84 | 2019-06-14 15:45:03 +0100 | [diff] [blame] | 140 | template<typename HalPolicy, |
| 141 | typename T, |
| 142 | typename HalModel = typename HalPolicy::Model, |
| 143 | typename HalOperandType = typename HalPolicy::OperandType, |
| 144 | typename HalOperandLifeTime = typename HalPolicy::OperandLifeTime> |
Nikhil Raj | 7760582 | 2018-09-03 11:25:56 +0100 | [diff] [blame] | 145 | void AddTensorOperand(HalModel& model, |
Matteo Martincigh | c743412 | 2018-11-14 12:27:04 +0000 | [diff] [blame] | 146 | const hidl_vec<uint32_t>& dimensions, |
| 147 | const T* values, |
Aron Virginas-Tar | 44cfd84 | 2019-06-14 15:45:03 +0100 | [diff] [blame] | 148 | HalOperandType operandType = HalOperandType::TENSOR_FLOAT32, |
| 149 | HalOperandLifeTime operandLifeTime = HalOperandLifeTime::CONSTANT_COPY) |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 150 | { |
Aron Virginas-Tar | 44cfd84 | 2019-06-14 15:45:03 +0100 | [diff] [blame] | 151 | using HalOperand = typename HalPolicy::Operand; |
| 152 | |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 153 | uint32_t totalElements = 1; |
| 154 | for (uint32_t dim : dimensions) |
| 155 | { |
| 156 | totalElements *= dim; |
| 157 | } |
| 158 | |
| 159 | DataLocation location = {}; |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 160 | location.length = totalElements * sizeof(T); |
| 161 | |
Aron Virginas-Tar | 44cfd84 | 2019-06-14 15:45:03 +0100 | [diff] [blame] | 162 | if(operandLifeTime == HalOperandLifeTime::CONSTANT_COPY) |
Kevin May | f29a2c5 | 2019-03-14 11:56:32 +0000 | [diff] [blame] | 163 | { |
| 164 | location.offset = model.operandValues.size(); |
| 165 | } |
| 166 | |
Aron Virginas-Tar | 44cfd84 | 2019-06-14 15:45:03 +0100 | [diff] [blame] | 167 | HalOperand op = {}; |
| 168 | op.type = operandType; |
| 169 | op.dimensions = dimensions; |
| 170 | op.lifetime = HalOperandLifeTime::CONSTANT_COPY; |
| 171 | op.location = location; |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 172 | |
| 173 | model.operandValues.resize(model.operandValues.size() + location.length); |
| 174 | for (uint32_t i = 0; i < totalElements; i++) |
| 175 | { |
| 176 | *(reinterpret_cast<T*>(&model.operandValues[location.offset]) + i) = values[i]; |
| 177 | } |
| 178 | |
Aron Virginas-Tar | 44cfd84 | 2019-06-14 15:45:03 +0100 | [diff] [blame] | 179 | AddOperand<HalPolicy>(model, op); |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 180 | } |
| 181 | |
Aron Virginas-Tar | 44cfd84 | 2019-06-14 15:45:03 +0100 | [diff] [blame] | 182 | template<typename HalPolicy, |
| 183 | typename T, |
| 184 | typename HalModel = typename HalPolicy::Model, |
| 185 | typename HalOperandType = typename HalPolicy::OperandType, |
| 186 | typename HalOperandLifeTime = typename HalPolicy::OperandLifeTime> |
Matteo Martincigh | c743412 | 2018-11-14 12:27:04 +0000 | [diff] [blame] | 187 | void AddTensorOperand(HalModel& model, |
| 188 | const hidl_vec<uint32_t>& dimensions, |
| 189 | const std::vector<T>& values, |
Aron Virginas-Tar | 44cfd84 | 2019-06-14 15:45:03 +0100 | [diff] [blame] | 190 | HalOperandType operandType = HalPolicy::OperandType::TENSOR_FLOAT32, |
| 191 | HalOperandLifeTime operandLifeTime = HalOperandLifeTime::CONSTANT_COPY) |
Matteo Martincigh | c743412 | 2018-11-14 12:27:04 +0000 | [diff] [blame] | 192 | { |
Aron Virginas-Tar | 44cfd84 | 2019-06-14 15:45:03 +0100 | [diff] [blame] | 193 | AddTensorOperand<HalPolicy, T>(model, dimensions, values.data(), operandType, operandLifeTime); |
Matteo Martincigh | c743412 | 2018-11-14 12:27:04 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Aron Virginas-Tar | 44cfd84 | 2019-06-14 15:45:03 +0100 | [diff] [blame] | 196 | template<typename HalPolicy, |
| 197 | typename HalModel = typename HalPolicy::Model, |
| 198 | typename HalOperandType = typename HalPolicy::OperandType> |
Nikhil Raj | 7760582 | 2018-09-03 11:25:56 +0100 | [diff] [blame] | 199 | void AddInputOperand(HalModel& model, |
Matteo Martincigh | c743412 | 2018-11-14 12:27:04 +0000 | [diff] [blame] | 200 | const hidl_vec<uint32_t>& dimensions, |
Aron Virginas-Tar | 44cfd84 | 2019-06-14 15:45:03 +0100 | [diff] [blame] | 201 | HalOperandType operandType = HalOperandType::TENSOR_FLOAT32) |
Nikhil Raj | 7760582 | 2018-09-03 11:25:56 +0100 | [diff] [blame] | 202 | { |
Aron Virginas-Tar | 44cfd84 | 2019-06-14 15:45:03 +0100 | [diff] [blame] | 203 | using HalOperand = typename HalPolicy::Operand; |
| 204 | using HalOperandLifeTime = typename HalPolicy::OperandLifeTime; |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 205 | |
Aron Virginas-Tar | 44cfd84 | 2019-06-14 15:45:03 +0100 | [diff] [blame] | 206 | HalOperand op = {}; |
| 207 | op.type = operandType; |
| 208 | op.scale = operandType == HalOperandType::TENSOR_QUANT8_ASYMM ? 1.f / 255.f : 0.f; |
| 209 | op.dimensions = dimensions; |
| 210 | op.lifetime = HalOperandLifeTime::MODEL_INPUT; |
| 211 | |
| 212 | AddOperand<HalPolicy>(model, op); |
Nikhil Raj | 7760582 | 2018-09-03 11:25:56 +0100 | [diff] [blame] | 213 | |
| 214 | model.inputIndexes.resize(model.inputIndexes.size() + 1); |
| 215 | model.inputIndexes[model.inputIndexes.size() - 1] = model.operands.size() - 1; |
| 216 | } |
| 217 | |
Aron Virginas-Tar | 44cfd84 | 2019-06-14 15:45:03 +0100 | [diff] [blame] | 218 | template<typename HalPolicy, |
| 219 | typename HalModel = typename HalPolicy::Model, |
| 220 | typename HalOperandType = typename HalPolicy::OperandType> |
Nikhil Raj | 7760582 | 2018-09-03 11:25:56 +0100 | [diff] [blame] | 221 | void AddOutputOperand(HalModel& model, |
Matteo Martincigh | c743412 | 2018-11-14 12:27:04 +0000 | [diff] [blame] | 222 | const hidl_vec<uint32_t>& dimensions, |
Aron Virginas-Tar | 44cfd84 | 2019-06-14 15:45:03 +0100 | [diff] [blame] | 223 | HalOperandType operandType = HalOperandType::TENSOR_FLOAT32) |
Nikhil Raj | 7760582 | 2018-09-03 11:25:56 +0100 | [diff] [blame] | 224 | { |
Aron Virginas-Tar | 44cfd84 | 2019-06-14 15:45:03 +0100 | [diff] [blame] | 225 | using HalOperand = typename HalPolicy::Operand; |
| 226 | using HalOperandLifeTime = typename HalPolicy::OperandLifeTime; |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 227 | |
Aron Virginas-Tar | 44cfd84 | 2019-06-14 15:45:03 +0100 | [diff] [blame] | 228 | HalOperand op = {}; |
| 229 | op.type = operandType; |
| 230 | op.scale = operandType == HalOperandType::TENSOR_QUANT8_ASYMM ? 1.f / 255.f : 0.f; |
| 231 | op.dimensions = dimensions; |
| 232 | op.lifetime = HalOperandLifeTime::MODEL_OUTPUT; |
| 233 | |
| 234 | AddOperand<HalPolicy>(model, op); |
Nikhil Raj | 7760582 | 2018-09-03 11:25:56 +0100 | [diff] [blame] | 235 | |
| 236 | model.outputIndexes.resize(model.outputIndexes.size() + 1); |
| 237 | model.outputIndexes[model.outputIndexes.size() - 1] = model.operands.size() - 1; |
| 238 | } |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 239 | |
Sadik Armagan | e6e54a8 | 2019-05-08 10:18:05 +0100 | [diff] [blame] | 240 | android::sp<V1_0::IPreparedModel> PrepareModelWithStatus(const V1_0::Model& model, |
| 241 | armnn_driver::ArmnnDriver& driver, |
| 242 | ErrorStatus& prepareStatus, |
| 243 | ErrorStatus expectedStatus = ErrorStatus::NONE); |
Nikhil Raj | 7760582 | 2018-09-03 11:25:56 +0100 | [diff] [blame] | 244 | |
Matteo Martincigh | a5f9e76 | 2019-06-17 13:26:34 +0100 | [diff] [blame] | 245 | #if defined(ARMNN_ANDROID_NN_V1_1) || defined(ARMNN_ANDROID_NN_V1_2) |
Nikhil Raj | 7760582 | 2018-09-03 11:25:56 +0100 | [diff] [blame] | 246 | |
Sadik Armagan | e6e54a8 | 2019-05-08 10:18:05 +0100 | [diff] [blame] | 247 | android::sp<V1_0::IPreparedModel> PrepareModelWithStatus(const V1_1::Model& model, |
Matteo Martincigh | a5f9e76 | 2019-06-17 13:26:34 +0100 | [diff] [blame] | 248 | armnn_driver::ArmnnDriver& driver, |
| 249 | ErrorStatus& prepareStatus, |
| 250 | ErrorStatus expectedStatus = ErrorStatus::NONE); |
Nikhil Raj | 7760582 | 2018-09-03 11:25:56 +0100 | [diff] [blame] | 251 | |
| 252 | #endif |
| 253 | |
| 254 | template<typename HalModel> |
Sadik Armagan | e6e54a8 | 2019-05-08 10:18:05 +0100 | [diff] [blame] | 255 | android::sp<V1_0::IPreparedModel> PrepareModel(const HalModel& model, |
Aron Virginas-Tar | 44cfd84 | 2019-06-14 15:45:03 +0100 | [diff] [blame] | 256 | armnn_driver::ArmnnDriver& driver) |
Nikhil Raj | 7760582 | 2018-09-03 11:25:56 +0100 | [diff] [blame] | 257 | { |
| 258 | ErrorStatus prepareStatus = ErrorStatus::NONE; |
| 259 | return PrepareModelWithStatus(model, driver, prepareStatus); |
| 260 | } |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 261 | |
Sadik Armagan | e6e54a8 | 2019-05-08 10:18:05 +0100 | [diff] [blame] | 262 | ErrorStatus Execute(android::sp<V1_0::IPreparedModel> preparedModel, |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 263 | const Request& request, |
Nikhil Raj | 7760582 | 2018-09-03 11:25:56 +0100 | [diff] [blame] | 264 | ErrorStatus expectedStatus = ErrorStatus::NONE); |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 265 | |
Sadik Armagan | e6e54a8 | 2019-05-08 10:18:05 +0100 | [diff] [blame] | 266 | android::sp<ExecutionCallback> ExecuteNoWait(android::sp<V1_0::IPreparedModel> preparedModel, |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 267 | const Request& request); |
| 268 | |
| 269 | } // namespace driverTestHelpers |