aboutsummaryrefslogtreecommitdiff
path: root/src/graph/nodes/ResidualLayer.cpp
blob: 87404f9e1f8cb7c4ccc2e64c8ca653454abd678e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
 * Copyright (c) 2017-2018 ARM Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#include "arm_compute/graph/nodes/ResidualLayer.h"

#include "arm_compute/graph/Error.h"
#include "arm_compute/graph/Graph.h"
#include "arm_compute/graph/NodeContext.h"
#include "arm_compute/graph/OperationRegistry.h"
#include "arm_compute/graph/SubGraph.h"
#include "arm_compute/graph/Tensor.h"
#include "arm_compute/runtime/IFunction.h"
#include "support/ToolchainSupport.h"
#include "utils/Utils.h"

#include <memory>
#include <tuple>
#include <vector>

using namespace arm_compute::graph;

/** Residual function */
class ResidualFunction final : public arm_compute::IFunction
{
public:
    /** Default Constructor */
    ResidualFunction(GraphContext &ctx, ITensorObject *output)
        : _ctx(ctx), _input(nullptr), _output(output), _func(nullptr), _graphs(), _graph_outputs()
    {
    }

    /** Prevent instances from being copy constructed */
    ResidualFunction(const ResidualFunction &) = delete;
    /** Prevent instances from being copy assigned */
    const ResidualFunction &operator=(const ResidualFunction &) = delete;
    /** Prevent instances from being move constructed */
    ResidualFunction(ResidualFunction &&) = delete;
    /** Prevent instances from being move assigned */
    ResidualFunction &operator=(ResidualFunction &&) = delete;
    /** Default destructor */
    ~ResidualFunction() override = default;

    /** Set the input (when using only one sub graph)
     *
     * @param[in] input Input to set
     */
    void set_input(std::unique_ptr<ITensorObject> input)
    {
        _input = std::move(input);
    }

    /** Registers graph to be executed by the residual function
     *
     * @param[in] graph  Graph to register
     * @param[in] output Output to register
     */
    void register_graph(std::unique_ptr<Graph> graph, std::unique_ptr<ITensorObject> output)
    {
        _graphs.push_back(std::move(graph));
        _graph_outputs.push_back(std::move(output));
    }

    /** Configure the function */
    void configure()
    {
        ARM_COMPUTE_ERROR_ON(_graphs.size() < 1 || _graphs.size() > 2);
        TargetHint target_hint = _ctx.hints().target_hint();

        // Create node context
        NodeContext node_ctx(OperationType::ArithmeticAddition);
        node_ctx.set_target(target_hint);

        if(_graphs.size() == 1)
        {
            arm_compute::ITensor *in = _input->tensor();
            node_ctx.add_input(in);
        }

        for(auto &o : _graph_outputs)
        {
            arm_compute::ITensor *in = o->tensor();
            node_ctx.add_input(in);
        }

        arm_compute::ITensor *out = _output->tensor();
        auto_init_if_empty(*out->info(), *_graph_outputs[0]->tensor()->info());
        node_ctx.add_output(out);

        _func = OperationRegistry::get().find_operation(OperationType::ArithmeticAddition, target_hint)->configure(node_ctx);

        for(auto &o : _graph_outputs)
        {
            o->allocate();
        }
    }

    // Inherited methods overriden:
    void run() override
    {
        ARM_COMPUTE_ERROR_ON(_graphs.size() < 1 || _graphs.size() > 2);

        for(auto &g : _graphs)
        {
            ARM_COMPUTE_ERROR_ON(g.get() == nullptr);
            g->run();
        }

        _func->run();
    }

private:
    GraphContext                                _ctx;
    std::unique_ptr<ITensorObject>              _input;
    ITensorObject                              *_output;
    std::unique_ptr<arm_compute::IFunction>     _func;
    std::vector<std::unique_ptr<Graph>>         _graphs;
    std::vector<std::unique_ptr<ITensorObject>> _graph_outputs;
};

std::unique_ptr<arm_compute::IFunction> ResidualLayer::instantiate_node(GraphContext &ctx, ITensorObject *input, ITensorObject *output)
{
    ARM_COMPUTE_ERROR_ON_UNALLOCATED_TENSOR_OBJECT(input, output);
    ARM_COMPUTE_ERROR_ON(dynamic_cast<Tensor *>(input) == nullptr);
    ARM_COMPUTE_ERROR_ON(dynamic_cast<Tensor *>(output) == nullptr);

    // Create residual function
    auto func = arm_compute::support::cpp14::make_unique<ResidualFunction>(ctx, output);

    if(_sub_graphs.size() == 1)
    {
        std::unique_ptr<ITensorObject> original_in;
        original_in = arm_compute::support::cpp14::make_unique<SubTensor>(*dynamic_cast<Tensor *>(input),
                                                                          input->tensor()->info()->tensor_shape(),
                                                                          Coordinates());
        func->set_input(std::move(original_in));
    }

    // Constuct all sub-graphs given the input/output
    for(auto &sg : _sub_graphs)
    {
        ARM_COMPUTE_ERROR_ON(sg.get() == nullptr);

        // IO buffers
        std::unique_ptr<ITensorObject> in;
        std::unique_ptr<ITensorObject> out;
        std::unique_ptr<ITensorObject> func_in;

        // Create input sub-tensor
        if(!sg->has_input())
        {
            in = arm_compute::support::cpp14::make_unique<SubTensor>(*dynamic_cast<Tensor *>(input),
                                                                     input->tensor()->info()->tensor_shape(),
                                                                     Coordinates());
        }

        // Create output sub-tensor
        if(!sg->has_output())
        {
            ITensorInfo *info = input->tensor()->info();
            func_in           = arm_compute::support::cpp14::make_unique<Tensor>(TensorInfo(info->num_channels(), info->data_type(), info->fixed_point_position()));
            func_in->set_target(ctx.hints().target_hint());
            out = arm_compute::support::cpp14::make_unique<SubTensor>(func_in->tensor(),
                                                                      TensorShape(),
                                                                      Coordinates(0, 0, 0),
                                                                      func_in->target(),
                                                                      true);
        }

        // Construct sub_graph
        auto g = sg->construct(ctx, std::move(in), std::move(out));

        // Register graph to function
        func->register_graph(std::move(g), std::move(func_in));
    }

    func->configure();

    return std::move(func);
}