eval_test.cpp 13.8 KB
Newer Older
Paul's avatar
Paul committed
1

Paul's avatar
Paul committed
2
3
4
#include <migraphx/program.hpp>
#include <migraphx/iterator_for.hpp>
#include <migraphx/instruction.hpp>
Paul's avatar
Paul committed
5
#include <migraphx/stringutils.hpp>
6
#include <migraphx/compile_options.hpp>
Paul's avatar
Paul committed
7
#include <sstream>
Paul's avatar
Paul committed
8
#include "test.hpp"
Paul's avatar
Paul committed
9
#include <basic_ops.hpp>
Paul's avatar
Paul committed
10

Paul's avatar
Paul committed
11
12
struct id_target
{
Paul's avatar
Paul committed
13
14
15
16
17
    struct context
    {
        void finish() const {}
    };
    migraphx::context ctx = context{};
Paul's avatar
Paul committed
18
    std::string name() const { return "id"; }
19
20
21
22
23
    std::vector<migraphx::pass> get_passes(migraphx::context&,
                                           const migraphx::compile_options&) const
    {
        return {};
    }
Paul's avatar
Paul committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
    migraphx::context get_context() const { return ctx; }
};

struct id_ctx_op
{
    std::string name() const { return "id_ctx_op"; }
    migraphx::argument
    compute(id_target::context&, const migraphx::shape&, std::vector<migraphx::argument> args) const
    {
        if(args.empty())
            return {};
        return args.front();
    }

    migraphx::shape compute_shape(std::vector<migraphx::shape> inputs) const
    {
        if(inputs.empty())
            return {};
        return inputs.front();
    }
    int output_alias(const std::vector<migraphx::shape>&) const { return 0; }
};

struct id_ctx_final_op
{
    std::string name() const { return "id_ctx_final_op"; }
Paul's avatar
Paul committed
50
    migraphx::argument compute(const migraphx::shape&, std::vector<migraphx::argument> args) const
Paul's avatar
Paul committed
51
52
53
54
55
56
    {
        if(args.empty())
            return {};
        return args.front();
    }

Paul's avatar
Paul committed
57
58
59
    void finalize(id_target::context&, const migraphx::shape&, const std::vector<migraphx::shape>&)
    {
    }
Paul's avatar
Paul committed
60
61
62
63
64
65
66
67

    migraphx::shape compute_shape(std::vector<migraphx::shape> inputs) const
    {
        if(inputs.empty())
            return {};
        return inputs.front();
    }
    int output_alias(const std::vector<migraphx::shape>&) const { return 0; }
Paul's avatar
Paul committed
68
69
};

Paul's avatar
Paul committed
70
71
struct reverse_pass
{
Paul's avatar
Paul committed
72
    std::string name() const { return "reverse_pass"; }
Paul's avatar
Paul committed
73

Paul's avatar
Paul committed
74
    void apply(migraphx::program& p) const { std::reverse(p.begin(), p.end()); }
Paul's avatar
Paul committed
75
76
77
78
79
};

struct reverse_target
{
    std::string name() const { return "reverse"; }
80
81
82
83
84
    std::vector<migraphx::pass> get_passes(migraphx::context&,
                                           const migraphx::compile_options&) const
    {
        return {reverse_pass{}};
    }
Paul's avatar
Paul committed
85
86
87
88
89
90
91
    migraphx::context get_context() const { return {}; }
};

struct invert_pass
{
    std::string name() const { return "invert_pass"; }

Paul's avatar
Paul committed
92
    void apply(migraphx::program& p) const
Paul's avatar
Paul committed
93
    {
Paul's avatar
Paul committed
94
        for(auto ins : migraphx::iterator_for(p))
Paul's avatar
Paul committed
95
        {
96
            if(ins->name() == "sum")
Paul's avatar
Paul committed
97
            {
98
                p.replace_instruction(ins, minus_op{}, ins->inputs());
Paul's avatar
Paul committed
99
            }
100
            else if(ins->name() == "minus")
Paul's avatar
Paul committed
101
            {
102
                p.replace_instruction(ins, sum_op{}, ins->inputs());
Paul's avatar
Paul committed
103
104
105
106
107
            }
        }
    }
};

