program.cpp 20.1 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
#include <migraphx/program.hpp>
#include <migraphx/stringutils.hpp>
#include <migraphx/instruction.hpp>
4
#include <migraphx/op/identity.hpp>
Paul's avatar
Paul committed
5
#include <migraphx/target.hpp>
Paul's avatar
Paul committed
6
7
8
9
#include <migraphx/env.hpp>
#include <migraphx/ranges.hpp>
#include <migraphx/time.hpp>
#include <migraphx/iterator_for.hpp>
10
#include <migraphx/pass_manager.hpp>
Paul's avatar
Paul committed
11
#include <iostream>
Paul's avatar
Paul committed
12
#include <sstream>
Paul's avatar
Paul committed
13
#include <algorithm>
Paul's avatar
Paul committed
14
#include <utility>
Paul's avatar
Paul committed
15

Paul's avatar
Paul committed
16
namespace migraphx {
Paul's avatar
Paul committed
17
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Paul committed
18

Paul's avatar
Paul committed
19
20
21
22
struct program_impl
{
    // A list is used to keep references to an instruction stable
    std::list<instruction> instructions;
Paul's avatar
Paul committed
23
    context ctx;
Paul's avatar
Paul committed
24
25
};

26
const operation& get_operation(instruction_ref ins) { return ins->get_operator(); }
Paul's avatar
Paul committed
27

Paul's avatar
Paul committed
28
29
30
static void print_instruction(std::ostream& os,
                              instruction_ref ins,
                              const std::unordered_map<instruction_ref, std::string>& names)
Paul's avatar
Paul committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
{
    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())
        {
            os << delim << names.at(arg);
            delim = ',';
        }
        os << ")";
    }
Paul's avatar
Paul committed
54

Paul's avatar
Paul committed
55
56
57
    os << " -> " << ins->get_shape();
}

Paul's avatar
Paul committed
58
template <class F>
Khalique's avatar
Khalique committed
59
static void print_program(const program& p, F print_func)
Paul's avatar
Paul committed
60
{
61
    std::unordered_map<instruction_ref, std::string> names;
Paul's avatar
Paul committed
62
63
    int count = 0;

64
    for(auto ins : iterator_for(p))
Paul's avatar
Paul committed
65
    {
Paul's avatar
Paul committed
66
        std::string var_name;
Paul's avatar
Paul committed
67
        if(ins->name() == "@param")
Paul's avatar
Paul committed
68
        {
69
            var_name = any_cast<builtin::param>(ins->get_operator()).parameter;
Paul's avatar
Paul committed
70
        }
Paul's avatar
Paul committed
71
72
73
74
75
        else
        {
            var_name = "@" + std::to_string(count);
            count++;
        }
Paul's avatar
Paul committed
76
        names.emplace(ins, var_name);
Paul's avatar
Paul committed
77

Paul's avatar
Paul committed
78
79
        // TODO: Use all_of
        for(auto&& arg : ins->inputs())
Paul's avatar
Paul committed
80
        {
Paul's avatar
Paul committed
81
82
            assert(p.has_instruction(arg) && "Instruction not found");
            (void)arg;
Paul's avatar
Paul committed
83
84
        }

85
        print_func(ins, names);
Paul's avatar
Paul committed
86
87
88
    }
}

Paul's avatar
Paul committed
89
program::program() : impl(std::make_unique<program_impl>()) {}
Paul's avatar
Paul committed
90

Paul's avatar
Paul committed
91
program::program(program&&) noexcept = default;
Shucai Xiao's avatar
Shucai Xiao committed
92
program::~program() noexcept         = default;
Paul's avatar
Paul committed
93

94
// copy constructor
Shucai Xiao's avatar
Shucai Xiao committed
95
program::program(const program& p) { assign(p); }
96
97

// copy assignment operator
Shucai Xiao's avatar
Shucai Xiao committed
98
program& program::operator=(program p)
99
{
Shucai Xiao's avatar
Shucai Xiao committed
100
    std::swap(p.impl, this->impl);
101
102
103
    return *this;
}

