From dd23f2af4ee4f41f34499ada9c6cd28c9479c6d3 Mon Sep 17 00:00:00 2001 From: Freddie Liardet Date: Thu, 17 Jun 2021 13:30:11 +0100 Subject: Add LayerData to all nodes Create LayerData type to store information about inputs/outputs of nodes, also adds convolution specific information. Resolves: COMPMID-4422 Signed-off-by: Freddie Liardet Change-Id: I1c3be1abe2fb5ed085108ab3d34b14a1b8561d38 Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/5876 Reviewed-by: Georgios Pinitas Tested-by: Arm Jenkins Comments-Addressed: Arm Jenkins --- arm_compute/graph/DataLayerVisitor.h | 61 ++++++++++ arm_compute/graph/INodeVisitor.h | 144 +++++------------------- arm_compute/graph/printers/DotGraphPrinter.h | 4 +- src/graph/DataLayerVisitor.cpp | 160 +++++++++++++++++++++++++++ src/graph/INodeVisitor.cpp | 146 ++++++++++++++++++++++++ src/graph/printers/DotGraphPrinter.cpp | 5 +- utils/TypePrinter.h | 2 +- 7 files changed, 403 insertions(+), 119 deletions(-) create mode 100644 arm_compute/graph/DataLayerVisitor.h create mode 100644 src/graph/DataLayerVisitor.cpp create mode 100644 src/graph/INodeVisitor.cpp diff --git a/arm_compute/graph/DataLayerVisitor.h b/arm_compute/graph/DataLayerVisitor.h new file mode 100644 index 0000000000..670b9f02b6 --- /dev/null +++ b/arm_compute/graph/DataLayerVisitor.h @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2021 Arm Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef ARM_COMPUTE_GRAPH_DATALAYERPRINTER_H +#define ARM_COMPUTE_GRAPH_DATALAYERPRINTER_H + +#include "arm_compute/graph/IGraphPrinter.h" +#include "arm_compute/graph/INodeVisitor.h" +#include "arm_compute/graph/Types.h" + +namespace arm_compute +{ +namespace graph +{ +/** Graph printer visitor. */ +class DataLayerVisitor final : public DefaultNodeVisitor +{ +public: + using LayerData = std::map; + /** Default Constructor **/ + DataLayerVisitor() = default; + + const LayerData &layer_data() const; + + // Reveal Parent method + using DefaultNodeVisitor::visit; + // Inherited methods overridden + void visit(ConvolutionLayerNode &n) override; + void visit(DepthwiseConvolutionLayerNode &n) override; + void visit(FusedConvolutionBatchNormalizationNode &n) override; + void visit(FusedDepthwiseConvolutionBatchNormalizationNode &n) override; + void visit(OutputNode &n) override; + + void default_visit(INode &n) override; + +private: + LayerData _layer_data{}; +}; +} // namespace graph +} // namespace arm_compute +#endif /* ARM_COMPUTE_GRAPH_DATALAYERPRINTER_H */ diff --git a/arm_compute/graph/INodeVisitor.h b/arm_compute/graph/INodeVisitor.h index 338de7a0ba..85da59022b 100644 --- a/arm_compute/graph/INodeVisitor.h +++ b/arm_compute/graph/INodeVisitor.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020 Arm Limited. + * Copyright (c) 2018-2021 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -191,124 +191,40 @@ public: #ifndef DOXYGEN_SKIP_THIS // Inherited methods overridden - virtual void visit(INode &) override - { - default_visit(); - } - virtual void visit(ActivationLayerNode &) override - { - default_visit(); - } - virtual void visit(BatchNormalizationLayerNode &) override - { - default_visit(); - } - virtual void visit(ConcatenateLayerNode &) override - { - default_visit(); - } - virtual void visit(ConstNode &) override - { - default_visit(); - } - virtual void visit(ConvolutionLayerNode &) override - { - default_visit(); - } - virtual void visit(DequantizationLayerNode &) override - { - default_visit(); - } - virtual void visit(DetectionOutputLayerNode &) override - { - default_visit(); - } - virtual void visit(DetectionPostProcessLayerNode &) override - { - default_visit(); - } - virtual void visit(DepthwiseConvolutionLayerNode &) override - { - default_visit(); - } - virtual void visit(EltwiseLayerNode &) override - { - default_visit(); - } - virtual void visit(FlattenLayerNode &) override - { - default_visit(); - } - virtual void visit(FullyConnectedLayerNode &) override - { - default_visit(); - } - virtual void visit(FusedConvolutionBatchNormalizationNode &) override - { - default_visit(); - } - virtual void visit(FusedDepthwiseConvolutionBatchNormalizationNode &) override - { - default_visit(); - } - virtual void visit(InputNode &) override - { - default_visit(); - } - virtual void visit(NormalizationLayerNode &) override - { - default_visit(); - } - virtual void visit(OutputNode &) override - { - default_visit(); - } - virtual void visit(PermuteLayerNode &) override - { - default_visit(); - } - virtual void visit(PoolingLayerNode &) override - { - default_visit(); - } - virtual void visit(PReluLayerNode &) override - { - default_visit(); - } - virtual void visit(PrintLayerNode &) override - { - default_visit(); - } - virtual void visit(PriorBoxLayerNode &) override - { - default_visit(); - } - virtual void visit(QuantizationLayerNode &) override - { - default_visit(); - } - virtual void visit(ReshapeLayerNode &) override - { - default_visit(); - } - virtual void visit(SoftmaxLayerNode &) override - { - default_visit(); - } - virtual void visit(SplitLayerNode &) override - { - default_visit(); - } - virtual void visit(StackLayerNode &) override - { - default_visit(); - } + virtual void visit(INode &n) override; + virtual void visit(ActivationLayerNode &n) override; + virtual void visit(BatchNormalizationLayerNode &n) override; + virtual void visit(ConcatenateLayerNode &n) override; + virtual void visit(ConstNode &n) override; + virtual void visit(ConvolutionLayerNode &n) override; + virtual void visit(DequantizationLayerNode &n) override; + virtual void visit(DetectionOutputLayerNode &n) override; + virtual void visit(DetectionPostProcessLayerNode &n) override; + virtual void visit(DepthwiseConvolutionLayerNode &n) override; + virtual void visit(EltwiseLayerNode &n) override; + virtual void visit(FlattenLayerNode &n) override; + virtual void visit(FullyConnectedLayerNode &n) override; + virtual void visit(FusedConvolutionBatchNormalizationNode &n) override; + virtual void visit(FusedDepthwiseConvolutionBatchNormalizationNode &n) override; + virtual void visit(InputNode &n) override; + virtual void visit(NormalizationLayerNode &n) override; + virtual void visit(OutputNode &n) override; + virtual void visit(PermuteLayerNode &n) override; + virtual void visit(PoolingLayerNode &n) override; + virtual void visit(PReluLayerNode &n) override; + virtual void visit(PrintLayerNode &n) override; + virtual void visit(PriorBoxLayerNode &n) override; + virtual void visit(QuantizationLayerNode &n) override; + virtual void visit(ReshapeLayerNode &n) override; + virtual void visit(SoftmaxLayerNode &n) override; + virtual void visit(SplitLayerNode &n) override; + virtual void visit(StackLayerNode &n) override; #endif /* DOXYGEN_SKIP_THIS */ /** Function to be overloaded by the client and implement default behavior for the * non-overloaded visitors */ - virtual void default_visit() = 0; + virtual void default_visit(INode &n) = 0; }; } // namespace graph } // namespace arm_compute diff --git a/arm_compute/graph/printers/DotGraphPrinter.h b/arm_compute/graph/printers/DotGraphPrinter.h index d39ddad98c..3c9de8cba9 100644 --- a/arm_compute/graph/printers/DotGraphPrinter.h +++ b/arm_compute/graph/printers/DotGraphPrinter.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2019 Arm Limited. + * Copyright (c) 2018-2019,2021 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -60,7 +60,7 @@ public: void visit(FusedDepthwiseConvolutionBatchNormalizationNode &n) override; void visit(NormalizationLayerNode &n) override; void visit(PoolingLayerNode &n) override; - void default_visit() override; + void default_visit(INode &n) override; private: std::string _info{}; diff --git a/src/graph/DataLayerVisitor.cpp b/src/graph/DataLayerVisitor.cpp new file mode 100644 index 0000000000..81bb8e523f --- /dev/null +++ b/src/graph/DataLayerVisitor.cpp @@ -0,0 +1,160 @@ +/* + * Copyright (c) 2021 Arm Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "arm_compute/graph/DataLayerVisitor.h" + +#include "arm_compute/core/Error.h" +#include "arm_compute/graph/Graph.h" +#include "arm_compute/graph/TypePrinter.h" +#include "arm_compute/graph/nodes/Nodes.h" + +namespace arm_compute +{ +namespace graph +{ +namespace +{ +template +void add_convolution_layer_data(DataLayerVisitor::LayerData &layer_data, T &node) +{ + PadStrideInfo ps_info = node.convolution_info(); + DataLayout layout = node.output(0)->desc().layout; + // Add data layout + layer_data["data_layout"] = to_string(layout); + // Add padding info + std::ostringstream padding; + padding << "[" << to_string(ps_info.pad_left()) << "," + << to_string(ps_info.pad_top()) << "," + << to_string(ps_info.pad_bottom()) << "," + << to_string(ps_info.pad_right()) << "]"; + + layer_data["pad"] = padding.str(); + + // Add stride info + std::ostringstream stride; + stride << "[" << to_string(ps_info.stride().first) << "," + << to_string(ps_info.stride().second) << "]"; + + layer_data["stride"] = stride.str(); + + // Add dilation info + // graph api does not support dilation > 1 + layer_data["dilation"] = "[1,1]"; + + // Add bias enabled? + // Assumes three inputs (input, weights, bias) + std::string bias_enabled = node.input(2) == nullptr ? "0" : "1"; + layer_data["bias_enabled"] = bias_enabled; + + // Change input names for weights / bias (if applicable) + // Assumes input(1) is weights and input(2) is bias + if(layer_data.count("input_shape1")) + { + layer_data["weights_shape"] = layer_data["input_shape1"]; + layer_data.erase("input_shape1"); + } + if(layer_data.count("input_shape2")) + { + layer_data["bias_shape"] = layer_data["input_shape2"]; + layer_data.erase("input_shape2"); + } +} + +template +void add_convolution_layer_method(DataLayerVisitor::LayerData &layer_data, T &node) +{ + std::ostringstream method; + method << node.convolution_method(); + layer_data["convolution_method"] = method.str(); +} + +template +void add_generic_layer_data(DataLayerVisitor::LayerData &layer_data, T &node) +{ + // Add layer name + layer_data["layer_name"] = node.name(); + // Loop over each input tensor + for(size_t tensor_no = 0; tensor_no < node.num_inputs(); ++tensor_no) + { + // Add input tensor shapes + if(node.input(tensor_no) != nullptr) + { + layer_data["input_shape" + to_string(tensor_no)] = "[" + to_string(node.input(tensor_no)->desc().shape) + "]"; + } + } + // Add output tensor shape + if(node.output(0) != nullptr) + { + layer_data["output_shape0"] = "[" + to_string(node.output(0)->desc().shape) + "]"; + } +} +} // namespace + +void DataLayerVisitor::visit(ConvolutionLayerNode &n) +{ + _layer_data.clear(); + add_generic_layer_data(_layer_data, n); + add_convolution_layer_data(_layer_data, n); + add_convolution_layer_method(_layer_data, n); +} + +void DataLayerVisitor::visit(DepthwiseConvolutionLayerNode &n) +{ + _layer_data.clear(); + add_generic_layer_data(_layer_data, n); + add_convolution_layer_data(_layer_data, n); +} + +void DataLayerVisitor::visit(FusedConvolutionBatchNormalizationNode &n) +{ + _layer_data.clear(); + add_generic_layer_data(_layer_data, n); + add_convolution_layer_data(_layer_data, n); + add_convolution_layer_method(_layer_data, n); +} + +void DataLayerVisitor::visit(FusedDepthwiseConvolutionBatchNormalizationNode &n) +{ + _layer_data.clear(); + add_generic_layer_data(_layer_data, n); + add_convolution_layer_data(_layer_data, n); +} + +void DataLayerVisitor::visit(OutputNode &n) +{ + _layer_data.clear(); + ARM_COMPUTE_UNUSED(n); +} + +void DataLayerVisitor::default_visit(INode &n) +{ + _layer_data.clear(); + add_generic_layer_data(_layer_data, n); +} + +const DataLayerVisitor::LayerData &DataLayerVisitor::layer_data() const +{ + return _layer_data; +} +} // namespace graph +} // namespace arm_compute diff --git a/src/graph/INodeVisitor.cpp b/src/graph/INodeVisitor.cpp new file mode 100644 index 0000000000..2bbf519cf5 --- /dev/null +++ b/src/graph/INodeVisitor.cpp @@ -0,0 +1,146 @@ +/* + * Copyright (c) 2021 Arm Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "arm_compute/graph/INodeVisitor.h" +#include "arm_compute/graph/nodes/Nodes.h" + +namespace arm_compute +{ +namespace graph +{ +#ifndef DOXYGEN_SKIP_THIS +void DefaultNodeVisitor::visit(INode &n) +{ + default_visit(n); +} +void DefaultNodeVisitor::visit(ActivationLayerNode &n) +{ + default_visit(n); +} +void DefaultNodeVisitor::visit(BatchNormalizationLayerNode &n) +{ + default_visit(n); +} +void DefaultNodeVisitor::visit(ConcatenateLayerNode &n) +{ + default_visit(n); +} +void DefaultNodeVisitor::visit(ConstNode &n) +{ + default_visit(n); +} +void DefaultNodeVisitor::visit(ConvolutionLayerNode &n) +{ + default_visit(n); +} +void DefaultNodeVisitor::visit(DequantizationLayerNode &n) +{ + default_visit(n); +} +void DefaultNodeVisitor::visit(DetectionOutputLayerNode &n) +{ + default_visit(n); +} +void DefaultNodeVisitor::visit(DetectionPostProcessLayerNode &n) +{ + default_visit(n); +} +void DefaultNodeVisitor::visit(DepthwiseConvolutionLayerNode &n) +{ + default_visit(n); +} +void DefaultNodeVisitor::visit(EltwiseLayerNode &n) +{ + default_visit(n); +} +void DefaultNodeVisitor::visit(FlattenLayerNode &n) +{ + default_visit(n); +} +void DefaultNodeVisitor::visit(FullyConnectedLayerNode &n) +{ + default_visit(n); +} +void DefaultNodeVisitor::visit(FusedConvolutionBatchNormalizationNode &n) +{ + default_visit(n); +} +void DefaultNodeVisitor::visit(FusedDepthwiseConvolutionBatchNormalizationNode &n) +{ + default_visit(n); +} +void DefaultNodeVisitor::visit(InputNode &n) +{ + default_visit(n); +} +void DefaultNodeVisitor::visit(NormalizationLayerNode &n) +{ + default_visit(n); +} +void DefaultNodeVisitor::visit(OutputNode &n) +{ + default_visit(n); +} +void DefaultNodeVisitor::visit(PermuteLayerNode &n) +{ + default_visit(n); +} +void DefaultNodeVisitor::visit(PoolingLayerNode &n) +{ + default_visit(n); +} +void DefaultNodeVisitor::visit(PReluLayerNode &n) +{ + default_visit(n); +} +void DefaultNodeVisitor::visit(PrintLayerNode &n) +{ + default_visit(n); +} +void DefaultNodeVisitor::visit(PriorBoxLayerNode &n) +{ + default_visit(n); +} +void DefaultNodeVisitor::visit(QuantizationLayerNode &n) +{ + default_visit(n); +} +void DefaultNodeVisitor::visit(ReshapeLayerNode &n) +{ + default_visit(n); +} +void DefaultNodeVisitor::visit(SoftmaxLayerNode &n) +{ + default_visit(n); +} +void DefaultNodeVisitor::visit(SplitLayerNode &n) +{ + default_visit(n); +} +void DefaultNodeVisitor::visit(StackLayerNode &n) +{ + default_visit(n); +} +#endif /* DOXYGEN_SKIP_THIS */ +} // namespace graph +} // namespace arm_compute \ No newline at end of file diff --git a/src/graph/printers/DotGraphPrinter.cpp b/src/graph/printers/DotGraphPrinter.cpp index 2e1e9d0951..08f7f25ee5 100644 --- a/src/graph/printers/DotGraphPrinter.cpp +++ b/src/graph/printers/DotGraphPrinter.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020 Arm Limited. + * Copyright (c) 2018-2021 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -111,8 +111,9 @@ void DotGraphVisitor::visit(PoolingLayerNode &n) _info = ss.str(); } -void DotGraphVisitor::default_visit() +void DotGraphVisitor::default_visit(INode &n) { + ARM_COMPUTE_UNUSED(n); _info.clear(); } diff --git a/utils/TypePrinter.h b/utils/TypePrinter.h index e30bdc4157..3ccba2c7e9 100644 --- a/utils/TypePrinter.h +++ b/utils/TypePrinter.h @@ -77,7 +77,7 @@ inline ::std::ostream &operator<<(::std::ostream &os, const Dimensions &dimen for(unsigned int d = 1; d < dimensions.num_dimensions(); ++d) { - os << "x" << dimensions[d]; + os << "," << dimensions[d]; } } -- cgit v1.2.1