blob: f0e28970d5ebf9bd8b0f2983275b961c6f441131 [file] [log] [blame]
telsoa015307bc12018-03-09 13:51:08 +00001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// See LICENSE file in the project root for full license information.
4//
5
6#pragma once
7
surmeh01deb3bdb2018-07-05 12:06:04 +01008#include "ArmnnDriver.hpp"
9
telsoa01ce3e84a2018-08-31 09:31:35 +010010#include <NeuralNetworks.h>
11#include <ActivationFunctor.h>
12
telsoa015307bc12018-03-09 13:51:08 +000013#include <armnn/ArmNN.hpp>
14#include <armnn/INetwork.hpp>
15#include <CpuExecutor.h>
16
17#include "Utils.hpp"
18
19#include <memory>
20#include <vector>
21#include <set>
22
23namespace armnn_driver
24{
25
26class ConstTensorPin;
27class LayerInputHandle;
28
29enum class ConversionResult
30{
31 Success,
32 ErrorMappingPools,
33 UnsupportedFeature
34};
35
36// A helper performing the conversion from an AndroidNN driver Model representation,
37// to an armnn::INetwork object
38class ModelToINetworkConverter
39{
40public:
telsoa01ce3e84a2018-08-31 09:31:35 +010041 ModelToINetworkConverter(armnn::Compute compute,
42 const ::android::hardware::neuralnetworks::V1_0::Model& model,
telsoa015307bc12018-03-09 13:51:08 +000043 const std::set<unsigned int>& forcedUnsupportedOperations);
44
45 ConversionResult GetConversionResult() const { return m_ConversionResult; }
46
47 // Returns the ArmNN INetwork corresponding to the input model, if preparation went smoothly, nullptr otherwise.
48 armnn::INetwork* GetINetwork() const { return m_Network.get(); }
49
50 bool IsOperationSupported(uint32_t operationIndex) const;
51
52private:
53 void Convert();
54
telsoa01ce3e84a2018-08-31 09:31:35 +010055 bool ConvertOperation(const ::android::hardware::neuralnetworks::V1_0::Operation& operation);
telsoa015307bc12018-03-09 13:51:08 +000056
telsoa01ce3e84a2018-08-31 09:31:35 +010057 bool ConvertAdd(const ::android::hardware::neuralnetworks::V1_0::Operation& operation);
telsoa015307bc12018-03-09 13:51:08 +000058
telsoa01ce3e84a2018-08-31 09:31:35 +010059 bool ConvertAveragePool2d(const ::android::hardware::neuralnetworks::V1_0::Operation& operation);
telsoa015307bc12018-03-09 13:51:08 +000060
telsoa01ce3e84a2018-08-31 09:31:35 +010061 bool ConvertConcatenation(const ::android::hardware::neuralnetworks::V1_0::Operation& operation);
telsoa015307bc12018-03-09 13:51:08 +000062
telsoa01ce3e84a2018-08-31 09:31:35 +010063 bool ConvertConv2d(const ::android::hardware::neuralnetworks::V1_0::Operation& operation);
telsoa015307bc12018-03-09 13:51:08 +000064
telsoa01ce3e84a2018-08-31 09:31:35 +010065 bool ConvertDepthwiseConv2d(const ::android::hardware::neuralnetworks::V1_0::Operation& operation);
telsoa015307bc12018-03-09 13:51:08 +000066
telsoa01ce3e84a2018-08-31 09:31:35 +010067 bool ConvertFloor(const ::android::hardware::neuralnetworks::V1_0::Operation& operation);
telsoa015307bc12018-03-09 13:51:08 +000068
telsoa01ce3e84a2018-08-31 09:31:35 +010069 bool ConvertFullyConnected(const ::android::hardware::neuralnetworks::V1_0::Operation& operation);
telsoa015307bc12018-03-09 13:51:08 +000070
telsoa01ce3e84a2018-08-31 09:31:35 +010071 bool ConvertLogistic(const ::android::hardware::neuralnetworks::V1_0::Operation& operation);
telsoa015307bc12018-03-09 13:51:08 +000072
telsoa01ce3e84a2018-08-31 09:31:35 +010073 bool ConvertLocalResponseNormalization(const ::android::hardware::neuralnetworks::V1_0::Operation& operation);
telsoa015307bc12018-03-09 13:51:08 +000074
telsoa01ce3e84a2018-08-31 09:31:35 +010075 bool ConvertL2Normalization(const ::android::hardware::neuralnetworks::V1_0::Operation& operation);
telsoa015307bc12018-03-09 13:51:08 +000076
telsoa01ce3e84a2018-08-31 09:31:35 +010077 bool ConvertL2Pool2d(const ::android::hardware::neuralnetworks::V1_0::Operation& operation);
telsoa015307bc12018-03-09 13:51:08 +000078
telsoa01ce3e84a2018-08-31 09:31:35 +010079 bool ConvertMaxPool2d(const ::android::hardware::neuralnetworks::V1_0::Operation& operation);
telsoa015307bc12018-03-09 13:51:08 +000080
telsoa01ce3e84a2018-08-31 09:31:35 +010081 bool ConvertMul(const ::android::hardware::neuralnetworks::V1_0::Operation& operation);
telsoa015307bc12018-03-09 13:51:08 +000082
telsoa01ce3e84a2018-08-31 09:31:35 +010083 bool ConvertReLu(const ::android::hardware::neuralnetworks::V1_0::Operation& operation);
telsoa015307bc12018-03-09 13:51:08 +000084
telsoa01ce3e84a2018-08-31 09:31:35 +010085 bool ConvertReLu1(const ::android::hardware::neuralnetworks::V1_0::Operation& operation);
telsoa015307bc12018-03-09 13:51:08 +000086
telsoa01ce3e84a2018-08-31 09:31:35 +010087 bool ConvertReLu6(const ::android::hardware::neuralnetworks::V1_0::Operation& operation);
telsoa015307bc12018-03-09 13:51:08 +000088
telsoa01ce3e84a2018-08-31 09:31:35 +010089 bool ConvertSoftmax(const ::android::hardware::neuralnetworks::V1_0::Operation& operation);
telsoa015307bc12018-03-09 13:51:08 +000090
telsoa01ce3e84a2018-08-31 09:31:35 +010091 bool ConvertTanH(const ::android::hardware::neuralnetworks::V1_0::Operation& operation);
telsoa015307bc12018-03-09 13:51:08 +000092
telsoa01ce3e84a2018-08-31 09:31:35 +010093 bool ConvertReshape(const ::android::hardware::neuralnetworks::V1_0::Operation& operation);
telsoa015307bc12018-03-09 13:51:08 +000094
telsoa01ce3e84a2018-08-31 09:31:35 +010095 bool ConvertResizeBilinear(const ::android::hardware::neuralnetworks::V1_0::Operation& operation);
telsoa015307bc12018-03-09 13:51:08 +000096
telsoa01ce3e84a2018-08-31 09:31:35 +010097 bool ConvertLstm(const ::android::hardware::neuralnetworks::V1_0::Operation& operation);
98
99 bool ConvertToActivation(const ::android::hardware::neuralnetworks::V1_0::Operation& operation,
100 const char* operationName,
telsoa015307bc12018-03-09 13:51:08 +0000101 const armnn::ActivationDescriptor& activationDesc);
102
telsoa01ce3e84a2018-08-31 09:31:35 +0100103 bool ConvertPooling2d(const ::android::hardware::neuralnetworks::V1_0::Operation& operation,
104 const char* name, armnn::PoolingAlgorithm poolType);
telsoa015307bc12018-03-09 13:51:08 +0000105
106
107 const void* GetOperandValueReadOnlyAddress(const Operand& operand) const;
108
telsoa01ce3e84a2018-08-31 09:31:35 +0100109 const Operand* GetInputOperand(const ::android::hardware::neuralnetworks::V1_0::Operation& operation,
110 uint32_t inputIndex) const;
telsoa015307bc12018-03-09 13:51:08 +0000111
telsoa01ce3e84a2018-08-31 09:31:35 +0100112 const Operand* GetOutputOperand(const ::android::hardware::neuralnetworks::V1_0::Operation& operation,
113 uint32_t outputIndex) const;
telsoa015307bc12018-03-09 13:51:08 +0000114
115 template<typename T>
telsoa01ce3e84a2018-08-31 09:31:35 +0100116 bool GetInputScalar(const ::android::hardware::neuralnetworks::V1_0::Operation& operation, uint32_t inputIndex,
117 OperandType type, T& outValue) const;
telsoa015307bc12018-03-09 13:51:08 +0000118
telsoa01ce3e84a2018-08-31 09:31:35 +0100119 bool GetInputInt32(const ::android::hardware::neuralnetworks::V1_0::Operation& operation, uint32_t inputIndex,
120 int32_t& outValue) const;
telsoa015307bc12018-03-09 13:51:08 +0000121
telsoa01ce3e84a2018-08-31 09:31:35 +0100122 bool GetInputFloat32(const ::android::hardware::neuralnetworks::V1_0::Operation& operation, uint32_t inputIndex,
123 float& outValue) const;
telsoa015307bc12018-03-09 13:51:08 +0000124
telsoa01ce3e84a2018-08-31 09:31:35 +0100125 bool GetInputActivationFunctionImpl(const ::android::hardware::neuralnetworks::V1_0::Operation& operation,
126 uint32_t inputIndex,
127 OperandType type,
128 ActivationFn& outActivationFunction) const;
telsoa015307bc12018-03-09 13:51:08 +0000129
telsoa01ce3e84a2018-08-31 09:31:35 +0100130 bool GetInputActivationFunction(const ::android::hardware::neuralnetworks::V1_0::Operation& operation,
131 uint32_t inputIndex,
132 ActivationFn& outActivationFunction) const;
telsoa015307bc12018-03-09 13:51:08 +0000133
telsoa01ce3e84a2018-08-31 09:31:35 +0100134 bool GetInputActivationFunctionFromTensor(const ::android::hardware::neuralnetworks::V1_0::Operation& operation,
135 uint32_t inputIndex,
136 ActivationFn& outActivationFunction) const;
telsoa015307bc12018-03-09 13:51:08 +0000137
telsoa01ce3e84a2018-08-31 09:31:35 +0100138 bool GetOptionalInputActivation(const ::android::hardware::neuralnetworks::V1_0::Operation& operation,
139 uint32_t inputIndex,
140 ActivationFn& activationFunction) const;
141
142 bool GetInputPaddingScheme(const ::android::hardware::neuralnetworks::V1_0::Operation& operation,
143 uint32_t inputIndex,
144 android::nn::PaddingScheme& outPaddingScheme) const;
145
146 LayerInputHandle ConvertToLayerInputHandle(const ::android::hardware::neuralnetworks::V1_0::Operation& operation,
147 uint32_t inputIndex);
148
149 ConstTensorPin ConvertOperationInputToConstTensorPin(
150 const ::android::hardware::neuralnetworks::V1_0::Operation& operation, uint32_t inputIndex,
151 const armnn::PermutationVector& dimensionMappings = g_DontPermute,
152 const armnn::TensorShape* overrideTensorShape = nullptr, bool optional = false);
telsoa015307bc12018-03-09 13:51:08 +0000153
154 ConstTensorPin ConvertOperandToConstTensorPin(const Operand& operand,
155 const armnn::PermutationVector& dimensionMappings = g_DontPermute,
telsoa01ce3e84a2018-08-31 09:31:35 +0100156 const armnn::TensorShape* overrideTensorShape = nullptr, bool optional = false);
telsoa015307bc12018-03-09 13:51:08 +0000157
158 bool GetTensorInt32Values(const Operand& operand, std::vector<int32_t>& outValues) const;
159
160
161 armnn::IConnectableLayer* ProcessActivation(const armnn::TensorInfo& tensorInfo, ActivationFn activation,
162 armnn::IConnectableLayer* prevLayer);
163
telsoa01ce3e84a2018-08-31 09:31:35 +0100164 bool SetupAndTrackLayerOutputSlot(const ::android::hardware::neuralnetworks::V1_0::Operation& operation,
165 uint32_t operationOutputIndex,
166 armnn::IConnectableLayer& layer,
167 uint32_t layerOutputIndex);
telsoa015307bc12018-03-09 13:51:08 +0000168
telsoa01ce3e84a2018-08-31 09:31:35 +0100169 bool SetupAndTrackLayerOutputSlot(const ::android::hardware::neuralnetworks::V1_0::Operation& operation,
170 uint32_t outputIndex,
telsoa015307bc12018-03-09 13:51:08 +0000171 armnn::IConnectableLayer& layer);
172
173
174 // Input data
telsoa01ce3e84a2018-08-31 09:31:35 +0100175 armnn::Compute m_Compute;
176 const ::android::hardware::neuralnetworks::V1_0::Model& m_Model;
177 const std::set<unsigned int>& m_ForcedUnsupportedOperations;
telsoa015307bc12018-03-09 13:51:08 +0000178
179 // Output data
telsoa01ce3e84a2018-08-31 09:31:35 +0100180 armnn::INetworkPtr m_Network;
181 ConversionResult m_ConversionResult;
182 std::map<uint32_t, bool> m_OperationSupported;
telsoa015307bc12018-03-09 13:51:08 +0000183
184 // Working/intermediate data
185 std::vector<armnn::IOutputSlot*> m_OutputSlotForOperand;
186 std::vector<android::nn::RunTimePoolInfo> m_MemPools;
187};
188
189} // armnn_driver