Shucai Xiao's avatar
Shucai Xiao committed
104
void program::assign(const program& p)
105
106
{
    // clean the current program
Shucai Xiao's avatar
Shucai Xiao committed
107
    if(!impl)
108
109
110
    {
        impl = std::make_unique<program_impl>();
    }
Shucai Xiao's avatar
Shucai Xiao committed
111
    else if(!impl->instructions.empty())
112
    {
Shucai Xiao's avatar
Shucai Xiao committed
113
        impl->instructions.clear();
114
    }
Shucai Xiao's avatar
Shucai Xiao committed
115
    impl->ctx         = p.impl->ctx;
116
    int8_quant_params = p.int8_quant_params;
117
118

    std::unordered_map<instruction_ref, instruction_ref> ins_map;
Shucai Xiao's avatar
Shucai Xiao committed
119
    for(auto ins : iterator_for(p))
120
121
    {
        instruction_ref copy_ins{};
Shucai Xiao's avatar
Shucai Xiao committed
122
        if(ins->name() == "@literal")
123
        {
Shucai Xiao's avatar
Shucai Xiao committed
124
            auto l   = ins->get_literal();
125
126
            copy_ins = impl->instructions.insert(impl->instructions.end(), instruction{l});
        }
Shucai Xiao's avatar
Shucai Xiao committed
127
        else if(ins->name() == "@param")
128
        {
Shucai Xiao's avatar
Shucai Xiao committed
129
130
            auto&& name = any_cast<builtin::param>(ins->get_operator()).parameter;
            auto s      = ins->get_shape();
Shucai Xiao's avatar
Shucai Xiao committed
131
132
            copy_ins    = impl->instructions.insert(impl->instructions.end(),
                                                 {builtin::param{name}, std::move(s), {}});
133
        }
Shucai Xiao's avatar
Shucai Xiao committed
134
        else if(ins->name() == "@outline")
135
136
        {
            auto s = ins->get_shape();
Shucai Xiao's avatar
Shucai Xiao committed
137
138
            copy_ins =
                impl->instructions.insert(impl->instructions.end(), {builtin::outline{s}, s, {}});
139
140
141
142
143
144
        }
        else
        {
            // retrieve its mapped input
            auto inputs = ins->inputs();
            // ensure all inputs have its corresponding copy instructions
Shucai Xiao's avatar
Shucai Xiao committed
145
146
            assert(std::all_of(
                inputs.begin(), inputs.end(), [&](auto i) { return ins_map.count(i) > 0; }));
147
            std::vector<instruction_ref> copy_inputs(inputs.size());
Shucai Xiao's avatar
Shucai Xiao committed
148
149
150
            std::transform(inputs.begin(), inputs.end(), copy_inputs.begin(), [&](auto i) {
                return ins_map[i];
            });
151
152
153
154
155
156
157
            copy_ins = add_instruction(ins->get_operator(), copy_inputs);
        }

        ins_map[ins] = copy_ins;
    }
}

Paul's avatar
Paul committed
158
instruction_ref program::add_instruction(const operation& op, std::vector<instruction_ref> args)
Paul's avatar
Paul committed
159
{
Paul's avatar
Paul committed
160
    return insert_instruction(impl->instructions.end(), op, std::move(args));
Paul's avatar
Paul committed
161
}
Paul's avatar
Paul committed
162
163
164
instruction_ref program::insert_instruction(instruction_ref ins,
                                            const operation& op,
                                            std::vector<instruction_ref> args)
Paul's avatar
Paul committed
165
{
Paul's avatar
Paul committed
166
167
168
    assert(std::all_of(
               args.begin(), args.end(), [&](instruction_ref x) { return has_instruction(x); }) &&
           "Argument is not an exisiting instruction");
Paul's avatar
Paul committed
169
    assert(not starts_with(op.name(), "@"));
Paul's avatar
Paul committed
170
    shape r     = compute_shape(op, args);
Paul's avatar
Paul committed
171
    auto result = impl->instructions.insert(ins, {op, r, std::move(args)});
Paul's avatar
Paul committed
172
    instruction::backreference(result);
Paul's avatar
Paul committed
173
    assert(result->valid(begin()));
Paul's avatar
Paul committed
174
    return result;
Paul's avatar
Paul committed
175
176
}

