"research/maskgan/model_utils/helper.py" did not exist on "813dd09aae74854dd3984226e7301fe804e0b825"
matcher.hpp 7.42 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
5
6
7
#ifndef MIGRAPH_GUARD_RTGLIB_MATCHER_HPP
#define MIGRAPH_GUARD_RTGLIB_MATCHER_HPP

#include <migraph/functional.hpp>
#include <migraph/ranges.hpp>
#include <migraph/instruction.hpp>
#include <migraph/program.hpp>
Paul's avatar
Paul committed
8
#include <migraph/iterator_for.hpp>
Paul's avatar
Paul committed
9
10
11
12
#include <unordered_map>

namespace migraph {

Paul's avatar
Paul committed
13
14
namespace matchers {

Paul's avatar
Paul committed
15
16
struct matcher_context
{
Paul's avatar
Paul committed
17
    matcher_context(instruction_ref i) : last(i) {}
Paul's avatar
Paul committed
18
    std::unordered_map<std::string, instruction_ref> instructions;
Paul's avatar
Paul committed
19
20
    instruction_ref not_found() const { return last; }

Paul's avatar
Paul committed
21
    private:
Paul's avatar
Paul committed
22
    instruction_ref last;
Paul's avatar
Paul committed
23
24
};

Paul's avatar
Paul committed
25
template <class P>
Paul's avatar
Paul committed
26
27
28
29
30
31
32
33
34
35
36
37
38
struct predicate_matcher
{
    P p;

    instruction_ref match(matcher_context& ctx, instruction_ref ins) const
    {
        assert(ins != ctx.not_found());
        if(p(ins))
            return ins;
        return ctx.not_found();
    }
};

Paul's avatar
Paul committed
39
template <class F>
Paul's avatar
Paul committed
40
41
42
43
44
45
46
47
48
49
50
struct function_matcher
{
    F f;

    instruction_ref match(matcher_context& ctx, instruction_ref ins) const
    {
        assert(ins != ctx.not_found());
        return f(ctx, ins);
    }
};

Paul's avatar
Paul committed
51
template <class F>
Paul's avatar
Paul committed
52
53
54
55
56
function_matcher<F> make_function_matcher(F f)
{
    return {f};
}

Paul's avatar
Paul committed
57
template <class M>
Paul's avatar
Paul committed
58
59
auto bind_match(M m, std::string name)
{
Paul's avatar
Paul committed
60
61
62
63
64
65
66
    return make_function_matcher(
        [ =, name = std::move(name) ](matcher_context & ctx, instruction_ref ins) {
            auto result = m.match(ctx, ins);
            if(result != ctx.not_found())
                ctx.instructions.emplace(name, ins);
            return result;
        });
Paul's avatar
Paul committed
67
68
}

Paul's avatar
Paul committed
69
template <class M>
Paul's avatar
Paul committed
70
71
72
73
struct bindable_matcher
{
    M m;

Paul's avatar
Paul committed
74
    auto bind(std::string name) { return bind_match(m, std::move(name)); }
Paul's avatar
Paul committed
75
76
77
78
79
80
81

