instruction.hpp 4.72 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
5
6
7
#ifndef MIGRAPH_GUARD_MIGRAPHLIB_INSTRUCTION_HPP
#define MIGRAPH_GUARD_MIGRAPHLIB_INSTRUCTION_HPP

#include <migraph/literal.hpp>
#include <migraph/shape.hpp>
#include <migraph/builtin.hpp>
#include <migraph/instruction_ref.hpp>
Paul's avatar
Paul committed
8
#include <migraph/operation.hpp>
Paul's avatar
Paul committed
9
#include <migraph/erase.hpp>
Paul's avatar
Paul committed
10
#include <string>
Paul's avatar
Paul committed
11
#include <utility>
Paul's avatar
Paul committed
12

Paul's avatar
Paul committed
13
namespace migraph {
Paul's avatar
Paul committed
14

Paul's avatar
Paul committed
15
shape compute_shape(const operation& op, const std::vector<instruction_ref>& args);
Paul's avatar
Paul committed
16

Paul's avatar
Paul committed
17
18
struct instruction
{
Paul's avatar
Paul committed
19
20
    instruction() {}

Paul's avatar
Paul committed
21
    instruction(operation o, shape r, std::vector<instruction_ref> args)
Paul's avatar
Paul committed
22
        : op(std::move(o)), result(std::move(r)), arguments(std::move(args))
Paul's avatar
Paul committed
23
24
    {
    }
Paul's avatar
Paul committed
25

Paul's avatar
Paul committed
26
    instruction(literal l) : op(builtin::literal{}), result(l.get_shape()), lit(std::move(l)) {}
Paul's avatar
Paul committed
27

28
    // internal
Paul's avatar
Paul committed
29
    void replace(operation o, const shape& r, std::vector<instruction_ref> args)
Paul's avatar
Paul committed
30
    {
Paul's avatar
Paul committed
31
32
        op = std::move(o);
        replace(r);
Paul's avatar
Paul committed
33
34
35
        replace(std::move(args));
    }

Paul's avatar
Paul committed
36
    void replace(const shape& r)
Paul's avatar
Paul committed
37
38
39
40
    {
        if(r != result)
        {
            result = r;
Paul's avatar
Paul committed
41
            for(auto&& ins : output)
Paul's avatar
Paul committed
42
            {
Paul's avatar
Paul committed
43
                assert(ins->op.name().front() != '@');
Paul's avatar
Paul committed
44
                ins->recompute_shape();
Paul's avatar
Paul committed
45
46
47
48
            }
        }
    }

Paul's avatar
Paul committed
49
    void recompute_shape() { replace(compute_shape(op, arguments)); }
Paul's avatar
Paul committed
50

51
    // internal
Paul's avatar
Paul committed
52
53
54
55
56
57
    void replace(std::vector<instruction_ref> args)
    {
        clear_arguments();
        arguments = std::move(args);
    }

58
    // internal
Paul's avatar
Paul committed
59
60
61
    void replace_argument(instruction_ref old, instruction_ref new_ins)
    {
        std::replace(arguments.begin(), arguments.end(), old, new_ins);
Paul's avatar
Paul committed
62
        old->remove_output(*this);
Paul's avatar
Paul committed
63
64
    }

Paul's avatar
Paul committed
65
66
    void clear_arguments()
    {
Paul's avatar
Paul committed
67
        for(auto&& arg : arguments)
Paul's avatar
Paul committed
68
        {
Paul's avatar
Paul committed
69
            arg->remove_output(*this);
Paul's avatar
Paul committed
70
        }
Paul's avatar
Paul committed
71
        arguments.clear();
Paul's avatar
Paul committed
72
73
74
75
76
77
78
    }

    friend bool operator==(const instruction& i, instruction_ref ref)
    {
        return std::addressof(i) == std::addressof(*ref);
    }

Paul's avatar
Paul committed
79
    bool valid(instruction_ref start) const
Paul's avatar
Paul committed
80
    {
Paul's avatar
Paul committed
81
        return valid() && std::all_of(arguments.begin(), arguments.end(), [&](instruction_ref i) {
Paul's avatar
Paul committed
82
83
84
85
86
87
88
                   auto self = std::find(i->output.begin(), i->output.end(), *this);
                   return self != i->output.end() &&
                          std::distance(start, i) < std::distance(start, *self);
               });
    }