Paul's avatar
Paul committed
177
178
179
instruction_ref program::replace_instruction(instruction_ref ins,
                                             const operation& op,
                                             std::vector<instruction_ref> args)
Paul's avatar
Paul committed
180
181
182
183
{
    assert(std::all_of(
               args.begin(), args.end(), [&](instruction_ref x) { return has_instruction(x); }) &&
           "Argument is not an exisiting instruction");
Paul's avatar
Paul committed
184
    assert(not starts_with(op.name(), "@"));
Paul's avatar
Paul committed
185

Paul's avatar
Paul committed
186
    shape r = compute_shape(op, args);
187
    instruction::replace(ins, op, r, std::move(args));
Paul's avatar
Paul committed
188
    assert(ins->valid(begin()));
Paul's avatar
Paul committed
189
190
191
    return ins;
}

Paul's avatar
Paul committed
192
instruction_ref program::replace_instruction(instruction_ref ins, instruction_ref rep)
Paul's avatar
Paul committed
193
{
Paul's avatar
Paul committed
194
195
196
    assert(has_instruction(ins));
    assert(has_instruction(rep));
    assert(ins != rep);
Shucai Xiao's avatar
Shucai Xiao committed
197

Shucai Xiao's avatar
Shucai Xiao committed
198
    if(ins == std::prev(this->end()))
Shucai Xiao's avatar
Shucai Xiao committed
199
    {
Shucai Xiao's avatar
Shucai Xiao committed
200
        return replace_instruction(ins, op::identity{}, rep);
Shucai Xiao's avatar
Shucai Xiao committed
201
202
    }

Paul's avatar
Paul committed
203
    // TODO: Should it be an error if the output is empty?
Paul's avatar
Paul committed
204
    if(ins->outputs().empty())
Paul's avatar
Paul committed
205
206
207
    {
        return rep;
    }
Paul's avatar
Paul committed
208
209
210
    // Make a copy of outputs which can be changed when calling replace_argument
    auto outputs = ins->outputs();
    for(auto out : outputs)
Paul's avatar
Paul committed
211
    {
Paul's avatar
Paul committed
212
213
        // TODO: Check for possible cycles
        if(out != rep)
Paul's avatar
Paul committed
214
        {
Paul's avatar
Paul committed
215
            instruction::replace_argument(out, ins, rep);
Paul's avatar
Paul committed
216
        }
Paul's avatar
Paul committed
217
        assert(out->valid(begin()));
Paul's avatar
Paul committed
218
    }
Paul's avatar
Paul committed
219
    // Replacement should not be dead code unless its the last instruction
Paul's avatar
Paul committed
220
    assert(!rep->outputs().empty() or rep == std::prev(end()));
Paul's avatar
Paul committed
221
    // Output of the original instruction should only be the replacement or empty
Paul's avatar
Paul committed
222
223
224
    assert(ins->outputs().empty() or std::all_of(ins->outputs().begin(),
                                                 ins->outputs().end(),
                                                 [&](auto i) { return i == rep; }));
Paul's avatar
Paul committed
225
    assert(ins->valid(begin()));
Paul's avatar
Paul committed
226
    assert(rep->valid(begin()));
Paul's avatar
Paul committed
227
228
229
    return rep;
}

Paul's avatar
Paul committed
230
instruction_ref program::remove_instruction(instruction_ref ins)
Paul's avatar
Paul committed
231
232
{
    assert(has_instruction(ins));
Paul's avatar
Paul committed
233
    assert(ins->outputs().empty());
Paul's avatar
Paul committed
234
235
236
237
    ins->clear_arguments();
    return impl->instructions.erase(ins);
}

238
239
instruction_ref program::remove_instructions(instruction_ref first, instruction_ref last)
{
Paul's avatar
Paul committed
240
241
    if(first == last)
        return first;
Paul's avatar
Paul committed
242
    // TODO: Check every element
243
    assert(has_instruction(first));
Paul's avatar
Paul committed
244
    std::for_each(first, last, [&](instruction& ins) { ins.clear_arguments(); });
Paul's avatar
Paul committed
245
    assert(std::all_of(first, last, [&](const instruction& ins) { return ins.outputs().empty(); }));
246
247
248
249
250
251
252
253
254
    return impl->instructions.erase(first, last);
}

