blob: 5f1c9c31869a484a742b71dc1bab69602b971b72 [file] [log] [blame]
Georgios Pinitasee33ea52018-03-08 16:01:29 +00001/*
2 * Copyright (c) 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 */
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010024#include "arm_compute/graph/mutators/SplitLayerSubTensorMutator.h"
Georgios Pinitasee33ea52018-03-08 16:01:29 +000025
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010026#include "arm_compute/graph/Graph.h"
27#include "arm_compute/graph/Logger.h"
Georgios Pinitas2a2db592018-08-15 12:14:46 +010028#include "arm_compute/graph/algorithms/TopologicalSort.h"
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010029#include "arm_compute/graph/backends/BackendRegistry.h"
30#include "arm_compute/graph/nodes/SplitLayerNode.h"
Georgios Pinitasee33ea52018-03-08 16:01:29 +000031
32#include "arm_compute/core/utils/misc/Cast.h"
33#include "arm_compute/core/utils/misc/Iterable.h"
34
35namespace arm_compute
36{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010037namespace graph
Georgios Pinitasee33ea52018-03-08 16:01:29 +000038{
39const char *SplitLayerSubTensorMutator::name()
40{
41 return "SplitLayerSubTensorMutator";
42}
43
44void SplitLayerSubTensorMutator::mutate(Graph &g)
45{
Georgios Pinitas2a2db592018-08-15 12:14:46 +010046 // Early exit if no Split layers exist in graph
47 if(g.nodes(NodeType::SplitLayer).empty())
Georgios Pinitasee33ea52018-03-08 16:01:29 +000048 {
Georgios Pinitas2a2db592018-08-15 12:14:46 +010049 return;
50 }
51
52 // Perform topological sort
53 std::vector<NodeID> topological_sorted_node_ids = dfs(g);
54
55 // Should be in reverse order of execution
56 for(auto &node_id : arm_compute::utils::iterable::reverse_iterate(topological_sorted_node_ids))
57 {
58 INode *node = g.node(node_id);
59 if(node != nullptr && node->type() == NodeType::SplitLayer && node->input(0) != nullptr)
Georgios Pinitasee33ea52018-03-08 16:01:29 +000060 {
61 // Get output tensor
62 Tensor *input_tensor = node->input(0);
63
64 // Check that all tensor have the same target and are valid
65 bool is_valid = std::all_of(node->outputs().cbegin(), node->outputs().cend(),
66 [&](const TensorID & tid)
67 {
68 return (g.tensor(tid) != nullptr) && (g.tensor(tid)->desc().target == input_tensor->desc().target);
69 });
70
71 // Create subtensors
72 if(is_valid && backends::BackendRegistry::get().find_backend(input_tensor->desc().target) != nullptr)
73 {
74 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Using sub-tensors for the node with ID : "
75 << node->id() << " and name : " << node->name() << std::endl);
76
Georgios Pinitas2a2db592018-08-15 12:14:46 +010077 auto *split_node = arm_compute::utils::cast::polymorphic_downcast<SplitLayerNode *>(node);
Georgios Pinitasee33ea52018-03-08 16:01:29 +000078
79 const unsigned int axis = split_node->axis();
80 const unsigned int num_splits = split_node->num_splits();
81 const bool extend_parent = (axis < 2);
82
83 // Create sub-tensor handles
84 for(unsigned int i = 0; i < node->outputs().size(); ++i)
85 {
86 Tensor *output_tensor = node->output(i);
87 const TensorShape output_shape = output_tensor->desc().shape;
88 Coordinates coords;
Georgios Pinitascac13b12018-04-27 19:07:19 +010089 std::tie(std::ignore, coords) = SplitLayerNode::compute_output_descriptor(input_tensor->desc(), num_splits, axis, i);
Georgios Pinitasee33ea52018-03-08 16:01:29 +000090
91 backends::IDeviceBackend *backend = backends::BackendRegistry::get().find_backend(output_tensor->desc().target);
92 std::unique_ptr<ITensorHandle> handle = backend->create_subtensor(input_tensor->handle(), output_shape, coords, extend_parent);
93 output_tensor->set_handle(std::move(handle));
94 }
95 }
96 }
97 }
98}
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010099} // namespace graph
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000100} // namespace arm_compute