blob: e90f7ecfa580284682ed09ad193f437a4118d5ae [file] [log] [blame]
surmeh0149b9e102018-05-17 14:11:25 +01001//
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
14namespace android
15{
16namespace hardware
17{
18namespace neuralnetworks
19{
20namespace V1_0
21{
22
23std::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
30namespace driverTestHelpers
31{
32
33std::ostream& operator<<(std::ostream& os, android::hardware::neuralnetworks::V1_0::ErrorStatus stat);
34
35struct 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
42private:
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
50class PreparedModelCallback : public IPreparedModelCallback
51{
52public:
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
64private:
65 ErrorStatus m_ErrorStatus;
66 android::sp<IPreparedModel> m_PreparedModel;
67};
68
69hidl_memory allocateSharedMemory(int64_t size);
70
71android::sp<IMemory> AddPoolAndGetData(uint32_t size, Request& request);
72
73void AddPoolAndSetData(uint32_t size, Request& request, const float* data);
74
75void AddOperand(Model& model, const Operand& op);
76
77void AddIntOperand(Model& model, int32_t value);
78
79template<typename T>
80OperandType TypeToOperandType();
81
82template<>
83OperandType TypeToOperandType<float>();
84
85template<>
86OperandType TypeToOperandType<int32_t>();
87
88template<typename T>
89void 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
116void AddInputOperand(Model& model, hidl_vec<uint32_t> dimensions);
117
118void AddOutputOperand(Model& model, hidl_vec<uint32_t> dimensions);
119
120android::sp<IPreparedModel> PrepareModel(const Model& model,
121 armnn_driver::ArmnnDriver& driver);
122
123android::sp<IPreparedModel> PrepareModelWithStatus(const Model& model,
124 armnn_driver::ArmnnDriver& driver,
125 ErrorStatus & prepareStatus,
126 ErrorStatus expectedStatus=ErrorStatus::NONE);
127
128ErrorStatus Execute(android::sp<IPreparedModel> preparedModel,
129 const Request& request,
130 ErrorStatus expectedStatus=ErrorStatus::NONE);
131
132android::sp<ExecutionCallback> ExecuteNoWait(android::sp<IPreparedModel> preparedModel,
133 const Request& request);
134
135} // namespace driverTestHelpers