blob: 33494ba6bcf22a3883c39661c5df503f93ff621d [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 */
24#include "arm_compute/graph2/mutators/SplitLayerSubTensorMutator.h"
25
26#include "arm_compute/graph2/Graph.h"
27#include "arm_compute/graph2/Logger.h"
28#include "arm_compute/graph2/backends/BackendRegistry.h"
29#include "arm_compute/graph2/nodes/SplitLayerNode.h"
30
31#include "arm_compute/core/utils/misc/Cast.h"
32#include "arm_compute/core/utils/misc/Iterable.h"
33
34namespace arm_compute
35{
36namespace graph2
37{
38const char *SplitLayerSubTensorMutator::name()
39{
40 return "SplitLayerSubTensorMutator";
41}
42
43void SplitLayerSubTensorMutator::mutate(Graph &g)
44{
45 // Should be in reverse order of execution
46 for(auto &node : arm_compute::utils::iterable::reverse_iterate(g.nodes()))
47 {
48 if(node && node->type() == NodeType::SplitLayer && node->input(0) != nullptr)
49 {
50 // Get output tensor
51 Tensor *input_tensor = node->input(0);
52
53 // Check that all tensor have the same target and are valid
54 bool is_valid = std::all_of(node->outputs().cbegin(), node->outputs().cend(),
55 [&](const TensorID & tid)
56 {
57 return (g.tensor(tid) != nullptr) && (g.tensor(tid)->desc().target == input_tensor->desc().target);
58 });
59
60 // Create subtensors
61 if(is_valid && backends::BackendRegistry::get().find_backend(input_tensor->desc().target) != nullptr)
62 {
63 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Using sub-tensors for the node with ID : "
64 << node->id() << " and name : " << node->name() << std::endl);
65
66 auto *split_node = arm_compute::utils::cast::polymorphic_downcast<SplitLayerNode *>(node.get());
67
68 const unsigned int axis = split_node->axis();
69 const unsigned int num_splits = split_node->num_splits();
70 const bool extend_parent = (axis < 2);
71
72 // Create sub-tensor handles
73 for(unsigned int i = 0; i < node->outputs().size(); ++i)
74 {
75 Tensor *output_tensor = node->output(i);
76 const TensorShape output_shape = output_tensor->desc().shape;
77 Coordinates coords;
78 std::tie(std::ignore, coords) = SplitLayerNode::compute_output_shape(input_tensor->desc().shape, num_splits, axis, i);
79
80 backends::IDeviceBackend *backend = backends::BackendRegistry::get().find_backend(output_tensor->desc().target);
81 std::unique_ptr<ITensorHandle> handle = backend->create_subtensor(input_tensor->handle(), output_shape, coords, extend_parent);
82 output_tensor->set_handle(std::move(handle));
83 }
84 }
85 }
86 }
87}
88} // namespace graph2
89} // namespace arm_compute