    instruction_ref match(matcher_context& ctx, instruction_ref ins) const
    {
        return m.match(ctx, ins);
    }
};

Paul's avatar
Paul committed
82
template <class M>
Paul's avatar
Paul committed
83
84
85
86
87
bindable_matcher<M> make_bindable_matcher(M m)
{
    return {m};
}

Paul's avatar
Paul committed
88
template <class F>
Paul's avatar
Paul committed
89
90
91
92
93
bindable_matcher<function_matcher<F>> make_bf_matcher(F f)
{
    return {{f}};
}

Paul's avatar
Paul committed
94
template <class F>
Paul's avatar
Paul committed
95
96
97
98
99
100
101
102
103
bindable_matcher<predicate_matcher<F>> make_bp_matcher(F f)
{
    return {{f}};
}

using bool_list = std::initializer_list<bool>;

struct id_matcher
{
Paul's avatar
Paul committed
104
    instruction_ref match(matcher_context&, instruction_ref ins) const { return ins; }
Paul's avatar
Paul committed
105
106
};

Paul's avatar
Paul committed
107
template <class M>
Paul's avatar
Paul committed
108
109
110
111
struct basic_matcher
{
    M m;

Paul's avatar
Paul committed
112
    template <class... Ts>
Paul's avatar
Paul committed
113
114
115
116
117
118
    auto operator()(Ts... ms) const
    {
        // Copy m because we cant capture `this` by value
        auto mm = m;
        return make_bf_matcher([=](matcher_context& ctx, instruction_ref ins) {
            auto result = mm.match(ctx, ins);
Paul's avatar
Paul committed
119
            if(result != ctx.not_found())
Paul's avatar
Paul committed
120
121
122
123
124
125
126
127
128
129
130
            {
                bool matches = fold([&](auto x, auto y) {
                    return x and y.match(ctx, result) != ctx.not_found();
                })(true, ms...);
                if(matches)
                    return result;
            }
            return ctx.not_found();
        });
    }

Paul's avatar
Paul committed
131
    auto bind(std::string name) { return bind_match(m, name); }
Paul's avatar
Paul committed
132
133
134
135
136
137
138