Paul's avatar
Paul committed
108
struct invert_target
Paul's avatar
Paul committed
109
{
Paul's avatar
Paul committed
110
    std::string name() const { return "invert"; }
111
112
113
114
115
    std::vector<migraphx::pass> get_passes(migraphx::context&,
                                           const migraphx::compile_options&) const
    {
        return {invert_pass{}};
    }
Paul's avatar
Paul committed
116
    migraphx::context get_context() const { return {}; }
Paul's avatar
Paul committed
117
118
};

Paul's avatar
Paul committed
119
struct double_invert_target
Paul's avatar
Paul committed
120
{
Paul's avatar
Paul committed
121
    std::string name() const { return "double_invert"; }
122
123
    std::vector<migraphx::pass> get_passes(migraphx::context&,
                                           const migraphx::compile_options&) const
Paul's avatar
Paul committed
124
    {
Paul's avatar
Paul committed
125
        return {invert_pass{}, invert_pass{}};
Paul's avatar
Paul committed
126
    }
Paul's avatar
Paul committed
127
    migraphx::context get_context() const { return {}; }
Paul's avatar
Paul committed
128
129
};

Paul's avatar
Paul committed
130
TEST_CASE(literal_test1)
Paul's avatar
Paul committed
131
{
Paul's avatar
Paul committed
132
    migraphx::program p;
Paul's avatar
Paul committed
133
134
135

    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
Paul's avatar
Paul committed
136
    p.add_instruction(sum_op{}, one, two);
Paul's avatar
Paul committed
137
    auto result = p.eval({});
Paul's avatar
Paul committed
138
139
    EXPECT(result == migraphx::literal{3});
    EXPECT(result != migraphx::literal{4});
Paul's avatar
Paul committed
140
141
}

Paul's avatar
Paul committed
142
TEST_CASE(literal_test2)
Paul's avatar
Paul committed
143
{
Paul's avatar
Paul committed
144
    migraphx::program p;
Paul's avatar
Paul committed
145

Paul's avatar
Paul committed
146
147
    auto one  = p.add_literal(1);
    auto two  = p.add_literal(2);
Paul's avatar
Paul committed
148
149
150
151
    auto sum1 = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(sum_op{}, sum1, two);

    auto result = p.eval({});
Paul's avatar
Paul committed
152
153
    EXPECT(result == migraphx::literal{5});
    EXPECT(result != migraphx::literal{3});
Paul's avatar
Paul committed
154
155
}

Paul's avatar
Paul committed
156
TEST_CASE(print_test)
Paul's avatar
Paul committed
157
{
Paul's avatar
Paul committed
158
    migraphx::program p;
Paul's avatar
Paul committed
159

Paul's avatar
Paul committed
160
    auto x   = p.add_parameter("x", {migraphx::shape::int32_type});
Paul's avatar
Paul committed
161
162
163
164
165
166
167
168
169
    auto two = p.add_literal(2);
    p.add_instruction(sum_op{}, x, two);

    std::stringstream ss;
    ss << p;
    std::string s = ss.str();
    EXPECT(!s.empty());
}

Paul's avatar
Paul committed
170
TEST_CASE(param_test)
Paul's avatar
Paul committed
171
{
Paul's avatar
Paul committed
172
    migraphx::program p;
Paul's avatar
Paul committed
173

Paul's avatar
Paul committed
174
175
    auto x = p.add_parameter("x", {migraphx::shape::int32_type});
    auto y = p.add_parameter("y", {migraphx::shape::int32_type});
Paul's avatar
Paul committed
176

Paul's avatar
Paul committed
177
    p.add_instruction(sum_op{}, x, y);
Paul's avatar
Paul committed
178
    auto result = p.eval(
Paul's avatar
Paul committed
179
180
181
        {{"x", migraphx::literal{1}.get_argument()}, {"y", migraphx::literal{2}.get_argument()}});
    EXPECT(result == migraphx::literal{3});
    EXPECT(result != migraphx::literal{4});
Paul's avatar
Paul committed
182
183
}

