program.cpp 37.6 KB
Newer Older
1
#include "migraphx/instruction_ref.hpp"
2
#include <functional>
Paul's avatar
Paul committed
3
4
5
#include <migraphx/program.hpp>
#include <migraphx/stringutils.hpp>
#include <migraphx/instruction.hpp>
6
#include <migraphx/op/identity.hpp>
Paul's avatar
Paul committed
7
#include <migraphx/target.hpp>
Paul's avatar
Paul committed
8
9
10
#include <migraphx/env.hpp>
#include <migraphx/ranges.hpp>
#include <migraphx/time.hpp>
11
#include <migraphx/pass_manager.hpp>
12
#include <migraphx/register_target.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
13
#include <migraphx/iterator_for.hpp>
14
#include <migraphx/iterator.hpp>
15
#include <migraphx/algorithm.hpp>
16
#include <migraphx/output_iterator.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
17
#include <migraphx/make_op.hpp>
18
#include <migraphx/marker.hpp>
Paul's avatar
Paul committed
19
#include <iostream>
20
#include <numeric>
Paul's avatar
Paul committed
21
#include <sstream>
Paul's avatar
Paul committed
22
#include <algorithm>
23
#include <set>
Paul's avatar
Paul committed
24
#include <utility>
25
#include <iomanip>
26

27
#include <unordered_set>
Shucai Xiao's avatar
Shucai Xiao committed
28
29
#include <map>
#include <cassert>
Paul's avatar
Paul committed
30

