operation.hpp 4.34 KB
Newer Older
Paul's avatar
Paul committed
1
2
#ifndef MIGRAPH_GUARD_MIGRAPHLIB_OPERAND_HPP
#define MIGRAPH_GUARD_MIGRAPHLIB_OPERAND_HPP
Paul's avatar
Paul committed
3

Paul's avatar
Paul committed
4
#include <cassert>
Paul's avatar
Paul committed
5
6
7
8
9
#include <string>
#include <functional>
#include <memory>
#include <type_traits>
#include <utility>
Paul's avatar
Paul committed
10
#include <migraph/shape.hpp>
Paul's avatar
Paul committed
11
#include <migraph/reflect.hpp>
Paul's avatar
Paul committed
12
#include <migraph/streamutils.hpp>
Paul's avatar
Paul committed
13
14
#include <migraph/argument.hpp>
#include <migraph/context.hpp>
Paul's avatar
Paul committed
15
#include <migraph/auto_any_cast.hpp>
Paul's avatar
Paul committed
16

Paul's avatar
Paul committed
17
namespace migraph {
Paul's avatar
Paul committed
18

Paul's avatar
Paul committed
19
20
21
22
23
24
25
26
27
28
29
#ifdef DOXYGEN

/// The operation interface represents an action an instruction will perform. All
/// operation classes must be CopyConstructible.
struct operation
{
    /// A unique name identifying the operation
    std::string name() const;
    /// This is used to compute the resulting shape from an operation. If an
    /// operation cannot be run with input shapes, then it should throw an
    /// exception.
Paul's avatar
Paul committed
30
    shape compute_shape(const std::vector<shape>& input) const;
Paul's avatar
Paul committed
31
    /**
Paul's avatar
Paul committed
32
33
     * @brief This performs the operation's computation.
     *
Paul's avatar
Paul committed
34
35
     * This method can be optional when the operation is only used as a placeholder to be lowered
     * later on.
Paul's avatar
Paul committed
36
37
38
39
40
     *
     * @param ctx This is the context created by the `target` during compilation. Implementations
     * can use the target's `context` class rather than the `context` interface class.
     * @param output This is the output shape. It is equivalent to running `compute_shape` with each
     * `shape` of the `argument`.
Paul's avatar
Paul committed
41
     * @param input This is the `argument` result from the previous instruction's computation.
Paul's avatar
Paul committed
42
43
     * @return Return an `argument` of the result computation. The `shape` of `argument` should be
     * the same the `output` shape.
Paul's avatar
Paul committed
44
     */
Paul's avatar
Paul committed
45
    argument compute(context& ctx, const shape& output, const std::vector<argument>& input) const;
Paul's avatar
Paul committed
46
47
    /// An optional stream operator to print the operation. When this is not
    /// implemented, it will just print the operation's name.
Paul's avatar
Paul committed
48
    friend std::ostream& operator<<(std::ostream& os, const operation& op);
Paul's avatar
Paul committed
49
50
51
52
};

#else

Paul's avatar
Paul committed
53
54
namespace operation_stream {

Paul's avatar
Paul committed
55
56
template <class T>
auto operator<<(std::ostream& os, const T& x) -> decltype(os << x.name())
Paul's avatar
Paul committed
57
{
Paul's avatar
Paul committed
58
59
60
61
    os << x.name();
    char delim = '[';
    reflect_each(x, [&](auto& y, auto name, auto&&...) {
        os << delim;
Paul's avatar
Paul committed
62
63
        os << name << "=";
        stream_write_value(os, y);
Paul's avatar
Paul committed
64
65
        delim = ',';
    });
Paul's avatar
Paul committed
66
67
    if(delim == ',')
        os << "]";
Paul's avatar
Paul committed
68
    return os;
Paul's avatar
Paul committed
69
70
}

Paul's avatar
Paul committed
71
} // namespace operation_stream
Paul's avatar
Paul committed
72

Paul's avatar
Paul committed
73
74
75
76
77
78
79
80
81
82
83
84
85
namespace operation_equal {

template <class T, class U>
auto operator==(const T& x, const U& y) -> decltype(x.name() == y.name())
{
    if(x.name() != y.name())
        return false;
    const auto& yy = any_cast<T>(y);
    return reflect_tie(x) == reflect_tie(yy);
}

} // namespace operation_equal

Paul's avatar
Paul committed
86
template <class T>
Paul's avatar
Paul committed
87
88
89
90
91
92
auto compute_op(rank<1>,
                const T& x,
                context& ctx,
                const shape& output_shape,
                const std::vector<argument>& input)
    -> decltype(x.compute(auto_any_cast(ctx), output_shape, input))
Paul's avatar
Paul committed
93
94
95
96
97
{
    return x.compute(auto_any_cast(ctx), output_shape, input);
}

template <class T>
Paul's avatar
Paul committed
98
argument compute_op(rank<0>, const T& x, context&, const shape&, const std::vector<argument>&)
Paul's avatar
Paul committed
99
{
Paul's avatar
Paul committed
100
101
    std::string name = x.name();
    MIGRAPH_THROW("Not computable: " + name);
Paul's avatar
Paul committed
102
103
}

Paul's avatar
Paul committed
104
template <class T>
Paul's avatar
Paul committed
105
106
argument
compute_op(const T& x, context& ctx, const shape& output_shape, const std::vector<argument>& input)
Paul's avatar
Paul committed
107
{
Paul's avatar
Paul committed
108
    return compute_op(rank<1>{}, x, ctx, output_shape, input);
Paul's avatar
Paul committed
109
110
}

111
<%
Paul's avatar
Paul committed
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
 interface(
     'operation',
     virtual('name', returns = 'std::string', const = True),
     virtual('compute_shape', returns = 'shape', input = 'const std::vector<shape>&', const = True),
     virtual('compute',
             returns = 'argument',
             ctx     = 'context&',
             output  = 'const shape&',
             input   = 'const std::vector<argument>&',
             const   = True,
             default = 'compute_op'),
     friend('operator<<',
            returns = 'std::ostream &',
            os      = 'std::ostream &',
            op      = 'const operation &',
            using   = 'migraph::operation_stream::operator<<'),
     friend('operator==',
            returns = 'bool',
            x       = 'const operation &',
            y       = 'const operation &',
            using   = 'migraph::operation_equal::operator==')) %>

    inline bool operator!=(const operation& x, const operation& y)
Paul's avatar
Paul committed
135
136
137
138
{
    return !(x == y);
}

Paul's avatar
Paul committed
139
140
#endif

Paul's avatar
Paul committed
141
} // namespace migraph
Paul's avatar
Paul committed
142
143

#endif