blob: c56f4c5106faa5fce14dc78b169d9c24398d1184 [file] [log] [blame]
Georgios Pinitasd8734b52017-12-22 15:27:52 +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/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"
28#include "arm_compute/graph/backends/BackendRegistry.h"
29#include "arm_compute/graph/nodes/DepthConcatenateLayerNode.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000030
31#include "arm_compute/core/utils/misc/Cast.h"
32#include "arm_compute/core/utils/misc/Iterable.h"
33
34namespace arm_compute
35{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010036namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +000037{
38const char *DepthConcatSubTensorMutator::name()
39{
40 return "DepthConcatSubTensorMutator";
41}
42
43void DepthConcatSubTensorMutator::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::DepthConcatenateLayer && node->output(0) != nullptr)
49 {
50 // Get output tensor
51 auto output_tensor = node->output(0);
52
53 // Check that all tensor have the same target and valid inputs
54 bool is_valid = std::all_of(node->input_edges().cbegin(), node->input_edges().cend(),
55 [&](const EdgeID & eid)
56 {
57 return (g.edge(eid) != nullptr) && (g.edge(eid)->tensor() != nullptr) && (g.edge(eid)->tensor()->desc().target == output_tensor->desc().target);
58 });
59
60 // Create subtensors
61 if(is_valid && backends::BackendRegistry::get().find_backend(output_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 // Create sub-tensor handles
66 unsigned depth = 0;
67 for(unsigned int i = 0; i < node->input_edges().size(); ++i)
68 {
69 auto input_tensor = node->input(i);
70 const auto input_shape = input_tensor->desc().shape;
71
72 auto backend = backends::BackendRegistry::get().find_backend(input_tensor->desc().target);
Georgios Pinitasee33ea52018-03-08 16:01:29 +000073 auto handle = backend->create_subtensor(output_tensor->handle(), input_shape, Coordinates(0, 0, depth), false);
Georgios Pinitasd8734b52017-12-22 15:27:52 +000074 input_tensor->set_handle(std::move(handle));
75
76 depth += input_shape.z();
77 }
78
79 auto *dc_node = arm_compute::utils::cast::polymorphic_downcast<DepthConcatenateLayerNode *>(node.get());
80 dc_node->set_enabled(false);
81 }
82 }
83 }
84}
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010085} // namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +000086} // namespace arm_compute