Paul's avatar
Paul committed
31
namespace migraphx {
Paul's avatar
Paul committed
32
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Paul committed
33

34
35
using milliseconds = std::chrono::duration<double, std::milli>;

Paul's avatar
Paul committed
36
37
struct program_impl
{
Shucai Xiao's avatar
Shucai Xiao committed
38
    // A map is used to keep references to modules of the program
39
    std::unordered_map<std::string, module> modules;
Paul's avatar
Paul committed
40
    context ctx;
41
    std::string target_name;
Paul's avatar
Paul committed
42
43
};

44
program::program() : impl(std::make_unique<program_impl>()) { this->create_module("main"); }
Paul's avatar
Paul committed
45

Paul's avatar
Paul committed
46
program::program(program&&) noexcept = default;
Shucai Xiao's avatar
Shucai Xiao committed
47
program::~program() noexcept         = default;
Paul's avatar
Paul committed
48

49
// copy constructor
Shucai Xiao's avatar
Shucai Xiao committed
50
program::program(const program& p) { assign(p); }
51
52

// copy assignment operator
Shucai Xiao's avatar
Shucai Xiao committed
53
program& program::operator=(program p)
54
{
Shucai Xiao's avatar
Shucai Xiao committed
55
    std::swap(p.impl, this->impl);
56
57
58
    return *this;
}

Shucai Xiao's avatar
Shucai Xiao committed
59
void program::assign(const program& p)
60
{
Shucai Xiao's avatar
Shucai Xiao committed
61
    if(!impl)
62
63
64
    {
        impl = std::make_unique<program_impl>();
    }
Shucai Xiao's avatar
Shucai Xiao committed
65
    else if(!impl->modules.empty())
66
    {
Shucai Xiao's avatar
Shucai Xiao committed
67
        impl->modules.clear();
68
    }
Shucai Xiao's avatar
Shucai Xiao committed
69

70
    impl->ctx         = p.impl->ctx;
Shucai Xiao's avatar
Shucai Xiao committed
71
72
    impl->target_name = p.impl->target_name;
    impl->modules     = p.impl->modules;
Shucai Xiao's avatar
Shucai Xiao committed
73
74
75
76

    // build a map from old ins to new ins
    // Build a map from old module to new module
    std::unordered_map<module_ref, module_ref> mod_map;
Paul's avatar
Paul committed
77
78
79
80
81
    std::transform(
        impl->modules.begin(),
        impl->modules.end(),
        std::inserter(mod_map, mod_map.begin()),
        [&](auto&& xp) { return std::make_pair(&p.impl->modules.at(xp.first), &xp.second); });
Shucai Xiao's avatar
Shucai Xiao committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

    std::unordered_map<instruction_ref, instruction_ref> ins_map;
    for(auto&& pp : mod_map)
    {
        auto old_ins = iterator_for(*pp.first);
        auto new_ins = iterator_for(*pp.second);
        std::transform(old_ins.begin(),
                       old_ins.end(),
                       new_ins.begin(),
                       std::inserter(ins_map, ins_map.begin()),
                       [](auto x, auto y) { return std::make_pair(x, y); });
    }

    // Update all references from all modules
    for(auto&& mp : impl->modules)
    {
98
        for(auto ins : iterator_for(mp.second))
Shucai Xiao's avatar
Shucai Xiao committed
99
100
            instruction::replace_refs(ins, ins_map, mod_map);
    }
101
102
}

Paul's avatar
Paul committed
103
shape program::get_parameter_shape(std::string name) const
Paul's avatar
Paul committed
104
{
Shucai Xiao's avatar
Shucai Xiao committed
105
106
    const auto* mm = this->get_main_module();
    return mm->get_parameter_shape(std::move(name));
Paul's avatar
Paul committed
107
108
}

109
110
std::vector<std::string> program::get_parameter_names() const
{
Shucai Xiao's avatar
Shucai Xiao committed
111
112
    const auto* mm = this->get_main_module();
    return mm->get_parameter_names();
113
114
}

mei-ye's avatar
mei-ye committed
115
116
instruction_ref program::get_parameter(std::string name) const
{
Shucai Xiao's avatar
Shucai Xiao committed
117
118
    const auto* mm = this->get_main_module();
    return mm->get_parameter(std::move(name));
mei-ye's avatar
mei-ye committed
119
120
}

Paul's avatar
Paul committed
121
122
std::unordered_map<std::string, shape> program::get_parameter_shapes() const
{
Shucai Xiao's avatar
Shucai Xiao committed
123
124
    const auto* mm = this->get_main_module();
    return mm->get_parameter_shapes();
Paul's avatar
Paul committed
125
126
}

Shucai Xiao's avatar
Shucai Xiao committed
127
std::size_t program::size() const { return impl->modules.size(); }
128

129
130
std::vector<shape> program::get_output_shapes() const
{
Shucai Xiao's avatar
Shucai Xiao committed
131
132
    const auto* mm = this->get_main_module();
    return mm->get_output_shapes();
133
}
Paul's avatar
Paul committed
134

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

Paul's avatar
Paul committed
137
138
instruction_ref program::validate() const
{
Shucai Xiao's avatar
Shucai Xiao committed
139
140
    const auto* mm = this->get_main_module();
    return mm->validate();
Paul's avatar
Paul committed
141
142
}

143
144
bool program::is_compiled() const { return not this->impl->target_name.empty(); }

145
void program::compile(const target& t, compile_options options)
Paul's avatar
Paul committed
146
{
147
148
149
    assert(not this->is_compiled());
    this->impl->target_name = t.name();
    this->impl->ctx         = t.get_context();
Paul's avatar
Paul committed
150
    if(enabled(MIGRAPHX_TRACE_COMPILE{}))
151
        options.trace = tracer{std::cout};
Shucai Xiao's avatar
Shucai Xiao committed
152

153
154
    options.trace(*this);
    options.trace();
Shucai Xiao's avatar
Shucai Xiao committed
155

Shucai Xiao's avatar
Shucai Xiao committed
156
    auto&& passes = t.get_passes(this->impl->ctx, options);
157
158
159
    run_passes(*this, passes, options.trace);

    auto mods = this->get_modules();
Shucai Xiao's avatar
Shucai Xiao committed
160

161
162
    // Validate and finalize
    for(const auto& mod : reverse(mods))
Paul's avatar
Paul committed
163
    {
Shucai Xiao's avatar
Shucai Xiao committed
164
165
166
167
168
169
        auto invalid = mod->validate();
        if(invalid != mod->end())
        {
            MIGRAPHX_THROW("Invalid module " + mod->name() + " from compilation at instruction " +
                           std::to_string(std::distance(mod->begin(), invalid)));
        }
170
171
172
173
174
        auto dangling = mod->find_dangling_reference();
        if(dangling != mod->end())
        {
            auto index = std::distance(mod->begin(), dangling);
            MIGRAPHX_THROW("Dangling reference in module " + mod->name() + " from instruction " +
175
                           std::to_string(index) + ", (" + dangling->name() + ")");
176
        }
Shucai Xiao's avatar
Shucai Xiao committed
177
        mod->finalize(this->impl->ctx);
Paul's avatar
Paul committed
178
    }
Paul's avatar
Paul committed
179
180
181
182
}

void program::finalize()
{
Shucai Xiao's avatar
Shucai Xiao committed
183
184
    auto* mm = this->get_main_module();
    mm->finalize(this->impl->ctx);
Paul's avatar
Paul committed
185
186
}

187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
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
243
template <class T>
std::string classify(T x)
{
    switch(std::fpclassify(x))
    {
    case FP_INFINITE: return "inf";
    case FP_NAN: return "nan";
    case FP_NORMAL: return "normal";
    case FP_SUBNORMAL: return "subnormal";
    case FP_ZERO: return "zero";
    default: return "unknown";
    }
}

std::unordered_set<std::string> classify_argument(const argument& a)
{
    std::unordered_set<std::string> result;
    a.visit(
        [&](auto t) {
            for(const auto& x : t)
                result.insert(classify(x));
        },
        [&](const auto& xs) {
            for(const auto& x : xs)
            {
                auto r = classify_argument(x);
                result.insert(r.begin(), r.end());
            }
        });
    return result;
}

void preview_argument(std::ostream& os, const argument& a)
{
    a.visit(
        [&](auto t) {
            if(t.size() <= 10)
            {
                os << t;
            }
            else
            {
                os << to_string_range(t.begin(), t.begin() + 5);
                os << ", ..., ";
                os << to_string_range(t.end() - 5, t.end());
            }
        },
        [&](const auto& xs) {
            for(const auto& x : xs)
            {
                os << '{';
                preview_argument(os, x);
                os << '}';
            }
        });
}

Paul's avatar
Paul committed
244
template <class F>
Shucai Xiao's avatar
Shucai Xiao committed
245
std::vector<argument> generic_eval(const module* mod,
246
247
                                   context& ctx,
                                   std::unordered_map<std::string, argument> params,
Shucai Xiao's avatar
Shucai Xiao committed
248
                                   std::unordered_map<instruction_ref, argument> results,
249
                                   F make_trace)
Paul's avatar
Paul committed
250
{
Shucai Xiao's avatar
Shucai Xiao committed
251
252
    assert(mod->validate() == mod->end());
    results.reserve(mod->size() * 2);
Paul's avatar
Paul committed
253
254
    std::vector<argument> values;
    values.reserve(16);
255
    auto trace = make_trace(mod);
Shucai Xiao's avatar
Shucai Xiao committed
256
    for(auto ins : iterator_for(*mod))
Paul's avatar
Paul committed
257
    {
258
        assert(results.find(ins) == results.end());
259
260
        const auto& name = ins->name();
        if(name == "@literal")
Paul's avatar
Paul committed
261
        {
Paul's avatar
Paul committed
262
            results.emplace(ins, trace(ins, [&] { return ins->get_literal().get_argument(); }));
Paul's avatar
Paul committed
263
        }
264
        else if(name == "@param")
Paul's avatar
Paul committed
265
        {
Paul's avatar
Paul committed
266
267
268
269
270
            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);
271
                    auto param = params[param_name];
Paul's avatar
Paul committed
272
273
274
275
276
                    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
277
        }
278
        else if(name == "@outline")
Paul's avatar
Paul committed
279
        {
Paul's avatar
Paul committed
280
            results.emplace(ins, trace(ins, [&] { return argument{ins->get_shape(), nullptr}; }));
Paul's avatar
Paul committed
281
        }
282
283
284
285
286
287
288
289
290
291
292
293
294
        else if(name == "@return")
        {
            std::vector<argument> prog_outputs;
            std::transform(ins->inputs().begin(),
                           ins->inputs().end(),
                           std::back_inserter(prog_outputs),
                           [&](instruction_ref i) {
                               assert(results.find(i) != results.end());
                               return results[i];
                           });

            return prog_outputs;
        }
Paul's avatar
Paul committed
295
296
        else
        {
Paul's avatar
Paul committed
297
            values.resize(ins->inputs().size());
Paul's avatar
Paul committed
298
299
300
301
302
            std::transform(
                ins->inputs().begin(), ins->inputs().end(), values.begin(), [&](instruction_ref i) {
                    assert(results.find(i) != results.end());
                    return results[i];
                });
Shucai Xiao's avatar
Shucai Xiao committed
303
304
305
306

            const auto& mod_args = ins->module_inputs();
            auto module_eval     = [&](module_ref smod,
                                   const std::unordered_map<std::string, argument>& inputs) {
Shucai Xiao's avatar
Shucai Xiao committed
307
308
                auto ssctx = ctx;
                return generic_eval(smod, ssctx, inputs, results, make_trace);
Shucai Xiao's avatar
Shucai Xiao committed
309
310
            };

Shucai Xiao's avatar
Shucai Xiao committed
311
312
313
314
            results.emplace(ins, trace(ins, [&] {
                                return ins->normalized_operator().compute(
                                    ctx, ins->get_shape(), values, mod_args, module_eval);
                            }));
Paul's avatar
Paul committed
315
        }
316
        assert(results.find(ins) != results.end());
317
        assert(results.at(ins).get_shape() == ins->get_shape());
Paul's avatar
Paul committed
318
    }
Shucai Xiao's avatar
Shucai Xiao committed
319
    return {results.at(std::prev(mod->end()))};
Paul's avatar
Paul committed
320
321
}