Paul's avatar
Paul committed
184
TEST_CASE(param_error_test)
Khalique's avatar
Khalique committed
185
{
Paul's avatar
Paul committed
186
    migraphx::program p;
Khalique's avatar
Khalique committed
187

Paul's avatar
Paul committed
188
189
    auto x = p.add_parameter("x", {migraphx::shape::int32_type});
    auto y = p.add_parameter("y", {migraphx::shape::int32_type});
Khalique's avatar
Khalique committed
190
191

    p.add_instruction(sum_op{}, x, y);
Paul's avatar
Paul committed
192
    EXPECT(test::throws<migraphx::exception>(
Khalique's avatar
Khalique committed
193
        [&] {
Paul's avatar
Paul committed
194
            p.eval({{"x", migraphx::literal{1}.get_argument()}});
Khalique's avatar
Khalique committed
195
        },
196
        "Parameter not found: y"));
Khalique's avatar
Khalique committed
197
198
}

Paul's avatar
Paul committed
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
TEST_CASE(param_error_shape_test)
{
    migraphx::program p;

    auto x = p.add_parameter("x", {migraphx::shape::int32_type, {1, 1}});
    auto y = p.add_parameter("y", {migraphx::shape::int32_type, {1, 1}});

    p.add_instruction(sum_op{}, x, y);
    EXPECT(test::throws<migraphx::exception>(
        [&] {
            p.eval({
                {"x", migraphx::literal{1}.get_argument()},
                {"y", migraphx::literal{{migraphx::shape::int32_type, {1, 1}}, {2}}.get_argument()},
            });
        },
        "Incorrect shape {int32_type, {1}, {0}} for parameter: x"));
}

Paul's avatar
Paul committed
217
TEST_CASE(get_param1)
Paul's avatar
Paul committed
218
219
{
    migraphx::program p;
Paul's avatar
Paul committed
220
221
222
223
224
225
226
227
    migraphx::shape s{migraphx::shape::int32_type, {1, 2}};
    auto x = p.add_parameter("x", s);
    auto y = p.add_parameter("y", s);
    p.add_instruction(sum_op{}, x, y);
    EXPECT(bool{p.get_parameter("x") == x});
    EXPECT(bool{p.get_parameter("y") == y});
    EXPECT(bool{p.get_parameter("nonexistent") == p.end()});
}
Paul's avatar
Paul committed
228

Paul's avatar
Paul committed
229
230
231
232
233
234
235
236
TEST_CASE(get_param2)
{
    migraphx::program p;
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
    p.add_instruction(sum_op{}, one, two);
    EXPECT(bool{p.get_parameter("nonexistent") == p.end()});
}
Paul's avatar
Paul committed
237

Paul's avatar
Paul committed
238
239
240
241
242
243
TEST_CASE(get_param_shapes)
{
    migraphx::program p;
    migraphx::shape s{migraphx::shape::int32_type, {1, 2}};
    auto x = p.add_parameter("x", s);
    auto y = p.add_parameter("y", s);
Paul's avatar
Paul committed
244
    p.add_instruction(sum_op{}, x, y);
Paul's avatar
Paul committed
245
246
247
248
    auto m = p.get_parameter_shapes();
    EXPECT(m.count("nonexistent") == 0);
    EXPECT(m.at("x") == s);
    EXPECT(m.at("y") == s);
Paul's avatar
Paul committed
249
250
}

