aboutsummaryrefslogtreecommitdiff
path: root/arm_compute/graph/NodeContext.h
blob: 8e8a761d22eb2c90b9acc66c5d597fb140bb83d6 (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
/*
 * Copyright (c) 2017 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.
 */
#ifndef __ARM_COMPUTE_GRAPH_NODE_CONTEXT_H__
#define __ARM_COMPUTE_GRAPH_NODE_CONTEXT_H__

#include "arm_compute/core/Error.h"
#include "arm_compute/graph/NodeParameter.h"
#include "arm_compute/graph/Types.h"
#include "support/ToolchainSupport.h"

#include <map>
#include <memory>
#include <string>

namespace arm_compute
{
namespace graph
{
/** Node Context class
 *
 * Node context class is used to hold all the parameters required by a node to execute
 */
class NodeContext
{
public:
    /** Default Constructor
     * (TODO(geopin01): Should we have an enum with all the supported ops instead?)
     *
     * @param[in] operation Name of the operation
     */
    NodeContext(std::string operation)
        : _operation(operation), _target(TargetHint::DONT_CARE), _inputs(), _outputs(), _parameters() {};
    /** Sets the execution target of the node
     *
     * @param[in] target Execution target of the node
     */
    void set_target(TargetHint target);
    /** Adds an input tensor to the context
     *
     * @param[in] input Input to add
     */
    void add_input(arm_compute::ITensor *input);
    /** Adds and output to the context
     *
     * @param[in] output Output to add
     */
    void add_output(arm_compute::ITensor *output);
    /** Adds a parameter to the context
     *
     * @param[in] name      Parameter name
     * @param[in] parameter Parameter to add
     */
    template <typename T>
    void add_parameter(std::string name, T parameter);
    /** Returns the operation of this node.
     *
     * @return The operation name
     */
    std::string operation() const;
    /** Returns the execution target of this node
     *
     * @return The execution target
     */
    TargetHint target() const;
    /** Returns input tensor of a given index
     *
     * @param[in] idx Index of the input tensor
     *
     * @return A pointer the requested input tensor else nullptr
     */
    arm_compute::ITensor *input(size_t idx) const;
    /** Returns output tensor of a given index
     *
     * @param[in] idx Index of the output tensor
     *
     * @return A pointer the requested output tensor else nullptr
     */
    arm_compute::ITensor *output(size_t idx) const;
    /** Returns the parameter with the given name
     *
     * @param[in] name Parameter name
     *
     * @return The requested parameter else an empty object
     */
    template <typename T>
    T parameter(std::string name) const;
    /** Returns number of inputs
     *
     * @return Number of inputs
     */
    size_t num_inputs() const;
    /** Returns number of output
     *
     * @return Number of outputs
     */
    size_t num_outputs() const;

private:
    std::string                         _operation;
    TargetHint                          _target;
    std::vector<arm_compute::ITensor *> _inputs;
    std::vector<arm_compute::ITensor *> _outputs;
    std::map<std::string, std::unique_ptr<NodeParameterBase>> _parameters;
};

template <typename T>
inline void NodeContext::add_parameter(std::string name, T parameter)
{
    ARM_COMPUTE_ERROR_ON_MSG(_parameters.find(name) != _parameters.end(), "Parameter already exists!");
    _parameters[name] = support::cpp14::make_unique<NodeParameter<T>>(name, parameter);
}

template <typename T>
inline T NodeContext::parameter(std::string name) const
{
    auto it = _parameters.find(name);
    ARM_COMPUTE_ERROR_ON(it == _parameters.end());
    return static_cast<NodeParameter<T> *>(it->second.get())->value();
}
} // namespace graph
} // namespace arm_compute
#endif /* __ARM_COMPUTE_GRAPH_NODE_CONTEXT_H__ */