Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 Arm Limited. |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [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 | */ |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 24 | #include "arm_compute/graph/mutators/DepthConcatSubTensorMutator.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 25 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 26 | #include "arm_compute/graph/Graph.h" |
| 27 | #include "arm_compute/graph/Logger.h" |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 28 | #include "arm_compute/graph/Utils.h" |
Georgios Pinitas | 2a2db59 | 2018-08-15 12:14:46 +0100 | [diff] [blame] | 29 | #include "arm_compute/graph/algorithms/TopologicalSort.h" |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 30 | #include "arm_compute/graph/backends/BackendRegistry.h" |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 31 | #include "arm_compute/graph/nodes/ConcatenateLayerNode.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 32 | |
| 33 | #include "arm_compute/core/utils/misc/Cast.h" |
| 34 | #include "arm_compute/core/utils/misc/Iterable.h" |
| 35 | |
| 36 | namespace arm_compute |
| 37 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 38 | namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 39 | { |
| 40 | const char *DepthConcatSubTensorMutator::name() |
| 41 | { |
| 42 | return "DepthConcatSubTensorMutator"; |
| 43 | } |
| 44 | |
Georgios Pinitas | f4261ad | 2019-12-02 11:58:19 +0000 | [diff] [blame] | 45 | IGraphMutator::MutationType DepthConcatSubTensorMutator::type() const |
| 46 | { |
| 47 | return IGraphMutator::MutationType::Backend; |
| 48 | } |
| 49 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 50 | void DepthConcatSubTensorMutator::mutate(Graph &g) |
| 51 | { |
Georgios Pinitas | 2a2db59 | 2018-08-15 12:14:46 +0100 | [diff] [blame] | 52 | // Early exit if no Concatenation layers exist in graph |
| 53 | if(g.nodes(NodeType::ConcatenateLayer).empty()) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 54 | { |
Georgios Pinitas | 2a2db59 | 2018-08-15 12:14:46 +0100 | [diff] [blame] | 55 | return; |
| 56 | } |
| 57 | |
| 58 | // Perform topological sort |
| 59 | std::vector<NodeID> topological_sorted_node_ids = dfs(g); |
| 60 | |
| 61 | // Should be in reverse order of execution |
| 62 | for(auto &node_id : arm_compute::utils::iterable::reverse_iterate(topological_sorted_node_ids)) |
| 63 | { |
| 64 | INode *node = g.node(node_id); |
| 65 | if(node != nullptr && node->type() == NodeType::ConcatenateLayer && node->output(0) != nullptr) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 66 | { |
| 67 | // Get output tensor |
| 68 | auto output_tensor = node->output(0); |
| 69 | |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 70 | // Check concatenation axis (Sub-tensor optimization is supported for concatenation axis >=2) |
Georgios Pinitas | 2a2db59 | 2018-08-15 12:14:46 +0100 | [diff] [blame] | 71 | auto *concat_node = arm_compute::utils::cast::polymorphic_downcast<ConcatenateLayerNode *>(node); |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 72 | if(output_tensor == nullptr || get_dimension_idx(output_tensor->desc().layout, concat_node->concatenation_axis()) < 2) |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 73 | { |
| 74 | continue; |
| 75 | } |
| 76 | |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 77 | // Check that all tensor have the same target, valid inputs and same quantization info |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 78 | bool is_valid = std::all_of(node->input_edges().cbegin(), node->input_edges().cend(), |
| 79 | [&](const EdgeID & eid) |
| 80 | { |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 81 | return (g.edge(eid) != nullptr) && (g.edge(eid)->tensor() != nullptr) && (g.edge(eid)->tensor()->desc().target == output_tensor->desc().target) |
| 82 | && (g.edge(eid)->tensor()->desc().quant_info == output_tensor->desc().quant_info); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 83 | }); |
| 84 | |
| 85 | // Create subtensors |
Anthony Barbier | 890ad1b | 2018-08-22 13:44:36 +0100 | [diff] [blame] | 86 | if(is_valid && is_target_supported(output_tensor->desc().target)) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 87 | { |
| 88 | ARM_COMPUTE_LOG_GRAPH_VERBOSE("Using sub-tensors for the node with ID : " |
| 89 | << node->id() << " and name : " << node->name() << std::endl); |
| 90 | // Create sub-tensor handles |
| 91 | unsigned depth = 0; |
| 92 | for(unsigned int i = 0; i < node->input_edges().size(); ++i) |
| 93 | { |
| 94 | auto input_tensor = node->input(i); |
| 95 | const auto input_shape = input_tensor->desc().shape; |
| 96 | |
Anthony Barbier | 890ad1b | 2018-08-22 13:44:36 +0100 | [diff] [blame] | 97 | backends::IDeviceBackend &backend = backends::BackendRegistry::get().get_backend(input_tensor->desc().target); |
| 98 | std::unique_ptr<ITensorHandle> handle = backend.create_subtensor(output_tensor->handle(), input_shape, Coordinates(0, 0, depth), false); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 99 | input_tensor->set_handle(std::move(handle)); |
| 100 | |
| 101 | depth += input_shape.z(); |
| 102 | } |
| 103 | |
Georgios Pinitas | 2a2db59 | 2018-08-15 12:14:46 +0100 | [diff] [blame] | 104 | auto *dc_node = arm_compute::utils::cast::polymorphic_downcast<ConcatenateLayerNode *>(node); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 105 | dc_node->set_enabled(false); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | } |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 110 | } // namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 111 | } // namespace arm_compute |