telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1 | // |
Mike Kelly | 04d8229 | 2023-01-19 18:29:40 +0000 | [diff] [blame] | 2 | // Copyright © 2017-2023 Arm Ltd and Contributors. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4 | // |
| 5 | #pragma once |
| 6 | |
Mike Kelly | 377fb21 | 2023-01-10 15:55:28 +0000 | [diff] [blame] | 7 | #include <armnn/Descriptors.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 8 | #include "armnn/INetwork.hpp" |
| 9 | #include "armnnTfLiteParser/ITfLiteParser.hpp" |
Nattapat Chaimanowong | b66504b | 2018-10-17 15:19:14 +0100 | [diff] [blame] | 10 | #include "armnn/Types.hpp" |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 11 | |
| 12 | #include <schema_generated.h> |
| 13 | #include <functional> |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 14 | #include <unordered_map> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 15 | #include <vector> |
| 16 | |
Matthew Sloyan | 4d217c0 | 2021-10-07 11:48:58 +0100 | [diff] [blame] | 17 | #include <tensorflow/lite/version.h> |
| 18 | |
| 19 | #if TF_MAJOR_VERSION > 2 || (TF_MAJOR_VERSION == 2 && TF_MINOR_VERSION > 3) |
| 20 | #define ARMNN_POST_TFLITE_2_3 |
| 21 | #endif |
| 22 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 23 | namespace armnnTfLiteParser |
| 24 | { |
| 25 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 26 | class TfLiteParserImpl |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 27 | { |
| 28 | public: |
| 29 | // Shorthands for TfLite types |
| 30 | using ModelPtr = std::unique_ptr<tflite::ModelT>; |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 31 | using SubgraphPtr = std::unique_ptr<tflite::SubGraphT>; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 32 | using OperatorPtr = std::unique_ptr<tflite::OperatorT>; |
| 33 | using OperatorCodePtr = std::unique_ptr<tflite::OperatorCodeT>; |
| 34 | using TensorPtr = std::unique_ptr<tflite::TensorT>; |
| 35 | using TensorRawPtr = const tflite::TensorT *; |
| 36 | using TensorRawPtrVector = std::vector<TensorRawPtr>; |
| 37 | using TensorIdRawPtr = std::pair<size_t, TensorRawPtr>; |
| 38 | using TensorIdRawPtrVector = std::vector<TensorIdRawPtr>; |
| 39 | using BufferPtr = std::unique_ptr<tflite::BufferT>; |
| 40 | using BufferRawPtr = const tflite::BufferT *; |
| 41 | |
| 42 | public: |
| 43 | /// Create the network from a flatbuffers binary file on disk |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 44 | armnn::INetworkPtr CreateNetworkFromBinaryFile(const char* graphFile); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 45 | |
| 46 | /// Create the network from a flatbuffers binary |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 47 | armnn::INetworkPtr CreateNetworkFromBinary(const std::vector<uint8_t> & binaryContent); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 48 | |
| 49 | |
| 50 | /// Retrieve binding info (layer id and tensor info) for the network input identified by |
| 51 | /// the given layer name and subgraph id |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 52 | BindingPointInfo GetNetworkInputBindingInfo(size_t subgraphId, |
| 53 | const std::string& name) const; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 54 | |
| 55 | /// Retrieve binding info (layer id and tensor info) for the network output identified by |
| 56 | /// the given layer name and subgraph id |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 57 | BindingPointInfo GetNetworkOutputBindingInfo(size_t subgraphId, |
| 58 | const std::string& name) const; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 59 | |
| 60 | /// Return the number of subgraphs in the parsed model |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 61 | size_t GetSubgraphCount() const; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 62 | |
| 63 | /// Return the input tensor names for a given subgraph |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 64 | std::vector<std::string> GetSubgraphInputTensorNames(size_t subgraphId) const; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 65 | |
| 66 | /// Return the output tensor names for a given subgraph |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 67 | std::vector<std::string> GetSubgraphOutputTensorNames(size_t subgraphId) const; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 68 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 69 | TfLiteParserImpl(const armnn::Optional<ITfLiteParser::TfLiteParserOptions>& options = armnn::EmptyOptional()); |
| 70 | ~TfLiteParserImpl() = default; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 71 | |
| 72 | public: |
| 73 | // testable helpers |
Finn Williams | b49ed18 | 2021-06-29 15:50:08 +0100 | [diff] [blame] | 74 | armnn::INetworkPtr CreateNetworkFromBinaryAsDynamic(const std::vector<uint8_t>& binaryContent); |
| 75 | |
| 76 | armnn::INetworkPtr LoadModel(std::unique_ptr<tflite::ModelT> model); |
| 77 | |
Teresa Charlin | 3ab8548 | 2021-06-08 16:59:29 +0100 | [diff] [blame] | 78 | static ModelPtr LoadModelFromFile(const char* fileName); |
| 79 | static ModelPtr LoadModelFromBinary(const uint8_t* binaryContent, size_t len); |
| 80 | static TensorRawPtrVector GetInputs(const ModelPtr& model, size_t subgraphIndex, size_t operatorIndex); |
| 81 | static TensorRawPtrVector GetOutputs(const ModelPtr& model, size_t subgraphIndex, size_t operatorIndex); |
| 82 | static TensorIdRawPtrVector GetSubgraphInputs(const ModelPtr& model, size_t subgraphIndex); |
| 83 | static TensorIdRawPtrVector GetSubgraphOutputs(const ModelPtr& model, size_t subgraphIndex); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 84 | static std::vector<int32_t>& GetInputTensorIds(const ModelPtr& model, size_t subgraphIndex, size_t operatorIndex); |
| 85 | static std::vector<int32_t>& GetOutputTensorIds(const ModelPtr& model, size_t subgraphIndex, size_t operatorIndex); |
| 86 | |
| 87 | static BufferRawPtr GetBuffer(const ModelPtr& model, size_t bufferIndex); |
Teresa Charlin | 3ab8548 | 2021-06-08 16:59:29 +0100 | [diff] [blame] | 88 | static armnn::TensorInfo OutputShapeOfSqueeze(std::vector<uint32_t> squeezeDims, |
| 89 | const armnn::TensorInfo& inputTensorInfo); |
| 90 | static armnn::TensorInfo OutputShapeOfReshape(const armnn::TensorInfo& inputTensorInfo, |
| 91 | const std::vector<int32_t>& targetDimsIn); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 92 | |
Matthew Sloyan | ac001ee | 2021-02-03 10:43:04 +0000 | [diff] [blame] | 93 | /// Retrieve version in X.Y.Z form |
| 94 | static const std::string GetVersion(); |
| 95 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 96 | private: |
Finn Williams | d4fa545 | 2021-03-01 12:31:41 +0000 | [diff] [blame] | 97 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 98 | // No copying allowed until it is wanted and properly implemented |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 99 | TfLiteParserImpl(const TfLiteParserImpl &) = delete; |
| 100 | TfLiteParserImpl & operator=(const TfLiteParserImpl &) = delete; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 101 | |
| 102 | /// Create the network from an already loaded flatbuffers model |
| 103 | armnn::INetworkPtr CreateNetworkFromModel(); |
| 104 | |
| 105 | // signature for the parser functions |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 106 | using OperatorParsingFunction = void(TfLiteParserImpl::*)(size_t subgraphIndex, size_t operatorIndex); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 107 | |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 108 | void ParseCustomOperator(size_t subgraphIndex, size_t operatorIndex); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 109 | void ParseUnsupportedOperator(size_t subgraphIndex, size_t operatorIndex); |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 110 | |
Matthew Sloyan | ed7fce4 | 2021-04-15 20:46:24 +0100 | [diff] [blame] | 111 | void ParseAbs(size_t subgraphIndex, size_t operatorIndex); |
Finn Williams | c42c384 | 2019-01-22 14:18:11 +0000 | [diff] [blame] | 112 | void ParseActivation(size_t subgraphIndex, size_t operatorIndex, armnn::ActivationFunction activationType); |
Nina Drozd | 200e380 | 2019-04-15 09:47:39 +0100 | [diff] [blame] | 113 | void ParseAdd(size_t subgraphIndex, size_t operatorIndex); |
Matthew Sloyan | 28f177c | 2021-04-09 14:38:52 +0100 | [diff] [blame] | 114 | void ParseArgMinMax(size_t subgraphIndex, size_t operatorIndex, armnn::ArgMinMaxFunction argMinMaxFunction); |
| 115 | void ParseArgMin(size_t subgraphIndex, size_t operatorIndex); |
| 116 | void ParseArgMax(size_t subgraphIndex, size_t operatorIndex); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 117 | void ParseAveragePool2D(size_t subgraphIndex, size_t operatorIndex); |
Samuel Yap | fd3ba5a | 2022-08-24 17:04:34 +0100 | [diff] [blame] | 118 | void ParseBatchMatMul(size_t subgraphIndex, size_t operatorIndex); |
Bruno Goncalves | db947e2 | 2019-02-08 18:52:21 -0200 | [diff] [blame] | 119 | void ParseBatchToSpaceND(size_t subgraphIndex, size_t operatorIndex); |
mathad01 | b392e98 | 2021-04-07 12:07:30 +0100 | [diff] [blame] | 120 | void ParseCast(size_t subgraphIndex, size_t operatorIndex); |
Teresa Charlin | 93f0ad0 | 2023-03-23 15:28:02 +0000 | [diff] [blame] | 121 | void ParseCeil(size_t subgraphIndex, size_t operatorIndex); |
Bruno Goncalves | 2d0eb86 | 2021-07-11 14:10:15 -0300 | [diff] [blame] | 122 | void ParseComparison(size_t subgraphIndex, size_t operatorIndex, armnn::ComparisonOperation comparisonOperation); |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 123 | void ParseConcatenation(size_t subgraphIndex, size_t operatorIndex); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 124 | void ParseConv2D(size_t subgraphIndex, size_t operatorIndex); |
Matthew Sloyan | 4d217c0 | 2021-10-07 11:48:58 +0100 | [diff] [blame] | 125 | // Conv3D support was added in TF 2.5, so for backwards compatibility a hash define is needed. |
Cathal Corbett | 80b4ef0 | 2022-05-25 11:21:11 +0100 | [diff] [blame] | 126 | #if defined(ARMNN_POST_TFLITE_2_4) |
Matthew Sloyan | eb5f810 | 2021-10-05 17:31:42 +0100 | [diff] [blame] | 127 | void ParseConv3D(size_t subgraphIndex, size_t operatorIndex); |
Matthew Sloyan | 4d217c0 | 2021-10-07 11:48:58 +0100 | [diff] [blame] | 128 | #endif |
Sadik Armagan | 2686849 | 2021-01-22 14:25:31 +0000 | [diff] [blame] | 129 | void ParseDepthToSpace(size_t subgraphIndex, size_t operatorIndex); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 130 | void ParseDepthwiseConv2D(size_t subgraphIndex, size_t operatorIndex); |
Finn Williams | ed66d14 | 2019-12-06 09:55:55 +0000 | [diff] [blame] | 131 | void ParseDequantize(size_t subgraphIndex, size_t operatorIndex); |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 132 | void ParseDetectionPostProcess(size_t subgraphIndex, size_t operatorIndex); |
Matthew Sloyan | 28f177c | 2021-04-09 14:38:52 +0100 | [diff] [blame] | 133 | void ParseDiv(size_t subgraphIndex, size_t operatorIndex); |
Matthew Sloyan | ed7fce4 | 2021-04-15 20:46:24 +0100 | [diff] [blame] | 134 | void ParseElementwiseUnary(size_t subgraphIndex, size_t operatorIndex, armnn::UnaryOperation unaryOperation); |
Matthew Sloyan | 7515d07 | 2020-12-16 12:50:01 +0000 | [diff] [blame] | 135 | void ParseElu(size_t subgraphIndex, size_t operatorIndex); |
Bruno Goncalves | 2d0eb86 | 2021-07-11 14:10:15 -0300 | [diff] [blame] | 136 | void ParseEqual(size_t subgraphIndex, size_t operatorIndex); |
Derek Lamberti | f017699 | 2020-04-28 13:37:49 +0100 | [diff] [blame] | 137 | void ParseExp(size_t subgraphIndex, size_t operatorIndex); |
Teresa Charlin | 3ab8548 | 2021-06-08 16:59:29 +0100 | [diff] [blame] | 138 | void ParseExpandDims(size_t subgraphIndex, size_t operatorIndex); |
Teresa Charlin | cdbd40b | 2022-02-25 13:21:55 +0000 | [diff] [blame] | 139 | void ParseFloorDiv(size_t subgraphIndex, size_t operatorIndex); |
Sadik Armagan | 8853c1f | 2018-10-22 09:04:18 +0100 | [diff] [blame] | 140 | void ParseFullyConnected(size_t subgraphIndex, size_t operatorIndex); |
Sadik Armagan | 2686849 | 2021-01-22 14:25:31 +0000 | [diff] [blame] | 141 | void ParseGather(size_t subgraphIndex, size_t operatorIndex); |
Teresa Charlin | 91a53ea | 2022-04-25 15:47:29 +0100 | [diff] [blame] | 142 | void ParseGatherNd(size_t subgraphIndex, size_t operatorIndex); |
Bruno Goncalves | 2d0eb86 | 2021-07-11 14:10:15 -0300 | [diff] [blame] | 143 | void ParseGreater(size_t subgraphIndex, size_t operatorIndex); |
| 144 | void ParseGreaterOrEqual(size_t subgraphIndex, size_t operatorIndex); |
Jan Eilers | 2f746b3 | 2020-07-28 14:00:06 +0100 | [diff] [blame] | 145 | void ParseHardSwish(size_t subgraphIndex, size_t operatorIndex); |
Sadik Armagan | 12239e7 | 2020-05-27 11:06:17 +0100 | [diff] [blame] | 146 | void ParseLeakyRelu(size_t subgraphIndex, size_t operatorIndex); |
Bruno Goncalves | 2d0eb86 | 2021-07-11 14:10:15 -0300 | [diff] [blame] | 147 | void ParseLess(size_t subgraphIndex, size_t operatorIndex); |
| 148 | void ParseLessOrEqual(size_t subgraphIndex, size_t operatorIndex); |
Teresa Charlin | 28aa669 | 2022-07-12 11:18:44 +0100 | [diff] [blame] | 149 | void ParseLog(size_t subgraphIndex, size_t operatorIndex); |
Mike Kelly | 31dce2b | 2021-09-01 21:22:37 +0100 | [diff] [blame] | 150 | void ParseLocalResponseNormalization(size_t subgraphIndex, size_t operatorIndex); |
Matthew Sloyan | ed7fce4 | 2021-04-15 20:46:24 +0100 | [diff] [blame] | 151 | void ParseLogicalNot(size_t subgraphIndex, size_t operatorIndex); |
Finn Williams | c42c384 | 2019-01-22 14:18:11 +0000 | [diff] [blame] | 152 | void ParseLogistic(size_t subgraphIndex, size_t operatorIndex); |
Teresa Charlin | fd33a69 | 2022-06-29 15:35:57 +0100 | [diff] [blame] | 153 | void ParseLogSoftmax(size_t subgraphIndex, size_t operatorIndex); |
Matthew Jackson | 28c9457 | 2019-07-18 10:47:03 +0100 | [diff] [blame] | 154 | void ParseL2Normalization(size_t subgraphIndex, size_t operatorIndex); |
Nattapat Chaimanowong | b66504b | 2018-10-17 15:19:14 +0100 | [diff] [blame] | 155 | void ParseMaxPool2D(size_t subgraphIndex, size_t operatorIndex); |
Bruno Goncalves | b8d805e | 2019-02-12 22:57:13 -0200 | [diff] [blame] | 156 | void ParseMaximum(size_t subgraphIndex, size_t operatorIndex); |
Nina Drozd | 200e380 | 2019-04-15 09:47:39 +0100 | [diff] [blame] | 157 | void ParseMean(size_t subgraphIndex, size_t operatorIndex); |
Bruno Goncalves | 8f6d7a7 | 2019-02-12 22:58:18 -0200 | [diff] [blame] | 158 | void ParseMinimum(size_t subgraphIndex, size_t operatorIndex); |
Matthew Sloyan | af3a4ef | 2021-10-22 15:48:12 +0100 | [diff] [blame] | 159 | void ParseMirrorPad(size_t subgraphIndex, size_t operatorIndex); |
Nina Drozd | 200e380 | 2019-04-15 09:47:39 +0100 | [diff] [blame] | 160 | void ParseMul(size_t subgraphIndex, size_t operatorIndex); |
Darshan Patel | 83fcf98 | 2020-05-26 22:22:42 +0530 | [diff] [blame] | 161 | void ParseNeg(size_t subgraphIndex, size_t operatorIndex); |
Bruno Goncalves | 2d0eb86 | 2021-07-11 14:10:15 -0300 | [diff] [blame] | 162 | void ParseNotEqual(size_t subgraphIndex, size_t operatorIndex); |
Matthew Jackson | bcca1f4 | 2019-07-16 11:39:21 +0100 | [diff] [blame] | 163 | void ParsePack(size_t subgraphIndex, size_t operatorIndex); |
Nina Drozd | 200e380 | 2019-04-15 09:47:39 +0100 | [diff] [blame] | 164 | void ParsePad(size_t subgraphIndex, size_t operatorIndex); |
| 165 | void ParsePool(size_t subgraphIndex, size_t operatorIndex, armnn::PoolingAlgorithm algorithm); |
John Mcloughlin | 0ec0087 | 2023-05-15 17:03:49 +0100 | [diff] [blame] | 166 | void ParsePower(size_t subgraphIndex, size_t operatorIndex); |
Narumol Prangnawarat | bfaee6b | 2021-05-24 18:50:24 +0100 | [diff] [blame] | 167 | void ParsePrelu(size_t subgraphIndex, size_t operatorIndex); |
Sadik Armagan | 66dedc7 | 2019-12-10 16:32:07 +0000 | [diff] [blame] | 168 | void ParseQuantize(size_t subgraphIndex, size_t operatorIndex); |
Sadik Armagan | a274748 | 2021-02-09 10:28:54 +0000 | [diff] [blame] | 169 | void ParseReduce(size_t subgraphIndex, size_t operatorIndex, armnn::ReduceOperation reduceOperation); |
| 170 | void ParseReduceMax(size_t subgraphIndex, size_t operatorIndex); |
| 171 | void ParseReduceMin(size_t subgraphIndex, size_t operatorIndex); |
Teresa Charlin | 4e3e831 | 2021-08-05 12:34:37 +0100 | [diff] [blame] | 172 | void ParseReduceProd(size_t subgraphIndex, size_t operatorIndex); |
Sadik Armagan | 58f3919 | 2018-09-17 14:14:39 +0100 | [diff] [blame] | 173 | void ParseRelu(size_t subgraphIndex, size_t operatorIndex); |
| 174 | void ParseRelu6(size_t subgraphIndex, size_t operatorIndex); |
Sadik | b94967b | 2018-09-19 15:30:00 +0100 | [diff] [blame] | 175 | void ParseReshape(size_t subgraphIndex, size_t operatorIndex); |
Sadik Armagan | a3b31f0 | 2019-12-05 09:08:53 +0000 | [diff] [blame] | 176 | void ParseResize(size_t subgraphIndex, size_t operatorIndex, armnn::ResizeMethod resizeMethod); |
Bruno Goncalves | 3f58ddb | 2019-02-07 18:40:11 -0200 | [diff] [blame] | 177 | void ParseResizeBilinear(size_t subgraphIndex, size_t operatorIndex); |
Sadik Armagan | a3b31f0 | 2019-12-05 09:08:53 +0000 | [diff] [blame] | 178 | void ParseResizeNearestNeighbor(size_t subgraphIndex, size_t operatorIndex); |
Tianle Cheng | e5a30ff | 2023-07-03 11:24:12 +0100 | [diff] [blame] | 179 | void ParseReverseV2(size_t subgraphIndex, size_t operatorIndex); |
Matthew Sloyan | ed7fce4 | 2021-04-15 20:46:24 +0100 | [diff] [blame] | 180 | void ParseRsqrt(size_t subgraphIndex, size_t operatorIndex); |
Keith Davis | 0176fd8 | 2021-06-01 17:36:32 +0100 | [diff] [blame] | 181 | void ParseShape(size_t subgraphIndex, size_t operatorIndex); |
Teresa Charlin | 28aa669 | 2022-07-12 11:18:44 +0100 | [diff] [blame] | 182 | void ParseSin(size_t subgraphIndex, size_t operatorIndex); |
josh minor | ba424d2 | 2019-11-13 10:55:17 -0600 | [diff] [blame] | 183 | void ParseSlice(size_t subgraphIndex, size_t operatorIndex); |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 184 | void ParseSoftmax(size_t subgraphIndex, size_t operatorIndex); |
Teresa Charlin | f0fce5b | 2022-05-04 17:24:43 +0100 | [diff] [blame] | 185 | void ParseSqrt(size_t subgraphIndex, size_t operatorIndex); |
Bruno Goncalves | baded14 | 2019-02-08 19:02:48 -0200 | [diff] [blame] | 186 | void ParseSpaceToBatchND(size_t subgraphIndex, size_t operatorIndex); |
Teresa Charlin | 2a764ad | 2023-02-24 18:17:31 +0000 | [diff] [blame] | 187 | void ParseSpaceToDepth(size_t subgraphIndex, size_t operatorIndex); |
Nina Drozd | 200e380 | 2019-04-15 09:47:39 +0100 | [diff] [blame] | 188 | void ParseSplit(size_t subgraphIndex, size_t operatorIndex); |
Derek Lamberti | f017699 | 2020-04-28 13:37:49 +0100 | [diff] [blame] | 189 | void ParseSplitV(size_t subgraphIndex, size_t operatorIndex); |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 190 | void ParseSqueeze(size_t subgraphIndex, size_t operatorIndex); |
John Mcloughlin | 0ec0087 | 2023-05-15 17:03:49 +0100 | [diff] [blame] | 191 | void ParseSquaredDifference(size_t subgraphIndex, size_t operatorIndex); |
Bruno Goncalves | 451d95b | 2019-02-12 22:59:22 -0200 | [diff] [blame] | 192 | void ParseStridedSlice(size_t subgraphIndex, size_t operatorIndex); |
Bruno Goncalves | bbeae26 | 2019-02-07 18:37:39 -0200 | [diff] [blame] | 193 | void ParseSub(size_t subgraphIndex, size_t operatorIndex); |
Sadik Armagan | 0c3ea5b | 2021-02-03 09:29:30 +0000 | [diff] [blame] | 194 | void ParseSum(size_t subgraphIndex, size_t operatorIndex); |
Nina Drozd | 9985176 | 2019-04-09 09:37:38 +0100 | [diff] [blame] | 195 | void ParseTanH(size_t subgraphIndex, size_t operatorIndex); |
Keith Davis | 4cd29a0 | 2019-09-09 14:49:20 +0100 | [diff] [blame] | 196 | void ParseTranspose(size_t subgraphIndex, size_t operatorIndex); |
Matthew Jackson | 74bf7da | 2019-08-16 16:51:42 +0100 | [diff] [blame] | 197 | void ParseTransposeConv(size_t subgraphIndex, size_t operatorIndex); |
Mike Kelly | 5880b91 | 2022-01-28 16:18:54 +0000 | [diff] [blame] | 198 | void ParseUnidirectionalSequenceLSTM(size_t subgraphIndex, size_t operatorIndex); |
Nina Drozd | 200e380 | 2019-04-15 09:47:39 +0100 | [diff] [blame] | 199 | void ParseUnpack(size_t subgraphIndex, size_t operatorIndex); |
Nattapat Chaimanowong | b66504b | 2018-10-17 15:19:14 +0100 | [diff] [blame] | 200 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 201 | void RegisterProducerOfTensor(size_t subgraphIndex, size_t tensorIndex, armnn::IOutputSlot* slot); |
| 202 | void RegisterConsumerOfTensor(size_t subgraphIndex, size_t tensorIndex, armnn::IInputSlot* slot); |
| 203 | void RegisterInputSlots(size_t subgraphIndex, |
| 204 | size_t operatorIndex, |
| 205 | armnn::IConnectableLayer* layer, |
Finn Williams | d4fa545 | 2021-03-01 12:31:41 +0000 | [diff] [blame] | 206 | const std::vector<unsigned int>& tensorIndexes, |
| 207 | unsigned int startingSlotIndex = 0); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 208 | void RegisterOutputSlots(size_t subgraphIndex, |
| 209 | size_t operatorIndex, |
| 210 | armnn::IConnectableLayer* layer, |
| 211 | const std::vector<unsigned int>& tensorIndexes); |
| 212 | |
Mike Kelly | 377fb21 | 2023-01-10 15:55:28 +0000 | [diff] [blame] | 213 | void SetupInputLayerTensorInfos(size_t subgraphIndex); |
| 214 | void SetupConstantLayerTensorInfos(size_t subgraphIndex); |
| 215 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 216 | void SetupInputLayers(size_t subgraphIndex); |
| 217 | void SetupOutputLayers(size_t subgraphIndex); |
Bruno Goncalves | 3d7efe9 | 2018-12-27 14:21:43 -0200 | [diff] [blame] | 218 | void SetupConstantLayers(size_t subgraphIndex); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 219 | |
| 220 | void ResetParser(); |
| 221 | |
Bruno Goncalves | 9c761a6 | 2018-12-27 14:20:35 -0200 | [diff] [blame] | 222 | void AddBroadcastReshapeLayer(size_t subgraphIndex, |
| 223 | size_t operatorIndex, |
| 224 | armnn::IConnectableLayer* layer); |
| 225 | |
Mike Kelly | 04d8229 | 2023-01-19 18:29:40 +0000 | [diff] [blame] | 226 | /// Attach an reshape layer to the one passed as a parameter |
| 227 | armnn::IConnectableLayer* AddReshapeLayer(armnn::IConnectableLayer* layer, |
| 228 | unsigned int outputSlot, |
| 229 | std::string reshapeLayerName, |
| 230 | armnn::TensorInfo outputShape); |
| 231 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 232 | /// Attach an activation layer to the one passed as a parameter |
Sadik Armagan | 58f3919 | 2018-09-17 14:14:39 +0100 | [diff] [blame] | 233 | armnn::IConnectableLayer* AddFusedActivationLayer(armnn::IConnectableLayer* layer, |
| 234 | unsigned int outputSlot, |
| 235 | tflite::ActivationFunctionType activationType); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 236 | |
Teresa Charlin | cdbd40b | 2022-02-25 13:21:55 +0000 | [diff] [blame] | 237 | /// Attach a floor layer to the one passed as a parameter |
| 238 | armnn::IConnectableLayer* AddFusedFloorLayer(armnn::IConnectableLayer* layer, unsigned int outputSlot); |
| 239 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 240 | // SupportedDataStorage's purpose is to hold data till we pass over to the network. |
| 241 | // We don't care about the content, and we want a single datatype to simplify the code. |
| 242 | struct SupportedDataStorage |
| 243 | { |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 244 | public: |
| 245 | // Convenience constructors |
| 246 | SupportedDataStorage(std::unique_ptr<float[]>&& data); |
| 247 | SupportedDataStorage(std::unique_ptr<uint8_t[]>&& data); |
Keith Davis | d305e1a | 2020-01-22 11:57:54 +0000 | [diff] [blame] | 248 | SupportedDataStorage(std::unique_ptr<int8_t[]>&& data); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 249 | SupportedDataStorage(std::unique_ptr<int32_t[]>&& data); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 250 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 251 | private: |
| 252 | // Pointers to the data buffers |
| 253 | std::unique_ptr<float[]> m_FloatData; |
| 254 | std::unique_ptr<uint8_t[]> m_Uint8Data; |
Keith Davis | d305e1a | 2020-01-22 11:57:54 +0000 | [diff] [blame] | 255 | std::unique_ptr<int8_t[]> m_Int8Data; |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 256 | std::unique_ptr<int32_t[]> m_Int32Data; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 257 | }; |
| 258 | |
Mike Kelly | 5880b91 | 2022-01-28 16:18:54 +0000 | [diff] [blame] | 259 | bool ShouldConstantTensorBeCreated(unsigned int tensorIndex); |
Mike Kelly | 0506ef0 | 2023-01-03 16:29:44 +0000 | [diff] [blame] | 260 | |
Finn Williams | d4fa545 | 2021-03-01 12:31:41 +0000 | [diff] [blame] | 261 | bool IsConstTensor(TensorRawPtr tensorPtr); |
Mike Kelly | 0506ef0 | 2023-01-03 16:29:44 +0000 | [diff] [blame] | 262 | |
| 263 | bool ShouldConstantTensorBeConverted(TfLiteParserImpl::TensorRawPtr tensorPtr, |
| 264 | armnn::DataType inputDataType, |
| 265 | armnn::DataType filterDataType); |
| 266 | |
Finn Williams | d4fa545 | 2021-03-01 12:31:41 +0000 | [diff] [blame] | 267 | armnn::ConstTensor CreateConstTensorNonPermuted(TensorRawPtr tensorPtr, |
| 268 | armnn::TensorInfo& tensorInfo); |
Mike Kelly | 5880b91 | 2022-01-28 16:18:54 +0000 | [diff] [blame] | 269 | |
Finn Williams | d4fa545 | 2021-03-01 12:31:41 +0000 | [diff] [blame] | 270 | std::pair<armnn::ConstTensor, SupportedDataStorage> |
| 271 | CreateConstTensorPermuted(TensorRawPtr tensorPtr, |
| 272 | armnn::TensorInfo& tensorInfo, |
| 273 | armnn::Optional<armnn::PermutationVector&> permutationVector); |
Mike Kelly | 0506ef0 | 2023-01-03 16:29:44 +0000 | [diff] [blame] | 274 | |
Mike Kelly | 5880b91 | 2022-01-28 16:18:54 +0000 | [diff] [blame] | 275 | std::pair<armnn::ConstTensor, std::unique_ptr<float[]>> |
| 276 | CreateConstTensorNonPermuted(TensorRawPtr tensorPtr, |
| 277 | armnn::TensorInfo& tensorInfo, |
| 278 | armnn::DataType inputDataType); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 279 | |
| 280 | template<typename T> |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 281 | std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage> |
| 282 | CreateConstTensorAndStoreData(TfLiteParserImpl::BufferRawPtr bufferPtr, |
| 283 | TfLiteParserImpl::TensorRawPtr tensorPtr, |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 284 | armnn::TensorInfo& tensorInfo, |
| 285 | armnn::Optional<armnn::PermutationVector&> permutationVector); |
Mike Kelly | 0506ef0 | 2023-01-03 16:29:44 +0000 | [diff] [blame] | 286 | |
Mike Kelly | 5880b91 | 2022-01-28 16:18:54 +0000 | [diff] [blame] | 287 | std::pair<armnn::ConstTensor*, std::unique_ptr<float[]>> |
| 288 | CreateConstTensorPtr(TensorRawPtr tensorPtr, |
| 289 | armnn::TensorInfo& inputTensorInfo); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 290 | |
Mike Kelly | 377fb21 | 2023-01-10 15:55:28 +0000 | [diff] [blame] | 291 | armnn::TensorInfo InputTensorInfo(size_t subgraphIndex, |
| 292 | size_t operatorIndex, |
| 293 | int input); |
| 294 | |
| 295 | armnn::TensorInfo OutputTensorInfoFromInputs(size_t subgraphIndex, |
| 296 | size_t operatorIndex, |
| 297 | armnn::IConnectableLayer* layer, |
| 298 | int output, |
| 299 | std::vector<int> inputs); |
| 300 | |
| 301 | armnn::TensorInfo OutputTensorInfoFromShapes(size_t subgraphIndex, |
| 302 | size_t operatorIndex, |
| 303 | armnn::IConnectableLayer* layer, |
| 304 | int output = 0, |
| 305 | std::vector<armnn::TensorShape> inputShapes = {}); |
| 306 | |
| 307 | /// Settings for configuring the TfLiteParser |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 308 | armnn::Optional<ITfLiteParser::TfLiteParserOptions> m_Options; |
| 309 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 310 | /// The network we're building. Gets cleared after it is passed to the user |
| 311 | armnn::INetworkPtr m_Network; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 312 | ModelPtr m_Model; |
| 313 | |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 314 | std::vector<OperatorParsingFunction> m_ParserFunctions; |
| 315 | std::unordered_map<std::string, OperatorParsingFunction> m_CustomParserFunctions; |
| 316 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 317 | /// A mapping of an output slot to each of the input slots it should be connected to |
| 318 | /// The outputSlot is from the layer that creates this tensor as one of its ouputs |
| 319 | /// The inputSlots are from the layers that use this tensor as one of their inputs |
| 320 | struct TensorSlots |
| 321 | { |
| 322 | armnn::IOutputSlot* outputSlot; |
| 323 | std::vector<armnn::IInputSlot*> inputSlots; |
| 324 | |
| 325 | TensorSlots() : outputSlot(nullptr) { } |
| 326 | }; |
| 327 | typedef std::vector<TensorSlots> TensorConnections; |
| 328 | /// Connections for tensors in each subgraph |
| 329 | /// The first index is the subgraph ID, the second index is the tensor ID |
| 330 | std::vector<TensorConnections> m_SubgraphConnections; |
Narumol Prangnawarat | 4628d05 | 2019-02-25 17:26:05 +0000 | [diff] [blame] | 331 | |
Mike Kelly | 5880b91 | 2022-01-28 16:18:54 +0000 | [diff] [blame] | 332 | /// This is used in case that the model does not specify the output. |
Narumol Prangnawarat | 4628d05 | 2019-02-25 17:26:05 +0000 | [diff] [blame] | 333 | /// The shape can be calculated from the options. |
Mike Kelly | 377fb21 | 2023-01-10 15:55:28 +0000 | [diff] [blame] | 334 | std::vector<std::vector<unsigned int>> m_OverriddenOutputShapes; |
Mike Kelly | 5880b91 | 2022-01-28 16:18:54 +0000 | [diff] [blame] | 335 | |
| 336 | std::vector<unsigned int> m_ConstantsToDequantize; |
| 337 | std::vector<unsigned int> m_ConstantsToBeCreated; |
Mike Kelly | 377fb21 | 2023-01-10 15:55:28 +0000 | [diff] [blame] | 338 | std::map<size_t, armnn::TensorInfo> m_TensorInfos; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 339 | }; |
| 340 | |
| 341 | } |