instruction_ref program::move_instruction(instruction_ref src, instruction_ref dst)
{
    impl->instructions.splice(dst, impl->instructions, src);
    return src;
}

Paul's avatar
Paul committed
255
instruction_ref program::add_literal(literal l)
Paul's avatar
Paul committed
256
{
Paul's avatar
Paul committed
257
258
259
260
    impl->instructions.emplace_front(std::move(l));
    return impl->instructions.begin();
}

Paul's avatar
Paul committed
261
instruction_ref program::add_outline(const shape& s)
Paul's avatar
Paul committed
262
263
264
{
    impl->instructions.push_front({builtin::outline{s}, s, {}});
    return impl->instructions.begin();
Paul's avatar
Paul committed
265
266
}

Paul's avatar
Paul committed
267
instruction_ref program::add_parameter(std::string name, shape s)
Paul's avatar
Paul committed
268
{
269
    assert(get_parameter_shape(name) == shape{});
Paul's avatar
Paul committed
270
    impl->instructions.push_front({builtin::param{std::move(name)}, std::move(s), {}});
Paul's avatar
Paul committed
271
272
273
    return impl->instructions.begin();
}

Paul's avatar
Paul committed
274
shape program::get_parameter_shape(std::string name) const
Paul's avatar
Paul committed
275
276
{
    auto ins = std::find_if(
Paul's avatar
Paul committed
277
        impl->instructions.begin(), impl->instructions.end(), [&](const instruction& x) {
Paul's avatar
Paul committed
278
            if(x.name() == "@param")
Paul's avatar
Paul committed
279
            {
280
                return any_cast<builtin::param>(x.get_operator()).parameter == name;
Paul's avatar
Paul committed
281
282
283
284
285
286
287
            }
            else
            {
                return false;
            }
        });
    if(ins != this->end())
Paul's avatar
Paul committed
288
        return ins->get_shape();
Paul's avatar
Paul committed
289
290
    else
        return {};
Paul's avatar
Paul committed
291
292
}

mei-ye's avatar
mei-ye committed
293
294
295
296
instruction_ref program::get_parameter(std::string name) const
{
    auto ins = std::find_if(
        impl->instructions.begin(), impl->instructions.end(), [&](const instruction& x) {
Paul's avatar
Paul committed
297
            if(x.name() == "@param")
mei-ye's avatar
mei-ye committed
298
            {
Paul's avatar
Paul committed
299
                return any_cast<builtin::param>(x.get_operator()).parameter == name;
mei-ye's avatar
mei-ye committed
300
301
302
303
304
305
306
307
308
            }
            else
            {
                return false;
            }
        });
    if(ins != this->end())
        return ins;
    else
mei-ye's avatar
mei-ye committed
309
        return this->end();
mei-ye's avatar
mei-ye committed
310
311
}

Paul's avatar
Paul committed
312
313
314
std::unordered_map<std::string, shape> program::get_parameter_shapes() const
{
    std::unordered_map<std::string, shape> result;
Paul's avatar
Paul committed
315
    for(auto&& ins : impl->instructions)
Paul's avatar
Paul committed
316
    {
Paul's avatar
Paul committed
317
        if(ins.name() == "@param")
Paul's avatar
Paul committed
318
        {
319
            auto&& name  = any_cast<builtin::param>(ins.get_operator()).parameter;
Paul's avatar
Paul committed
320
            result[name] = ins.get_shape();
Paul's avatar
Paul committed
321
322
323
324
325
        }
    }
    return result;
}

Paul's avatar
Paul committed
326
bool program::has_instruction(instruction_ref ins) const
Paul's avatar
Paul committed
327
{
Paul's avatar
Paul committed
328
329
330
331
    return std::find_if(
               impl->instructions.begin(), impl->instructions.end(), [&](const instruction& x) {
                   return std::addressof(*ins) == std::addressof(x);
               }) != impl->instructions.end();
Paul's avatar
Paul committed
332
333
}

Paul's avatar
Paul committed
334
std::size_t program::size() const { return impl->instructions.size(); }
Paul's avatar
Paul committed
335
336
instruction_ref program::begin() const { return impl->instructions.begin(); }
instruction_ref program::end() const { return impl->instructions.end(); }
337

