blob: 533f8944cf4d9185c2978c06cc9437952bbd9974 [file] [log] [blame]
Georgios Pinitasee33ea52018-03-08 16:01:29 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 Arm Limited.
Georgios Pinitasee33ea52018-03-08 16:01:29 +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/SplitLayerSubTensorMutator.h"
Georgios Pinitasee33ea52018-03-08 16:01:29 +000025
Georgios Pinitas2a2db592018-08-15 12:14:46 +010026#include "arm_compute/graph/algorithms/TopologicalSort.h"
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010027#include "arm_compute/graph/backends/BackendRegistry.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010028#include "arm_compute/graph/Graph.h"
29#include "arm_compute/graph/Logger.h"
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010030#include "arm_compute/graph/nodes/SplitLayerNode.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010031#include "arm_compute/graph/Utils.h"
Georgios Pinitasee33ea52018-03-08 16:01:29 +000032
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010033#include "support/Cast.h"
34#include "support/Iterable.h"
Georgios Pinitasee33ea52018-03-08 16:01:29 +000035
36namespace arm_compute
37{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010038namespace graph
Georgios Pinitasee33ea52018-03-08 16:01:29 +000039{
40const char *SplitLayerSubTensorMutator::name()
41{
42 return "SplitLayerSubTensorMutator";
43}
44
Georgios Pinitasf4261ad2019-12-02 11:58:19 +000045IGraphMutator::MutationType SplitLayerSubTensorMutator::type() const
46{
47 return IGraphMutator::MutationType::Backend;
48}
49
Georgios Pinitasee33ea52018-03-08 16:01:29 +000050void SplitLayerSubTensorMutator::mutate(Graph &g)
51{
Georgios Pinitas2a2db592018-08-15 12:14:46 +010052 // Early exit if no Split layers exist in graph
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010053 if (g.nodes(NodeType::SplitLayer).empty())
Georgios Pinitasee33ea52018-03-08 16:01:29 +000054 {
Georgios Pinitas2a2db592018-08-15 12:14:46 +010055 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
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010062 for (auto &node_id : arm_compute::utils::iterable::reverse_iterate(topological_sorted_node_ids))
Georgios Pinitas2a2db592018-08-15 12:14:46 +010063 {
64 INode *node = g.node(node_id);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010065 if (node != nullptr && node->type() == NodeType::SplitLayer && node->input(0) != nullptr)
Georgios Pinitasee33ea52018-03-08 16:01:29 +000066 {
67 // Get output tensor
68 Tensor *input_tensor = node->input(0);
69
70 // Check that all tensor have the same target and are valid
71 bool is_valid = std::all_of(node->outputs().cbegin(), node->outputs().cend(),
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010072 [&](const TensorID &tid) {
73 return (g.tensor(tid) != nullptr) &&
74 (g.tensor(tid)->desc().target == input_tensor->desc().target);
75 });
Georgios Pinitasee33ea52018-03-08 16:01:29 +000076
77 // Create subtensors
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010078 if (is_valid && is_target_supported(input_tensor->desc().target))
Georgios Pinitasee33ea52018-03-08 16:01:29 +000079 {
80 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Using sub-tensors for the node with ID : "
81 << node->id() << " and name : " << node->name() << std::endl);
82
Georgios Pinitas2a2db592018-08-15 12:14:46 +010083 auto *split_node = arm_compute::utils::cast::polymorphic_downcast<SplitLayerNode *>(node);
Georgios Pinitasee33ea52018-03-08 16:01:29 +000084
Sheri Zhang16dddd22020-05-27 15:03:48 +010085 const int axis = split_node->axis();
Georgios Pinitasee33ea52018-03-08 16:01:29 +000086 const unsigned int num_splits = split_node->num_splits();
87 const bool extend_parent = (axis < 2);
88
89 // Create sub-tensor handles
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010090 for (unsigned int i = 0; i < node->outputs().size(); ++i)
Georgios Pinitasee33ea52018-03-08 16:01:29 +000091 {
92 Tensor *output_tensor = node->output(i);
93 const TensorShape output_shape = output_tensor->desc().shape;
94 Coordinates coords;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010095 std::tie(std::ignore, coords) =
96 split_node->compute_output_descriptor(input_tensor->desc(), num_splits, axis, i);
Georgios Pinitasee33ea52018-03-08 16:01:29 +000097
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010098 backends::IDeviceBackend &backend =
99 backends::BackendRegistry::get().get_backend(output_tensor->desc().target);
100 std::unique_ptr<ITensorHandle> handle =
101 backend.create_subtensor(input_tensor->handle(), output_shape, coords, extend_parent);
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000102 output_tensor->set_handle(std::move(handle));
103 }
104 }
105 }
106 }
107}
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100108} // namespace graph
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000109} // namespace arm_compute