blob: 87404f9e1f8cb7c4ccc2e64c8ca653454abd678e [file] [log] [blame]
Alex Gilday8913d8d2018-02-15 11:07:18 +00001/*
2 * Copyright (c) 2017-2018 ARM Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
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
22 * SOFTWARE.
23 */
24#include "arm_compute/graph/nodes/ResidualLayer.h"
25
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"
35
36#include <memory>
37#include <tuple>
38#include <vector>
39
40using namespace arm_compute::graph;
41
42/** Residual function */
43class ResidualFunction final : public arm_compute::IFunction
44{
45public:
46 /** Default Constructor */
47 ResidualFunction(GraphContext &ctx, ITensorObject *output)
48 : _ctx(ctx), _input(nullptr), _output(output), _func(nullptr), _graphs(), _graph_outputs()
49 {
50 }
51
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;
62
63 /** Set the input (when using only one sub graph)
64 *
65 * @param[in] input Input to set
66 */
67 void set_input(std::unique_ptr<ITensorObject> input)
68 {
69 _input = std::move(input);
70 }
71
72 /** Registers graph to be executed by the residual function
73 *
74 * @param[in] graph Graph to register
75 * @param[in] output Output to register
76 */
77 void register_graph(std::unique_ptr<Graph> graph, std::unique_ptr<ITensorObject> output)
78 {
79 _graphs.push_back(std::move(graph));
80 _graph_outputs.push_back(std::move(output));
81 }
82
83 /** Configure the function */
84 void configure()
85 {
86 ARM_COMPUTE_ERROR_ON(_graphs.size() < 1 || _graphs.size() > 2);
87 TargetHint target_hint = _ctx.hints().target_hint();
88
89 // Create node context
90 NodeContext node_ctx(OperationType::ArithmeticAddition);
91 node_ctx.set_target(target_hint);
92
93 if(_graphs.size() == 1)
94 {
95 arm_compute::ITensor *in = _input->tensor();
96 node_ctx.add_input(in);
97 }
98
99 for(auto &o : _graph_outputs)
100 {
101 arm_compute::ITensor *in = o->tensor();
102 node_ctx.add_input(in);
103 }
104
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);
108
109 _func = OperationRegistry::get().find_operation(OperationType::ArithmeticAddition, target_hint)->configure(node_ctx);
110
111 for(auto &o : _graph_outputs)
112 {
113 o->allocate();
114 }
115 }
116
117 // Inherited methods overriden:
118 void run() override
119 {
120 ARM_COMPUTE_ERROR_ON(_graphs.size() < 1 || _graphs.size() > 2);
121
122 for(auto &g : _graphs)
123 {
124 ARM_COMPUTE_ERROR_ON(g.get() == nullptr);
125 g->run();
126 }
127
128 _func->run();
129 }
130
131private:
132 GraphContext _ctx;
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;
138};
139
140std::unique_ptr<arm_compute::IFunction> ResidualLayer::instantiate_node(GraphContext &ctx, ITensorObject *input, ITensorObject *output)
141{
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);
145
146 // Create residual function
147 auto func = arm_compute::support::cpp14::make_unique<ResidualFunction>(ctx, output);
148
149 if(_sub_graphs.size() == 1)
150 {
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(),
154 Coordinates());
155 func->set_input(std::move(original_in));
156 }
157
158 // Constuct all sub-graphs given the input/output
159 for(auto &sg : _sub_graphs)
160 {
161 ARM_COMPUTE_ERROR_ON(sg.get() == nullptr);
162
163 // IO buffers
164 std::unique_ptr<ITensorObject> in;
165 std::unique_ptr<ITensorObject> out;
166 std::unique_ptr<ITensorObject> func_in;
167
168 // Create input sub-tensor
169 if(!sg->has_input())
170 {
171 in = arm_compute::support::cpp14::make_unique<SubTensor>(*dynamic_cast<Tensor *>(input),
172 input->tensor()->info()->tensor_shape(),
173 Coordinates());
174 }
175
176 // Create output sub-tensor
177 if(!sg->has_output())
178 {
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(),
183 TensorShape(),
184 Coordinates(0, 0, 0),
185 func_in->target(),
186 true);
187 }
188
189 // Construct sub_graph
190 auto g = sg->construct(ctx, std::move(in), std::move(out));
191
192 // Register graph to function
193 func->register_graph(std::move(g), std::move(func_in));
194 }
195
196 func->configure();
197
198 return std::move(func);
199}