2 * Copyright (c) 2017-2018 ARM Limited.
4 * SPDX-License-Identifier: MIT
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 #include "arm_compute/graph/nodes/ResidualLayer.h"
26 #include "arm_compute/graph/Error.h"
27 #include "arm_compute/graph/Graph.h"
28 #include "arm_compute/graph/NodeContext.h"
29 #include "arm_compute/graph/OperationRegistry.h"
30 #include "arm_compute/graph/SubGraph.h"
31 #include "arm_compute/graph/Tensor.h"
32 #include "arm_compute/runtime/IFunction.h"
33 #include "support/ToolchainSupport.h"
34 #include "utils/Utils.h"
40 using namespace arm_compute::graph;
42 /** Residual function */
43 class ResidualFunction final : public arm_compute::IFunction
46 /** Default Constructor */
47 ResidualFunction(GraphContext &ctx, ITensorObject *output)
48 : _ctx(ctx), _input(nullptr), _output(output), _func(nullptr), _graphs(), _graph_outputs()
52 /** Prevent instances from being copy constructed */
53 ResidualFunction(const ResidualFunction &) = delete;
54 /** Prevent instances from being copy assigned */
55 const ResidualFunction &operator=(const ResidualFunction &) = delete;
56 /** Prevent instances from being move constructed */
57 ResidualFunction(ResidualFunction &&) = delete;
58 /** Prevent instances from being move assigned */
59 ResidualFunction &operator=(ResidualFunction &&) = delete;
60 /** Default destructor */
61 ~ResidualFunction() override = default;
63 /** Set the input (when using only one sub graph)
65 * @param[in] input Input to set
67 void set_input(std::unique_ptr<ITensorObject> input)
69 _input = std::move(input);
72 /** Registers graph to be executed by the residual function
74 * @param[in] graph Graph to register
75 * @param[in] output Output to register
77 void register_graph(std::unique_ptr<Graph> graph, std::unique_ptr<ITensorObject> output)
79 _graphs.push_back(std::move(graph));
80 _graph_outputs.push_back(std::move(output));
83 /** Configure the function */
86 ARM_COMPUTE_ERROR_ON(_graphs.size() < 1 || _graphs.size() > 2);
87 TargetHint target_hint = _ctx.hints().target_hint();
89 // Create node context
90 NodeContext node_ctx(OperationType::ArithmeticAddition);
91 node_ctx.set_target(target_hint);
93 if(_graphs.size() == 1)
95 arm_compute::ITensor *in = _input->tensor();
96 node_ctx.add_input(in);
99 for(auto &o : _graph_outputs)
101 arm_compute::ITensor *in = o->tensor();
102 node_ctx.add_input(in);
105 arm_compute::ITensor *out = _output->tensor();
106 auto_init_if_empty(*out->info(), *_graph_outputs[0]->tensor()->info());
107 node_ctx.add_output(out);
109 _func = OperationRegistry::get().find_operation(OperationType::ArithmeticAddition, target_hint)->configure(node_ctx);
111 for(auto &o : _graph_outputs)
117 // Inherited methods overriden:
120 ARM_COMPUTE_ERROR_ON(_graphs.size() < 1 || _graphs.size() > 2);
122 for(auto &g : _graphs)
124 ARM_COMPUTE_ERROR_ON(g.get() == nullptr);
133 std::unique_ptr<ITensorObject> _input;
134 ITensorObject *_output;
135 std::unique_ptr<arm_compute::IFunction> _func;
136 std::vector<std::unique_ptr<Graph>> _graphs;
137 std::vector<std::unique_ptr<ITensorObject>> _graph_outputs;
140 std::unique_ptr<arm_compute::IFunction> ResidualLayer::instantiate_node(GraphContext &ctx, ITensorObject *input, ITensorObject *output)
142 ARM_COMPUTE_ERROR_ON_UNALLOCATED_TENSOR_OBJECT(input, output);
143 ARM_COMPUTE_ERROR_ON(dynamic_cast<Tensor *>(input) == nullptr);
144 ARM_COMPUTE_ERROR_ON(dynamic_cast<Tensor *>(output) == nullptr);
146 // Create residual function
147 auto func = arm_compute::support::cpp14::make_unique<ResidualFunction>(ctx, output);
149 if(_sub_graphs.size() == 1)
151 std::unique_ptr<ITensorObject> original_in;
152 original_in = arm_compute::support::cpp14::make_unique<SubTensor>(*dynamic_cast<Tensor *>(input),
153 input->tensor()->info()->tensor_shape(),
155 func->set_input(std::move(original_in));
158 // Constuct all sub-graphs given the input/output
159 for(auto &sg : _sub_graphs)
161 ARM_COMPUTE_ERROR_ON(sg.get() == nullptr);
164 std::unique_ptr<ITensorObject> in;
165 std::unique_ptr<ITensorObject> out;
166 std::unique_ptr<ITensorObject> func_in;
168 // Create input sub-tensor
171 in = arm_compute::support::cpp14::make_unique<SubTensor>(*dynamic_cast<Tensor *>(input),
172 input->tensor()->info()->tensor_shape(),
176 // Create output sub-tensor
177 if(!sg->has_output())
179 ITensorInfo *info = input->tensor()->info();
180 func_in = arm_compute::support::cpp14::make_unique<Tensor>(TensorInfo(info->num_channels(), info->data_type(), info->fixed_point_position()));
181 func_in->set_target(ctx.hints().target_hint());
182 out = arm_compute::support::cpp14::make_unique<SubTensor>(func_in->tensor(),
184 Coordinates(0, 0, 0),
189 // Construct sub_graph
190 auto g = sg->construct(ctx, std::move(in), std::move(out));
192 // Register graph to function
193 func->register_graph(std::move(g), std::move(func_in));
198 return std::move(func);