Shucai Xiao's avatar
Shucai Xiao committed
322
323
324
325
template <class F>
std::vector<argument> generic_eval(const program& p,
                                   context& ctx,
                                   std::unordered_map<std::string, argument> params,
326
                                   F make_trace)
Shucai Xiao's avatar
Shucai Xiao committed
327
{
Shucai Xiao's avatar
Shucai Xiao committed
328
    const module* mm = p.get_main_module();
329
    return generic_eval(mm, ctx, params, {}, make_trace);
Shucai Xiao's avatar
Shucai Xiao committed
330
331
}

Shucai Xiao's avatar
Shucai Xiao committed
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
static void print_space(std::ostream& os, int n)
{
    for(int i = 0; i < n; ++i)
    {
        os << ' ';
    }
}

using op_flops = std::function<double(const std::vector<shape>& vec_ss)>;
auto& get_flops_funcs()
{
    static std::unordered_map<std::string, op_flops> op_funcs;
    op_funcs.emplace("gemm", [&](const std::vector<shape>& vec_ss) {
        assert(vec_ss.size() >= 2);
        auto sa     = vec_ss.front();
        auto sb     = vec_ss.at(1);
        auto batch  = 1;
        auto lens_a = sa.lens();
        batch =
            std::accumulate(lens_a.rbegin() + 2, lens_a.rend(), 1, std::multiplies<std::size_t>{});
        auto m      = lens_a[lens_a.size() - 2];
        auto k      = lens_a.back();
        auto lens_b = sb.lens();
        assert(k == lens_b[lens_b.size() - 2]);
        auto n = lens_b.back();

        return 2.0 * m * n * k * batch;
    });

    op_funcs.emplace("convolution", [&](const std::vector<shape>& vec_ss) {
        assert(vec_ss.size() >= 2);
        auto alens = vec_ss.front().lens();
        auto blens = vec_ss.at(1).lens();
        auto olens = vec_ss.back().lens();

        auto n  = alens.front();
        auto k  = blens.front();
        auto c  = alens.at(1);
        auto y  = blens.at(2);
        auto x  = blens.back();
        auto ho = olens.at(2);
        auto wo = olens.back();

        return 2.0 * n * k * ho * wo * c * y * x;
    });

    return op_funcs;
}

int program::max_ins_length() const
{
    std::unordered_map<instruction_ref, std::string> names;
    int max_ins_len = 0;

    this->print(names, [&](auto ins, auto ins_names) {
        std::stringstream ss;
        instruction::print(ss, ins, ins_names);
        if(max_ins_len < ss.str().length())
        {
            max_ins_len = ss.str().length();
        }

        // skip return instruction
        if(ins->name() == "@return")
            return;
    });

    return max_ins_len;
}

static auto& get_titles()
{
    static std::vector<std::string> titles = {"Instructions",
                                              "Time(ms)    \t",
                                              "Percentage  \t",
                                              "(b, m, n, k)                    \t",
                                              "Flops(TFlops/s)  \t",
                                              "Throughput(GB/s)"};

    return titles;
}

static void print_title(std::ostream& os, std::size_t max_ins_len)
{
    auto titles      = get_titles();
    std::string& str = titles.front();
    str.append(max_ins_len + 1 - str.length(), ' ');
    str.append(1, '\t');
    for(auto& s : titles)
    {
        os << s;
    }
    os << std::endl;
}

