From e8f05da5fb919aa209e1bf0e5c70dd15fff84b7f Mon Sep 17 00:00:00 2001 From: thecha01 Date: Mon, 24 Aug 2020 17:21:41 +0100 Subject: Add ArgMinMax layer node to Graph API Change-Id: I2ccb2c65edd2932b76e905af3d747324b65c2f7f Signed-off-by: thecha01 Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/3910 Comments-Addressed: Arm Jenkins Reviewed-by: Michele Di Giorgio Tested-by: Arm Jenkins --- arm_compute/graph/GraphBuilder.h | 15 ++++++ arm_compute/graph/TypePrinter.h | 3 ++ arm_compute/graph/Types.h | 1 + arm_compute/graph/backends/FunctionHelpers.h | 37 +++++++++++++ arm_compute/graph/backends/ValidateHelpers.h | 23 ++++++++ arm_compute/graph/frontend/Layers.h | 42 +++++++++++++++ arm_compute/graph/nodes/ArgMinMaxLayerNode.h | 81 ++++++++++++++++++++++++++++ arm_compute/graph/nodes/Nodes.h | 1 + arm_compute/graph/nodes/NodesFwd.h | 1 + 9 files changed, 204 insertions(+) create mode 100644 arm_compute/graph/nodes/ArgMinMaxLayerNode.h (limited to 'arm_compute/graph') diff --git a/arm_compute/graph/GraphBuilder.h b/arm_compute/graph/GraphBuilder.h index ff40497667..2632bf1484 100644 --- a/arm_compute/graph/GraphBuilder.h +++ b/arm_compute/graph/GraphBuilder.h @@ -84,6 +84,21 @@ public: */ static NodeID add_activation_node(Graph &g, NodeParams params, NodeIdxPair input, ActivationLayerInfo act_info, const QuantizationInfo &out_quant_info = QuantizationInfo()); + /** Adds an activation layer node to the graph + * + * @param[in] g Graph to add the node to + * @param[in] params Common node parameters + * @param[in] input Input to the activation layer node as a NodeID-Index pair + * @param[in] op Reduction Operation: min or max + * @param[in] axis Axis to perform reduction operation across + * @param[in] out_data_type (Optional) Output data type + * @param[in] out_quant_info (Optional) Output quantization info + * + * @return Node ID of the created node, EmptyNodeID in case of error + */ + static NodeID add_arg_min_max_node(Graph &g, NodeParams params, NodeIdxPair input, ReductionOperation op, unsigned int axis, + DataType out_data_type = DataType::UNKNOWN, + const QuantizationInfo &out_quant_info = QuantizationInfo()); /** Adds a batch normalization layer node to the graph * * @param[in] g Graph to add the node to diff --git a/arm_compute/graph/TypePrinter.h b/arm_compute/graph/TypePrinter.h index c24f6e159c..30433bf3e0 100644 --- a/arm_compute/graph/TypePrinter.h +++ b/arm_compute/graph/TypePrinter.h @@ -65,6 +65,9 @@ inline ::std::ostream &operator<<(::std::ostream &os, const NodeType &node_type) case NodeType::ActivationLayer: os << "ActivationLayer"; break; + case NodeType::ArgMinMaxLayer: + os << "ArgMinMaxLayer"; + break; case NodeType::BatchNormalizationLayer: os << "BatchNormalizationLayer"; break; diff --git a/arm_compute/graph/Types.h b/arm_compute/graph/Types.h index bb54b60ec0..2b9a4f3ca4 100644 --- a/arm_compute/graph/Types.h +++ b/arm_compute/graph/Types.h @@ -141,6 +141,7 @@ enum class FastMathHint enum class NodeType { ActivationLayer, + ArgMinMaxLayer, BatchNormalizationLayer, BoundingBoxTransformLayer, ChannelShuffleLayer, diff --git a/arm_compute/graph/backends/FunctionHelpers.h b/arm_compute/graph/backends/FunctionHelpers.h index 5b0e7d174c..05695fb797 100644 --- a/arm_compute/graph/backends/FunctionHelpers.h +++ b/arm_compute/graph/backends/FunctionHelpers.h @@ -131,6 +131,43 @@ std::unique_ptr create_activation_layer(ActivationLayerNode &node) return RETURN_UNIQUE_PTR(func); } +/** Creates a backend argminmax layer function + * + * @tparam ArgMinMaxLayerFunction Backend activation function + * @tparam TargetInfo Target-specific information + * + * @param[in] node Node to create the backend function for + * + * @return Backend argminmax layer function + */ +template +std::unique_ptr create_arg_min_max_layer(ArgMinMaxLayerNode &node) +{ + validate_node(node, 1 /* expected inputs */, 1 /* expected outputs */); + + // Extract IO and info + typename TargetInfo::TensorType *input = get_backing_tensor(node.input(0)); + typename TargetInfo::TensorType *output = get_backing_tensor(node.output(0)); + const ReductionOperation op = node.reduction_operation(); + unsigned int axis = node.axis(); + + // Create function + auto func = support::cpp14::make_unique(); + func->configure(input, axis, output, op); + + ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " + << node.name() + << " Type: " << node.type() + << " Target: " << TargetInfo::TargetType + << " Data Type: " << input->info()->data_type() + << " Shape: " << input->info()->tensor_shape() + << " Reduction Operation: " << op + << " axis: " << axis + << std::endl); + + return RETURN_UNIQUE_PTR(func); +} + /** Create a backend batch normalization layer function * * @tparam BatchNormalizationLayerFunction Backend batch normalization function diff --git a/arm_compute/graph/backends/ValidateHelpers.h b/arm_compute/graph/backends/ValidateHelpers.h index fcebc5c418..23f6cc5627 100644 --- a/arm_compute/graph/backends/ValidateHelpers.h +++ b/arm_compute/graph/backends/ValidateHelpers.h @@ -52,6 +52,29 @@ inline arm_compute::ITensorInfo *get_backing_tensor_info(arm_compute::graph::Ten return ((tensor == nullptr) || (tensor->handle() == nullptr)) ? nullptr : tensor->handle()->tensor().info(); } +/** Validates a ArgMinMax layer node + * + * @tparam ArgMinMax layer function type + * + * @param[in] node Node to validate + * + * @return Status + */ +template +Status validate_arg_min_max_layer(ArgMinMaxLayerNode &node) +{ + ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating ArgMinMaxLayer node with ID : " << node.id() << " and Name: " << node.name() << std::endl); + ARM_COMPUTE_RETURN_ERROR_ON(node.num_inputs() != 1); + ARM_COMPUTE_RETURN_ERROR_ON(node.num_outputs() != 1); + + // Extract IO and info + arm_compute::ITensorInfo *input = detail::get_backing_tensor_info(node.input(0)); + arm_compute::ITensorInfo *output = get_backing_tensor_info(node.output(0)); + + // Validate function + return ArgMinMaxLayer::validate(input, node.axis(), output, node.reduction_operation()); +} + /** Validates a Bounding Box Transform layer node * * @tparam BoundingBoxTransformLayer Bounding Box Transform layer function type diff --git a/arm_compute/graph/frontend/Layers.h b/arm_compute/graph/frontend/Layers.h index 2dd8b31ccf..da664e0448 100644 --- a/arm_compute/graph/frontend/Layers.h +++ b/arm_compute/graph/frontend/Layers.h @@ -145,6 +145,48 @@ private: const QuantizationInfo _out_quant_info; }; +/** ArgMinMax Layer */ +class ArgMinMaxLayer final : public ILayer +{ +public: + /** Construct an activation layer. + * + * @param[in] op Reduction Operation: min or max + * @param[in] axis Axis to perform reduction along + * @param[in] out_data_type (Optional) Output tensor data type + * @param[in] out_quant_info (Optional) Output quantization info + */ + ArgMinMaxLayer(ReductionOperation op, + unsigned int axis, + DataType out_data_type = DataType::UNKNOWN, + const QuantizationInfo out_quant_info = QuantizationInfo()) + : _op(op), + _axis(axis), + _out_data_type(out_data_type), + _out_quant_info(std::move(out_quant_info)) + { + } + + /** Create layer and add to the given stream. + * + * @param[in] s Stream to add layer to. + * + * @return ID of the created node. + */ + NodeID create_layer(IStream &s) override + { + NodeParams common_params = { name(), s.hints().target_hint }; + NodeIdxPair input = { s.tail_node(), 0 }; + return GraphBuilder::add_arg_min_max_node(s.graph(), common_params, input, _op, _axis, _out_data_type, std::move(_out_quant_info)); + } + +private: + ReductionOperation _op; + unsigned int _axis; + DataType _out_data_type; + QuantizationInfo _out_quant_info; +}; + /** Batchnormalization Layer */ class BatchNormalizationLayer final : public ILayer { diff --git a/arm_compute/graph/nodes/ArgMinMaxLayerNode.h b/arm_compute/graph/nodes/ArgMinMaxLayerNode.h new file mode 100644 index 0000000000..69191add99 --- /dev/null +++ b/arm_compute/graph/nodes/ArgMinMaxLayerNode.h @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2020 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_ARGMINMAX_LAYER_NODE_H +#define ARM_COMPUTE_GRAPH_ARGMINMAX_LAYER_NODE_H + +#include "arm_compute/graph/INode.h" + +namespace arm_compute +{ +namespace graph +{ +/** Arg Min/Max Layer node */ +class ArgMinMaxLayerNode final : public INode +{ +public: + /** Constructor + * + * @param[in] op Operation to perform: min or max + * @param[in] axis Axis along which to reduce. Supported reduction axis : 0,1,2,3 + * @param[in] out_data_type (Optional) Output data type + * @param[in] out_quant_info (Optional) Output quantization info + */ + ArgMinMaxLayerNode(ReductionOperation op, + unsigned int axis, + DataType out_data_type = DataType::UNKNOWN, + QuantizationInfo out_quant_info = QuantizationInfo()); + /** Operator accessor + * + * @return The operator the layer performs: min or max + */ + ReductionOperation reduction_operation() const; + /** Axis accessor + * + * @return The axis along which the reduction is operating + */ + unsigned int axis() const; + /** Output data type accessor + * + * @return The output data type + */ + DataType out_data_type() const; + + // Inherited overridden methods: + NodeType type() const override; + bool forward_descriptors() override; + TensorDescriptor configure_output(size_t idx) const override; + void accept(INodeVisitor &v) override; + +public: + static constexpr NodeType node_type = NodeType::ArgMinMaxLayer; + +private: + ReductionOperation _op; + unsigned int _axis; + DataType _out_data_type; + QuantizationInfo _out_quant_info; +}; +} // namespace graph +} // namespace arm_compute +#endif /* ARM_COMPUTE_GRAPH_ARGMINMAX_LAYER_NODE_H */ diff --git a/arm_compute/graph/nodes/Nodes.h b/arm_compute/graph/nodes/Nodes.h index 0e4ccd067d..2b400a9140 100644 --- a/arm_compute/graph/nodes/Nodes.h +++ b/arm_compute/graph/nodes/Nodes.h @@ -25,6 +25,7 @@ #define ARM_COMPUTE_GRAPH_NODES_H #include "arm_compute/graph/nodes/ActivationLayerNode.h" +#include "arm_compute/graph/nodes/ArgMinMaxLayerNode.h" #include "arm_compute/graph/nodes/BatchNormalizationLayerNode.h" #include "arm_compute/graph/nodes/BoundingBoxTransformLayerNode.h" #include "arm_compute/graph/nodes/ChannelShuffleLayerNode.h" diff --git a/arm_compute/graph/nodes/NodesFwd.h b/arm_compute/graph/nodes/NodesFwd.h index f829955fb1..8692476a09 100644 --- a/arm_compute/graph/nodes/NodesFwd.h +++ b/arm_compute/graph/nodes/NodesFwd.h @@ -31,6 +31,7 @@ namespace graph // Forward declarations class INode; class ActivationLayerNode; +class ArgMinMaxLayerNode; class BatchNormalizationLayerNode; class BoundingBoxTransformLayerNode; class ChannelShuffleLayerNode; -- cgit v1.2.1