blob: 28f58c68151750a873d99faff856ccabd87a4f36 [file] [log] [blame]
Georgios Pinitase2c82fe2017-10-02 18:51:47 +01001/*
2 * Copyright (c) 2017 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/BranchLayer.h"
25
26#include "arm_compute/graph/Graph.h"
27#include "arm_compute/graph/SubGraph.h"
28#include "arm_compute/graph/Tensor.h"
29#include "arm_compute/runtime/IFunction.h"
30#include "support/ToolchainSupport.h"
31#include "utils/TypePrinter.h"
32
33#include <memory>
34#include <tuple>
35#include <vector>
36
37using namespace arm_compute::graph;
38
39namespace
40{
41void depth_concatenate_output_info(ITensorInfo *info, ITensorInfo *sub_tensor_info)
42{
43 ARM_COMPUTE_ERROR_ON(info == nullptr);
44 ARM_COMPUTE_ERROR_ON(sub_tensor_info == nullptr);
45
46 TensorShape info_shape = info->tensor_shape();
47 const TensorShape &sub_tensor_info_shape = sub_tensor_info->tensor_shape();
48
49 // Update parent info and valid region
50 if(info_shape.total_size() == 0)
51 {
52 arm_compute::auto_init_if_empty(*info,
53 sub_tensor_info->tensor_shape(),
54 sub_tensor_info->num_channels(),
55 sub_tensor_info->data_type(), sub_tensor_info->fixed_point_position());
56 info->set_valid_region(sub_tensor_info->valid_region());
57 }
58 else
59 {
60 ARM_COMPUTE_ERROR_ON(info->num_channels() != sub_tensor_info->num_channels());
61 ARM_COMPUTE_ERROR_ON(info->data_type() != sub_tensor_info->data_type());
62 ARM_COMPUTE_ERROR_ON(info->fixed_point_position() != sub_tensor_info->fixed_point_position());
63
64 // Concatenate depth
65 ARM_COMPUTE_ERROR_ON(info_shape.x() != sub_tensor_info_shape.x());
66 ARM_COMPUTE_ERROR_ON(info_shape.y() != sub_tensor_info_shape.y());
67 info_shape.set(2, info_shape.z() + sub_tensor_info_shape.z());
68 info->set_tensor_shape(info_shape);
69
70 // Update valid region
71 arm_compute::ValidRegion info_valid_region = info->valid_region();
72 info_valid_region.shape.set(2, info_shape.z());
73 arm_compute::ValidRegion updated_region = arm_compute::intersect_valid_regions(info_valid_region, sub_tensor_info->valid_region());
74 info->set_valid_region(updated_region);
75 }
76}
77} // namespace
78
79/** Branch function */
80class BranchFunction final : public arm_compute::IFunction
81{
82public:
83 /** Default Constructor */
84 BranchFunction()
85 : _graphs()
86 {
87 }
88 /** Registers graph to be executed by the branch function
89 *
90 * @param[in] graph Graph to register
91 */
92 void register_graph(std::unique_ptr<Graph> graph)
93 {
94 _graphs.push_back(std::move(graph));
95 }
96 // Inherited methods overriden:
97 void run() override
98 {
99 for(auto &g : _graphs)
100 {
101 ARM_COMPUTE_ERROR_ON(g.get() == nullptr);
102 g->run();
103 }
104 }
105
106private:
107 std::vector<std::unique_ptr<Graph>> _graphs;
108};
109
110std::unique_ptr<arm_compute::IFunction> BranchLayer::instantiate_node(GraphContext &ctx, ITensorObject *input, ITensorObject *output)
111{
112 ARM_COMPUTE_ERROR_ON(_branch_merge_method != BranchMergeMethod::DEPTH_CONCATENATE);
113 ARM_COMPUTE_UNUSED(_branch_merge_method);
114 ARM_COMPUTE_ERROR_ON(input == nullptr || input->tensor() == nullptr);
115 ARM_COMPUTE_ERROR_ON(output == nullptr || output->tensor() == nullptr);
116
117 // Create branch function
118 auto func = arm_compute::support::cpp14::make_unique<BranchFunction>();
119
120 // Track output SubTensorInfo and depth
121 TensorInfo out_info;
122 int depth = 0;
123
124 // Constuct all sub-graphs given the input/output
125 for(auto &sg : _sub_graphs)
126 {
127 ARM_COMPUTE_ERROR_ON(sg.get() == nullptr);
128
129 // IO buffers
130 std::unique_ptr<ITensorObject> in;
131 std::unique_ptr<ITensorObject> out;
132 SubTensor *out_sub_tensor = nullptr;
133
134 // Create input sub-tensor
135 if(!sg->has_input())
136 {
137 ARM_COMPUTE_ERROR_ON(dynamic_cast<Tensor *>(input) == nullptr);
138 in = arm_compute::support::cpp14::make_unique<SubTensor>(*dynamic_cast<Tensor *>(input),
139 input->tensor()->info()->tensor_shape(),
140 Coordinates());
141 }
142
143 // Create output sub-tensor
144 if(!sg->has_output())
145 {
146 ARM_COMPUTE_ERROR_ON(dynamic_cast<Tensor *>(output) == nullptr);
147 out = arm_compute::support::cpp14::make_unique<SubTensor>(*dynamic_cast<Tensor *>(output),
148 output->tensor()->info()->tensor_shape(),
149 Coordinates(0, 0, depth));
150 out_sub_tensor = dynamic_cast<SubTensor *>(out.get());
151 }
152
153 // Construct sub_graph
154 auto g = sg->construct(ctx.hints().target_hint(), std::move(in), std::move(out));
155
156 // Register graph to function
157 func->register_graph(std::move(g));
158
159 // Update and track depth
160 if(out_sub_tensor != nullptr)
161 {
162 ARM_COMPUTE_ERROR_ON(out_sub_tensor->tensor() == nullptr);
163 depth += out_sub_tensor->tensor()->info()->tensor_shape()[2];
164 depth_concatenate_output_info(&out_info, out_sub_tensor->tensor()->info());
165 }
166 }
167
168 // Auto-init output
169 arm_compute::auto_init_if_empty(*output->tensor()->info(),
170 out_info.tensor_shape(),
171 out_info.num_channels(),
172 out_info.data_type(),
173 out_info.fixed_point_position());
174
175 return std::move(func);
176}