xref: /aosp_15_r20/external/ComputeLibrary/src/graph/mutators/SplitLayerSubTensorMutator.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2018-2020 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  */
24 #include "arm_compute/graph/mutators/SplitLayerSubTensorMutator.h"
25 
26 #include "arm_compute/graph/Graph.h"
27 #include "arm_compute/graph/Logger.h"
28 #include "arm_compute/graph/Utils.h"
29 #include "arm_compute/graph/algorithms/TopologicalSort.h"
30 #include "arm_compute/graph/backends/BackendRegistry.h"
31 #include "arm_compute/graph/nodes/SplitLayerNode.h"
32 
33 #include "support/Cast.h"
34 #include "support/Iterable.h"
35 
36 namespace arm_compute
37 {
38 namespace graph
39 {
name()40 const char *SplitLayerSubTensorMutator::name()
41 {
42     return "SplitLayerSubTensorMutator";
43 }
44 
type() const45 IGraphMutator::MutationType SplitLayerSubTensorMutator::type() const
46 {
47     return IGraphMutator::MutationType::Backend;
48 }
49 
mutate(Graph & g)50 void SplitLayerSubTensorMutator::mutate(Graph &g)
51 {
52     // Early exit if no Split layers exist in graph
53     if(g.nodes(NodeType::SplitLayer).empty())
54     {
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::SplitLayer && node->input(0) != nullptr)
66         {
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(),
72                                         [&](const TensorID & tid)
73             {
74                 return (g.tensor(tid) != nullptr) && (g.tensor(tid)->desc().target == input_tensor->desc().target);
75             });
76 
77             // Create subtensors
78             if(is_valid && is_target_supported(input_tensor->desc().target))
79             {
80                 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Using sub-tensors for the node with ID : "
81                                               << node->id() << " and name : " << node->name() << std::endl);
82 
83                 auto *split_node = arm_compute::utils::cast::polymorphic_downcast<SplitLayerNode *>(node);
84 
85                 const int          axis          = split_node->axis();
86                 const unsigned int num_splits    = split_node->num_splits();
87                 const bool         extend_parent = (axis < 2);
88 
89                 // Create sub-tensor handles
90                 for(unsigned int i = 0; i < node->outputs().size(); ++i)
91                 {
92                     Tensor           *output_tensor = node->output(i);
93                     const TensorShape output_shape  = output_tensor->desc().shape;
94                     Coordinates       coords;
95                     std::tie(std::ignore, coords) = split_node->compute_output_descriptor(input_tensor->desc(), num_splits, axis, i);
96 
97                     backends::IDeviceBackend      &backend = backends::BackendRegistry::get().get_backend(output_tensor->desc().target);
98                     std::unique_ptr<ITensorHandle> handle  = backend.create_subtensor(input_tensor->handle(), output_shape, coords, extend_parent);
99                     output_tensor->set_handle(std::move(handle));
100                 }
101             }
102         }
103     }
104 }
105 } // namespace graph
106 } // namespace arm_compute
107