Paul's avatar
Paul committed
338
shape program::get_shape() const { return impl->instructions.back().get_shape(); }
Paul's avatar
Paul committed
339

Paul's avatar
Paul committed
340
341
context& program::get_context() const { return impl->ctx; }

Paul's avatar
Paul committed
342
343
instruction_ref program::validate() const
{
Paul's avatar
Paul committed
344
345
    return std::find_if(impl->instructions.begin(),
                        impl->instructions.end(),
Paul's avatar
Paul committed
346
                        [&](const instruction& i) { return !i.valid(impl->instructions.begin()); });
Paul's avatar
Paul committed
347
348
}

mei-ye's avatar
mei-ye committed
349
void program::compile(const target& t, tracer trace)
Paul's avatar
Paul committed
350
{
Paul's avatar
Paul committed
351
    assert(this->validate() == impl->instructions.end());
mei-ye's avatar
mei-ye committed
352
    this->impl->ctx = t.get_context();
Paul's avatar
Paul committed
353
    if(enabled(MIGRAPHX_TRACE_COMPILE{}))
Paul's avatar
Paul committed
354
355
356
        trace = tracer{std::cout};
    trace(*this);
    trace();
357
    run_passes(*this, t.get_passes(this->impl->ctx), trace);
Paul's avatar
Paul committed
358
    auto invalid = this->validate();
Paul's avatar
Paul committed
359
360
    if(invalid != impl->instructions.end())
    {
Paul's avatar
Paul committed
361
        auto index = std::distance(impl->instructions.begin(), invalid);
Paul's avatar
Paul committed
362
        MIGRAPHX_THROW("Invalid program from compilation at instruction " + std::to_string(index));
Paul's avatar
Paul committed
363
    }
Paul's avatar
Paul committed
364
365
366
367
368
369
370
371
372
    this->finalize();
}

void program::finalize()
{
    for(auto ins : iterator_for(*this))
    {
        ins->finalize(this->impl->ctx);
    }
Paul's avatar
Paul committed
373
374
}

Paul's avatar
Paul committed
375
376
377
378
379
template <class F>
argument generic_eval(const program& p,
                      context& ctx,
                      std::unordered_map<std::string, argument> params,
                      F trace)
Paul's avatar
Paul committed
380
{
Paul's avatar
Paul committed
381
    assert(p.validate() == p.end());
382
    std::unordered_map<instruction_ref, argument> results;
Paul's avatar
Paul committed
383
    results.reserve(p.size() * 2);
Paul's avatar
Paul committed
384
385
    std::vector<argument> values;
    values.reserve(16);
386
    for(auto ins : iterator_for(p))
Paul's avatar
Paul committed
387
    {
Paul's avatar
Paul committed
388
        if(ins->name() == "@literal")
Paul's avatar
Paul committed
389
        {
Paul's avatar
Paul committed
390
            results.emplace(ins, trace(ins, [&] { return ins->get_literal().get_argument(); }));
Paul's avatar
Paul committed
391
        }
Paul's avatar
Paul committed
392
        else if(ins->name() == "@param")
Paul's avatar
Paul committed
393
        {
Paul's avatar
Paul committed
394
395
396
397
398
399
400
401
402
403
404
            results.emplace(
                ins, trace(ins, [&] {
                    auto param_name = any_cast<builtin::param>(ins->get_operator()).parameter;
                    if(not contains(params, param_name))
                        MIGRAPHX_THROW("Parameter not found: " + param_name);
                    auto param = params.at(param_name);
                    if(param.get_shape() != ins->get_shape())
                        MIGRAPHX_THROW("Incorrect shape {" + to_string(param.get_shape()) +
                                       "} for parameter: " + param_name);
                    return param;
                }));
Paul's avatar
Paul committed
405
        }
Paul's avatar
Paul committed
406
        else if(ins->name() == "@outline")
Paul's avatar
Paul committed
407
        {
Paul's avatar
Paul committed
408
            results.emplace(ins, trace(ins, [&] { return argument{ins->get_shape(), nullptr}; }));
Paul's avatar
Paul committed
409
        }
Paul's avatar
Paul committed
410
411
        else
        {
Paul's avatar
Paul committed
412
            values.resize(ins->inputs().size());
Paul's avatar
Paul committed
413
414
415
416
417
            std::transform(
                ins->inputs().begin(), ins->inputs().end(), values.begin(), [&](instruction_ref i) {
                    assert(results.find(i) != results.end());
                    return results[i];
                });
Paul's avatar
Paul committed
418
419
420
            results.emplace(ins, trace(ins, [&] {
                                return ins->get_operator().compute(ctx, ins->get_shape(), values);
                            }));
Paul's avatar
Paul committed
421
        }
422
        assert(results.find(ins) != results.end());
Paul's avatar
Paul committed
423
    }
424
    return results.at(std::prev(p.end()));
Paul's avatar
Paul committed
425
426
}

