surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame^] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // See LICENSE file in the project root for full license information. |
| 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> |
| 13 | |
| 14 | namespace android |
| 15 | { |
| 16 | namespace hardware |
| 17 | { |
| 18 | namespace neuralnetworks |
| 19 | { |
| 20 | namespace V1_0 |
| 21 | { |
| 22 | |
| 23 | std::ostream& operator<<(std::ostream& os, ErrorStatus stat); |
| 24 | |
| 25 | } // namespace android::hardware::neuralnetworks::V1_0 |
| 26 | } // namespace android::hardware::neuralnetworks |
| 27 | } // namespace android::hardware |
| 28 | } // namespace android |
| 29 | |
| 30 | namespace driverTestHelpers |
| 31 | { |
| 32 | |
| 33 | std::ostream& operator<<(std::ostream& os, android::hardware::neuralnetworks::V1_0::ErrorStatus stat); |
| 34 | |
| 35 | struct ExecutionCallback : public IExecutionCallback |
| 36 | { |
| 37 | ExecutionCallback() : mNotified(false) {} |
| 38 | Return<void> notify(ErrorStatus status) override; |
| 39 | /// wait until the callback has notified us that it is done |
| 40 | Return<void> wait(); |
| 41 | |
| 42 | private: |
| 43 | // use a mutex and a condition variable to wait for asynchronous callbacks |
| 44 | std::mutex mMutex; |
| 45 | std::condition_variable mCondition; |
| 46 | // and a flag, in case we are notified before the wait call |
| 47 | bool mNotified; |
| 48 | }; |
| 49 | |
| 50 | class PreparedModelCallback : public IPreparedModelCallback |
| 51 | { |
| 52 | public: |
| 53 | PreparedModelCallback() |
| 54 | : m_ErrorStatus(ErrorStatus::NONE) |
| 55 | , m_PreparedModel() |
| 56 | { } |
| 57 | ~PreparedModelCallback() override { } |
| 58 | |
| 59 | Return<void> notify(ErrorStatus status, |
| 60 | const android::sp<IPreparedModel>& preparedModel) override; |
| 61 | ErrorStatus GetErrorStatus() { return m_ErrorStatus; } |
| 62 | android::sp<IPreparedModel> GetPreparedModel() { return m_PreparedModel; } |
| 63 | |
| 64 | private: |
| 65 | ErrorStatus m_ErrorStatus; |
| 66 | android::sp<IPreparedModel> m_PreparedModel; |
| 67 | }; |
| 68 | |
| 69 | hidl_memory allocateSharedMemory(int64_t size); |
| 70 | |
| 71 | android::sp<IMemory> AddPoolAndGetData(uint32_t size, Request& request); |
| 72 | |
| 73 | void AddPoolAndSetData(uint32_t size, Request& request, const float* data); |
| 74 | |
| 75 | void AddOperand(Model& model, const Operand& op); |
| 76 | |
| 77 | void AddIntOperand(Model& model, int32_t value); |
| 78 | |
| 79 | template<typename T> |
| 80 | OperandType TypeToOperandType(); |
| 81 | |
| 82 | template<> |
| 83 | OperandType TypeToOperandType<float>(); |
| 84 | |
| 85 | template<> |
| 86 | OperandType TypeToOperandType<int32_t>(); |
| 87 | |
| 88 | template<typename T> |
| 89 | void AddTensorOperand(Model& model, hidl_vec<uint32_t> dimensions, T* values) |
| 90 | { |
| 91 | uint32_t totalElements = 1; |
| 92 | for (uint32_t dim : dimensions) |
| 93 | { |
| 94 | totalElements *= dim; |
| 95 | } |
| 96 | |
| 97 | DataLocation location = {}; |
| 98 | location.offset = model.operandValues.size(); |
| 99 | location.length = totalElements * sizeof(T); |
| 100 | |
| 101 | Operand op = {}; |
| 102 | op.type = TypeToOperandType<T>(); |
| 103 | op.dimensions = dimensions; |
| 104 | op.lifetime = OperandLifeTime::CONSTANT_COPY; |
| 105 | op.location = location; |
| 106 | |
| 107 | model.operandValues.resize(model.operandValues.size() + location.length); |
| 108 | for (uint32_t i = 0; i < totalElements; i++) |
| 109 | { |
| 110 | *(reinterpret_cast<T*>(&model.operandValues[location.offset]) + i) = values[i]; |
| 111 | } |
| 112 | |
| 113 | AddOperand(model, op); |
| 114 | } |
| 115 | |
| 116 | void AddInputOperand(Model& model, hidl_vec<uint32_t> dimensions); |
| 117 | |
| 118 | void AddOutputOperand(Model& model, hidl_vec<uint32_t> dimensions); |
| 119 | |
| 120 | android::sp<IPreparedModel> PrepareModel(const Model& model, |
| 121 | armnn_driver::ArmnnDriver& driver); |
| 122 | |
| 123 | android::sp<IPreparedModel> PrepareModelWithStatus(const Model& model, |
| 124 | armnn_driver::ArmnnDriver& driver, |
| 125 | ErrorStatus & prepareStatus, |
| 126 | ErrorStatus expectedStatus=ErrorStatus::NONE); |
| 127 | |
| 128 | ErrorStatus Execute(android::sp<IPreparedModel> preparedModel, |
| 129 | const Request& request, |
| 130 | ErrorStatus expectedStatus=ErrorStatus::NONE); |
| 131 | |
| 132 | android::sp<ExecutionCallback> ExecuteNoWait(android::sp<IPreparedModel> preparedModel, |
| 133 | const Request& request); |
| 134 | |
| 135 | } // namespace driverTestHelpers |