    bool valid() const
Paul's avatar
Paul committed
89
    {
Paul's avatar
Paul committed
90
        shape computed;
Paul's avatar
Paul committed
91
        if(op.name() == "@literal")
Paul's avatar
Paul committed
92
        {
Paul's avatar
Paul committed
93
            computed = lit.get_shape();
Paul's avatar
Paul committed
94
        }
Paul's avatar
Paul committed
95
        else if(op.name() == "@param")
Paul's avatar
Paul committed
96
        {
Paul's avatar
Paul committed
97
98
            computed = result;
        }
Paul's avatar
Paul committed
99
100
        else
        {
Paul's avatar
Paul committed
101
102
103
104
105
106
107
108
            try
            {
                computed = compute_shape(op, arguments);
            }
            catch(migraph::exception&)
            {
                return false;
            }
Paul's avatar
Paul committed
109
        }
Paul's avatar
Paul committed
110
        return result == computed &&
Paul's avatar
Paul committed
111
112
113
114
               std::all_of(output.begin(), output.end(), [&](instruction_ref i) {
                   return std::find(i->arguments.begin(), i->arguments.end(), *this) !=
                          i->arguments.end();
               });
Paul's avatar
Paul committed
115
116
    }

wsttiger's avatar
wsttiger committed
117
    shape get_shape() const { return result; }
118

Paul's avatar
Paul committed
119
    friend bool operator==(instruction_ref ref, const instruction& i) { return i == ref; }
Paul's avatar
Paul committed
120

Paul's avatar
Paul committed
121
    friend bool operator!=(const instruction& i, instruction_ref ref) { return !(i == ref); }
Paul's avatar
Paul committed
122

Paul's avatar
Paul committed
123
    friend bool operator!=(instruction_ref ref, const instruction& i) { return !(i == ref); }
Paul's avatar
Paul committed
124

Paul's avatar
Paul committed
125
126
127
128
129
130
    void add_output(instruction_ref ins)
    {
        if(std::find(output.begin(), output.end(), ins) == output.end())
            output.push_back(ins);
    }

Paul's avatar
Paul committed
131
    template <class T>
Paul's avatar
Paul committed
132
133
134
135
136
    void remove_output(const T& ins)
    {
        migraph::erase(output, ins);
    }

Paul's avatar
Paul committed
137
    operation op;
Paul's avatar
Paul committed
138
    shape result;
Paul's avatar
Paul committed
139
    std::vector<instruction_ref> output;
Paul's avatar
Paul committed
140
    std::vector<instruction_ref> arguments;
Paul's avatar
Paul committed
141
    literal lit;
Paul's avatar
Paul committed
142
143
};

Paul's avatar
Paul committed
144
145
146
inline void backreference(instruction_ref ref)
{
    for(auto&& arg : ref->arguments)
Paul's avatar
Paul committed
147
        arg->add_output(ref);
Paul's avatar
Paul committed
148
149
}

Paul's avatar
Paul committed
150
151
152
153
154
155
156
inline void replace_argument(instruction_ref ins, instruction_ref old, instruction_ref new_ins)
{
    ins->replace_argument(old, new_ins);
    backreference(ins);
    ins->recompute_shape();
}

Paul's avatar
Paul committed
157
158
// TODO: Move to a cpp file
// TODO: Use const ref for vector
Paul's avatar
Paul committed
159
inline shape compute_shape(const operation& op, const std::vector<instruction_ref>& args)
Paul's avatar
Paul committed
160
161
162
163
164
165
166
{
    std::vector<shape> shapes(args.size());
    std::transform(
        args.begin(), args.end(), shapes.begin(), [](instruction_ref i) { return i->result; });
    return op.compute_shape(shapes);
}

Paul's avatar
Paul committed
167
} // namespace migraph
Paul's avatar
Paul committed
168

Paul's avatar
Paul committed
169
170
171
namespace std {
template <>
struct hash<migraph::instruction_ref>
172
{
Paul's avatar
Paul committed
173
174
175
    using argument_type = migraph::instruction_ref;
    using result_type   = std::size_t;
    result_type operator()(const argument_type& x) const noexcept
176
    {
Paul's avatar
Paul committed
177
178
179
        return std::hash<migraph::instruction*>{}(&*x);
    }
};
180
181
} // namespace std

Paul's avatar
Paul committed
182
#endif