Paul's avatar
Paul committed
427
428
argument program::eval(std::unordered_map<std::string, argument> params) const
{
Paul's avatar
Paul committed
429
430
    auto& ctx = this->impl->ctx;
#ifndef NDEBUG
Paul's avatar
Paul committed
431
    auto sctx          = ctx;
Paul's avatar
Paul committed
432
433
434
    auto check_context = [&](auto f) {
        assert(is_shared(ctx, sctx));
        auto x = f();
Paul's avatar
Paul committed
435
        sctx   = ctx;
Paul's avatar
Paul committed
436
437
438
        return x;
    };
#else
Paul's avatar
Paul committed
439
    auto check_context = [](auto f) { return f(); };
Paul's avatar
Paul committed
440
#endif
Paul's avatar
Paul committed
441
442
443
444

    auto trace_level = value_of(MIGRAPHX_TRACE_EVAL{});

    if(trace_level > 0)
Paul's avatar
Paul committed
445
    {
Paul's avatar
Paul committed
446
        return generic_eval(*this, ctx, std::move(params), [&](auto& ins, auto f) {
Paul's avatar
Paul committed
447
            ctx.finish();
Paul's avatar
Paul committed
448
449
            std::cout << "Run instruction: ";
            this->debug_print(ins);
Paul's avatar
Paul committed
450
451
            auto result = check_context(f);
            ctx.finish();
Paul's avatar
Paul committed
452
            if(trace_level > 1 and ins->name().front() != '@' and ins->name() != "load")
Paul's avatar
Paul committed
453
454
                std::cout << "Ouput: " << result << std::endl;
            return result;
Paul's avatar
Paul committed
455
        });
Paul's avatar
Paul committed
456
457
458
459
    }
    else
    {
        return generic_eval(
Paul's avatar
Paul committed
460
            *this, ctx, std::move(params), [&](auto&, auto f) { return check_context(f); });
Paul's avatar
Paul committed
461
    }
Paul's avatar
Paul committed
462
463
}

Paul's avatar
Paul committed
464
465
466
double common_average(const std::vector<double>& v)
{
    std::size_t n = v.size() / 4;
Paul's avatar
Paul committed
467
468
    double total  = std::accumulate(v.begin() + n, v.end() - n, 0.0);
    return total / std::distance(v.begin() + n, v.end() - n);
Paul's avatar
Paul committed
469
470
}

