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 --- src/graph/DataLayerVisitor.cpp | 160 +++++++++++++++++++++++++++++++++ src/graph/INodeVisitor.cpp | 146 ++++++++++++++++++++++++++++++ src/graph/printers/DotGraphPrinter.cpp | 5 +- 3 files changed, 309 insertions(+), 2 deletions(-) create mode 100644 src/graph/DataLayerVisitor.cpp create mode 100644 src/graph/INodeVisitor.cpp (limited to 'src') 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(); } -- cgit v1.2.1