Paul's avatar
Paul committed
251
TEST_CASE(replace_test)
Paul's avatar
Paul committed
252
{
Paul's avatar
Paul committed
253
    migraphx::program p;
Paul's avatar
Paul committed
254
255
256
257
258

    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.replace_instruction(sum, minus_op{}, two, one);
Paul's avatar
Paul committed
259
    EXPECT(bool{p.validate() == p.end()});
Paul's avatar
Paul committed
260
261

    auto result = p.eval({});
Paul's avatar
Paul committed
262
263
    EXPECT(result == migraphx::literal{1});
    EXPECT(result != migraphx::literal{3});
Paul's avatar
Paul committed
264
265
}

Paul's avatar
Paul committed
266
TEST_CASE(replace_ins_test)
Paul's avatar
Paul committed
267
{
Paul's avatar
Paul committed
268
    migraphx::program p;
Paul's avatar
Paul committed
269

Paul's avatar
Paul committed
270
271
272
    auto one   = p.add_literal(1);
    auto two   = p.add_literal(2);
    auto sum   = p.add_instruction(sum_op{}, one, two);
Paul's avatar
Paul committed
273
274
    auto minus = p.add_instruction(minus_op{}, two, one);
    p.replace_instruction(sum, minus);
Paul's avatar
Paul committed
275
    EXPECT(bool{p.validate() == p.end()});
Paul's avatar
Paul committed
276
277

    auto result = p.eval({});
Paul's avatar
Paul committed
278
279
    EXPECT(result == migraphx::literal{1});
    EXPECT(result != migraphx::literal{3});
Paul's avatar
Paul committed
280
281
}

Paul's avatar
Paul committed
282
TEST_CASE(replace_ins_test2)
Paul's avatar
Paul committed
283
{
Paul's avatar
Paul committed
284
    migraphx::program p;
Paul's avatar
Paul committed
285

Paul's avatar
Paul committed
286
287
288
    auto one   = p.add_literal(1);
    auto two   = p.add_literal(2);
    auto sum   = p.add_instruction(sum_op{}, one, two);
Paul's avatar
Paul committed
289
290
    auto minus = p.add_instruction(minus_op{}, two, one);
    p.add_instruction(pass_op{}, minus);
Paul's avatar
Paul committed
291
    p.replace_instruction(two, sum);
Paul's avatar
Paul committed
292
    EXPECT(bool{p.validate() == p.end()});
Paul's avatar
Paul committed
293
294

    auto result = p.eval({});
Paul's avatar
Paul committed
295
296
    EXPECT(result == migraphx::literal{2});
    EXPECT(result != migraphx::literal{3});
Paul's avatar
Paul committed
297
298
}

Paul's avatar
Paul committed
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
TEST_CASE(replace_op_test)
{
    migraphx::program p;

    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
    auto sum = p.add_instruction(sum_op{}, two, one);
    sum->replace(minus_op{});
    EXPECT(bool{p.validate() == p.end()});

    auto result = p.eval({});
    EXPECT(result == migraphx::literal{1});
    EXPECT(result != migraphx::literal{3});
}

TEST_CASE(replace_op_recompute_shape_throw)
{
    migraphx::program p;

    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
    auto sum = p.add_instruction(sum_op{}, one, two);
Paul's avatar
Paul committed
321
    EXPECT(test::throws<migraphx::exception>([&] { sum->replace(unary_pass_op{}); }));
Paul's avatar
Paul committed
322
323
}

Paul's avatar
Paul committed
324
TEST_CASE(insert_replace_test)
Paul's avatar
Paul committed
325
{
Paul's avatar
Paul committed
326
    migraphx::program p;
Paul's avatar
Paul committed
327

Paul's avatar
Paul committed
328
329
    auto one  = p.add_literal(1);
    auto two  = p.add_literal(2);
Paul's avatar
Paul committed
330
331
332
333
334
    auto sum1 = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(sum_op{}, sum1, two);

    auto sum0 = p.insert_instruction(sum1, sum_op{}, two, two);
    p.replace_instruction(sum1, minus_op{}, sum0, two);
Paul's avatar
Paul committed
335
    EXPECT(bool{p.validate() == p.end()});
Paul's avatar
Paul committed
336
337

    auto result = p.eval({});
Paul's avatar
Paul committed
338
339
    EXPECT(result == migraphx::literal{4});
    EXPECT(result != migraphx::literal{5});
Paul's avatar
Paul committed
340
341
}

