blob: 7994541b78c61fb5dd348226aa569667e8da04a0 [file] [log] [blame]
Georgios Pinitasd8734b52017-12-22 15:27:52 +00001/*
Isabella Gottardi0ae5de92019-03-14 10:32:11 +00002 * Copyright (c) 2018-2019 ARM Limited.
Georgios Pinitasd8734b52017-12-22 15:27:52 +00003 *
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 Pinitasd9eb2752018-04-03 13:44:29 +010024#include "arm_compute/graph/mutators/DepthConcatSubTensorMutator.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000025
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010026#include "arm_compute/graph/Graph.h"
27#include "arm_compute/graph/Logger.h"
Georgios Pinitase2220552018-07-20 13:23:44 +010028#include "arm_compute/graph/Utils.h"
Georgios Pinitas2a2db592018-08-15 12:14:46 +010029#include "arm_compute/graph/algorithms/TopologicalSort.h"
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010030#include "arm_compute/graph/backends/BackendRegistry.h"
Georgios Pinitase2220552018-07-20 13:23:44 +010031#include "arm_compute/graph/nodes/ConcatenateLayerNode.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000032
33#include "arm_compute/core/utils/misc/Cast.h"
34#include "arm_compute/core/utils/misc/Iterable.h"
35
36namespace arm_compute
37{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010038namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +000039{
40const char *DepthConcatSubTensorMutator::name()
41{
42 return "DepthConcatSubTensorMutator";
43}
44
45void DepthConcatSubTensorMutator::mutate(Graph &g)
46{
Georgios Pinitas2a2db592018-08-15 12:14:46 +010047 // Early exit if no Concatenation layers exist in graph
48 if(g.nodes(NodeType::ConcatenateLayer).empty())
Georgios Pinitasd8734b52017-12-22 15:27:52 +000049 {
Georgios Pinitas2a2db592018-08-15 12:14:46 +010050 return;
51 }
52
53 // Perform topological sort
54 std::vector<NodeID> topological_sorted_node_ids = dfs(g);
55
56 // Should be in reverse order of execution
57 for(auto &node_id : arm_compute::utils::iterable::reverse_iterate(topological_sorted_node_ids))
58 {
59 INode *node = g.node(node_id);
60 if(node != nullptr && node->type() == NodeType::ConcatenateLayer && node->output(0) != nullptr)
Georgios Pinitasd8734b52017-12-22 15:27:52 +000061 {
62 // Get output tensor
63 auto output_tensor = node->output(0);
64
Georgios Pinitas9e4824c2019-04-12 13:15:58 +010065 // Check concatenation axis (Sub-tensor optimization is supported for concatenation axis >=2)
Georgios Pinitas2a2db592018-08-15 12:14:46 +010066 auto *concat_node = arm_compute::utils::cast::polymorphic_downcast<ConcatenateLayerNode *>(node);
Georgios Pinitas9e4824c2019-04-12 13:15:58 +010067 if(output_tensor == nullptr || get_dimension_idx(output_tensor->desc().layout, concat_node->concatenation_axis()) < 2)
Georgios Pinitase2220552018-07-20 13:23:44 +010068 {
69 continue;
70 }
71
Isabella Gottardi0ae5de92019-03-14 10:32:11 +000072 // Check that all tensor have the same target, valid inputs and same quantization info
Georgios Pinitasd8734b52017-12-22 15:27:52 +000073 bool is_valid = std::all_of(node->input_edges().cbegin(), node->input_edges().cend(),
74 [&](const EdgeID & eid)
75 {
Isabella Gottardi0ae5de92019-03-14 10:32:11 +000076 return (g.edge(eid) != nullptr) && (g.edge(eid)->tensor() != nullptr) && (g.edge(eid)->tensor()->desc().target == output_tensor->desc().target)
77 && (g.edge(eid)->tensor()->desc().quant_info == output_tensor->desc().quant_info);
Georgios Pinitasd8734b52017-12-22 15:27:52 +000078 });
79
80 // Create subtensors
Anthony Barbier890ad1b2018-08-22 13:44:36 +010081 if(is_valid && is_target_supported(output_tensor->desc().target))
Georgios Pinitasd8734b52017-12-22 15:27:52 +000082 {
83 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Using sub-tensors for the node with ID : "
84 << node->id() << " and name : " << node->name() << std::endl);
85 // Create sub-tensor handles
86 unsigned depth = 0;
87 for(unsigned int i = 0; i < node->input_edges().size(); ++i)
88 {
89 auto input_tensor = node->input(i);
90 const auto input_shape = input_tensor->desc().shape;
91
Anthony Barbier890ad1b2018-08-22 13:44:36 +010092 backends::IDeviceBackend &backend = backends::BackendRegistry::get().get_backend(input_tensor->desc().target);
93 std::unique_ptr<ITensorHandle> handle = backend.create_subtensor(output_tensor->handle(), input_shape, Coordinates(0, 0, depth), false);
Georgios Pinitasd8734b52017-12-22 15:27:52 +000094 input_tensor->set_handle(std::move(handle));
95
96 depth += input_shape.z();
97 }
98
Georgios Pinitas2a2db592018-08-15 12:14:46 +010099 auto *dc_node = arm_compute::utils::cast::polymorphic_downcast<ConcatenateLayerNode *>(node);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000100 dc_node->set_enabled(false);
101 }
102 }
103 }
104}
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100105} // namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000106} // namespace arm_compute