    instruction_ref match(matcher_context& ctx, instruction_ref ins) const
    {
        return m.match(ctx, ins);
    }
};

Paul's avatar
Paul committed
139
template <class M>
Paul's avatar
Paul committed
140
141
142
143
144
basic_matcher<M> make_basic_matcher(M m)
{
    return {m};
}

Paul's avatar
Paul committed
145
template <class F>
Paul's avatar
Paul committed
146
147
148
149
150
basic_matcher<function_matcher<F>> make_basic_fun_matcher(F f)
{
    return {{f}};
}

Paul's avatar
Paul committed
151
template <class P>
Paul's avatar
Paul committed
152
153
154
155
156
basic_matcher<predicate_matcher<P>> make_basic_pred_matcher(P p)
{
    return {{p}};
}

Paul's avatar
Paul committed
157
158
159
160
161
#define MIGRAPH_BASIC_MATCHER(name, ...)                                        \
    struct name##_m                                                             \
    {                                                                           \
        instruction_ref match(__VA_ARGS__) const;                               \
    };                                                                          \
Paul's avatar
Paul committed
162
    const constexpr auto name = migraph::matchers::basic_matcher<name##_m>{{}}; \
Paul's avatar
Paul committed
163
164
    inline instruction_ref name##_m::match(__VA_ARGS__) const

Paul's avatar
Paul committed
165
166
167
168
169
#define MIGRAPH_PRED_MATCHER(name, ...)                                                            \
    struct name##_m                                                                                \
    {                                                                                              \
        bool operator()(__VA_ARGS__) const;                                                        \
    };                                                                                             \
Paul's avatar
Paul committed
170
    const constexpr auto name = migraph::matchers::basic_matcher<predicate_matcher<name##_m>>{{}}; \
Paul's avatar
Paul committed
171
    inline bool name##_m::operator()(__VA_ARGS__) const
Paul's avatar
Paul committed
172
173
174
175
176
177
178

struct matcher_result
{
    std::unordered_map<std::string, instruction_ref> instructions;
    instruction_ref result;
};

Paul's avatar
Paul committed
179
template <class M>
Paul's avatar
Paul committed
180
181
182
183
184
matcher_result match_instruction(program& p, instruction_ref ins, M&& m)
{
    assert(ins != p.end());
    matcher_result result;
    matcher_context ctx{p.end()};
Paul's avatar
Paul committed
185
    result.result       = m.match(ctx, ins);
Paul's avatar
Paul committed
186
    result.instructions = ctx.instructions;
Paul's avatar
Paul committed
187
    return result;
Paul's avatar
Paul committed
188
189
}

Paul's avatar
Paul committed
190
template <class... Ms>
Paul's avatar
Paul committed
191
192
void find_matches(program& p, Ms&&... ms)
{
Paul's avatar
Paul committed
193
    for(auto ins : iterator_for(p))
Paul's avatar
Paul committed
194
195
    {
        bool match = false;
Paul's avatar
Paul committed
196
197
        each_args(
            [&](auto&& m) {
Paul's avatar
Paul committed
198
                // cppcheck-suppress knownConditionTrueFalse
Paul's avatar
Paul committed
199
200
201
202
203
204
205
206
207
                if(match)
                    return;
                auto r = match_instruction(p, ins, m.matcher());
                if(r.result == p.end())
                    return;
                m.apply(r);
                match = true;
            },
            ms...);
Paul's avatar
Paul committed
208
209
210
    }
}

Paul's avatar
Paul committed
211
template <class... Ts>
Paul's avatar
Paul committed
212
213
214
215
216
217
218
219
220
221
222
223
auto all_of(Ts... ms)
{
    return make_bf_matcher([=](matcher_context& ctx, instruction_ref ins) {
        bool matches = fold([&](auto x, auto y) {
            return x and y.match(ctx, ins) != ctx.not_found();
        })(true, ms...);
        if(matches)
            return ins;
        return ctx.not_found();
    });
}

Paul's avatar
Paul committed
224
template <class... Ts>
Paul's avatar
Paul committed
225
226
227
228
229
230
231
232
233
234
235
236
auto none_of(Ts... ms)
{
    return make_bf_matcher([=](matcher_context& ctx, instruction_ref ins) {
        bool matches = fold([&](auto x, auto y) {
            return x and y.match(ctx, ins) == ctx.not_found();
        })(true, ms...);
        if(matches)
            return ins;
        return ctx.not_found();
    });
}

Paul's avatar
Paul committed
237
template <class... Ts>
Paul's avatar
Paul committed
238
239
240
auto any_of(Ts... ms)
{
    return make_bf_matcher([=](matcher_context& ctx, instruction_ref ins) {
Paul's avatar
Paul committed
241
242
        bool matches = fold(
            [&](auto x, auto y) { return x or y.match(ctx, ins) != ctx.not_found(); })(true, ms...);
Paul's avatar
Paul committed
243
244
245
246
247
248
        if(matches)
            return ins;
        return ctx.not_found();
    });
}

Paul's avatar
Paul committed
249
MIGRAPH_PRED_MATCHER(standard_shape, instruction_ref ins) { return ins->get_shape().standard(); }
Paul's avatar
Paul committed
250
251
252

inline auto name(std::string name)
{
Paul's avatar
Paul committed
253
254
    return make_basic_pred_matcher(
        [ =, name = std::move(name) ](instruction_ref ins) { return ins->name() == name; });
Paul's avatar
Paul committed
255
256
257
258
259
260
261
262
263
264
265
266
}

inline auto arg(std::size_t i)
{
    return make_basic_fun_matcher([=](matcher_context& ctx, instruction_ref ins) {
        if(i < ins->inputs().size())
            return ins->inputs()[i];
        return ctx.not_found();
    });
}

// Workaround for bugs in clang
Paul's avatar
Paul committed
267
268
269
270
template <std::size_t...>
struct args_impl_ints
{
};
Paul's avatar
Paul committed
271

Paul's avatar
Paul committed
272
template <std::size_t... Ns, class... Ms>
Paul's avatar
Paul committed
273
274
275
276
277
auto args_impl(args_impl_ints<Ns...>, Ms... ms)
{
    return matchers::all_of(arg(Ns)(ms)...);
}

Paul's avatar
Paul committed
278
template <class... Ms>
Paul's avatar
Paul committed
279
280
281
282
283
auto args(Ms... ms)
{
    return sequence_c<sizeof...(Ms)>([=](auto... is) {
        // It needs to be written as `decltype(is)::value` for gcc 5
        return args_impl(args_impl_ints<decltype(is)::value...>{}, ms...);
Paul's avatar
Paul committed
284
    });
Paul's avatar
Paul committed
285
286
287
288
289
290
291
}

} // namespace matchers

} // namespace migraph

#endif