Paul's avatar
Paul committed
342
343
344
345
TEST_CASE(remove_test1)
{
    migraphx::program p;

Paul's avatar
Paul committed
346
347
348
    auto one     = p.add_literal(1);
    auto two     = p.add_literal(2);
    auto sum     = p.add_instruction(sum_op{}, one, two);
Paul's avatar
Paul committed
349
350
351
352
353
354
355
356
357
358
359
360
361
    auto removed = p.add_instruction(minus_op{}, sum, one);
    p.remove_instruction(removed);
    EXPECT(bool{p.validate() == p.end()});

    auto result = p.eval({});
    EXPECT(result == migraphx::literal{3});
    EXPECT(result != migraphx::literal{1});
}

TEST_CASE(remove_test2)
{
    migraphx::program p;

Paul's avatar
Paul committed
362
363
    auto one     = p.add_literal(1);
    auto two     = p.add_literal(2);
Paul's avatar
Paul committed
364
365
366
367
368
369
370
371
372
373
    auto removed = p.add_instruction(minus_op{}, two, one);
    p.add_instruction(sum_op{}, one, two);
    p.remove_instruction(removed);
    EXPECT(bool{p.validate() == p.end()});

    auto result = p.eval({});
    EXPECT(result == migraphx::literal{3});
    EXPECT(result != migraphx::literal{1});
}

Paul's avatar
Paul committed
374
TEST_CASE(target_test)
Paul's avatar
Paul committed
375
{
Paul's avatar
Paul committed
376
    migraphx::program p;
Paul's avatar
Paul committed
377
378
379
380
381
382

    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
    p.add_instruction(sum_op{}, one, two);
    p.compile(id_target{});
    auto result = p.eval({});
Paul's avatar
Paul committed
383
384
    EXPECT(result == migraphx::literal{3});
    EXPECT(result != migraphx::literal{4});
Paul's avatar
Paul committed
385
386
}

Paul's avatar
Paul committed
387
TEST_CASE(invert_target_test)
Paul's avatar
Paul committed
388
{
Paul's avatar
Paul committed
389
    migraphx::program p;
Paul's avatar
Paul committed
390
391
392
393

    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
    p.add_instruction(sum_op{}, two, one);
Paul's avatar
Paul committed
394
    p.compile(invert_target{});
Paul's avatar
Paul committed
395
    auto result = p.eval({});
Paul's avatar
Paul committed
396
397
    EXPECT(result == migraphx::literal{1});
    EXPECT(result != migraphx::literal{4});
Paul's avatar
Paul committed
398
399
}

Paul's avatar
Paul committed
400
TEST_CASE(double_invert_target_test)
Paul's avatar
Paul committed
401
{
Paul's avatar
Paul committed
402
    migraphx::program p;
Paul's avatar
Paul committed
403
404
405
406

    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
    p.add_instruction(sum_op{}, two, one);
Paul's avatar
Paul committed
407
    p.compile(double_invert_target{});
Paul's avatar
Paul committed
408
    auto result = p.eval({});
Paul's avatar
Paul committed
409
410
    EXPECT(result == migraphx::literal{3});
    EXPECT(result != migraphx::literal{4});
Paul's avatar
Paul committed
411
412
}

Paul's avatar
Paul committed
413
414
415
416
417
418
419
TEST_CASE(reverse_target_test)
{
    migraphx::program p;

    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
    p.add_instruction(sum_op{}, one, two);
Paul's avatar
Paul committed
420
    EXPECT(test::throws<migraphx::exception>([&] { p.compile(reverse_target{}); }));
Paul's avatar
Paul committed
421
422
}

