Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 1 | /* |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 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 | |
Michalis Spyrou | ed194b1 | 2017-10-31 15:04:34 +0000 | [diff] [blame] | 26 | #include "arm_compute/graph/Error.h" |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 27 | #include "arm_compute/graph/Graph.h" |
| 28 | #include "arm_compute/graph/SubGraph.h" |
| 29 | #include "arm_compute/graph/Tensor.h" |
| 30 | #include "arm_compute/runtime/IFunction.h" |
| 31 | #include "support/ToolchainSupport.h" |
| 32 | #include "utils/TypePrinter.h" |
| 33 | |
| 34 | #include <memory> |
| 35 | #include <tuple> |
| 36 | #include <vector> |
| 37 | |
| 38 | using namespace arm_compute::graph; |
| 39 | |
| 40 | namespace |
| 41 | { |
| 42 | void depth_concatenate_output_info(ITensorInfo *info, ITensorInfo *sub_tensor_info) |
| 43 | { |
| 44 | ARM_COMPUTE_ERROR_ON(info == nullptr); |
| 45 | ARM_COMPUTE_ERROR_ON(sub_tensor_info == nullptr); |
| 46 | |
| 47 | TensorShape info_shape = info->tensor_shape(); |
| 48 | const TensorShape &sub_tensor_info_shape = sub_tensor_info->tensor_shape(); |
| 49 | |
| 50 | // Update parent info and valid region |
| 51 | if(info_shape.total_size() == 0) |
| 52 | { |
| 53 | arm_compute::auto_init_if_empty(*info, |
| 54 | sub_tensor_info->tensor_shape(), |
| 55 | sub_tensor_info->num_channels(), |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame^] | 56 | sub_tensor_info->data_type(), sub_tensor_info->fixed_point_position(), sub_tensor_info->quantization_info()); |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 57 | info->set_valid_region(sub_tensor_info->valid_region()); |
| 58 | } |
| 59 | else |
| 60 | { |
| 61 | ARM_COMPUTE_ERROR_ON(info->num_channels() != sub_tensor_info->num_channels()); |
| 62 | ARM_COMPUTE_ERROR_ON(info->data_type() != sub_tensor_info->data_type()); |
| 63 | ARM_COMPUTE_ERROR_ON(info->fixed_point_position() != sub_tensor_info->fixed_point_position()); |
| 64 | |
| 65 | // Concatenate depth |
| 66 | ARM_COMPUTE_ERROR_ON(info_shape.x() != sub_tensor_info_shape.x()); |
| 67 | ARM_COMPUTE_ERROR_ON(info_shape.y() != sub_tensor_info_shape.y()); |
| 68 | info_shape.set(2, info_shape.z() + sub_tensor_info_shape.z()); |
| 69 | info->set_tensor_shape(info_shape); |
| 70 | |
| 71 | // Update valid region |
| 72 | arm_compute::ValidRegion info_valid_region = info->valid_region(); |
| 73 | info_valid_region.shape.set(2, info_shape.z()); |
| 74 | arm_compute::ValidRegion updated_region = arm_compute::intersect_valid_regions(info_valid_region, sub_tensor_info->valid_region()); |
| 75 | info->set_valid_region(updated_region); |
| 76 | } |
| 77 | } |
| 78 | } // namespace |
| 79 | |
| 80 | /** Branch function */ |
| 81 | class BranchFunction final : public arm_compute::IFunction |
| 82 | { |
| 83 | public: |
| 84 | /** Default Constructor */ |
| 85 | BranchFunction() |
| 86 | : _graphs() |
| 87 | { |
| 88 | } |
| 89 | /** Registers graph to be executed by the branch function |
| 90 | * |
| 91 | * @param[in] graph Graph to register |
| 92 | */ |
| 93 | void register_graph(std::unique_ptr<Graph> graph) |
| 94 | { |
| 95 | _graphs.push_back(std::move(graph)); |
| 96 | } |
| 97 | // Inherited methods overriden: |
| 98 | void run() override |
| 99 | { |
| 100 | for(auto &g : _graphs) |
| 101 | { |
| 102 | ARM_COMPUTE_ERROR_ON(g.get() == nullptr); |
| 103 | g->run(); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | private: |
| 108 | std::vector<std::unique_ptr<Graph>> _graphs; |
| 109 | }; |
| 110 | |
| 111 | std::unique_ptr<arm_compute::IFunction> BranchLayer::instantiate_node(GraphContext &ctx, ITensorObject *input, ITensorObject *output) |
| 112 | { |
| 113 | ARM_COMPUTE_ERROR_ON(_branch_merge_method != BranchMergeMethod::DEPTH_CONCATENATE); |
| 114 | ARM_COMPUTE_UNUSED(_branch_merge_method); |
Michalis Spyrou | ed194b1 | 2017-10-31 15:04:34 +0000 | [diff] [blame] | 115 | ARM_COMPUTE_ERROR_ON_UNALLOCATED_TENSOR_OBJECT(input, output); |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 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 |
Georgios Pinitas | 1250a5a | 2018-01-02 13:27:37 +0000 | [diff] [blame] | 154 | auto g = sg->construct(ctx, std::move(in), std::move(out)); |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 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(), |
Giorgio Arena | a66eaa2 | 2017-12-21 19:50:06 +0000 | [diff] [blame^] | 173 | out_info.fixed_point_position(), |
| 174 | out_info.quantization_info()); |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 175 | |
| 176 | return std::move(func); |
| 177 | } |