Paul's avatar
Paul committed
471
472
473
void program::perf_report(std::ostream& os, std::size_t n, parameter_map params) const
{
    using milliseconds = std::chrono::duration<double, std::milli>;
Paul's avatar
Paul committed
474
    auto& ctx          = this->impl->ctx;
Paul's avatar
Paul committed
475
476
    // Run once by itself
    eval(params);
Paul's avatar
Paul committed
477
    ctx.finish();
Paul's avatar
Paul committed
478
    // Run and time entire program
Paul's avatar
Paul committed
479
480
    std::vector<double> total_vec;
    total_vec.reserve(n);
Paul's avatar
Paul committed
481
    for(std::size_t i = 0; i < n; i++)
Paul's avatar
Paul committed
482
    {
Paul's avatar
Paul committed
483
484
485
486
        total_vec.push_back(time<milliseconds>([&] {
            eval(params);
            ctx.finish();
        }));
Paul's avatar
Paul committed
487
    }
Paul's avatar
Paul committed
488
489
    std::sort(total_vec.begin(), total_vec.end());
    std::unordered_map<instruction_ref, std::vector<double>> ins_vec;
Paul's avatar
Paul committed
490
    // Fill the map
Paul's avatar
Paul committed
491
    generic_eval(*this, ctx, params, [&](auto ins, auto) {
Paul's avatar
Paul committed
492
        ins_vec[ins].reserve(n);
Paul's avatar
Paul committed
493
494
        return argument{};
    });
Paul's avatar
Paul committed
495
    // Run and time each instruction
Paul's avatar
Paul committed
496
    for(std::size_t i = 0; i < n; i++)
Paul's avatar
Paul committed
497
    {
Paul's avatar
Paul committed
498
        generic_eval(*this, ctx, params, [&](auto ins, auto f) {
499
            argument result;
Paul's avatar
Paul committed
500
501
502
503
            ins_vec[ins].push_back(time<milliseconds>([&] {
                result = f();
                ctx.finish();
            }));
504
            return result;
Paul's avatar
Paul committed
505
506
        });
    }
Paul's avatar
Paul committed
507
508
    for(auto&& p : ins_vec)
        std::sort(p.second.begin(), p.second.end());
Paul's avatar
Paul committed
509
    // Run and time implicit overhead
Paul's avatar
Paul committed
510
511
    std::vector<double> overhead_vec;
    overhead_vec.reserve(n);
Paul's avatar
Paul committed
512
    for(std::size_t i = 0; i < n; i++)
Paul's avatar
Paul committed
513
    {
Paul's avatar
Paul committed
514
        overhead_vec.push_back(time<milliseconds>([&] { dry_run(params); }));
Paul's avatar
Paul committed
515
516
    }

Paul's avatar
Paul committed
517
    double total_time             = common_average(total_vec);
Paul's avatar
Paul committed
518
    double rate                   = 1000.0 / total_time;
Paul's avatar
Paul committed
519
    double overhead_time          = common_average(overhead_vec);
Paul's avatar
Paul committed
520
    double overhead_percent       = overhead_time * 100.0 / total_time;
Paul's avatar
Paul committed
521
    double total_instruction_time = 0.0;
Paul's avatar
Paul committed
522
    std::unordered_map<std::string, double> op_times;
Paul's avatar
Paul committed
523
    for(auto&& p : ins_vec)
Paul's avatar
Paul committed
524
525
    {
        double avg = common_average(p.second);
Paul's avatar
Paul committed
526
        op_times[p.first->name()] += avg;
Paul's avatar
Paul committed
527
528
        total_instruction_time += avg;
    }
Paul's avatar
Paul committed
529
530
    double calculate_overhead_time    = total_time - total_instruction_time;
    double calculate_overhead_percent = calculate_overhead_time * 100.0 / total_time;
Paul's avatar
Paul committed
531

532
    print_program(*this, [&](auto ins, const auto& names) {
Khalique's avatar
Khalique committed
533
        print_instruction(std::cout, ins, names);
Paul's avatar
Paul committed
534
535
536
        double avg     = common_average(ins_vec[ins]);
        double percent = std::ceil(100.0 * avg / total_instruction_time);
        os << ": " << avg << "ms, " << percent << "%";
537
        os << std::endl;
Paul's avatar
Paul committed
538
    });
Paul's avatar
Paul committed
539
540
541

    os << std::endl;
    os << "Summary:" << std::endl;
Paul's avatar
Paul committed
542
    for(auto&& p : op_times)
Paul's avatar
Paul committed
543
    {
Paul's avatar
Paul committed
544
545
        auto&& name    = p.first;
        double avg     = p.second;
Paul's avatar
Paul committed
546
547
548
549
550
        double percent = std::ceil(100.0 * avg / total_instruction_time);
        os << name << ": " << avg << "ms, " << percent << "%" << std::endl;
    }

    os << std::endl;
Paul's avatar
Paul committed
551

Paul's avatar
Paul committed
552
    os << "Rate: " << rate << "/sec" << std::endl;
Paul's avatar
Paul committed
553
554
    os << "Total time: " << total_time << "ms" << std::endl;
    os << "Total instructions time: " << total_instruction_time << "ms" << std::endl;
Paul's avatar
Paul committed
555
556
557
558
    os << "Overhead time: " << overhead_time << "ms"
       << ", " << calculate_overhead_time << "ms" << std::endl;
    os << "Overhead: " << std::round(overhead_percent) << "%"
       << ", " << std::round(calculate_overhead_percent) << "%" << std::endl;
Paul's avatar
Paul committed
559
560
}

