operation.hpp 3.3 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/rank.hpp>
Paul's avatar
Paul committed
12
13
#include <migraph/argument.hpp>
#include <migraph/context.hpp>
Paul's avatar
Paul committed
14
#include <migraph/auto_any_cast.hpp>
Paul's avatar
Paul committed
15

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

Paul's avatar
Paul committed
18
19
20
21
22
23
24
25
26
27
28
#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
29
    shape compute_shape(const std::vector<shape>& input) const;
Paul's avatar
Paul committed
30
    /**
Paul's avatar
Paul committed
31
32
33
34
     * @brief This performs the operation's computation. 
     * 
     * This method can be optional when the operation is only used as a placeholder to be lowered
     * later on.
Paul's avatar
Paul committed
35
36
37
38
39
     *
     * @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
40
     * @param input This is the `argument` result from the previous instruction's computation.
Paul's avatar
Paul committed
41
42
     * @return Return an `argument` of the result computation. The `shape` of `argument` should be
     * the same the `output` shape.
Paul's avatar
Paul committed
43
     */
Paul's avatar
Paul committed
44
    argument compute(context& ctx, const shape& output, const std::vector<argument>& input) const;
Paul's avatar
Paul committed
45
46
    /// 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
47
    friend std::ostream& operator<<(std::ostream& os, const operation& op);
Paul's avatar
Paul committed
48
49
50
51
};

#else

Paul's avatar
Paul committed
52
53
namespace operation_stream {

Paul's avatar
Paul committed
54
55
template <class T>
auto operator<<(std::ostream& os, const T& x) -> decltype(os << x.name())
Paul's avatar
Paul committed
56
{
Paul's avatar
Paul committed
57
    return os << x.name();
Paul's avatar
Paul committed
58
59
}

Paul's avatar
Paul committed
60
} // namespace operation_stream
Paul's avatar
Paul committed
61

Paul's avatar
Paul committed
62
template <class T>
Paul's avatar
Paul committed
63
64
65
66
67
68
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
69
70
71
72
73
{
    return x.compute(auto_any_cast(ctx), output_shape, input);
}

template <class T>
Paul's avatar
Paul committed
74
argument compute_op(rank<0>, const T& x, context&, const shape&, const std::vector<argument>&)
Paul's avatar
Paul committed
75
{
Paul's avatar
Paul committed
76
77
    std::string name = x.name();
    MIGRAPH_THROW("Not computable: " + name);
Paul's avatar
Paul committed
78
79
}

Paul's avatar
Paul committed
80
template <class T>
Paul's avatar
Paul committed
81
82
argument
compute_op(const T& x, context& ctx, const shape& output_shape, const std::vector<argument>& input)
Paul's avatar
Paul committed
83
{
Paul's avatar
Paul committed
84
    return compute_op(rank<1>{}, x, ctx, output_shape, input);
Paul's avatar
Paul committed
85
86
}

87
<%
Paul's avatar
Paul committed
88
89
interface('operation',
    virtual('name', returns='std::string', const=True),
Paul's avatar
Paul committed
90
91
    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'),
Paul's avatar
Paul committed
92
    friend('operator<<', returns='std::ostream &', os='std::ostream &', op='const operation &', using='migraph::operation_stream::operator<<')
Paul's avatar
Paul committed
93
)
94
%>
Paul's avatar
Paul committed
95

Paul's avatar
Paul committed
96
97
#endif

Paul's avatar
Paul committed
98
} // namespace migraph
Paul's avatar
Paul committed
99
100

#endif