static void print_ins_perf(std::ostream& os,
                           const std::vector<std::string>& titles,
                           instruction_ref ins,
                           double t,
                           double total_t)
{
    auto& time_str  = titles.at(1);
    auto& time_per  = titles.at(2);
    auto& size_str  = titles.at(3);
    auto& flops_str = titles.at(4);
    auto& thrpt_str = titles.at(5);

    auto& flops_funcs = get_flops_funcs();
    std::string tms   = std::to_string(t);
    tms.append(time_str.length() - tms.length(), ' ');
    tms.append(1, '\t');
    double percent   = 100.0 * t / total_t;
    std::string pers = std::to_string(percent);
    auto loc         = pers.find('.');
    if(loc != std::string::npos)
    {
        pers.erase(pers.begin() + loc + 6, pers.end());
    }
    pers.append(time_per.length() - pers.length(), ' ');
    pers.append(1, '\t');

    // calculate flops
    std::string szs;
    std::string flps;
    std::string op_name = ins->name();
    auto nloc           = op_name.find("::");
    op_name.erase(op_name.begin(), op_name.begin() + nloc + 2);
    auto inss = to_shapes(ins->inputs());
    if(contains(flops_funcs, op_name))
    {
        // print size
        auto alens = inss.front().lens();
        auto blens = inss.at(1).lens();
        auto mb =
            std::accumulate(alens.rbegin() + 2, alens.rend(), 1, std::multiplies<std::size_t>{});
        int mm = alens[alens.size() - 2];
        int mk = alens.back();
        int mn = blens.back();

        szs = "{";
        szs.append(std::to_string(mb));
        szs.append(1, ',');
        szs.append(std::to_string(mm));
        szs.append(1, ',');
        szs.append(std::to_string(mk));
        szs.append(1, ',');
        szs.append(std::to_string(mn));
        szs.append("}");
        szs.append(size_str.length() - szs.length(), ' ');

        auto op_flop_func = flops_funcs.at(op_name);
        double flops      = op_flop_func(inss);
        flops /= t;
        // convert to GFlops
        flops /= 1.0e9;
        flps      = std::to_string(flops);
        auto floc = flps.find('.');
        if(floc != std::string::npos)
        {
            flps.erase(flps.begin() + floc + 4, flps.end());
        }
    }
    szs.append(size_str.length() - szs.length(), ' ');
    flps.append(flops_str.length() - flps.length(), ' ');

    // print throughput for pointwise instruction
    auto alias_num = ins->get_operator().output_alias({});
    std::string thrpt;
    if(alias_num != 0)
    {
        auto size =
            std::accumulate(inss.begin(), inss.end(), std::size_t{0}, [&](auto init, auto s) {
                return init + s.bytes();
            });

        double throughput = size / t;
        // convert to GB/s
        throughput /= 1.0e6;
        thrpt     = std::to_string(throughput);
        auto floc = flps.find('.');
        if(floc != std::string::npos)
        {
            thrpt.erase(thrpt.begin() + floc + 4, thrpt.end());
        }
    }
    thrpt.append(thrpt_str.length() - thrpt.length(), ' ');

    os << tms << pers << szs << flps << thrpt << std::endl;
}

522
std::vector<argument> program::eval(parameter_map params) const
Paul's avatar
Paul committed
523
{
Paul's avatar
Paul committed
524
525
    auto& ctx = this->impl->ctx;
#ifndef NDEBUG
526
527
528
529
530
531
532
533
534
535
536
    auto with_check_context = [&](auto f) {
        return [=, &ctx](auto&&) {
            auto sctx          = std::make_shared<context>(ctx);
            auto check_context = [=, &ctx](auto g) {
                assert(is_shared(ctx, *sctx));
                auto x = g();
                *sctx  = ctx;
                return x;
            };
            return [=](auto&&... xs) { return f(xs..., check_context); };
        };
Paul's avatar
Paul committed
537
538
    };
#else
539
540
541
542
543
    auto with_check_context = [](auto f) {
        return [=](auto&&) {
            return [=](auto&&... xs) { return f(xs..., [](auto g) { return g(); }); };
        };
    };
Paul's avatar
Paul committed
544
#endif
Paul's avatar
Paul committed
545
546
547
548

    auto trace_level = value_of(MIGRAPHX_TRACE_EVAL{});

    if(trace_level > 0)
Paul's avatar
Paul committed
549
    {
Shucai Xiao's avatar
Shucai Xiao committed
550
551
        std::unordered_map<instruction_ref, std::string> ins_names;
        this->print(ins_names, [&](auto, auto) { });
552
553
554
555
556
557
        return generic_eval(*this,
                            ctx,
                            std::move(params),
                            with_check_context([&](auto& ins, auto f, auto&& check_context) {
                                ctx.finish();
                                std::cout << "Run instruction: ";
Shucai Xiao's avatar
Shucai Xiao committed
558
                                this->debug_print(ins, ins_names);
559
560
561
562
563
                                timer t{};
                                auto result = check_context(f);
                                double t1   = t.record<milliseconds>();
                                ctx.finish();
                                double t2 = t.record<milliseconds>();
Shucai Xiao's avatar
Shucai Xiao committed
564
565
                                std::cout << "Time: " << t1 << "ms, " << t2 << "ms, execution time:\t";
                                if(trace_level ==2 and ins->name().front() != '@' and
Shucai Xiao's avatar
Shucai Xiao committed
566
567
                                   ins->name() != "load" and not result.empty())
                                {
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
                                    target tgt  = make_target(this->impl->target_name);
                                    auto buffer = tgt.copy_from(result);
                                    if(trace_level == 2)
                                    {
                                        std::cout << "Output has "
                                                  << to_string_range(classify_argument(buffer))
                                                  << std::endl;
                                        std::cout << "Output: ";
                                        preview_argument(std::cout, buffer);
                                        std::cout << std::endl;
                                    }
                                    else
                                    {
                                        std::cout << "Output: " << buffer << std::endl;
                                    }
Shucai Xiao's avatar
Shucai Xiao committed
583
                                }
Shucai Xiao's avatar
Shucai Xiao committed
584
585
586
587
588
589
590
                                else if (trace_level == 3)
                                {
                                    // count max instruction length
                                    auto titles           = get_titles();
                                    double exec_t = t2 - t1;
                                    print_ins_perf(std::cout, titles, ins, exec_t, exec_t);
                                }
591
592
                                return result;
                            }));
Paul's avatar
Paul committed
593
594
595
    }
    else
    {
596
597
598
599
600
601
        return generic_eval(*this,
                            ctx,
                            std::move(params),
                            with_check_context([&](auto&, auto f, auto&& check_context) {
                                return check_context(f);
                            }));
Paul's avatar
Paul committed
602
    }
Paul's avatar
Paul committed
603
604
}

