Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2020 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
| 8 | #include "ArmnnDriver.hpp" |
| 9 | #include "ArmnnDriverImpl.hpp" |
| 10 | #include "RequestThread.hpp" |
| 11 | #include "ModelToINetworkConverter.hpp" |
| 12 | |
| 13 | #include <NeuralNetworks.h> |
| 14 | #include <armnn/ArmNN.hpp> |
| 15 | |
| 16 | #include <string> |
| 17 | #include <vector> |
| 18 | |
| 19 | namespace armnn_driver |
| 20 | { |
| 21 | using CallbackAsync_1_3 = std::function< |
| 22 | void(V1_3::ErrorStatus errorStatus, |
| 23 | std::vector<::android::hardware::neuralnetworks::V1_2::OutputShape> outputShapes, |
| 24 | const ::android::hardware::neuralnetworks::V1_2::Timing& timing, |
| 25 | std::string callingFunction)>; |
| 26 | |
| 27 | struct ExecutionContext_1_3 |
| 28 | { |
| 29 | ::android::hardware::neuralnetworks::V1_2::MeasureTiming measureTimings = |
| 30 | ::android::hardware::neuralnetworks::V1_2::MeasureTiming::NO; |
| 31 | TimePoint driverStart; |
Kevin May | 949a69e | 2020-04-24 10:21:40 +0100 | [diff] [blame^] | 32 | TimePoint driverEnd; |
Sadik Armagan | d7be72e | 2020-04-23 12:56:05 +0100 | [diff] [blame] | 33 | TimePoint deviceStart; |
| 34 | TimePoint deviceEnd; |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | using CallbackContext_1_3 = CallbackContext<CallbackAsync_1_3, ExecutionContext_1_3>; |
| 38 | |
| 39 | using executeFenced_cb = std::function<void(::android::hardware::neuralnetworks::V1_3::ErrorStatus status, |
| 40 | const ::android::hardware::hidl_handle& syncFence, |
| 41 | const ::android::sp<::android::hardware::neuralnetworks::V1_3::IFencedExecutionCallback>& callback)>; |
| 42 | |
| 43 | template <typename HalVersion> |
| 44 | class ArmnnPreparedModel_1_3 : public V1_3::IPreparedModel |
| 45 | { |
| 46 | public: |
| 47 | using HalModel = typename V1_3::Model; |
| 48 | |
| 49 | ArmnnPreparedModel_1_3(armnn::NetworkId networkId, |
| 50 | armnn::IRuntime* runtime, |
| 51 | const HalModel& model, |
| 52 | const std::string& requestInputsAndOutputsDumpDir, |
| 53 | const bool gpuProfilingEnabled); |
| 54 | |
| 55 | virtual ~ArmnnPreparedModel_1_3(); |
| 56 | |
| 57 | Return<V1_0::ErrorStatus> execute(const V1_0::Request& request, |
| 58 | const sp<V1_0::IExecutionCallback>& callback) override; |
| 59 | |
| 60 | Return<V1_0::ErrorStatus> execute_1_2(const V1_0::Request& request, MeasureTiming measure, |
| 61 | const sp<V1_2::IExecutionCallback>& callback) override; |
| 62 | |
| 63 | Return<V1_3::ErrorStatus> execute_1_3(const V1_3::Request& request, |
| 64 | V1_2::MeasureTiming measure, |
| 65 | const V1_3::OptionalTimePoint&, |
Kevin May | 352d838 | 2020-03-31 15:03:42 +0100 | [diff] [blame] | 66 | const V1_3::OptionalTimeoutDuration&, |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 67 | const sp<V1_3::IExecutionCallback>& callback) override; |
| 68 | |
| 69 | Return<void> executeSynchronously(const V1_0::Request &request, |
| 70 | MeasureTiming measure, |
| 71 | V1_3::IPreparedModel::executeSynchronously_cb cb) override; |
| 72 | |
| 73 | Return<void> executeSynchronously_1_3(const V1_3::Request &request, |
| 74 | MeasureTiming measure, |
| 75 | const V1_3::OptionalTimePoint& deadline, |
Kevin May | 352d838 | 2020-03-31 15:03:42 +0100 | [diff] [blame] | 76 | const V1_3::OptionalTimeoutDuration& loopTimeoutDuration, |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 77 | V1_3::IPreparedModel::executeSynchronously_1_3_cb cb) override; |
| 78 | |
| 79 | Return<void> executeFenced(const V1_3::Request& request, |
Sadik Armagan | d7be72e | 2020-04-23 12:56:05 +0100 | [diff] [blame] | 80 | const android::hardware::hidl_vec<android::hardware::hidl_handle>& fenceWaitFor, |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 81 | MeasureTiming measure, |
| 82 | const V1_3::OptionalTimePoint& deadline, |
Kevin May | 352d838 | 2020-03-31 15:03:42 +0100 | [diff] [blame] | 83 | const V1_3::OptionalTimeoutDuration& loopTimeoutDuration, |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 84 | const V1_3::OptionalTimeoutDuration& duration, |
| 85 | executeFenced_cb callback) override; |
| 86 | |
| 87 | Return<void> configureExecutionBurst( |
| 88 | const sp<V1_2::IBurstCallback>& callback, |
| 89 | const android::hardware::MQDescriptorSync<V1_2::FmqRequestDatum>& requestChannel, |
| 90 | const android::hardware::MQDescriptorSync<V1_2::FmqResultDatum>& resultChannel, |
| 91 | configureExecutionBurst_cb cb) override; |
| 92 | |
| 93 | template<typename CallbackContext> |
| 94 | Return<void> ExecuteSynchronously(const V1_3::Request& request, CallbackContext cbCtx); |
| 95 | |
| 96 | /// execute the graph prepared from the request |
| 97 | template<typename CallbackContext> |
Sadik Armagan | d7be72e | 2020-04-23 12:56:05 +0100 | [diff] [blame] | 98 | Return <V1_3::ErrorStatus> ExecuteGraph( |
| 99 | std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& pMemPools, |
| 100 | armnn::InputTensors& inputTensors, |
| 101 | armnn::OutputTensors& outputTensors, |
| 102 | CallbackContext callback); |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 103 | |
| 104 | /// Executes this model with dummy inputs (e.g. all zeroes). |
| 105 | /// \return false on failure, otherwise true |
| 106 | bool ExecuteWithDummyInputs(); |
| 107 | |
| 108 | private: |
| 109 | Return <V1_3::ErrorStatus> Execute(const V1_3::Request& request, |
| 110 | MeasureTiming measureTiming, |
| 111 | CallbackAsync_1_3 callback); |
| 112 | |
| 113 | Return<V1_3::ErrorStatus> PrepareMemoryForInputs( |
| 114 | armnn::InputTensors& inputs, |
| 115 | const V1_3::Request& request, |
| 116 | const std::vector<android::nn::RunTimePoolInfo>& memPools); |
| 117 | |
| 118 | Return<V1_3::ErrorStatus> PrepareMemoryForOutputs( |
| 119 | armnn::OutputTensors& outputs, |
| 120 | std::vector<OutputShape> &outputShapes, |
| 121 | const V1_3::Request& request, |
| 122 | const std::vector<android::nn::RunTimePoolInfo>& memPools); |
| 123 | |
| 124 | std::tuple<V1_3::ErrorStatus, hidl_vec<OutputShape>, Timing, std::string> PrepareMemoryForIO( |
| 125 | armnn::InputTensors& inputs, |
| 126 | armnn::OutputTensors& outputs, |
| 127 | std::vector<android::nn::RunTimePoolInfo>& memPools, |
| 128 | const V1_3::Request& request); |
| 129 | |
| 130 | template <typename TensorBindingCollection> |
| 131 | void DumpTensorsIfRequired(char const* tensorNamePrefix, const TensorBindingCollection& tensorBindings); |
| 132 | |
| 133 | armnn::NetworkId m_NetworkId; |
| 134 | armnn::IRuntime* m_Runtime; |
| 135 | V1_3::Model m_Model; |
| 136 | // There must be a single RequestThread for all ArmnnPreparedModel objects to ensure serial execution of workloads |
| 137 | // It is specific to this class, so it is declared as static here |
| 138 | static RequestThread<ArmnnPreparedModel_1_3, HalVersion, CallbackContext_1_3> m_RequestThread; |
| 139 | uint32_t m_RequestCount; |
| 140 | const std::string& m_RequestInputsAndOutputsDumpDir; |
| 141 | const bool m_GpuProfilingEnabled; |
| 142 | }; |
| 143 | |
| 144 | } |