Paul's avatar
Paul committed
561
562
void program::debug_print() const { std::cout << *this << std::endl; }
void program::debug_print(instruction_ref ins) const
Paul's avatar
Paul committed
563
{
Paul's avatar
Paul committed
564
565
566
567
568
569
570
571
572
573
    if(ins == this->end())
    {
        std::cout << "End instruction" << std::endl;
        return;
    }
    if(not has_instruction(ins))
    {
        std::cout << "Instruction not part of program" << std::endl;
        return;
    }
Paul's avatar
Paul committed
574
    std::stringstream ss;
575
    print_program(*this, [&](auto x, const auto& names) {
Paul's avatar
Paul committed
576
        if(x == ins)
Paul's avatar
Paul committed
577
578
579
580
581
582
        {
            print_instruction(std::cout, x, names);
            std::cout << std::endl;
        }
    });
}
Paul's avatar
Paul committed
583
void program::debug_print(const std::vector<instruction_ref>& inss) const
Paul's avatar
Paul committed
584
{
Paul's avatar
Paul committed
585
    for(auto ins : inss)
Paul's avatar
Paul committed
586
587
588
589
        debug_print(ins);
    std::cout << std::endl;
}

Khalique's avatar
Khalique committed
590
static std::string enclose_name(const std::string& name)
591
{
Khalique's avatar
Khalique committed
592
    return '"' + replace_string(name, "\"", "\\\"") + '"';
593
594
}

595
596
597
598
void program::print_graph(std::ostream& os) const
{
    os << "digraph {" << std::endl;
    os << "\trankdir=LR;" << std::endl;
599
    print_program(*this, [&](auto ins, const auto& names) {
Khalique's avatar
Khalique committed
600
601
        os << "\t" << enclose_name(names.at(ins))
           << "[label=" << enclose_name(to_string(ins->get_operator())) << "];";
602
603
604
605
606
607
608
609
610
611
612
        os << std::endl;
        if(!ins->inputs().empty())
        {
            for(auto&& arg : ins->inputs())
            {
                os << "\t" << enclose_name(names.at(arg)) << " -> " << enclose_name(names.at(ins));
                os << "[label=" << enclose_name(to_string(ins->get_shape())) << "];";
                os << std::endl;
            }
        }
    });
613
614
615
    os << "}" << std::endl;
}

Paul's avatar
Paul committed
616
617
void program::dry_run(std::unordered_map<std::string, argument> params) const
{
Paul's avatar
Paul committed
618
    auto& ctx = this->impl->ctx;
Paul's avatar
Paul committed
619
    generic_eval(*this, ctx, std::move(params), [](auto&&...) { return argument{}; });
Paul's avatar
Paul committed
620
621
}

Paul's avatar
Paul committed
622
623
void program::annotate(std::ostream& os, std::function<void(instruction_ref)> a) const
{
624
625
626
627
628
    print_program(*this, [&](auto ins, const auto& names) {
        print_instruction(os, ins, names);
        a(ins);
        os << std::endl;
    });
Paul's avatar
Paul committed
629
630
}

Paul's avatar
Paul committed
631
bool operator==(const program& x, const program& y) { return to_string(x) == to_string(y); }
Paul's avatar
Paul committed
632

Paul's avatar
Paul committed
633
std::ostream& operator<<(std::ostream& os, const program& p)
Paul's avatar
Paul committed
634
{
635
636
637
638
    print_program(p, [&](auto ins, const auto& names) {
        print_instruction(os, ins, names);
        os << std::endl;
    });
Paul's avatar
Paul committed
639
    return os;
Paul's avatar
Paul committed
640
}
Paul's avatar
Paul committed
641

Paul's avatar
Paul committed
642
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
643
} // namespace migraphx