migraphx.hpp 25.8 KB
Newer Older
Paul Fultz II's avatar
Paul Fultz II committed
1
2
3
4
5
6
7
8
#ifndef MIGRAPHX_GUARD_API_RTGLIB_MIGRAPHX_HPP
#define MIGRAPHX_GUARD_API_RTGLIB_MIGRAPHX_HPP

#include <migraphx/migraphx.h>
#include <memory>
#include <exception>
#include <vector>
#include <cassert>
Shucai Xiao's avatar
Shucai Xiao committed
9
#include <iostream>
Paul Fultz II's avatar
Paul Fultz II committed
10
11

namespace migraphx {
12
#ifndef DOXYGEN
Paul Fultz II's avatar
Paul Fultz II committed
13
inline namespace api { // NOLINT
14
#endif
Paul Fultz II's avatar
Paul Fultz II committed
15
16
17
18
19
20
21
22
23
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154

template <class T, class F, class... Ts>
T* make(F f, Ts&&... xs)
{
    T* result = nullptr;
    // cppcheck-suppress redundantInitialization
    // cppcheck-suppress redundantAssignment
    // cppcheck-suppress unreadVariable
    auto e = f(&result, std::forward<Ts>(xs)...);
    if(e != migraphx_status_success)
        throw std::runtime_error("Failed to call function");
    return result;
}

template <class F, class... Ts>
void call(F f, Ts&&... xs)
{
    // cppcheck-suppress redundantInitialization
    // cppcheck-suppress redundantAssignment
    // cppcheck-suppress unreadVariable
    auto e = f(std::forward<Ts>(xs)...);
    if(e != migraphx_status_success)
        throw std::runtime_error("Failed to call function");
}

template <class F, class Iterator = std::size_t>
struct iota_iterator
{
    Iterator index;
    F f;

    using difference_type   = std::ptrdiff_t;
    using reference         = decltype(f(std::declval<Iterator>()));
    using value_type        = typename std::remove_reference<reference>::type;
    using pointer           = typename std::add_pointer<value_type>::type;
    using iterator_category = std::input_iterator_tag;

    iota_iterator& operator+=(int n)
    {
        index += n;
        return *this;
    }

    iota_iterator& operator-=(int n)
    {
        index += n;
        return *this;
    }

    iota_iterator& operator++()
    {
        index++;
        return *this;
    }

    iota_iterator& operator--()
    {
        index--;
        return *this;
    }

    iota_iterator operator++(int) // NOLINT
    {
        iota_iterator it = *this;
        index++;
        return it;
    }

    iota_iterator operator--(int) // NOLINT
    {
        iota_iterator it = *this;
        index--;
        return it;
    }
    // TODO: operator->
    reference operator*() const { return (*f)(index); }
};

template <class F, class Iterator>
inline iota_iterator<F, Iterator> operator+(iota_iterator<F, Iterator> x,
                                            iota_iterator<F, Iterator> y)
{
    return iota_iterator<F, Iterator>(x.index + y.index, x.f);
}

template <class F, class Iterator>
inline iota_iterator<F, Iterator> operator-(iota_iterator<F, Iterator> x,
                                            iota_iterator<F, Iterator> y)
{
    return iota_iterator<F, Iterator>(x.index - y.index, x.f);
}

template <class F, class Iterator>
inline bool operator==(iota_iterator<F, Iterator> x, iota_iterator<F, Iterator> y)
{
    return x.index == y.index;
}

template <class F, class Iterator>
inline bool operator!=(iota_iterator<F, Iterator> x, iota_iterator<F, Iterator> y)
{
    return x.index != y.index;
}

template <class Derived>
struct array_base
{
    const Derived& derived() const { return static_cast<const Derived&>(*this); }

    template <class T>
    using value_type_t = decltype(std::declval<T>()[0]);

    template <class T>
    using iterator_t = iota_iterator<typename T::iterator_read>;

    template <class D = Derived>
    value_type_t<D> front() const
    {
        return derived()[0];
    }

    template <class D = Derived>
    value_type_t<D> back() const
    {
        return derived()[derived().size() - 1];
    }

    template <class D = Derived>
    iterator_t<D> begin() const
    {
        return {0, {derived().get_handle_ptr()}};
    }

    template <class D = Derived>
    iterator_t<D> end() const
    {
        return {derived().size(), {derived().get_handle_ptr()}};
    }
};

155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wnon-template-friend"
#endif

template <class T>
struct holder
{
    // Friend injection
    friend auto migraphx_adl_handle_lookup(holder<T>);
    // Function left unimplemented since its only used in non-evaluated
    // context
    T get() const;
};

template <class C, class T>
struct handle_lookup
{
    friend auto migraphx_adl_handle_lookup(holder<T>) { return holder<C>{}; }
};

#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic pop
#endif

template <class T>
using as_handle = decltype(
    migraphx_adl_handle_lookup(holder<std::remove_cv_t<std::remove_pointer_t<T>>>{}).get());

Paul Fultz II's avatar
Paul Fultz II committed
184
185
186
187
188
189
190
struct own
{
};
struct borrow
{
};

191
192
template <class Derived, class T, class D, D Deleter, class A, A Assigner>
struct handle_base : handle_lookup<Derived, std::remove_cv_t<T>>
Paul Fultz II's avatar
Paul Fultz II committed
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
{
    handle_base() : m_handle(nullptr) {}
    template <class F, class... Ts>
    void make_handle(F f, Ts&&... xs)
    {
        using type = typename std::remove_cv<T>::type;
        set_handle(make<type>(f, std::forward<Ts>(xs)...), own{});
    }

    const std::shared_ptr<T>& get_handle() const { return m_handle; }

    T* get_handle_ptr() const
    {
        assert(m_handle != nullptr);
        return get_handle().get();
    }

    template <class U>
    void set_handle(U* ptr, own)
    {
        m_handle = std::shared_ptr<U>{ptr, Deleter};
    }

    template <class U>
    void set_handle(U* ptr, borrow)
    {
        m_handle = std::shared_ptr<U>{ptr, [](U*) {}};
    }

222
223
224
225
226
227
    template <class U>
    void assign_to_handle(U* x)
    {
        Assigner(x, this->get_handle_ptr());
    }

Paul Fultz II's avatar
Paul Fultz II committed
228
229
230
231
    protected:
    std::shared_ptr<T> m_handle;
};

232
233
234
#ifdef DOXYGEN
#define MIGRAPHX_DETAIL_HANDLE_BASE(name, const_) handle_base<>
#else
235
#define MIGRAPHX_DETAIL_HANDLE_BASE(name, const_)       \
236
237
    handle_base<name,                                   \
                const_ migraphx_##name,                 \
238
239
240
241
                decltype(&migraphx_##name##_destroy),   \
                migraphx_##name##_destroy,              \
                decltype(&migraphx_##name##_assign_to), \
                migraphx_##name##_assign_to>
242
#endif
Paul Fultz II's avatar
Paul Fultz II committed
243
244
245
246
247
// NOLINTNEXTLINE
#define MIGRAPHX_HANDLE_BASE(name) MIGRAPHX_DETAIL_HANDLE_BASE(name, )
// NOLINTNEXTLINE
#define MIGRAPHX_CONST_HANDLE_BASE(name) MIGRAPHX_DETAIL_HANDLE_BASE(name, const)

248
249
250
251
252
/**
 * @brief Describe shape of tensor
 * @details A shape consists of a data type, lengths of multi-dimension tensor, and strides
 *
 */
Paul Fultz II's avatar
Paul Fultz II committed
253
254
255
256
257
258
259
260
261
262
struct shape : MIGRAPHX_CONST_HANDLE_BASE(shape)
{
    shape() {}

    shape(const migraphx_shape* p) { this->set_handle(p, borrow{}); }

    shape(migraphx_shape* p, own) { this->set_handle(p, own{}); }

    shape(migraphx_shape* p, borrow) { this->set_handle(p, borrow{}); }

263
    /// Construct a scalar shape
264
265
266
267
268
    shape(migraphx_shape_datatype_t type)
    {
        this->make_handle(&migraphx_shape_create_scalar, type);
    }

269
270
    /// Construct a shape with its type and lengths. The strides are
    /// automatically computed assumming a packed layout.
Paul Fultz II's avatar
Paul Fultz II committed
271
272
273
274
275
    shape(migraphx_shape_datatype_t type, std::vector<size_t> plengths)
    {
        this->make_handle(&migraphx_shape_create, type, plengths.data(), plengths.size());
    }

276
277
278
    shape(migraphx_shape_datatype_t type,
          std::vector<size_t> plengths,
          std::vector<size_t> pstrides)
279
    {
280
281
282
283
284
285
        this->make_handle(&migraphx_shape_create_with_strides,
                          type,
                          plengths.data(),
                          plengths.size(),
                          pstrides.data(),
                          pstrides.size());
286
287
    }

Paul Fultz II's avatar
Paul Fultz II committed
288
289
290
291
292
    std::vector<size_t> lengths() const
    {
        const size_t* pout;
        size_t pout_size;
        call(&migraphx_shape_lengths, &pout, &pout_size, this->get_handle_ptr());
293
        return {pout, pout + pout_size};
Paul Fultz II's avatar
Paul Fultz II committed
294
295
296
297
298
299
300
    }

    std::vector<size_t> strides() const
    {
        const size_t* pout;
        size_t pout_size;
        call(&migraphx_shape_strides, &pout, &pout_size, this->get_handle_ptr());
301
        return {pout, pout + pout_size};
Paul Fultz II's avatar
Paul Fultz II committed
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
    }

    migraphx_shape_datatype_t type() const
    {
        migraphx_shape_datatype_t pout;
        call(&migraphx_shape_type, &pout, this->get_handle_ptr());
        return pout;
    }

    size_t bytes() const
    {
        size_t pout;
        call(&migraphx_shape_bytes, &pout, this->get_handle_ptr());
        return pout;
    }

    friend bool operator==(const shape& px, const shape& py)
    {
        bool pout;
        call(&migraphx_shape_equal, &pout, px.get_handle_ptr(), py.get_handle_ptr());
        return pout;
    }

    friend bool operator!=(const shape& px, const shape& py) { return !(px == py); }
};

328
329
330
331
332
333
/**
 * @brief Arguments to be passed to an migraphx arguments
 *
 * An `argument` represents a raw buffer of data with a shape.
 *
 */
Paul Fultz II's avatar
Paul Fultz II committed
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
struct argument : MIGRAPHX_CONST_HANDLE_BASE(argument)
{
    argument() {}

    argument(migraphx_argument* p, borrow) { this->set_handle(p, borrow{}); }

    argument(migraphx_argument* p, own) { this->set_handle(p, own{}); }

    argument(const migraphx_argument* p) { this->set_handle(p, borrow{}); }

    argument(shape pshape, void* pbuffer)
    {
        this->make_handle(&migraphx_argument_create, pshape.get_handle_ptr(), pbuffer);
    }

    shape get_shape() const
    {
        const_migraphx_shape_t pout;
        call(&migraphx_argument_shape, &pout, this->get_handle_ptr());
353
        return {pout};
Paul Fultz II's avatar
Paul Fultz II committed
354
355
356
357
358
359
360
361
362
    }

    char* data() const
    {
        char* pout;
        call(&migraphx_argument_buffer, &pout, this->get_handle_ptr());
        return pout;
    }

363
    /// Generate an argument using random data
Paul Fultz II's avatar
Paul Fultz II committed
364
365
    static argument generate(shape ps, size_t pseed = 0)
    {
366
367
        return {make<migraphx_argument>(&migraphx_argument_generate, ps.get_handle_ptr(), pseed),
                own{}};
Paul Fultz II's avatar
Paul Fultz II committed
368
369
370
371
372
373
374
375
376
377
378
379
    }

    friend bool operator==(const argument& px, const argument& py)
    {
        bool pout;
        call(&migraphx_argument_equal, &pout, px.get_handle_ptr(), py.get_handle_ptr());
        return pout;
    }

    friend bool operator!=(const argument& px, const argument& py) { return !(px == py); }
};

380
/// A target for compilation
Paul Fultz II's avatar
Paul Fultz II committed
381
382
383
384
385
386
387
388
struct target : MIGRAPHX_HANDLE_BASE(target)
{
    target() {}

    target(migraphx_target* p, own) { this->set_handle(p, own{}); }

    target(migraphx_target* p, borrow) { this->set_handle(p, borrow{}); }

389
    /// Construct a target from its name
Paul Fultz II's avatar
Paul Fultz II committed
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
    target(const char* name) { this->make_handle(&migraphx_target_create, name); }
};

struct program_parameter_shapes : MIGRAPHX_HANDLE_BASE(program_parameter_shapes)
{
    program_parameter_shapes() {}

    program_parameter_shapes(migraphx_program_parameter_shapes* p, own)
    {
        this->set_handle(p, own{});
    }

    program_parameter_shapes(migraphx_program_parameter_shapes* p, borrow)
    {
        this->set_handle(p, borrow{});
    }

    size_t size() const
    {
        size_t pout;
        call(&migraphx_program_parameter_shapes_size, &pout, this->get_handle_ptr());
        return pout;
    }

    shape operator[](const char* pname) const
    {
        const_migraphx_shape_t pout;
        call(&migraphx_program_parameter_shapes_get, &pout, this->get_handle_ptr(), pname);
418
        return {pout};
Paul Fultz II's avatar
Paul Fultz II committed
419
420
421
422
423
    }

    std::vector<const char*> names() const
    {
        std::vector<const char*> result(this->size());
Shucai Xiao's avatar
Shucai Xiao committed
424
425
426
427
        if(!result.empty())
        {
            call(&migraphx_program_parameter_shapes_names, result.data(), this->get_handle_ptr());
        }
Paul Fultz II's avatar
Paul Fultz II committed
428
429
430
431
        return result;
    }
};

432
/// A class to construct the inputs parameters for a program
Paul Fultz II's avatar
Paul Fultz II committed
433
434
435
436
437
438
struct program_parameters : MIGRAPHX_HANDLE_BASE(program_parameters)
{
    program_parameters(migraphx_program_parameters* p, own) { this->set_handle(p, own{}); }

    program_parameters(migraphx_program_parameters* p, borrow) { this->set_handle(p, borrow{}); }

Shucai Xiao's avatar
Shucai Xiao committed
439
440
    program_parameters(migraphx_program_parameters* p) { this->set_handle(p, borrow{}); }

Paul Fultz II's avatar
Paul Fultz II committed
441
442
    program_parameters() { this->make_handle(&migraphx_program_parameters_create); }

443
    /// Construct the parameters from initializer_list
444
445
446
447
448
449
450
    program_parameters(std::initializer_list<std::pair<std::string, argument>> l)
    {
        this->make_handle(&migraphx_program_parameters_create);
        for(auto&& p : l)
            this->add(p.first.c_str(), p.second);
    }

451
    /// Add a new parameter
Paul Fultz II's avatar
Paul Fultz II committed
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
    void add(const char* pname, const argument& pargument) const
    {
        call(&migraphx_program_parameters_add,
             this->get_handle_ptr(),
             pname,
             pargument.get_handle_ptr());
    }
};

struct arguments : MIGRAPHX_HANDLE_BASE(arguments), array_base<arguments>
{
    arguments(migraphx_arguments* p, own) { this->set_handle(p, own{}); }

    arguments(migraphx_arguments* p, borrow) { this->set_handle(p, borrow{}); }

    size_t size() const
    {
        size_t pout;
        call(&migraphx_arguments_size, &pout, this->get_handle_ptr());
        return pout;
    }

    argument operator[](size_t pidx) const
    {
        const_migraphx_argument_t pout;
        call(&migraphx_arguments_get, &pout, this->get_handle_ptr(), pidx);
478
        return {pout};
Paul Fultz II's avatar
Paul Fultz II committed
479
480
481
482
483
484
485
486
487
    }

    struct iterator_read
    {
        migraphx_arguments* self;
        argument operator()(size_t pidx) const
        {
            const_migraphx_argument_t pout;
            call(&migraphx_arguments_get, &pout, self, pidx);
Shucai Xiao's avatar
Shucai Xiao committed
488

489
            return {pout};
Paul Fultz II's avatar
Paul Fultz II committed
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
        }
    };
};

struct shapes : MIGRAPHX_HANDLE_BASE(shapes), array_base<shapes>
{
    shapes(migraphx_shapes* p, own) { this->set_handle(p, own{}); }

    shapes(migraphx_shapes* p, borrow) { this->set_handle(p, borrow{}); }

    size_t size() const
    {
        size_t pout;
        call(&migraphx_shapes_size, &pout, this->get_handle_ptr());
        return pout;
    }

    shape operator[](size_t pidx) const
    {
        const_migraphx_shape_t pout;
        call(&migraphx_shapes_get, &pout, this->get_handle_ptr(), pidx);
511
        return {pout};
Paul Fultz II's avatar
Paul Fultz II committed
512
513
514
515
516
517
518
519
520
    }

    struct iterator_read
    {
        migraphx_shapes* self;
        shape operator()(size_t pidx) const
        {
            const_migraphx_shape_t pout;
            call(&migraphx_shapes_get, &pout, self, pidx);
521
            return {pout};
Paul Fultz II's avatar
Paul Fultz II committed
522
523
524
525
        }
    };
};

Shucai Xiao's avatar
Shucai Xiao committed
526
527
528
529
530
531
532
533
struct module
{
    migraphx_module_t mm;
    module(const migraphx_module_t& m) : mm(m) {}

    void print() const { call(&migraphx_module_print, mm); }
};

kahmed10's avatar
kahmed10 committed
534
535
536
537
538
539
540
struct context
{
    migraphx_context_t ctx;

    void finish() const { call(&migraphx_context_finish, ctx); }
};

541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
struct compile_options : MIGRAPHX_HANDLE_BASE(compile_options)
{
    compile_options() { this->make_handle(&migraphx_compile_options_create); }

    compile_options(migraphx_compile_options* p, own) { this->set_handle(p, own()); }

    /// For targets with offloaded memory(such as the gpu), this will insert
    /// instructions during compilation to copy the input parameters to the
    /// offloaded memory and to copy the final result from the offloaded
    /// memory back to main memory.
    void set_offload_copy(bool value = true)
    {
        call(&migraphx_compile_options_set_offload_copy, this->get_handle_ptr(), value);
    }

    /// Optimize math functions to use faster approximate versions. There may
    /// be slight accuracy degredation when enabled.
    void set_fast_math(bool value = true)
    {
        call(&migraphx_compile_options_set_fast_math, this->get_handle_ptr(), value);
    }
};

564
/// A program represents the all computation graphs to be compiled and executed
Paul Fultz II's avatar
Paul Fultz II committed
565
566
567
568
569
570
571
572
struct program : MIGRAPHX_HANDLE_BASE(program)
{
    program() {}

    program(migraphx_program* p, own) { this->set_handle(p, own{}); }

    program(migraphx_program* p, borrow) { this->set_handle(p, borrow{}); }

573
    /// Compile the program for a specific target to be ran on
574
    void compile(const target& ptarget, const compile_options& poptions) const
Paul Fultz II's avatar
Paul Fultz II committed
575
    {
576
577
578
579
        call(&migraphx_program_compile,
             this->get_handle_ptr(),
             ptarget.get_handle_ptr(),
             poptions.get_handle_ptr());
Paul Fultz II's avatar
Paul Fultz II committed
580
581
    }

582
    /// Compile the program for a specific target to be ran on
Paul Fultz II's avatar
Paul Fultz II committed
583
584
    void compile(const target& ptarget) const
    {
585
586
587
588
        call(&migraphx_program_compile,
             this->get_handle_ptr(),
             ptarget.get_handle_ptr(),
             migraphx::compile_options{}.get_handle_ptr());
Paul Fultz II's avatar
Paul Fultz II committed
589
590
    }

591
    /// Return the shapes for the input parameters
Paul Fultz II's avatar
Paul Fultz II committed
592
593
594
595
596
597
598
    program_parameter_shapes get_parameter_shapes() const
    {
        migraphx_program_parameter_shapes_t pout;
        call(&migraphx_program_get_parameter_shapes, &pout, this->get_handle_ptr());
        return program_parameter_shapes(pout, own{});
    }

599
    /// Get the shapes of all the outputs returned by this program
Paul Fultz II's avatar
Paul Fultz II committed
600
601
602
603
604
605
606
    shapes get_output_shapes() const
    {
        migraphx_shapes_t pout;
        call(&migraphx_program_get_output_shapes, &pout, this->get_handle_ptr());
        return shapes(pout, own{});
    }

607
    /// Run the program using the inputs passed in
Paul Fultz II's avatar
Paul Fultz II committed
608
609
610
611
612
613
614
615
616
    arguments eval(const program_parameters& pparams) const
    {
        migraphx_arguments_t pout;
        call(&migraphx_program_run, &pout, this->get_handle_ptr(), pparams.get_handle_ptr());
        return arguments(pout, own{});
    }

    void print() const { call(&migraphx_program_print, this->get_handle_ptr()); }

617
618
619
620
621
622
    program sort()
    {
        call(&migraphx_program_sort, this->get_handle_ptr());
        return *this;
    }

Paul Fultz II's avatar
Paul Fultz II committed
623
624
625
626
627
628
629
    friend bool operator==(const program& px, const program& py)
    {
        bool pout;
        call(&migraphx_program_equal, &pout, px.get_handle_ptr(), py.get_handle_ptr());
        return pout;
    }

Shucai Xiao's avatar
Shucai Xiao committed
630
631
632
633
634
635
636
    module get_main_module()
    {
        migraphx_module_t p_modu;
        call(&migraphx_program_get_main_module, &p_modu, this->get_handle_ptr());
        return module{p_modu};
    }

kahmed10's avatar
kahmed10 committed
637
638
639
640
641
642
643
    context get_context()
    {
        migraphx_context_t ctx;
        call(&migraphx_program_get_context, &ctx, this->get_handle_ptr());
        return context{ctx};
    }

Paul Fultz II's avatar
Paul Fultz II committed
644
645
646
    friend bool operator!=(const program& px, const program& py) { return !(px == py); }
};

647
648
649
650
651
652
struct operation : MIGRAPHX_HANDLE_BASE(operation)
{
    operation(migraphx_operation* p, own) { this->set_handle(p, own{}); }

    operation(migraphx_operation* p, borrow) { this->set_handle(p, borrow{}); }

653
654
    template <class... Ts>
    operation(const char* name, const char* attributes = nullptr, Ts... xs)
655
    {
656
        this->make_handle(&migraphx_operation_create, name, attributes, xs...);
657
658
659
660
661
662
    }

    std::string name()
    {
        std::array<char, 1024> out_name;
        call(&migraphx_operation_name, out_name.data(), 1024, this->get_handle_ptr());
663
        return {out_name.data()};
664
665
666
    }
};

667
668
669
670
671
672
673
674
675
676
677
678
679
680
// options for migraphx file format options
struct file_options : MIGRAPHX_HANDLE_BASE(file_options)
{
    file_options() { this->make_handle(&migraphx_file_options_create); }

    file_options(migraphx_file_options* p, own) { this->set_handle(p, own()); }

    // set file format
    void set_file_format(const char* format)
    {
        call(&migraphx_file_options_set_file_format, this->get_handle_ptr(), format);
    }
};

681
/// Load a saved migraphx program from a file
682
inline program load(const char* filename, const file_options& options)
683
{
684
685
    return program(make<migraphx_program>(&migraphx_load, filename, options.get_handle_ptr()),
                   own{});
686
687
}

688
/// Load a saved migraphx program from a file
689
690
inline program load(const char* filename)
{
691
692
693
    return program(
        make<migraphx_program>(&migraphx_load, filename, migraphx::file_options{}.get_handle_ptr()),
        own{});
694
695
}

696
/// Save a program to a file
697
inline void save(const program& p, const char* filename, const file_options& options)
698
{
699
    call(&migraphx_save, p.get_handle_ptr(), filename, options.get_handle_ptr());
700
701
}

702
/// Save a program to a file
703
704
inline void save(const program& p, const char* filename)
{
705
    call(&migraphx_save, p.get_handle_ptr(), filename, migraphx::file_options{}.get_handle_ptr());
706
707
}

708
/// Options for parsing onnx options
709
struct onnx_options : MIGRAPHX_HANDLE_BASE(onnx_options)
Paul Fultz II's avatar
Paul Fultz II committed
710
{
711
712
713
714
    onnx_options() { this->make_handle(&migraphx_onnx_options_create); }

    onnx_options(migraphx_onnx_options* p, own) { this->set_handle(p, own{}); }

715
    /// Make onnx parser treat an inputs with a certain dimensions
716
717
718
719
720
721
722
723
724
    void set_input_parameter_shape(const std::string& name, std::vector<std::size_t> dim)
    {
        call(&migraphx_onnx_options_set_input_parameter_shape,
             this->get_handle_ptr(),
             name.c_str(),
             dim.data(),
             dim.size());
    }

kahmed10's avatar
kahmed10 committed
725
    /// When there is a dimension parameter, then use this default value
726
727
728
729
    void set_default_dim_value(unsigned int value)
    {
        call(&migraphx_onnx_options_set_default_dim_value, this->get_handle_ptr(), value);
    }
Shucai Xiao's avatar
Shucai Xiao committed
730
731
732
733
734
735

    /// Set default max iteration number for the loop operator
    void set_default_loop_iterations(int64_t value)
    {
        call(&migraphx_onnx_options_set_default_loop_iterations, this->get_handle_ptr(), value);
    }
736
737
};

738
/// Parse an onnx file into a migraphx program
739
740
741
742
inline program parse_onnx(const char* filename, const migraphx::onnx_options& options)
{
    return program(make<migraphx_program>(&migraphx_parse_onnx, filename, options.get_handle_ptr()),
                   own{});
Paul Fultz II's avatar
Paul Fultz II committed
743
744
}

745
/// Parse an onnx file into a migraphx program
Paul Fultz II's avatar
Paul Fultz II committed
746
747
inline program parse_onnx(const char* filename)
{
748
749
750
    migraphx::onnx_options options;
    return program(make<migraphx_program>(&migraphx_parse_onnx, filename, options.get_handle_ptr()),
                   own{});
Paul Fultz II's avatar
Paul Fultz II committed
751
752
}

753
/// Parse a buffer of memory as an onnx file
754
755
inline program
parse_onnx_buffer(const void* data, size_t size, const migraphx::onnx_options& options)
Paul Fultz II's avatar
Paul Fultz II committed
756
{
757
758
759
    return program(
        make<migraphx_program>(&migraphx_parse_onnx_buffer, data, size, options.get_handle_ptr()),
        own{});
Paul Fultz II's avatar
Paul Fultz II committed
760
761
}

762
/// Parse a buffer of memory as an onnx file
Paul Fultz II's avatar
Paul Fultz II committed
763
764
inline program parse_onnx_buffer(const void* data, size_t size)
{
765
766
767
768
    migraphx::onnx_options options;
    return program(
        make<migraphx_program>(&migraphx_parse_onnx_buffer, data, size, options.get_handle_ptr()),
        own{});
Paul Fultz II's avatar
Paul Fultz II committed
769
770
}

771
/// Parse a buffer of memory as an onnx file
772
inline program parse_onnx_buffer(const std::string& buffer, const migraphx::onnx_options& options)
Paul Fultz II's avatar
Paul Fultz II committed
773
774
{
    return program(
775
776
        make<migraphx_program>(
            &migraphx_parse_onnx_buffer, buffer.data(), buffer.size(), options.get_handle_ptr()),
Paul Fultz II's avatar
Paul Fultz II committed
777
778
779
        own{});
}

780
/// Parse a buffer of memory as an onnx file
Paul Fultz II's avatar
Paul Fultz II committed
781
782
inline program parse_onnx_buffer(const std::string& buffer)
{
783
    migraphx::onnx_options options;
Paul Fultz II's avatar
Paul Fultz II committed
784
    return program(
785
786
        make<migraphx_program>(
            &migraphx_parse_onnx_buffer, buffer.data(), buffer.size(), options.get_handle_ptr()),
Paul Fultz II's avatar
Paul Fultz II committed
787
788
789
        own{});
}

kahmed10's avatar
kahmed10 committed
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
/// Options for parsing tf options
struct tf_options : MIGRAPHX_HANDLE_BASE(tf_options)
{
    tf_options() { this->make_handle(&migraphx_tf_options_create); }

    tf_options(migraphx_tf_options* p, own) { this->set_handle(p, own{}); }

    /// Make tf parser treat an inputs with a certain dimensions
    void set_input_parameter_shape(const std::string& name, std::vector<std::size_t> dim)
    {
        call(&migraphx_tf_options_set_input_parameter_shape,
             this->get_handle_ptr(),
             name.c_str(),
             dim.data(),
             dim.size());
    }

    /// Change data layout to NHWC (default is NCHW)
    void set_nhwc(bool is_nhwc = true)
    {
        call(&migraphx_tf_options_set_nhwc, this->get_handle_ptr(), is_nhwc);
    }

    /// When there is a dimension parameter, then use this default value
    void set_default_dim_value(unsigned int value)
    {
        call(&migraphx_tf_options_set_default_dim_value, this->get_handle_ptr(), value);
    }

    /// Set output node names to return specific outputs from graph
    void set_output_names(std::vector<const char*> names)
    {
        call(&migraphx_tf_options_set_output_names,
             this->get_handle_ptr(),
             names.data(),
             names.size());
    }
};

/// Parse a tf file into a migraphx program
inline program parse_tf(const char* filename, const migraphx::tf_options& options)
{
    return program(make<migraphx_program>(&migraphx_parse_tf, filename, options.get_handle_ptr()),
                   own{});
}

/// Parse a tf file into a migraphx program
inline program parse_tf(const char* filename)
{
    migraphx::tf_options options;
    return program(make<migraphx_program>(&migraphx_parse_tf, filename, options.get_handle_ptr()),
                   own{});
}

Shucai Xiao's avatar
Shucai Xiao committed
844
845
846
847
848
849
850
851
852
853
854
855
struct quantize_op_names : MIGRAPHX_HANDLE_BASE(quantize_op_names)
{
    quantize_op_names() { this->make_handle(&migraphx_quantize_op_names_create); }

    quantize_op_names(migraphx_quantize_op_names* p, own) { this->set_handle(p, own{}); }

    void add(const std::string& name)
    {
        call(&migraphx_quantize_op_names_add, this->get_handle_ptr(), name.c_str());
    }
};

856
/// Quantize program to use fp16
Shucai Xiao's avatar
Shucai Xiao committed
857
858
859
860
861
inline void quantize_fp16(const program& prog, const quantize_op_names& names)
{
    call(&migraphx_quantize_fp16_with_op_names, prog.get_handle_ptr(), names.get_handle_ptr());
}

862
/// Quantize program to use fp16
Shucai Xiao's avatar
Shucai Xiao committed
863
864
865
866
867
inline void quantize_fp16(const program& prog)
{
    call(&migraphx_quantize_fp16, prog.get_handle_ptr());
}

868
/// Options to be passed when quantizing for int8
Shucai Xiao's avatar
Shucai Xiao committed
869
870
871
872
873
874
875
876
877
878
879
struct quantize_int8_options : MIGRAPHX_HANDLE_BASE(quantize_int8_options)
{
    quantize_int8_options() { this->make_handle(&migraphx_quantize_int8_options_create); }

    quantize_int8_options(migraphx_quantize_int8_options* p, own) { this->set_handle(p, own{}); }

    quantize_int8_options(migraphx_quantize_int8_options* p, borrow)
    {
        this->set_handle(p, borrow{});
    }

880
    /// Add an operator that should be quantized
Shucai Xiao's avatar
Shucai Xiao committed
881
882
883
884
885
    void add_op_name(const std::string& name)
    {
        call(&migraphx_quantize_int8_options_add_op_name, this->get_handle_ptr(), name.c_str());
    }

886
    /// Add calibrartion data to be used for quantizing
Shucai Xiao's avatar
Shucai Xiao committed
887
888
889
890
891
892
893
894
    void add_calibration_data(const program_parameters& pp)
    {
        call(&migraphx_quantize_int8_options_add_calibration_data,
             this->get_handle_ptr(),
             pp.get_handle_ptr());
    }
};

895
/// Quantize program to use int8
Shucai Xiao's avatar
Shucai Xiao committed
896
897
898
899
900
901
902
903
904
inline void
quantize_int8(const program& prog, const target& ptarget, const quantize_int8_options& options)
{
    call(&migraphx_quantize_int8,
         prog.get_handle_ptr(),
         ptarget.get_handle_ptr(),
         options.get_handle_ptr());
}

905
#ifndef DOXYGEN
Paul Fultz II's avatar
Paul Fultz II committed
906
} // namespace api
907
#endif
Paul Fultz II's avatar
Paul Fultz II committed
908
909
910
} // namespace migraphx

#endif