Paul's avatar
Paul committed
423
424
// Check that the program doesnt modify the context directly, and only the operators modify the
// context
Paul's avatar
Paul committed
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
TEST_CASE(eval_context1)
{
    migraphx::program p;
    id_target t{};
    EXPECT(is_shared(t.ctx, t.get_context()));
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
    p.add_instruction(sum_op{}, one, two);
    p.compile(t);
    EXPECT(is_shared(t.ctx, p.get_context()));
    p.eval({});
    EXPECT(is_shared(t.ctx, p.get_context()));
}

TEST_CASE(eval_context2)
{
    migraphx::program p;
    id_target t{};
    EXPECT(is_shared(t.ctx, t.get_context()));
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
    p.add_instruction(id_ctx_op{}, one, two);
    p.compile(t);
    EXPECT(is_shared(t.ctx, p.get_context()));
    p.eval({});
    // id_ctx_op will modify the context
    EXPECT(not is_shared(t.ctx, p.get_context()));
}

TEST_CASE(eval_context3)
{
    migraphx::program p;
    id_target t{};
    EXPECT(is_shared(t.ctx, t.get_context()));
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
    p.add_instruction(id_ctx_final_op{}, one, two);
    p.compile(t);
    // Finalizer will modify the context
    EXPECT(not is_shared(t.ctx, p.get_context()));
    auto ctx = p.get_context();
    p.eval({});
    EXPECT(is_shared(ctx, p.get_context()));
    EXPECT(not is_shared(t.ctx, p.get_context()));
}

Paul's avatar
Paul committed
471
472
473
474
475
476
struct cout_redirect
{
    cout_redirect()                     = delete;
    cout_redirect(const cout_redirect&) = delete;
    template <class T>
    cout_redirect(T& stream) : old(std::cout.rdbuf(stream.rdbuf()))
Paul's avatar
Paul committed
477
478
    {
    }
Paul's avatar
Paul committed
479
    ~cout_redirect() { std::cout.rdbuf(old); }
Paul's avatar
Paul committed
480

Paul's avatar
Paul committed
481
482
    private:
    std::streambuf* old;
Paul's avatar
Paul committed
483
484
};

Paul's avatar
Paul committed
485
template <class F>
Paul's avatar
Paul committed
486
487
488
489
490
491
492
493
494
495
496
std::string capture_output(F f)
{
    std::stringstream ss;
    cout_redirect cr{ss};
    f();
    return ss.str();
}

TEST_CASE(debug_print_test)
{
    migraphx::program p;
Paul's avatar
Paul committed
497
    auto one                                    = p.add_literal(1);
Paul's avatar
Paul committed
498
    std::vector<migraphx::instruction_ref> onev = {one};
Paul's avatar
Paul committed
499
500
501
502

    migraphx::program p2;
    auto one2 = p2.add_literal(1);

Paul's avatar
Paul committed
503
504
    auto program_out = migraphx::trim(capture_output([&] { p.debug_print(); }));
    auto ins_out     = migraphx::trim(capture_output([&] { p.debug_print(one); }));
Paul's avatar
Paul committed
505
    auto inss_out    = migraphx::trim(capture_output([&] { p.debug_print(onev); }));
Paul's avatar
Paul committed
506
507
    auto end_out     = migraphx::trim(capture_output([&] { p.debug_print(p.end()); }));
    auto p2_ins_out  = migraphx::trim(capture_output([&] { p.debug_print(one2); }));
Paul's avatar
Paul committed
508
509
510
511
512
513
514

    EXPECT(program_out == ins_out);
    EXPECT(inss_out == ins_out);
    EXPECT(end_out == "End instruction");
    EXPECT(p2_ins_out == "Instruction not part of program");
}

Paul's avatar
Paul committed
515
int main(int argc, const char* argv[]) { test::run(argc, argv); }