instruction.cpp 11.6 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
#include <migraphx/instruction.hpp>
#include <migraphx/builtin.hpp>
#include <migraphx/erase.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
4
5
#include <migraphx/module.hpp>
#include <migraphx/ranges.hpp>
Paul's avatar
Paul committed
6

Paul's avatar
Paul committed
7
namespace migraphx {
Paul's avatar
Paul committed
8
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Paul committed
9

Paul's avatar
Paul committed
10
11
12
13
instruction::instruction(operation o, shape r, std::vector<instruction_ref> args)
    : op(std::move(o)), result(std::move(r)), arguments(std::move(args))
{
}
Paul's avatar
Paul committed
14

Shucai Xiao's avatar
Shucai Xiao committed
15
16
17
18
19
20
21
22
23
24
25
instruction::instruction(operation o,
                         shape r,
                         std::vector<instruction_ref> args,
                         std::vector<module_ref> modules)
    : op(std::move(o)),
      result(std::move(r)),
      arguments(std::move(args)),
      module_args(std::move(modules))
{
}

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

Paul's avatar
Paul committed
31
32
33
void instruction::replace(const shape& r)
{
    if(r != result)
Paul's avatar
Paul committed
34
    {
Paul's avatar
Paul committed
35
36
        result = r;
        for(auto&& ins : output)
Paul's avatar
Paul committed
37
        {
38
39
40
            if(ins->name() == "@return")
                continue;

Paul's avatar
Paul committed
41
42
            assert(ins->name().front() != '@');
            ins->recompute_shape();
Paul's avatar
Paul committed
43
44
        }
    }
Paul's avatar
Paul committed
45
}
Paul's avatar
Paul committed
46

Paul's avatar
Paul committed
47
void instruction::replace(operation o)
Paul's avatar
Paul committed
48
{
49
50
    normalized = false;
    op         = std::move(o);
Paul's avatar
Paul committed
51
52
53
    recompute_shape();
}

Shucai Xiao's avatar
Shucai Xiao committed
54
void instruction::recompute_shape() { replace(compute_shape(op, arguments, module_args)); }
Paul's avatar
Paul committed
55

Paul's avatar
Paul committed
56
57
58
void instruction::clear_arguments()
{
    for(auto&& arg : arguments)
Paul's avatar
Paul committed
59
    {
Paul's avatar
Paul committed
60
        arg->remove_output(*this);
Paul's avatar
Paul committed
61
    }
Paul's avatar
Paul committed
62
    arguments.clear();
Shucai Xiao's avatar
Shucai Xiao committed
63
    module_args.clear();
Paul's avatar
Paul committed
64
65
66
67
68
69
}

bool operator==(const instruction& i, instruction_ref ref)
{
    return std::addressof(i) == std::addressof(*ref);
}
Paul's avatar
Paul committed
70

Shucai Xiao's avatar
Shucai Xiao committed
71
bool instruction::valid(instruction_ref start, bool check_order) const
Paul's avatar
Paul committed
72
73
74
{
    return valid() && std::all_of(arguments.begin(), arguments.end(), [&](instruction_ref i) {
               auto self = std::find(i->outputs().begin(), i->outputs().end(), *this);
Shucai Xiao's avatar
Shucai Xiao committed
75
76
77
78
79
80
               bool ret  = self != i->outputs().end();
               if(check_order)
               {
                   ret = ret and (std::distance(start, i) < std::distance(start, *self));
               }
               return ret;
Paul's avatar
Paul committed
81
82
83
84
85
86
87
           });
}

bool instruction::valid() const
{
    shape computed;
    if(op.name() == "@literal")
Paul's avatar
Paul committed
88
    {
Paul's avatar
Paul committed
89
        computed = lit.get_shape();
Paul's avatar
Paul committed
90
    }
Paul's avatar
Paul committed
91
    else if(op.name() == "@param")
Paul's avatar
Paul committed
92
    {
Paul's avatar
Paul committed
93
        computed = result;
Paul's avatar
Paul committed
94
    }
95
96
97
98
    else if(op.name() == "@return")
    {
        computed = {};
    }
Paul's avatar
Paul committed
99
    else
Paul's avatar
Paul committed
100
    {
Paul's avatar
Paul committed
101
        try
Paul's avatar
Paul committed
102
        {
Shucai Xiao's avatar
Shucai Xiao committed
103
            computed = compute_shape(op, arguments, module_args);
Paul's avatar
Paul committed
104
        }
Paul's avatar
Paul committed
105
        catch(migraphx::exception&)
Paul's avatar
Paul committed
106
        {
Paul's avatar
Paul committed
107
            return false;
Paul's avatar
Paul committed
108
109
        }
    }
110

Shucai Xiao's avatar
Shucai Xiao committed
111
112
    return (result == computed) &&
           std::all_of(output.begin(), output.end(), [&](instruction_ref i) {
Paul's avatar
Paul committed
113
114
115
               return std::find(i->inputs().begin(), i->inputs().end(), *this) != i->inputs().end();
           });
}
Paul's avatar
Paul committed
116

Paul's avatar
Paul committed
117
118
119
120
121
122
shape instruction::get_shape() const { return result; }
const literal& instruction::get_literal() const
{
    assert(op.name() == "@literal");
    return lit;
}
Paul's avatar
Paul committed
123

Paul's avatar
Paul committed
124
const operation& instruction::get_operator() const { return op; }
Paul's avatar
Paul committed
125

Paul's avatar
Paul committed
126
std::string instruction::name() const { return op.name(); }
Paul's avatar
Paul committed
127

Paul's avatar
Paul committed
128
const std::vector<instruction_ref>& instruction::inputs() const { return arguments; }
Paul's avatar
Paul committed
129

Shucai Xiao's avatar
Shucai Xiao committed
130
131
const std::vector<module_ref>& instruction::module_inputs() const { return module_args; }

Paul's avatar
Paul committed
132
const std::vector<instruction_ref>& instruction::outputs() const { return output; }
Paul's avatar
Paul committed
133

Paul's avatar
Paul committed
134
135
bool operator==(const instruction& x, const instruction& y)
{
136
    if(std::tie(x.result, x.op, x.arguments) != std::tie(y.result, y.op, y.arguments))
Paul's avatar
Paul committed
137
138
139
140
141
        return false;
    if(x.name() == "@literal")
        return x.lit == y.lit;
    return true;
}
Paul's avatar
Paul committed
142

Paul's avatar
Paul committed
143
144
bool operator!=(const instruction& x, const instruction& y) { return !(x == y); }

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

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

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

Paul's avatar
Paul committed
151
152
153
154
155
void instruction::add_output(instruction_ref ins)
{
    if(std::find(output.begin(), output.end(), ins) == output.end())
        output.push_back(ins);
}
Paul's avatar
Paul committed
156

Paul's avatar
Paul committed
157
158
159
160
161
void instruction::backreference(instruction_ref ref)
{
    for(auto&& arg : ref->inputs())
        arg->add_output(ref);
}
Paul's avatar
Paul committed
162

Paul's avatar
Paul committed
163
164
165
166
167
168
169
170
void instruction::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
171

Shucai Xiao's avatar
Shucai Xiao committed
172
173
174
175
176
177
178
void instruction::replace_mod_argument(instruction_ref ins, module_ref old, module_ref new_mod)
{
    ins->replace_mod_argument(old, new_mod);
    backreference(ins);
    ins->recompute_shape();
}

Paul's avatar
Paul committed
179
180
181
182
183
184
185
186
void instruction::replace(instruction_ref ins,
                          operation o,
                          const shape& r,
                          std::vector<instruction_ref> args)
{
    ins->replace(std::move(o), r, std::move(args));
    backreference(ins);
}
Paul's avatar
Paul committed
187

Shucai Xiao's avatar
Shucai Xiao committed
188
189
190
191
192
193
194
195
196
197
void instruction::replace(instruction_ref ins,
                          operation o,
                          const shape& r,
                          std::vector<instruction_ref> args,
                          std::vector<module_ref> module_args)
{
    ins->replace(std::move(o), r, std::move(args), std::move(module_args));
    backreference(ins);
}

Paul's avatar
Paul committed
198
199
void instruction::replace(operation o, const shape& r, std::vector<instruction_ref> args)
{
200
201
    normalized = false;
    op         = std::move(o);
Paul's avatar
Paul committed
202
203
204
    replace(r);
    replace(std::move(args));
}
Paul's avatar
Paul committed
205

Shucai Xiao's avatar
Shucai Xiao committed
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
void instruction::replace(operation o,
                          const shape& r,
                          std::vector<instruction_ref> args,
                          std::vector<module_ref> mdl_args)
{
    op = std::move(o);
    replace(r);
    replace(std::move(args), std::move(mdl_args));
}

void instruction::replace_refs(
    instruction_ref ins,
    const std::unordered_map<instruction_ref, instruction_ref>& map_insts,
    const std::unordered_map<module_ref, module_ref>& map_mods)
{
    const auto& args = ins->inputs();
    for(const auto& arg : args)
    {
        if(contains(map_insts, arg))
        {
            instruction::replace_argument(ins, arg, map_insts.at(arg));
        }
    }

    const auto& module_args = ins->module_inputs();
    if(module_args.empty())
        return;

    for(const auto& mod : module_args)
    {
        if(contains(map_mods, mod))
        {
            instruction::replace_mod_argument(ins, mod, map_mods.at(mod));
        }
    }
}

Paul's avatar
Paul committed
243
244
245
246
247
void instruction::replace(std::vector<instruction_ref> args)
{
    clear_arguments();
    arguments = std::move(args);
}
Paul's avatar
Paul committed
248

Shucai Xiao's avatar
Shucai Xiao committed
249
250
251
252
253
254
255
void instruction::replace(std::vector<instruction_ref> args, std::vector<module_ref> mdl_args)
{
    clear_arguments();
    arguments   = std::move(args);
    module_args = std::move(mdl_args);
}

Paul's avatar
Paul committed
256
257
void instruction::replace_argument(instruction_ref old, instruction_ref new_ins)
{
Paul's avatar
Paul committed
258
    assert(std::any_of(arguments.begin(), arguments.end(), [&](auto i) { return i == old; }));
Paul's avatar
Paul committed
259
260
261
    std::replace(arguments.begin(), arguments.end(), old, new_ins);
    old->remove_output(*this);
}
Paul's avatar
Paul committed
262

Shucai Xiao's avatar
Shucai Xiao committed
263
264
265
266
267
268
void instruction::replace_mod_argument(module_ref old, module_ref new_mod)
{
    assert(std::any_of(module_args.begin(), module_args.end(), [&](auto i) { return i == old; }));
    std::replace(module_args.begin(), module_args.end(), old, new_mod);
}

Paul's avatar
Paul committed
269
270
271
272
273
274
bool instruction::can_eval() const
{
    if(op.name() == "@literal")
    {
        return true;
    }
Paul's avatar
Paul committed
275
    else if(is_context_free(op))
Paul's avatar
Paul committed
276
    {
Paul's avatar
Paul committed
277
278
        return std::all_of(
            this->inputs().begin(), this->inputs().end(), [](auto arg) { return arg->can_eval(); });
Paul's avatar
Paul committed
279
280
281
282
283
284
285
    }
    else
    {
        return false;
    }
}

Paul's avatar
Paul committed
286
argument instruction::eval(bool check_eval) const
Paul's avatar
Paul committed
287
288
289
290
291
{
    if(op.name() == "@literal")
    {
        return this->get_literal().get_argument();
    }
Paul's avatar
Paul committed
292
    if(is_context_free(op))
Paul's avatar
Paul committed
293
    {
Paul's avatar
Paul committed
294
        if(check_eval and not this->can_eval())
Paul's avatar
Paul committed
295
            return {};
Paul's avatar
Paul committed
296
        std::vector<argument> args;
Paul's avatar
Paul committed
297
298
299
        std::transform(this->inputs().begin(),
                       this->inputs().end(),
                       std::back_inserter(args),
Paul's avatar
Paul committed
300
                       [](auto arg) { return arg->eval(false); });
301
        return normalized_operator().compute(result, args);
Paul's avatar
Paul committed
302
303
304
305
    }
    return {};
}

Paul's avatar
Paul committed
306
307
void instruction::finalize(context& ctx)
{
Paul's avatar
Paul committed
308
    if(has_finalize(this->op))
Paul's avatar
Paul committed
309
310
311
        this->op.finalize(ctx, this->get_shape(), to_shapes(this->inputs()));
}

312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
void instruction::print(std::ostream& os,
                        instruction_ref ins,
                        const std::unordered_map<instruction_ref, std::string>& names)
{
    os << names.at(ins) << " = ";

    os << ins->get_operator();

    if(ins->name() == "@literal")
    {
        if(ins->get_literal().get_shape().elements() > 10)
            os << "{ ... }";
        else
            os << "{" << ins->get_literal() << "}";
    }

    if(!ins->inputs().empty())
    {
        char delim = '(';
        for(auto&& arg : ins->inputs())
        {
Shucai Xiao's avatar
Shucai Xiao committed
333
334
            std::string arg_name = contains(names, arg) ? names.at(arg) : "?";
            os << delim << arg_name;
335
336
337
338
339
            delim = ',';
        }
        os << ")";
    }

Shucai Xiao's avatar
Shucai Xiao committed
340
341
342
343
344
345
346
347
348
349
350
351
    // print module inputs
    if(!ins->module_inputs().empty())
    {
        std::string delim = ", [";
        for(auto&& mod_arg : ins->module_inputs())
        {
            os << delim << mod_arg->name();
            delim = ", ";
        }
        os << "]";
    }

352
353
354
355
356
    // skip return instruction shape
    if(ins->name() != "@return")
        os << " -> " << ins->get_shape();
}

Paul Fultz II's avatar
Paul Fultz II committed
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
static void debug_name(std::ostream& os, const instruction& ins)
{
    if(ins.name() == "@literal")
    {
        os << "@literal";
        if(ins.get_literal().get_shape().elements() > 10)
            os << "{ ... }";
        else
            os << "{" << ins.get_literal() << "}";
    }
    else
    {
        os << ins.get_operator();
    }
}

void instruction::debug_print() const
{
    debug_name(std::cout, *this);
    std::string delim = "(";
    for(auto arg : this->inputs())
    {
        std::cout << delim;
        debug_name(std::cout, *arg);
        delim = ", ";
    }
    if(not this->inputs().empty())
        std::cout << ")";
    std::cout << " -> " << this->get_shape() << std::endl;
}

Paul's avatar
Paul committed
388
instruction_ref instruction::get_output_alias(instruction_ref ins, bool shallow)
Paul's avatar
Paul committed
389
{
Paul's avatar
Paul committed
390
    auto i = ins->get_operator().output_alias(to_shapes(ins->inputs()));
Paul's avatar
Paul committed
391
392
    if(i < 0)
        return ins;
Paul's avatar
Paul committed
393
    if(shallow)
Paul's avatar
Paul committed
394
        return ins->inputs().at(i);
Paul's avatar
Paul committed
395
396
397
    return get_output_alias(ins->inputs().at(i));
}

398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
void instruction::set_normalized(bool value) { normalized = value; }

bool instruction::is_normalized() const { return normalized; }

bool instruction::need_normalization() const
{
    return this->get_operator().need_normalization() and not normalized;
}

operation instruction::normalized_operator() const
{
    operation o = this->get_operator();
    if(this->need_normalization())
    {
        auto lens = this->inputs().front()->get_shape().lens();
        if(!normalize_attributes(o, lens))
            return this->get_operator();
    }
    return o;
}

Paul's avatar
Paul committed
419
420
421
422
423
424
425
426
std::vector<shape> to_shapes(const std::vector<instruction_ref>& args)
{
    std::vector<shape> shapes(args.size());
    std::transform(
        args.begin(), args.end(), shapes.begin(), [](instruction_ref i) { return i->get_shape(); });
    return shapes;
}

Paul's avatar
Paul committed
427
428
shape compute_shape(const operation& op, const std::vector<instruction_ref>& args)
{
Paul's avatar
Paul committed
429
    return op.compute_shape(to_shapes(args));
Paul's avatar
Paul committed
430
431
}

Shucai Xiao's avatar
Shucai Xiao committed
432
433
434
435
436
437
438
439
440
441
442
443
444
shape compute_shape(const operation& op,
                    const std::vector<instruction_ref>& args,
                    const std::vector<module_ref>& mods)
{
    if(mods.empty())
    {
        return op.compute_shape(to_shapes(args));
    }
    else
    {
        return op.compute_shape(to_shapes(args), mods);
    }
}
Paul's avatar
Paul committed
445
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
446
} // namespace migraphx