605
const int program_file_version = 5;
606
607
608
609
610
611
612
613

value program::to_value() const
{
    value result;
    result["version"] = program_file_version;
    result["target"]  = this->impl->target_name;
    if(not this->impl->target_name.empty())
        result["context"] = this->impl->ctx.to_value();
Shucai Xiao's avatar
Shucai Xiao committed
614

615
    value module_vals = value::object{};
Shucai Xiao's avatar
Shucai Xiao committed
616
    std::unordered_map<instruction_ref, std::string> names;
617
    for(auto& mod : this->get_modules())
Shucai Xiao's avatar
Shucai Xiao committed
618
    {
Shucai Xiao's avatar
Shucai Xiao committed
619
620
        value mod_val;
        value nodes;
621
622
        mod_val["name"] = mod->name();
        names           = mod->print(
Shucai Xiao's avatar
Shucai Xiao committed
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
            [&](auto ins, auto ins_names) {
                value node;
                node["output"]     = ins_names.at(ins);
                node["name"]       = ins->name();
                node["shape"]      = migraphx::to_value(ins->get_shape());
                node["normalized"] = ins->is_normalized();
                if(ins->name() == "@literal")
                    node["literal"] = migraphx::to_value(ins->get_literal());
                node["operator"] = ins->get_operator().to_value();
                std::vector<std::string> inputs;
                std::transform(ins->inputs().begin(),
                               ins->inputs().end(),
                               std::back_inserter(inputs),
                               [&](auto i) {
                                   assert(contains(ins_names, i));
                                   return ins_names.at(i);
                               });
                node["inputs"]   = inputs;
                auto module_args = ins->module_inputs();
                if(not module_args.empty())
                {
                    std::vector<std::string> module_inputs;
                    std::transform(module_args.begin(),
                                   module_args.end(),
                                   std::back_inserter(module_inputs),
                                   [&](auto mod_ref) { return mod_ref->name(); });
                    node["module_inputs"] = module_inputs;
                }

                nodes.push_back(node);
            },
            names);
        mod_val["nodes"] = nodes;

657
        module_vals[mod->name()] = mod_val;
Shucai Xiao's avatar
Shucai Xiao committed
658
    }
Shucai Xiao's avatar
Shucai Xiao committed
659
660
661

    result["modules"] = module_vals;

662
663
    return result;
}
Shucai Xiao's avatar
Shucai Xiao committed
664

Shucai Xiao's avatar
Shucai Xiao committed
665
666
667
668
669
static void mod_from_val(module_ref mod,
                         const value& v,
                         std::unordered_map<std::string, instruction_ref>& instructions,
                         const std::unordered_map<std::string, module_ref>& map_mods)
{
670
    const auto& module_val = v.at(mod->name());
Shucai Xiao's avatar
Shucai Xiao committed
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
    for(const value& node : module_val.at("nodes"))
    {
        instruction_ref output;
        auto name       = node.at("name").to<std::string>();
        auto fields     = node.at("operator");
        auto normalized = node.at("normalized").to<bool>();

        if(name == "@param")
        {
            output = mod->add_parameter(fields["parameter"].to<std::string>(),
                                        migraphx::from_value<shape>(node.at("shape")));
        }
        else if(name == "@literal")
        {
            output = mod->add_literal(migraphx::from_value<literal>(node.at("literal")));
        }
        else
        {
            auto op = make_op(name, fields);
            std::vector<instruction_ref> inputs;
            std::transform(node.at("inputs").begin(),
                           node.at("inputs").end(),
                           std::back_inserter(inputs),
                           [&](const value& i) {
                               auto i_name = i.to<std::string>();
                               assert(contains(instructions, i_name));
                               return instructions.at(i_name);
                           });

            std::vector<module_ref> module_inputs;
            if(node.contains("module_inputs"))
            {
                std::transform(node.at("module_inputs").begin(),
                               node.at("module_inputs").end(),
                               std::back_inserter(module_inputs),
                               [&](const value& i) { return map_mods.at(i.to<std::string>()); });

                for(auto& smod : module_inputs)
                {
                    mod_from_val(smod, v, instructions, map_mods);
                }
            }

            if(name == "@return")
            {
                output = mod->add_return(inputs);
            }
            else if(module_inputs.empty())
            {
                output = mod->add_instruction(op, inputs);
            }
            else
            {
                output = mod->add_instruction(op, inputs, module_inputs);
            }
        }
        output->set_normalized(normalized);
        instructions[node.at("output").to<std::string>()] = output;
    }
}

732
733
734
735
void program::from_value(const value& v)
{
    auto version = v.at("version").to<int>();
    if(version != program_file_version)
Shucai Xiao's avatar
Shucai Xiao committed
736
737
738
739
    {
        MIGRAPHX_THROW("Warning: Program version mismatch");
    }

740
741
742
743
744
745
746
747
    this->impl->target_name = v.at("target").to<std::string>();
    if(not this->impl->target_name.empty())
    {
        target t        = make_target(this->impl->target_name);
        this->impl->ctx = t.get_context();
        this->impl->ctx.from_value(v.at("context"));
    }

Shucai Xiao's avatar
Shucai Xiao committed
748
749
    auto module_vals = v.at("modules");
    for(const auto& vv : module_vals)
750
    {
751
        const auto& name = vv.get_key();
Shucai Xiao's avatar
Shucai Xiao committed
752
753
        if(name == "main")
            continue;
754
        impl->modules.emplace(name, name);
755
    }
756
    std::unordered_map<std::string, module_ref> map_mods;
Paul's avatar
Paul committed
757
758
759
760
    std::transform(impl->modules.begin(),
                   impl->modules.end(),
                   std::inserter(map_mods, map_mods.end()),
                   [&](auto&& pp) { return std::make_pair(pp.first, &pp.second); });
Shucai Xiao's avatar
Shucai Xiao committed
761
762
763
764
765

    std::unordered_map<std::string, instruction_ref> map_insts;
    auto* mm = get_main_module();
    mod_from_val(mm, module_vals, map_insts, map_mods);

766
767
768
    this->finalize();
}

