From 4e1c3f336133319cfe45b037befdb252ffd9b744 Mon Sep 17 00:00:00 2001 From: Michalis Spyrou Date: Thu, 20 Sep 2018 17:14:03 +0100 Subject: COMPMID-1595 Add UpsampleLayer to graph API Change-Id: I7427b0fbb222ff9760d47b01a59aee654f2f860f Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/149587 Tested-by: bsgcomp Reviewed-by: Georgios Pinitas --- arm_compute/graph/GraphBuilder.h | 11 +++++ arm_compute/graph/TypePrinter.h | 3 ++ arm_compute/graph/Types.h | 1 + arm_compute/graph/backends/FunctionHelpers.h | 41 +++++++++++++++ arm_compute/graph/backends/ValidateHelpers.h | 22 +++++++++ arm_compute/graph/frontend/Layers.h | 26 ++++++++++ arm_compute/graph/nodes/Nodes.h | 1 + arm_compute/graph/nodes/NodesFwd.h | 1 + arm_compute/graph/nodes/UpsampleLayerNode.h | 74 ++++++++++++++++++++++++++++ 9 files changed, 180 insertions(+) create mode 100644 arm_compute/graph/nodes/UpsampleLayerNode.h (limited to 'arm_compute/graph') diff --git a/arm_compute/graph/GraphBuilder.h b/arm_compute/graph/GraphBuilder.h index 3821eb3968..1302d2a2b3 100644 --- a/arm_compute/graph/GraphBuilder.h +++ b/arm_compute/graph/GraphBuilder.h @@ -351,6 +351,17 @@ public: * @return Node ID of the created node, EmptyNodeID in case of error */ static NodeID add_split_node(Graph &g, NodeParams params, NodeIdxPair input, unsigned int num_splits, unsigned int axis = 0); + /** Adds an upsample layer to the graph + * + * @param[in] g Graph to add the node to + * @param[in] params Common node parameters + * @param[in] input Input to the yolo layer node as a NodeID-Index pair + * @param[in] info Upsample layer stride info + * @param[in] upsampling_policy Upsampling policy used + * + * @return Node ID of the created node, EmptyNodeID in case of error + */ + static NodeID add_upsample_node(Graph &g, NodeParams params, NodeIdxPair input, Size2D info, InterpolationPolicy upsampling_policy); /** Adds a yolo layer 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 983400ffe3..b0d0fb0242 100644 --- a/arm_compute/graph/TypePrinter.h +++ b/arm_compute/graph/TypePrinter.h @@ -122,6 +122,9 @@ inline ::std::ostream &operator<<(::std::ostream &os, const NodeType &node_type) case NodeType::SplitLayer: os << "SplitLayer"; break; + case NodeType::UpsampleLayer: + os << "UpsampleLayer"; + break; case NodeType::YOLOLayer: os << "YOLOLayer"; break; diff --git a/arm_compute/graph/Types.h b/arm_compute/graph/Types.h index 1e613f7e29..1f252097af 100644 --- a/arm_compute/graph/Types.h +++ b/arm_compute/graph/Types.h @@ -150,6 +150,7 @@ enum class NodeType SoftmaxLayer, SliceLayer, SplitLayer, + UpsampleLayer, YOLOLayer, Input, diff --git a/arm_compute/graph/backends/FunctionHelpers.h b/arm_compute/graph/backends/FunctionHelpers.h index 7f5093aa24..32ef0aaf13 100644 --- a/arm_compute/graph/backends/FunctionHelpers.h +++ b/arm_compute/graph/backends/FunctionHelpers.h @@ -911,6 +911,47 @@ std::unique_ptr create_softmax_layer(SoftmaxLayerNode &node, GraphCon return std::move(func); } +/** Create a backend Upsample layer function + * + * @tparam UpsampleLayerFunction Backend Upsample function + * @tparam TargetInfo Target-specific information + * + * @param[in] node Node to create the backend function for + * @param[in] ctx Graph context + * + * @return Backend Upsample layer function + */ +template +std::unique_ptr create_upsample_layer(UpsampleLayerNode &node, GraphContext &ctx) +{ + 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 Size2D info = node.info(); + const InterpolationPolicy upsampling_policy = node.upsampling_policy(); + ARM_COMPUTE_ERROR_ON(upsampling_policy != InterpolationPolicy::NEAREST_NEIGHBOR); + ARM_COMPUTE_ERROR_ON(info.x() != 2 || info.y() != 2); + ARM_COMPUTE_ERROR_ON(input == nullptr); + ARM_COMPUTE_ERROR_ON(output == nullptr); + + // Create and configure function + auto func = support::cpp14::make_unique(); + func->configure(input, output, info, upsampling_policy); + + // Log info + ARM_COMPUTE_LOG_GRAPH_INFO("Instantiated " << node.type() + << " Target " << TargetInfo::TargetType + << " Data Type: " << input->info()->data_type() + << " Input shape: " << input->info()->tensor_shape() + << " Output shape: " << output->info()->tensor_shape() + << " Strides: " << info + << " Upsampling policy: " << upsampling_policy + << std::endl); + + return std::move(func); +} /** Create a backend YOLO layer function * * @tparam YoloLayerFunction Backend YOLO function diff --git a/arm_compute/graph/backends/ValidateHelpers.h b/arm_compute/graph/backends/ValidateHelpers.h index 2dc349174d..ff644c4946 100644 --- a/arm_compute/graph/backends/ValidateHelpers.h +++ b/arm_compute/graph/backends/ValidateHelpers.h @@ -273,6 +273,28 @@ Status validate_slice_layer(SliceLayerNode &node) return SliceLayer::validate(input, output, starts, ends); } +/** Validates a Upsample layer node + * + * @tparam UpsampleLayer Upsample layer type + * + * @param[in] node Node to validate + * + * @return Status + */ +template +Status validate_upsample_layer(UpsampleLayerNode &node) +{ + ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating UpsampleLayer 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 input and output + 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 UpsampleLayer::validate(input, output, node.info(), node.upsampling_policy()); +} /** Validates a YOLO layer node * * @tparam YOLOLayer YOLO layer type diff --git a/arm_compute/graph/frontend/Layers.h b/arm_compute/graph/frontend/Layers.h index 3a45115d7c..9c1ef1e140 100644 --- a/arm_compute/graph/frontend/Layers.h +++ b/arm_compute/graph/frontend/Layers.h @@ -768,6 +768,32 @@ private: float _beta; }; +/** Upsample Layer */ +class UpsampleLayer final : public ILayer +{ +public: + /** Construct a Upsample layer. + * + * @param[in] info Stride info + * @param[in] upsampling_policy Upsampling policy + */ + UpsampleLayer(Size2D info, InterpolationPolicy upsampling_policy) + : _info(info), _upsampling_policy(upsampling_policy) + { + } + + NodeID create_layer(IStream &s) override + { + NodeParams common_params = { name(), s.hints().target_hint }; + NodeIdxPair input = { s.tail_node(), 0 }; + return GraphBuilder::add_upsample_node(s.graph(), common_params, input, _info, _upsampling_policy); + } + +private: + Size2D _info; + InterpolationPolicy _upsampling_policy; +}; + /** YOLO Layer */ class YOLOLayer final : public ILayer { diff --git a/arm_compute/graph/nodes/Nodes.h b/arm_compute/graph/nodes/Nodes.h index f1b899f784..2bda7e01ba 100644 --- a/arm_compute/graph/nodes/Nodes.h +++ b/arm_compute/graph/nodes/Nodes.h @@ -48,6 +48,7 @@ #include "arm_compute/graph/nodes/SliceLayerNode.h" #include "arm_compute/graph/nodes/SoftmaxLayerNode.h" #include "arm_compute/graph/nodes/SplitLayerNode.h" +#include "arm_compute/graph/nodes/UpsampleLayerNode.h" #include "arm_compute/graph/nodes/YOLOLayerNode.h" #endif /* __ARM_COMPUTE_GRAPH_NODES_H__ */ diff --git a/arm_compute/graph/nodes/NodesFwd.h b/arm_compute/graph/nodes/NodesFwd.h index 18a8e2efa2..ae5ed7c405 100644 --- a/arm_compute/graph/nodes/NodesFwd.h +++ b/arm_compute/graph/nodes/NodesFwd.h @@ -54,6 +54,7 @@ class ResizeLayerNode; class SoftmaxLayerNode; class SliceLayerNode; class SplitLayerNode; +class UpsampleLayerNode; class YOLOLayerNode; } // namespace graph } // namespace arm_compute diff --git a/arm_compute/graph/nodes/UpsampleLayerNode.h b/arm_compute/graph/nodes/UpsampleLayerNode.h new file mode 100644 index 0000000000..38afabe068 --- /dev/null +++ b/arm_compute/graph/nodes/UpsampleLayerNode.h @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2018 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_UPSAMPLE_LAYER_NODE_H__ +#define __ARM_COMPUTE_GRAPH_UPSAMPLE_LAYER_NODE_H__ + +#include "arm_compute/graph/INode.h" + +namespace arm_compute +{ +namespace graph +{ +/** Upsample Layer node */ +class UpsampleLayerNode final : public INode +{ +public: + /** Constructor + * + * @param[in] info Stride info + * @param[in] upsampling_policy Upsampling policy + */ + UpsampleLayerNode(Size2D info, InterpolationPolicy upsampling_policy); + /** Stride info metadata accessor + * + * @return The stride info of the layer + */ + Size2D info() const; + /** Upsampling policy metadata accessor + * + * @return The upsampling policy of the layer + */ + InterpolationPolicy upsampling_policy() const; + /** Computes upsample output descriptor + * + * @param[in] input_descriptor Input descriptor + * @param[in] info Stride information + * + * @return Output descriptor + */ + static TensorDescriptor compute_output_descriptor(const TensorDescriptor &input_descriptor, Size2D info); + + // Inherited overridden methods: + NodeType type() const override; + bool forward_descriptors() override; + TensorDescriptor configure_output(size_t idx) const override; + void accept(INodeVisitor &v) override; + +private: + Size2D _info; + InterpolationPolicy _upsampling_policy; +}; +} // namespace graph +} // namespace arm_compute +#endif /* __ARM_COMPUTE_GRAPH_UPSAMPLE_LAYER_NODE_H__ */ -- cgit v1.2.1