Paul's avatar
Paul committed
769
770
771
double common_average(const std::vector<double>& v)
{
    std::size_t n = v.size() / 4;
Paul's avatar
Paul committed
772
773
    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
774
775
}

Paul Fultz II's avatar
Paul Fultz II committed
776
777
778
779
780
781
782
783
std::string perf_group(const operation& op)
{
    auto attr = op.attributes();
    if(attr.contains("group"))
        return attr.at("group").to<std::string>();
    return op.name();
}

784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
void program::mark(const parameter_map& params, marker&& m)
{
    auto& ctx = this->impl->ctx;
    // Run once by itself
    eval(params);
    ctx.finish();
    // Start marking
    m.mark_start(*this);
    generic_eval(*this, ctx, params, always([&](auto ins, auto f) {
        argument result;
        m.mark_start(ins);
        result = f();
        m.mark_stop(ins);
        return result;
    }));
    m.mark_stop(*this);
}

802
803
804
805
void program::perf_report(std::ostream& os,
                          std::size_t n,
                          parameter_map params,
                          std::size_t batch) const
Paul's avatar
Paul committed
806
{
807
    auto& ctx = this->impl->ctx;
Paul's avatar
Paul committed
808
809
    // Run once by itself
    eval(params);
Paul's avatar
Paul committed
810
    ctx.finish();
Paul's avatar
Paul committed
811
    // Run and time entire program
Paul's avatar
Paul committed
812
813
    std::vector<double> total_vec;
    total_vec.reserve(n);
Paul's avatar
Paul committed
814
    for(std::size_t i = 0; i < n; i++)
Paul's avatar
Paul committed
815
    {
Paul's avatar
Paul committed
816
817
818
819
        total_vec.push_back(time<milliseconds>([&] {
            eval(params);
            ctx.finish();
        }));
Paul's avatar
Paul committed
820
    }
Paul's avatar
Paul committed
821
822
    std::sort(total_vec.begin(), total_vec.end());
    std::unordered_map<instruction_ref, std::vector<double>> ins_vec;
Paul's avatar
Paul committed
823
    // Fill the map
824
    generic_eval(*this, ctx, params, always([&](auto ins, auto) {
Paul's avatar
Paul committed
825
        ins_vec[ins].reserve(n);
826
        return argument{ins->get_shape(), nullptr};
827
    }));
828

Paul's avatar
Paul committed
829
    // Run and time each instruction
Paul's avatar
Paul committed
830
    for(std::size_t i = 0; i < n; i++)
Paul's avatar
Paul committed
831
    {
832
        generic_eval(*this, ctx, params, always([&](auto ins, auto f) {
833
            argument result;
Paul's avatar
Paul committed
834
835
836
837
            ins_vec[ins].push_back(time<milliseconds>([&] {
                result = f();
                ctx.finish();
            }));
838
            return result;
839
        }));
Paul's avatar
Paul committed
840
    }
Paul's avatar
Paul committed
841
842
    for(auto&& p : ins_vec)
        std::sort(p.second.begin(), p.second.end());
Paul's avatar
Paul committed
843
    // Run and time implicit overhead
Paul's avatar
Paul committed
844
845
    std::vector<double> overhead_vec;
    overhead_vec.reserve(n);
Paul's avatar
Paul committed
846
    for(std::size_t i = 0; i < n; i++)
Paul's avatar
Paul committed
847
    {
Paul's avatar
Paul committed
848
        overhead_vec.push_back(time<milliseconds>([&] { dry_run(params); }));
Paul's avatar
Paul committed
849
850
    }

Paul's avatar
Paul committed
851
    double total_time             = common_average(total_vec);
Paul's avatar
Paul committed
852
    double rate                   = 1000.0 / total_time;
Paul's avatar
Paul committed
853
    double overhead_time          = common_average(overhead_vec);
Paul's avatar
Paul committed
854
    double overhead_percent       = overhead_time * 100.0 / total_time;
Paul's avatar
Paul committed
855
    double total_instruction_time = 0.0;
Paul's avatar
Paul committed
856
    std::unordered_map<std::string, double> op_times;
Paul's avatar
Paul committed
857
    for(auto&& p : ins_vec)
Paul's avatar
Paul committed
858
859
    {
        double avg = common_average(p.second);
Paul Fultz II's avatar
Paul Fultz II committed
860
        op_times[perf_group(p.first->get_operator())] += avg;
Paul's avatar
Paul committed
861
862
        total_instruction_time += avg;
    }
Paul's avatar
Paul committed
863
864
    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
865

Shucai Xiao's avatar
Shucai Xiao committed
866
    std::unordered_map<instruction_ref, std::string> names;
867
868

    // count max instruction length
Shucai Xiao's avatar
Shucai Xiao committed
869
    auto titles           = get_titles();
870
871
    const int max_ins_len = max_ins_length();
    print_title(os, max_ins_len);
872

Shucai Xiao's avatar
Shucai Xiao committed
873
    this->print(names, [&](auto ins, auto ins_names) {
874
875
876
        std::stringstream ss;
        instruction::print(ss, ins, ins_names);
        os << ss.str();
877
878
879
880
881

        // skip return instruction
        if(ins->name() == "@return")
            return;

882
883
        // insert space to align
        print_space(os, max_ins_len - ss.str().length());
Shucai Xiao's avatar
Shucai Xiao committed
884
        os << "\t";
Shucai Xiao's avatar
Shucai Xiao committed
885
        double avg = common_average(ins_vec[ins]);
886
        print_ins_perf(os, titles, ins, avg, total_instruction_time);
Paul's avatar
Paul committed
887
    });
Paul's avatar
Paul committed
888
889
890

    os << std::endl;
    os << "Summary:" << std::endl;
891
892
893
894
895
896
897
    std::vector<std::pair<double, std::string>> op_times_sorted;
    std::transform(op_times.begin(),
                   op_times.end(),
                   std::back_inserter(op_times_sorted),
                   [](auto p) { return std::make_pair(p.second, p.first); });
    std::sort(op_times_sorted.begin(), op_times_sorted.end(), std::greater<>{});
    for(auto&& p : op_times_sorted)
Paul's avatar
Paul committed
898
    {
899
900
        auto&& name    = p.second;
        double avg     = p.first;
Paul's avatar
Paul committed
901
902
903
904
905
        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
906

907
908
    os << "Batch size: " << batch << std::endl;
    os << "Rate: " << rate * batch << "/sec" << std::endl;
Paul's avatar
Paul committed
909
910
    os << "Total time: " << total_time << "ms" << std::endl;
    os << "Total instructions time: " << total_instruction_time << "ms" << std::endl;
Paul's avatar
Paul committed
911
912
913
914
    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
915
916
}

Paul's avatar
Paul committed
917
918
void program::debug_print() const { std::cout << *this << std::endl; }
void program::debug_print(instruction_ref ins) const
Paul's avatar
Paul committed
919
{
Shucai Xiao's avatar
Shucai Xiao committed
920
    std::unordered_map<instruction_ref, std::string> names;
921
    if(std::any_of(this->impl->modules.begin(), this->impl->modules.end(), [&](const auto& pp) {
922
           return is_end(pp.second.end(), ins);
Shucai Xiao's avatar
Shucai Xiao committed
923
       }))
Paul's avatar
Paul committed
924
925
926
927
    {
        std::cout << "End instruction" << std::endl;
        return;
    }
928
929
    else if(std::none_of(this->impl->modules.begin(),
                         this->impl->modules.end(),
930
                         [&](const auto& pp) { return pp.second.has_instruction(ins); }))
Paul's avatar
Paul committed
931
932
933
934
    {
        std::cout << "Instruction not part of program" << std::endl;
        return;
    }
Shucai Xiao's avatar
Shucai Xiao committed
935

Paul's avatar
Paul committed
936
    std::stringstream ss;
Shucai Xiao's avatar
Shucai Xiao committed
937
    this->print(names, [&](auto x, auto ins_names) {
Paul's avatar
Paul committed
938
        if(x == ins)
Paul's avatar
Paul committed
939
        {
Shucai Xiao's avatar
Shucai Xiao committed
940
            instruction::print(std::cout, x, ins_names);
Paul's avatar
Paul committed
941
942
943
944
945
            std::cout << std::endl;
        }
    });
}

Shucai Xiao's avatar
Shucai Xiao committed
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
void program::debug_print(instruction_ref ins, const std::unordered_map<instruction_ref, std::string>& names) const
{
    if(std::any_of(this->impl->modules.begin(), this->impl->modules.end(), [&](const auto& pp) {
           return is_end(pp.second.end(), ins);
       }))
    {
        std::cout << "End instruction" << std::endl;
        return;
    }
    else if(std::none_of(this->impl->modules.begin(),
                         this->impl->modules.end(),
                         [&](const auto& pp) { return pp.second.has_instruction(ins); }))
    {
        std::cout << "Instruction not part of program" << std::endl;
        return;
    }

    if (contains(names, ins))
    {
        instruction::print(std::cout, ins, names);
        std::cout << std::endl;
    }
}

Shucai Xiao's avatar
Shucai Xiao committed
970
971
972
973
void program::print(
    std::unordered_map<instruction_ref, std::string>& names,
    const std::function<void(instruction_ref, std::unordered_map<instruction_ref, std::string>)>&
        print_func) const
974
{
975
    for(const auto& pp : this->impl->modules)
976
    {
977
        names = pp.second.print(print_func, names);
978
979
980
    }
}

Shucai Xiao's avatar
Shucai Xiao committed
981
void program::print_graph(std::ostream& os, bool brief) const
982
{
Shucai Xiao's avatar
Shucai Xiao committed
983
984
    const auto* mm = this->get_main_module();
    mm->print_graph(os, brief);
985
986
987
988
}

void program::print_cpp(std::ostream& os) const
{
Shucai Xiao's avatar
Shucai Xiao committed
989
990
991
992
993
994
995
996
    auto vec_modules = this->get_modules();
    std::unordered_map<instruction_ref, std::string> names;
    for(auto& mod : vec_modules)
    {
        os << "module: \"" << mod->name() << "\"" << std::endl;
        names = mod->print_cpp(os, names);
        os << std::endl;
    }
997
998
}

Paul's avatar
Paul committed
999
1000
void program::dry_run(std::unordered_map<std::string, argument> params) const
{
Paul's avatar
Paul committed
1001
    auto& ctx = this->impl->ctx;
1002
1003
1004
    generic_eval(*this, ctx, std::move(params), always([](auto ins, auto&&...) {
        return argument{ins->get_shape(), nullptr};
    }));
Paul's avatar
Paul committed
1005
1006
}

Shucai Xiao's avatar
Shucai Xiao committed
1007
void program::annotate(std::ostream& os, const std::function<void(instruction_ref)>& a) const
Paul's avatar
Paul committed
1008
{
1009
    for(auto& pp : this->impl->modules)
Shucai Xiao's avatar
Shucai Xiao committed
1010
    {
1011
1012
        std::cout << pp.first << ":" << std::endl;
        pp.second.annotate(os, a);
Shucai Xiao's avatar
Shucai Xiao committed
1013
    }
Paul's avatar
Paul committed
1014
1015
}

Paul's avatar
Paul committed
1016
const module* program::get_module(const std::string& name) const { return &impl->modules.at(name); }
Shucai Xiao's avatar
Shucai Xiao committed
1017
1018
1019

module* program::create_module(const std::string& name)
{
1020
    assert(not contains(impl->modules, name));
1021
1022
    auto r = impl->modules.emplace(name, name);
    return &(r.first->second);
Shucai Xiao's avatar
Shucai Xiao committed
1023
1024
}

Paul's avatar
Paul committed
1025
module* program::get_module(const std::string& name) { return &impl->modules.at(name); }
Shucai Xiao's avatar
Shucai Xiao committed
1026
1027
1028
1029
1030

module* program::get_main_module() { return get_module("main"); }

const module* program::get_main_module() const { return get_module("main"); }

Paul's avatar
Paul committed
1031
template <class T>
1032
std::vector<T*> generic_get_modules(T* mm)
Shucai Xiao's avatar
Shucai Xiao committed
1033
{
1034
    std::vector<T*> vec_modules;
Shucai Xiao's avatar
Shucai Xiao committed
1035
1036
1037
1038
1039
    vec_modules.push_back(mm);
    auto sub_modules = mm->get_sub_modules();
    vec_modules.insert(vec_modules.end(), sub_modules.begin(), sub_modules.end());
    return vec_modules;
}
Shucai Xiao's avatar
Shucai Xiao committed
1040

Paul's avatar
Paul committed
1041
template <class Map, class T, class OutputIterator>
1042
void generic_get_unused_modules(Map& m, const std::vector<T*>& mods, OutputIterator out)
Shucai Xiao's avatar
Shucai Xiao committed
1043
{
1044
1045
1046
1047
    std::unordered_set<std::string> used;
    std::transform(mods.begin(), mods.end(), std::inserter(used, used.end()), [](auto&& mod) {
        return mod->name();
    });
Paul's avatar
Paul committed
1048
1049
1050
1051
1052
    transform_if(m.begin(),
                 m.end(),
                 out,
                 [&](auto&& pp) { return not contains(used, pp.first); },
                 [](auto&& pp) { return &pp.second; });
1053
}
Shucai Xiao's avatar
Shucai Xiao committed
1054

1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
std::vector<const module*> program::get_modules() const
{
    auto result = generic_get_modules(this->get_main_module());
    generic_get_unused_modules(impl->modules, result, std::back_inserter(result));
    return result;
}

std::vector<module*> program::get_modules()
{
    auto result = generic_get_modules(this->get_main_module());
    generic_get_unused_modules(impl->modules, result, std::back_inserter(result));
    return result;
Shucai Xiao's avatar
Shucai Xiao committed
1067
1068
}

1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
template <class Map, class T>
bool is_unused_module(Map& m, const std::vector<T*>& mods, const std::string& name)
{
    bool is_unused = false;
    generic_get_unused_modules(m, mods, make_function_output_iterator([&](auto* mod) {
                                   if(mod->name() == name)
                                       is_unused = true;
                               }));
    return is_unused;
}

template <class Map>
bool references_instruction(Map& m, const instruction& ins, const std::string& name)
{
    return std::any_of(m.begin(), m.end(), [&](auto&& p) {
        if(p.first == name)
            return false;
        return std::any_of(p.second.begin(), p.second.end(), [&](auto&& i) {
            return std::any_of(i.inputs().begin(), i.inputs().end(), [&](auto&& j) {
                return std::addressof(*j) == std::addressof(ins);
            });
        });
    });
}

void program::remove_module(const std::string& name)
{
    // cppcheck-suppress assertWithSideEffect
    assert(is_unused_module(impl->modules, generic_get_modules(this->get_main_module()), name) &&
           "Module used in program");
    assert(std::none_of(
               impl->modules.at(name).begin(),
               impl->modules.at(name).end(),
               [&](auto&& ins) { return references_instruction(impl->modules, ins, name); }) &&
           "Instruction referenced in another module");
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119

    // if an instruction has an input out side of the current module, need to remove
    // the instruction from its input's outputs
    auto& mod = impl->modules.at(name);
    for(auto ins : iterator_for(mod))
    {
        auto inputs = ins->inputs();
        for(auto in : inputs)
        {
            if(not mod.has_instruction(in))
            {
                in->remove_output(ins);
            }
        }
    }

1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
    impl->modules.erase(name);
}

void program::remove_unused_modules()
{
    std::vector<module*> unused;
    generic_get_unused_modules(
        impl->modules, generic_get_modules(this->get_main_module()), std::back_inserter(unused));
    for(auto* m : unused)
        this->remove_module(m->name());
}

1132
1133
program& program::sort()
{
1134
    for(auto& pp : this->impl->modules)
Shucai Xiao's avatar
Shucai Xiao committed
1135
    {
1136
        pp.second.sort();
Shucai Xiao's avatar
Shucai Xiao committed
1137
1138
    }

1139
1140
1141
    return *this;
}

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

Paul's avatar
Paul committed
1144
std::ostream& operator<<(std::ostream& os, const program& p)
Paul's avatar
Paul committed
1145
{
Shucai Xiao's avatar
Shucai Xiao committed
1146
1147
1148
    auto vec_modules = p.get_modules();
    std::unordered_map<instruction_ref, std::string> names;
    for(auto& mod : vec_modules)
Shucai Xiao's avatar
Shucai Xiao committed
1149
    {
Shucai Xiao's avatar
Shucai Xiao committed
1150
1151
1152
1153
1154
1155
1156
        os << "module: \"" << mod->name() << "\"" << std::endl;
        names = mod->print(
            [&](auto ins, auto ins_names) {
                instruction::print(os, ins, ins_names);
                os << std::endl;
            },
            names);
1157
        os << std::endl;
Shucai Xiao's avatar
Shucai Xiao committed
1158
1159
    }

Paul's avatar
Paul committed
1160
    return os;
Paul's avatar
Paul committed
1161
}
Paul's avatar
Paul committed
1162

Paul's avatar
Paul committed
1163
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
1164
} // namespace migraphx