api.cpp 42.1 KB
Newer Older
Paul Fultz II's avatar
Paul Fultz II committed
1
2
3
4
5
#include <migraphx/migraphx.h>
#include <migraphx/rank.hpp>
#include <migraphx/shape.hpp>
#include <migraphx/program.hpp>
#include <migraphx/onnx.hpp>
kahmed10's avatar
kahmed10 committed
6
#include <migraphx/tf.hpp>
7
#include <migraphx/register_target.hpp>
Paul Fultz II's avatar
Paul Fultz II committed
8
#include <migraphx/generate.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
9
#include <migraphx/quantization.hpp>
10
#include <migraphx/ref/target.hpp>
11
#include <migraphx/load_save.hpp>
12
13
14
15
#include <migraphx/make_op.hpp>
#include <migraphx/json.hpp>
#include <migraphx/convert_to_json.hpp>
#include <algorithm>
16
#include <cstdarg>
Paul Fultz II's avatar
Paul Fultz II committed
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

namespace migraphx {

template <class F>
migraphx_status try_(F f, bool output = true) // NOLINT
{
    try
    {
        f();
    }
    catch(const migraphx::exception& ex)
    {
        if(output)
            std::cerr << "MIGraphX Error: " << ex.what() << std::endl;
        if(ex.error > 0)
            return migraphx_status(ex.error);
        else
            return migraphx_status_unknown_error;
    }
    catch(const std::exception& ex)
    {
        if(output)
            std::cerr << "MIGraphX Error: " << ex.what() << std::endl;
        return migraphx_status_unknown_error;
    }
    catch(...)
    {
        return migraphx_status_unknown_error;
    }
    return migraphx_status_success;
}

shape::type_t to_shape_type(migraphx_shape_datatype_t t)
{
    switch(t)
    {
Paul Fultz II's avatar
Paul Fultz II committed
53
    case migraphx_shape_tuple_type: return shape::tuple_type;
Paul Fultz II's avatar
Paul Fultz II committed
54
55
56
57
58
59
60
61
62
63
64
65
#define MIGRAPHX_DETAIL_SHAPE_CASE_CONVERT(x, y) \
    case migraphx_shape_##x: return shape::x;
        MIGRAPHX_SHAPE_VISIT_TYPES(MIGRAPHX_DETAIL_SHAPE_CASE_CONVERT)
#undef MIGRAPHX_DETAIL_SHAPE_CASE_CONVERT
    }
    MIGRAPHX_THROW(migraphx_status_bad_param, "Unknown type");
}

migraphx_shape_datatype_t to_shape_type(shape::type_t t)
{
    switch(t)
    {
Paul Fultz II's avatar
Paul Fultz II committed
66
    case shape::tuple_type: return migraphx_shape_tuple_type;
Paul Fultz II's avatar
Paul Fultz II committed
67
68
69
70
71
72
73
74
#define MIGRAPHX_DETAIL_SHAPE_CASE_CONVERT(x, y) \
    case shape::x: return migraphx_shape_##x;
        MIGRAPHX_SHAPE_VISIT_TYPES(MIGRAPHX_DETAIL_SHAPE_CASE_CONVERT)
#undef MIGRAPHX_DETAIL_SHAPE_CASE_CONVERT
    }
    MIGRAPHX_THROW(migraphx_status_bad_param, "Unknown type");
}

75
target get_target(const std::string& name) { return make_target(name); }
Paul Fultz II's avatar
Paul Fultz II committed
76

77
78
79
void set_offload_copy(compile_options& options, bool value) { options.offload_copy = value; }

void set_fast_math(compile_options& options, bool value) { options.fast_math = value; }
Paul Fultz II's avatar
Paul Fultz II committed
80

81
void set_file_format(file_options& options, const char* format) { options.format = format; }
82

83
void set_default_dim_value(onnx_options& options, size_t value)
Paul Fultz II's avatar
Paul Fultz II committed
84
{
85
86
87
    options.default_dim_value = value;
}

Shucai Xiao's avatar
Shucai Xiao committed
88
89
90
91
92
void set_default_loop_iterations(onnx_options& options, int64_t value)
{
    options.max_loop_iterations = value;
}

kahmed10's avatar
kahmed10 committed
93
94
95
96
void set_nhwc(tf_options& options, bool is_nhwc) { options.is_nhwc = is_nhwc; }

void set_default_dim_value(tf_options& options, size_t value) { options.batch_size = value; }

Shucai Xiao's avatar
Shucai Xiao committed
97
void set_input_parameter_shape(onnx_options& options, const char* name, std::vector<int> dims)
98
{
99
    options.map_input_dims[std::string(name)] = std::move(dims);
Paul Fultz II's avatar
Paul Fultz II committed
100
101
}

Shucai Xiao's avatar
Shucai Xiao committed
102
void set_input_parameter_shape(tf_options& options, const char* name, std::vector<int> dims)
kahmed10's avatar
kahmed10 committed
103
104
105
106
107
108
109
110
111
{
    options.map_input_dims[std::string(name)] = std::move(dims);
}

void set_output_names(tf_options& options, std::vector<const char*> names)
{
    options.output_node_names = std::vector<std::string>(names.begin(), names.end());
}

Paul Fultz II's avatar
Paul Fultz II committed
112
113
114
115
116
117
118
119
120
template <class Value>
std::vector<const char*> get_names(const std::unordered_map<std::string, Value>& m)
{
    std::vector<const char*> result;
    std::transform(
        m.begin(), m.end(), std::back_inserter(result), [](auto&& p) { return p.first.c_str(); });
    return result;
}

Shucai Xiao's avatar
Shucai Xiao committed
121
122
123
124
125
126
127
128
129
130
131
132
void quantize_fp16_with_op_names(program& prog, std::vector<std::string>& names)
{
    if(names.empty())
    {
        names = {"all"};
    }

    migraphx::quantize_fp16(prog, names);
}

struct quantize_int8_options
{
133
134
    std::vector<parameter_map> calibration = {};
    std::vector<std::string> op_names      = {};
Shucai Xiao's avatar
Shucai Xiao committed
135
136
137
138
139
140
141
};

void add_op_name(quantize_int8_options& options, const char* name)
{
    options.op_names.push_back(name);
}

142
void add_calibration_data(quantize_int8_options& options, parameter_map& data)
Shucai Xiao's avatar
Shucai Xiao committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
{
    options.calibration.push_back(data);
}

void quantize_int8_wrap(program& prog, const target& t, quantize_int8_options& options)
{
    if(options.op_names.empty())
    {
        options.op_names = {"dot", "convolution"};
    }

    migraphx::quantize_int8(prog, t, options.calibration, options.op_names);
}

157
158
159
160
161
162
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wformat-nonliteral"
#endif

operation create_op(const char* name, const char* attributes, va_list vlist)
163
{
164
165
166
    std::string sattributes = attributes == nullptr ? "" : attributes;
    std::vector<char> buffer(sattributes.size() * 2);
    std::vsnprintf(buffer.data(), buffer.size(), sattributes.c_str(), vlist);
167
168
169
    value v = value::object{};
    if(attributes != nullptr)
    {
170
        v = from_json_string(convert_to_json(std::string(buffer.data())));
171
172
173
174
175
176
    }
    auto op = make_op(name, v);

    return op;
}

177
178
179
180
#ifdef __clang__
#pragma clang diagnostic pop
#endif

Paul Fultz II's avatar
Paul Fultz II committed
181
182
183
184
185
186
template <class T>
bool equal(const T& x, const T& y)
{
    return x == y;
}

187
std::vector<argument> run(program& p, const parameter_map& params) { return p.eval(params); }
Paul Fultz II's avatar
Paul Fultz II committed
188

189
std::vector<shape> get_output_shapes(program& p) { return p.get_output_shapes(); }
Paul Fultz II's avatar
Paul Fultz II committed
190

Shucai Xiao's avatar
Shucai Xiao committed
191
192
193
void print_program(const program& p) { std::cout << p << std::endl; }

void print_module(const module& m) { std::cout << m << std::endl; }
Paul Fultz II's avatar
Paul Fultz II committed
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289

} // namespace migraphx

template <class T, class U, class Target = std::remove_pointer_t<T>>
Target* object_cast(U* x)
{
    return reinterpret_cast<Target*>(x);
}
template <class T, class U, class Target = std::remove_pointer_t<T>>
const Target* object_cast(const U* x)
{
    return reinterpret_cast<const Target*>(x);
}

template <class T, class... Ts, class Target = std::remove_pointer_t<T>>
Target* allocate(Ts&&... xs)
{
    return new Target(std::forward<Ts>(xs)...); // NOLINT
}

template <class T>
void destroy(T* x)
{
    delete x; // NOLINT
}

extern "C" struct migraphx_shape;
struct migraphx_shape
{
    template <class... Ts>
    migraphx_shape(Ts&&... xs) : object(std::forward<Ts>(xs)...)
    {
    }
    migraphx::shape object;
};

extern "C" struct migraphx_argument;
struct migraphx_argument
{
    template <class... Ts>
    migraphx_argument(Ts&&... xs) : object(std::forward<Ts>(xs)...)
    {
    }
    migraphx::argument object;
};

extern "C" struct migraphx_target;
struct migraphx_target
{
    template <class... Ts>
    migraphx_target(Ts&&... xs) : object(std::forward<Ts>(xs)...)
    {
    }
    migraphx::target object;
};

extern "C" struct migraphx_program_parameter_shapes;
struct migraphx_program_parameter_shapes
{
    template <class... Ts>
    migraphx_program_parameter_shapes(Ts&&... xs) : object(std::forward<Ts>(xs)...)
    {
    }
    std::unordered_map<std::string, migraphx::shape> object;
};

extern "C" struct migraphx_program_parameters;
struct migraphx_program_parameters
{
    template <class... Ts>
    migraphx_program_parameters(Ts&&... xs) : object(std::forward<Ts>(xs)...)
    {
    }
    std::unordered_map<std::string, migraphx::argument> object;
};

extern "C" struct migraphx_arguments;
struct migraphx_arguments
{
    template <class... Ts>
    migraphx_arguments(Ts&&... xs) : object(std::forward<Ts>(xs)...)
    {
    }
    std::vector<migraphx::argument> object;
};

extern "C" struct migraphx_shapes;
struct migraphx_shapes
{
    template <class... Ts>
    migraphx_shapes(Ts&&... xs) : object(std::forward<Ts>(xs)...)
    {
    }
    std::vector<migraphx::shape> object;
};

Shucai Xiao's avatar
Shucai Xiao committed
290
291
292
293
294
295
296
297
298
299
extern "C" struct migraphx_module;
struct migraphx_module
{
    template <class... Ts>
    migraphx_module(Ts&&... xs) : object(std::forward<Ts>(xs)...)
    {
    }
    migraphx::module object;
};

Paul Fultz II's avatar
Paul Fultz II committed
300
301
302
303
304
305
306
307
308
309
extern "C" struct migraphx_program;
struct migraphx_program
{
    template <class... Ts>
    migraphx_program(Ts&&... xs) : object(std::forward<Ts>(xs)...)
    {
    }
    migraphx::program object;
};

310
311
312
313
314
315
316
317
318
319
extern "C" struct migraphx_operation;
struct migraphx_operation
{
    template <class... Ts>
    migraphx_operation(Ts&&... xs) : object(std::forward<Ts>(xs)...)
    {
    }
    migraphx::operation object;
};

320
321
322
323
324
325
326
327
328
329
extern "C" struct migraphx_onnx_options;
struct migraphx_onnx_options
{
    template <class... Ts>
    migraphx_onnx_options(Ts&&... xs) : object(std::forward<Ts>(xs)...)
    {
    }
    migraphx::onnx_options object;
};

330
331
332
333
334
335
336
337
338
339
extern "C" struct migraphx_file_options;
struct migraphx_file_options
{
    template <class... Ts>
    migraphx_file_options(Ts&&... xs) : object(std::forward<Ts>(xs)...)
    {
    }
    migraphx::file_options object;
};

340
341
342
343
344
345
346
347
348
349
extern "C" struct migraphx_compile_options;
struct migraphx_compile_options
{
    template <class... Ts>
    migraphx_compile_options(Ts&&... xs) : object(std::forward<Ts>(xs)...)
    {
    }
    migraphx::compile_options object;
};

kahmed10's avatar
kahmed10 committed
350
351
352
353
354
355
356
357
358
359
extern "C" struct migraphx_tf_options;
struct migraphx_tf_options
{
    template <class... Ts>
    migraphx_tf_options(Ts&&... xs) : object(std::forward<Ts>(xs)...)
    {
    }
    migraphx::tf_options object;
};

Shucai Xiao's avatar
Shucai Xiao committed
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
extern "C" struct migraphx_quantize_op_names;
struct migraphx_quantize_op_names
{
    template <class... Ts>
    migraphx_quantize_op_names(Ts&&... xs) : object(std::forward<Ts>(xs)...)
    {
    }
    std::vector<std::string> object;
};

extern "C" struct migraphx_quantize_int8_options;
struct migraphx_quantize_int8_options
{
    template <class... Ts>
    migraphx_quantize_int8_options(Ts&&... xs) : object(std::forward<Ts>(xs)...)
    {
    }
    migraphx::quantize_int8_options object;
};

Paul Fultz II's avatar
Paul Fultz II committed
380
381
extern "C" migraphx_status migraphx_shape_destroy(migraphx_shape_t shape)
{
382
383
    auto api_error_result = migraphx::try_([&] { destroy((shape)); });
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
384
385
386
387
}

extern "C" migraphx_status migraphx_shape_create(migraphx_shape_t* shape,
                                                 migraphx_shape_datatype_t type,
388
389
                                                 int* lengths,
                                                 size_t lengths_size)
Paul Fultz II's avatar
Paul Fultz II committed
390
{
391
    auto api_error_result = migraphx::try_([&] {
392
        if(lengths == nullptr and lengths_size != 0)
Paul Fultz II's avatar
Paul Fultz II committed
393
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter lengths: Null pointer");
Shucai Xiao's avatar
Shucai Xiao committed
394
395
        *shape = object_cast<migraphx_shape_t>(allocate<migraphx::shape>(
            (migraphx::to_shape_type(type)), (std::vector<int>(lengths, lengths + lengths_size))));
Paul Fultz II's avatar
Paul Fultz II committed
396
    });
397
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
398
399
}

400
401
extern "C" migraphx_status migraphx_shape_create_with_strides(migraphx_shape_t* shape,
                                                              migraphx_shape_datatype_t type,
402
403
404
405
                                                              int* lengths,
                                                              size_t lengths_size,
                                                              int* strides,
                                                              size_t strides_size)
406
{
407
    auto api_error_result = migraphx::try_([&] {
408
409
410
411
412
413
        if(lengths == nullptr and lengths_size != 0)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter lengths: Null pointer");
        if(strides == nullptr and strides_size != 0)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter strides: Null pointer");
        *shape = object_cast<migraphx_shape_t>(
            allocate<migraphx::shape>((migraphx::to_shape_type(type)),
414
415
                                      (std::vector<int>(lengths, lengths + lengths_size)),
                                      (std::vector<int>(strides, strides + strides_size))));
416
    });
417
    return api_error_result;
418
419
}

420
421
422
extern "C" migraphx_status migraphx_shape_create_scalar(migraphx_shape_t* shape,
                                                        migraphx_shape_datatype_t type)
{
423
    auto api_error_result = migraphx::try_([&] {
424
425
426
        *shape = object_cast<migraphx_shape_t>(
            allocate<migraphx::shape>((migraphx::to_shape_type(type))));
    });
427
    return api_error_result;
428
429
}

Paul Fultz II's avatar
Paul Fultz II committed
430
extern "C" migraphx_status
Shucai Xiao's avatar
Shucai Xiao committed
431
migraphx_shape_lengths(const int** out, int* out_size, const_migraphx_shape_t shape)
Paul Fultz II's avatar
Paul Fultz II committed
432
{
433
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
434
435
436
437
438
439
440
441
        if(out == nullptr or out_size == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter out: Null pointer");
        if(shape == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter shape: Null pointer");
        auto&& api_result = (shape->object).lens();
        *out              = api_result.data();
        *out_size         = api_result.size();
    });
442
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
443
444
445
}

extern "C" migraphx_status
Shucai Xiao's avatar
Shucai Xiao committed
446
migraphx_shape_strides(const int** out, int* out_size, const_migraphx_shape_t shape)
Paul Fultz II's avatar
Paul Fultz II committed
447
{
448
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
449
450
451
452
453
454
455
456
        if(out == nullptr or out_size == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter out: Null pointer");
        if(shape == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter shape: Null pointer");
        auto&& api_result = (shape->object).strides();
        *out              = api_result.data();
        *out_size         = api_result.size();
    });
457
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
458
459
460
461
462
}

extern "C" migraphx_status migraphx_shape_type(migraphx_shape_datatype_t* out,
                                               const_migraphx_shape_t shape)
{
463
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
464
465
466
467
468
469
        if(out == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter out: Null pointer");
        if(shape == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter shape: Null pointer");
        *out = migraphx::to_shape_type((shape->object).type());
    });
470
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
471
472
473
474
}

extern "C" migraphx_status migraphx_shape_bytes(size_t* out, const_migraphx_shape_t shape)
{
475
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
476
477
478
479
        if(shape == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter shape: Null pointer");
        *out = (shape->object).bytes();
    });
480
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
481
482
483
484
485
}

extern "C" migraphx_status
migraphx_shape_equal(bool* out, const_migraphx_shape_t shape, const_migraphx_shape_t x)
{
486
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
487
488
489
490
491
492
        if(shape == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter shape: Null pointer");
        if(x == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter x: Null pointer");
        *out = migraphx::equal((shape->object), (x->object));
    });
493
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
494
495
496
497
}

extern "C" migraphx_status migraphx_argument_destroy(migraphx_argument_t argument)
{
498
499
    auto api_error_result = migraphx::try_([&] { destroy((argument)); });
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
500
501
502
503
504
}

extern "C" migraphx_status
migraphx_argument_create(migraphx_argument_t* argument, const_migraphx_shape_t shape, void* buffer)
{
505
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
506
507
508
509
510
        if(shape == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter shape: Null pointer");
        *argument = object_cast<migraphx_argument_t>(
            allocate<migraphx::argument>((shape->object), (buffer)));
    });
511
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
512
513
514
515
516
}

extern "C" migraphx_status migraphx_argument_shape(const_migraphx_shape_t* out,
                                                   const_migraphx_argument_t argument)
{
517
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
518
519
520
521
        if(argument == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter argument: Null pointer");
        *out = object_cast<const_migraphx_shape_t>(&((argument->object).get_shape()));
    });
522
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
523
524
525
526
}

extern "C" migraphx_status migraphx_argument_buffer(char** out, const_migraphx_argument_t argument)
{
527
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
528
529
530
531
        if(argument == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter argument: Null pointer");
        *out = (argument->object).data();
    });
532
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
533
534
535
536
537
}

extern "C" migraphx_status
migraphx_argument_equal(bool* out, const_migraphx_argument_t argument, const_migraphx_argument_t x)
{
538
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
539
540
541
542
543
544
        if(argument == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter argument: Null pointer");
        if(x == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter x: Null pointer");
        *out = migraphx::equal((argument->object), (x->object));
    });
545
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
546
547
548
549
550
}

extern "C" migraphx_status
migraphx_argument_generate(migraphx_argument_t* out, const_migraphx_shape_t s, size_t seed)
{
551
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
552
553
554
555
        if(s == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter s: Null pointer");
        *out = allocate<migraphx_argument_t>(migraphx::generate_argument((s->object), (seed)));
    });
556
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
557
558
559
560
}

extern "C" migraphx_status migraphx_target_destroy(migraphx_target_t target)
{
561
562
    auto api_error_result = migraphx::try_([&] { destroy((target)); });
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
563
564
565
566
}

extern "C" migraphx_status migraphx_target_create(migraphx_target_t* target, const char* name)
{
567
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
568
569
570
        *target = object_cast<migraphx_target_t>(
            allocate<migraphx::target>(migraphx::get_target((name))));
    });
571
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
572
573
574
575
576
}

extern "C" migraphx_status migraphx_program_parameter_shapes_destroy(
    migraphx_program_parameter_shapes_t program_parameter_shapes)
{
577
578
    auto api_error_result = migraphx::try_([&] { destroy((program_parameter_shapes)); });
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
579
580
581
582
583
584
}

extern "C" migraphx_status
migraphx_program_parameter_shapes_size(size_t* out,
                                       migraphx_program_parameter_shapes_t program_parameter_shapes)
{
585
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
586
587
588
589
590
        if(program_parameter_shapes == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param,
                           "Bad parameter program_parameter_shapes: Null pointer");
        *out = (program_parameter_shapes->object).size();
    });
591
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
592
593
594
595
596
597
598
}

extern "C" migraphx_status
migraphx_program_parameter_shapes_get(const_migraphx_shape_t* out,
                                      migraphx_program_parameter_shapes_t program_parameter_shapes,
                                      const char* name)
{
599
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
600
601
602
603
604
605
        if(program_parameter_shapes == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param,
                           "Bad parameter program_parameter_shapes: Null pointer");
        *out =
            object_cast<const_migraphx_shape_t>(&((program_parameter_shapes->object).at((name))));
    });
606
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
607
608
609
610
611
}

extern "C" migraphx_status migraphx_program_parameter_shapes_names(
    const char** out, migraphx_program_parameter_shapes_t program_parameter_shapes)
{
612
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
613
614
615
616
617
618
619
620
        if(out == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter out: Null pointer");
        if(program_parameter_shapes == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param,
                           "Bad parameter program_parameter_shapes: Null pointer");
        auto&& api_result = migraphx::get_names((program_parameter_shapes->object));
        std::copy(api_result.begin(), api_result.end(), out);
    });
621
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
622
623
624
625
626
}

extern "C" migraphx_status
migraphx_program_parameters_destroy(migraphx_program_parameters_t program_parameters)
{
627
628
    auto api_error_result = migraphx::try_([&] { destroy((program_parameters)); });
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
629
630
631
632
633
}

extern "C" migraphx_status
migraphx_program_parameters_create(migraphx_program_parameters_t* program_parameters)
{
634
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
635
636
637
        *program_parameters = object_cast<migraphx_program_parameters_t>(
            allocate<std::unordered_map<std::string, migraphx::argument>>());
    });
638
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
639
640
641
642
643
644
645
}

extern "C" migraphx_status
migraphx_program_parameters_add(migraphx_program_parameters_t program_parameters,
                                const char* name,
                                const_migraphx_argument_t argument)
{
646
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
647
648
649
650
651
652
653
        if(program_parameters == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param,
                           "Bad parameter program_parameters: Null pointer");
        if(argument == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter argument: Null pointer");
        (program_parameters->object)[(name)] = (argument->object);
    });
654
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
655
656
657
658
}

extern "C" migraphx_status migraphx_arguments_destroy(migraphx_arguments_t arguments)
{
659
660
    auto api_error_result = migraphx::try_([&] { destroy((arguments)); });
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
661
662
663
664
}

extern "C" migraphx_status migraphx_arguments_size(size_t* out, migraphx_arguments_t arguments)
{
665
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
666
667
668
669
        if(arguments == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter arguments: Null pointer");
        *out = (arguments->object).size();
    });
670
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
671
672
673
674
675
}

extern "C" migraphx_status
migraphx_arguments_get(const_migraphx_argument_t* out, migraphx_arguments_t arguments, size_t idx)
{
676
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
677
678
679
680
        if(arguments == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter arguments: Null pointer");
        *out = object_cast<const_migraphx_argument_t>(&((arguments->object).at((idx))));
    });
681
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
682
683
684
685
}

extern "C" migraphx_status migraphx_shapes_destroy(migraphx_shapes_t shapes)
{
686
687
    auto api_error_result = migraphx::try_([&] { destroy((shapes)); });
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
688
689
690
691
}

extern "C" migraphx_status migraphx_shapes_size(size_t* out, migraphx_shapes_t shapes)
{
692
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
693
694
695
696
        if(shapes == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter shapes: Null pointer");
        *out = (shapes->object).size();
    });
697
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
698
699
700
701
702
}

extern "C" migraphx_status
migraphx_shapes_get(const_migraphx_shape_t* out, migraphx_shapes_t shapes, size_t idx)
{
703
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
704
705
706
707
        if(shapes == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter shapes: Null pointer");
        *out = object_cast<const_migraphx_shape_t>(&((shapes->object).at((idx))));
    });
708
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
709
710
}

Shucai Xiao's avatar
Shucai Xiao committed
711
712
extern "C" migraphx_status migraphx_module_print(const_migraphx_module_t module)
{
713
    auto api_error_result = migraphx::try_([&] {
Shucai Xiao's avatar
Shucai Xiao committed
714
715
716
717
        if(module == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter module: Null pointer");
        migraphx::print_module((module->object));
    });
718
    return api_error_result;
Shucai Xiao's avatar
Shucai Xiao committed
719
720
}

Paul Fultz II's avatar
Paul Fultz II committed
721
722
extern "C" migraphx_status migraphx_program_destroy(migraphx_program_t program)
{
723
724
    auto api_error_result = migraphx::try_([&] { destroy((program)); });
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
725
726
}

Shucai Xiao's avatar
Shucai Xiao committed
727
728
729
extern "C" migraphx_status migraphx_program_get_main_module(migraphx_module_t* out,
                                                            migraphx_program_t program)
{
730
    auto api_error_result = migraphx::try_([&] {
Shucai Xiao's avatar
Shucai Xiao committed
731
732
733
734
        if(program == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter program: Null pointer");
        *out = object_cast<migraphx_module_t>((program->object).get_main_module());
    });
735
    return api_error_result;
Shucai Xiao's avatar
Shucai Xiao committed
736
737
}

Paul Fultz II's avatar
Paul Fultz II committed
738
739
extern "C" migraphx_status migraphx_program_compile(migraphx_program_t program,
                                                    migraphx_target_t target,
740
                                                    migraphx_compile_options_t options)
Paul Fultz II's avatar
Paul Fultz II committed
741
{
742
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
743
744
745
746
        if(program == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter program: Null pointer");
        if(target == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter target: Null pointer");
747
748
749
        if(options == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter options: Null pointer");
        (program->object).compile((target->object), (options->object));
Paul Fultz II's avatar
Paul Fultz II committed
750
    });
751
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
752
753
754
755
756
757
}

extern "C" migraphx_status
migraphx_program_get_parameter_shapes(migraphx_program_parameter_shapes_t* out,
                                      migraphx_program_t program)
{
758
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
759
760
761
762
763
        if(program == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter program: Null pointer");
        *out =
            allocate<migraphx_program_parameter_shapes_t>((program->object).get_parameter_shapes());
    });
764
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
765
766
767
768
769
}

extern "C" migraphx_status migraphx_program_get_output_shapes(migraphx_shapes_t* out,
                                                              migraphx_program_t program)
{
770
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
771
772
773
774
        if(program == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter program: Null pointer");
        *out = allocate<migraphx_shapes_t>(migraphx::get_output_shapes((program->object)));
    });
775
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
776
777
778
779
}

extern "C" migraphx_status migraphx_program_print(const_migraphx_program_t program)
{
780
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
781
782
        if(program == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter program: Null pointer");
Shucai Xiao's avatar
Shucai Xiao committed
783
        migraphx::print_program((program->object));
Paul Fultz II's avatar
Paul Fultz II committed
784
    });
785
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
786
787
}

788
789
extern "C" migraphx_status migraphx_program_sort(migraphx_program_t program)
{
790
    auto api_error_result = migraphx::try_([&] {
791
792
793
794
        if(program == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter program: Null pointer");
        (program->object).sort();
    });
795
    return api_error_result;
796
797
}

Paul Fultz II's avatar
Paul Fultz II committed
798
799
800
801
extern "C" migraphx_status migraphx_program_run(migraphx_arguments_t* out,
                                                migraphx_program_t program,
                                                migraphx_program_parameters_t params)
{
802
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
803
804
805
806
807
808
        if(program == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter program: Null pointer");
        if(params == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter params: Null pointer");
        *out = allocate<migraphx_arguments_t>(migraphx::run((program->object), (params->object)));
    });
809
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
810
811
812
813
814
}

extern "C" migraphx_status
migraphx_program_equal(bool* out, const_migraphx_program_t program, const_migraphx_program_t x)
{
815
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
816
817
818
819
820
821
        if(program == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter program: Null pointer");
        if(x == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter x: Null pointer");
        *out = migraphx::equal((program->object), (x->object));
    });
822
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
823
824
}

825
826
extern "C" migraphx_status migraphx_operation_destroy(migraphx_operation_t operation)
{
827
828
    auto api_error_result = migraphx::try_([&] { destroy((operation)); });
    return api_error_result;
829
830
}

831
832
833
834
extern "C" migraphx_status migraphx_operation_create(migraphx_operation_t* operation,
                                                     const char* name,
                                                     const char* attributes,
                                                     ...)
835
{
836
837
838
    va_list vlist;
    va_start(vlist, attributes);
    auto api_error_result = migraphx::try_([&] {
839
        *operation = object_cast<migraphx_operation_t>(
840
            allocate<migraphx::operation>(migraphx::create_op((name), (attributes), (vlist))));
841
    });
842
843
    va_end(vlist);
    return api_error_result;
844
845
846
}

extern "C" migraphx_status
Shucai Xiao's avatar
Shucai Xiao committed
847
migraphx_operation_name(char* out, int out_size, migraphx_operation_t operation)
848
{
849
    auto api_error_result = migraphx::try_([&] {
850
851
852
853
854
        if(out == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter out: Null pointer");
        if(operation == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter operation: Null pointer");
        auto&& api_result = (operation->object).name();
Shucai Xiao's avatar
Shucai Xiao committed
855
856
857
        auto* it          = std::copy_n(
            api_result.begin(), std::min(static_cast<int>(api_result.size()), out_size - 1), out);
        *it = '\0';
858
    });
859
    return api_error_result;
860
861
}

862
extern "C" migraphx_status
863
migraphx_load(migraphx_program_t* out, const char* name, migraphx_file_options_t options)
864
{
865
    auto api_error_result = migraphx::try_([&] {
866
867
868
        if(options == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter options: Null pointer");
        *out = allocate<migraphx_program_t>(migraphx::load((name), (options->object)));
869
    });
870
    return api_error_result;
871
872
873
}

extern "C" migraphx_status
874
migraphx_save(migraphx_program_t p, const char* name, migraphx_file_options_t options)
875
{
876
    auto api_error_result = migraphx::try_([&] {
877
878
        if(p == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter p: Null pointer");
879
880
881
        if(options == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter options: Null pointer");
        migraphx::save((p->object), (name), (options->object));
882
    });
883
    return api_error_result;
884
885
}

886
887
extern "C" migraphx_status migraphx_onnx_options_destroy(migraphx_onnx_options_t onnx_options)
{
888
889
    auto api_error_result = migraphx::try_([&] { destroy((onnx_options)); });
    return api_error_result;
890
891
892
893
}

extern "C" migraphx_status migraphx_onnx_options_create(migraphx_onnx_options_t* onnx_options)
{
894
    auto api_error_result = migraphx::try_([&] {
895
896
        *onnx_options = object_cast<migraphx_onnx_options_t>(allocate<migraphx::onnx_options>());
    });
897
    return api_error_result;
898
899
}

900
extern "C" migraphx_status migraphx_onnx_options_set_input_parameter_shape(
901
    migraphx_onnx_options_t onnx_options, const char* name, int* dims, size_t dims_size)
902
{
903
    auto api_error_result = migraphx::try_([&] {
904
905
        if(onnx_options == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter onnx_options: Null pointer");
906
907
908
        if(dims == nullptr and dims_size != 0)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter dims: Null pointer");
        migraphx::set_input_parameter_shape(
Shucai Xiao's avatar
Shucai Xiao committed
909
            (onnx_options->object), (name), (std::vector<int>(dims, dims + dims_size)));
910
    });
911
    return api_error_result;
912
913
914
915
916
}

extern "C" migraphx_status
migraphx_onnx_options_set_default_dim_value(migraphx_onnx_options_t onnx_options, size_t value)
{
917
    auto api_error_result = migraphx::try_([&] {
918
919
920
921
        if(onnx_options == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter onnx_options: Null pointer");
        migraphx::set_default_dim_value((onnx_options->object), (value));
    });
922
    return api_error_result;
923
924
}

925
926
927
928
extern "C" migraphx_status
migraphx_onnx_options_set_default_loop_iterations(migraphx_onnx_options_t onnx_options,
                                                  int64_t value)
{
929
    auto api_error_result = migraphx::try_([&] {
930
931
932
933
        if(onnx_options == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter onnx_options: Null pointer");
        migraphx::set_default_loop_iterations((onnx_options->object), (value));
    });
934
    return api_error_result;
935
936
}

937
938
extern "C" migraphx_status migraphx_file_options_destroy(migraphx_file_options_t file_options)
{
939
940
    auto api_error_result = migraphx::try_([&] { destroy((file_options)); });
    return api_error_result;
941
942
943
944
}

extern "C" migraphx_status migraphx_file_options_create(migraphx_file_options_t* file_options)
{
945
    auto api_error_result = migraphx::try_([&] {
946
947
        *file_options = object_cast<migraphx_file_options_t>(allocate<migraphx::file_options>());
    });
948
    return api_error_result;
949
950
951
952
953
}

extern "C" migraphx_status
migraphx_file_options_set_file_format(migraphx_file_options_t file_options, const char* format)
{
954
    auto api_error_result = migraphx::try_([&] {
955
956
957
958
        if(file_options == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter file_options: Null pointer");
        migraphx::set_file_format((file_options->object), (format));
    });
959
    return api_error_result;
960
961
}

Shucai Xiao's avatar
Shucai Xiao committed
962
extern "C" migraphx_status
963
964
migraphx_compile_options_destroy(migraphx_compile_options_t compile_options)
{
965
966
    auto api_error_result = migraphx::try_([&] { destroy((compile_options)); });
    return api_error_result;
967
968
969
970
}

extern "C" migraphx_status
migraphx_compile_options_create(migraphx_compile_options_t* compile_options)
Shucai Xiao's avatar
Shucai Xiao committed
971
{
972
    auto api_error_result = migraphx::try_([&] {
973
974
975
        *compile_options =
            object_cast<migraphx_compile_options_t>(allocate<migraphx::compile_options>());
    });
976
    return api_error_result;
977
978
979
980
981
}

extern "C" migraphx_status
migraphx_compile_options_set_offload_copy(migraphx_compile_options_t compile_options, bool value)
{
982
    auto api_error_result = migraphx::try_([&] {
983
984
985
986
987
        if(compile_options == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param,
                           "Bad parameter compile_options: Null pointer");
        migraphx::set_offload_copy((compile_options->object), (value));
    });
988
    return api_error_result;
989
990
991
992
993
}

extern "C" migraphx_status
migraphx_compile_options_set_fast_math(migraphx_compile_options_t compile_options, bool value)
{
994
    auto api_error_result = migraphx::try_([&] {
995
996
997
998
        if(compile_options == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param,
                           "Bad parameter compile_options: Null pointer");
        migraphx::set_fast_math((compile_options->object), (value));
Shucai Xiao's avatar
Shucai Xiao committed
999
    });
1000
    return api_error_result;
Shucai Xiao's avatar
Shucai Xiao committed
1001
1002
}

Paul Fultz II's avatar
Paul Fultz II committed
1003
extern "C" migraphx_status
1004
migraphx_parse_onnx(migraphx_program_t* out, const char* name, migraphx_onnx_options_t options)
Paul Fultz II's avatar
Paul Fultz II committed
1005
{
1006
    auto api_error_result = migraphx::try_([&] {
1007
1008
1009
        if(options == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter options: Null pointer");
        *out = allocate<migraphx_program_t>(migraphx::parse_onnx((name), (options->object)));
Paul Fultz II's avatar
Paul Fultz II committed
1010
    });
1011
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1012
1013
1014
1015
1016
}

extern "C" migraphx_status migraphx_parse_onnx_buffer(migraphx_program_t* out,
                                                      const void* data,
                                                      size_t size,
1017
                                                      migraphx_onnx_options_t options)
Paul Fultz II's avatar
Paul Fultz II committed
1018
{
1019
    auto api_error_result = migraphx::try_([&] {
1020
1021
1022
1023
        if(options == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter options: Null pointer");
        *out = allocate<migraphx_program_t>(
            migraphx::parse_onnx_buffer((data), (size), (options->object)));
Paul Fultz II's avatar
Paul Fultz II committed
1024
    });
1025
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1026
}
Shucai Xiao's avatar
Shucai Xiao committed
1027

kahmed10's avatar
kahmed10 committed
1028
1029
extern "C" migraphx_status migraphx_tf_options_destroy(migraphx_tf_options_t tf_options)
{
1030
1031
    auto api_error_result = migraphx::try_([&] { destroy((tf_options)); });
    return api_error_result;
kahmed10's avatar
kahmed10 committed
1032
1033
1034
1035
}

extern "C" migraphx_status migraphx_tf_options_create(migraphx_tf_options_t* tf_options)
{
1036
    auto api_error_result = migraphx::try_([&] {
kahmed10's avatar
kahmed10 committed
1037
1038
        *tf_options = object_cast<migraphx_tf_options_t>(allocate<migraphx::tf_options>());
    });
1039
    return api_error_result;
kahmed10's avatar
kahmed10 committed
1040
1041
1042
1043
1044
}

extern "C" migraphx_status migraphx_tf_options_set_nhwc(migraphx_tf_options_t tf_options,
                                                        bool is_nhwc)
{
1045
    auto api_error_result = migraphx::try_([&] {
kahmed10's avatar
kahmed10 committed
1046
1047
1048
1049
        if(tf_options == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter tf_options: Null pointer");
        migraphx::set_nhwc((tf_options->object), (is_nhwc));
    });
1050
    return api_error_result;
kahmed10's avatar
kahmed10 committed
1051
1052
1053
}

extern "C" migraphx_status migraphx_tf_options_set_input_parameter_shape(
Shucai Xiao's avatar
Shucai Xiao committed
1054
    migraphx_tf_options_t tf_options, const char* name, int* dims, int dims_size)
kahmed10's avatar
kahmed10 committed
1055
{
1056
    auto api_error_result = migraphx::try_([&] {
kahmed10's avatar
kahmed10 committed
1057
1058
1059
1060
1061
        if(tf_options == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter tf_options: Null pointer");
        if(dims == nullptr and dims_size != 0)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter dims: Null pointer");
        migraphx::set_input_parameter_shape(
Shucai Xiao's avatar
Shucai Xiao committed
1062
            (tf_options->object), (name), (std::vector<int>(dims, dims + dims_size)));
kahmed10's avatar
kahmed10 committed
1063
    });
1064
    return api_error_result;
kahmed10's avatar
kahmed10 committed
1065
1066
1067
1068
1069
}

extern "C" migraphx_status
migraphx_tf_options_set_default_dim_value(migraphx_tf_options_t tf_options, size_t value)
{
1070
    auto api_error_result = migraphx::try_([&] {
kahmed10's avatar
kahmed10 committed
1071
1072
1073
1074
        if(tf_options == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter tf_options: Null pointer");
        migraphx::set_default_dim_value((tf_options->object), (value));
    });
1075
    return api_error_result;
kahmed10's avatar
kahmed10 committed
1076
1077
1078
1079
}

extern "C" migraphx_status migraphx_tf_options_set_output_names(migraphx_tf_options_t tf_options,
                                                                const char** names,
Shucai Xiao's avatar
Shucai Xiao committed
1080
                                                                int names_size)
kahmed10's avatar
kahmed10 committed
1081
{
1082
    auto api_error_result = migraphx::try_([&] {
kahmed10's avatar
kahmed10 committed
1083
1084
1085
1086
1087
1088
1089
        if(tf_options == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter tf_options: Null pointer");
        if(names == nullptr and names_size != 0)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter names: Null pointer");
        migraphx::set_output_names((tf_options->object),
                                   (std::vector<const char*>(names, names + names_size)));
    });
1090
    return api_error_result;
kahmed10's avatar
kahmed10 committed
1091
1092
1093
1094
1095
}

extern "C" migraphx_status
migraphx_parse_tf(migraphx_program_t* out, const char* name, migraphx_tf_options_t options)
{
1096
    auto api_error_result = migraphx::try_([&] {
kahmed10's avatar
kahmed10 committed
1097
1098
1099
1100
        if(options == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter options: Null pointer");
        *out = allocate<migraphx_program_t>(migraphx::parse_tf((name), (options->object)));
    });
1101
    return api_error_result;
kahmed10's avatar
kahmed10 committed
1102
1103
}

Shucai Xiao's avatar
Shucai Xiao committed
1104
1105
1106
extern "C" migraphx_status
migraphx_quantize_op_names_destroy(migraphx_quantize_op_names_t quantize_op_names)
{
1107
1108
    auto api_error_result = migraphx::try_([&] { destroy((quantize_op_names)); });
    return api_error_result;
Shucai Xiao's avatar
Shucai Xiao committed
1109
1110
1111
1112
1113
}

extern "C" migraphx_status
migraphx_quantize_op_names_create(migraphx_quantize_op_names_t* quantize_op_names)
{
1114
    auto api_error_result = migraphx::try_([&] {
Shucai Xiao's avatar
Shucai Xiao committed
1115
1116
1117
        *quantize_op_names =
            object_cast<migraphx_quantize_op_names_t>(allocate<std::vector<std::string>>());
    });
1118
    return api_error_result;
Shucai Xiao's avatar
Shucai Xiao committed
1119
1120
1121
1122
1123
}

extern "C" migraphx_status
migraphx_quantize_op_names_add(migraphx_quantize_op_names_t quantize_op_names, const char* name)
{
1124
    auto api_error_result = migraphx::try_([&] {
Shucai Xiao's avatar
Shucai Xiao committed
1125
1126
1127
1128
1129
        if(quantize_op_names == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param,
                           "Bad parameter quantize_op_names: Null pointer");
        (quantize_op_names->object).push_back((name));
    });
1130
    return api_error_result;
Shucai Xiao's avatar
Shucai Xiao committed
1131
1132
1133
1134
1135
}

extern "C" migraphx_status migraphx_quantize_fp16_with_op_names(migraphx_program_t prog,
                                                                migraphx_quantize_op_names_t name)
{
1136
    auto api_error_result = migraphx::try_([&] {
Shucai Xiao's avatar
Shucai Xiao committed
1137
1138
1139
1140
1141
1142
        if(prog == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter prog: Null pointer");
        if(name == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter name: Null pointer");
        migraphx::quantize_fp16_with_op_names((prog->object), (name->object));
    });
1143
    return api_error_result;
Shucai Xiao's avatar
Shucai Xiao committed
1144
1145
1146
1147
}

extern "C" migraphx_status migraphx_quantize_fp16(migraphx_program_t prog)
{
1148
    auto api_error_result = migraphx::try_([&] {
Shucai Xiao's avatar
Shucai Xiao committed
1149
1150
1151
1152
        if(prog == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter prog: Null pointer");
        migraphx::quantize_fp16((prog->object));
    });
1153
    return api_error_result;
Shucai Xiao's avatar
Shucai Xiao committed
1154
1155
1156
1157
1158
}

extern "C" migraphx_status
migraphx_quantize_int8_options_destroy(migraphx_quantize_int8_options_t quantize_int8_options)
{
1159
1160
    auto api_error_result = migraphx::try_([&] { destroy((quantize_int8_options)); });
    return api_error_result;
Shucai Xiao's avatar
Shucai Xiao committed
1161
1162
1163
1164
1165
}

extern "C" migraphx_status
migraphx_quantize_int8_options_create(migraphx_quantize_int8_options_t* quantize_int8_options)
{
1166
    auto api_error_result = migraphx::try_([&] {
Shucai Xiao's avatar
Shucai Xiao committed
1167
1168
1169
        *quantize_int8_options = object_cast<migraphx_quantize_int8_options_t>(
            allocate<migraphx::quantize_int8_options>());
    });
1170
    return api_error_result;
Shucai Xiao's avatar
Shucai Xiao committed
1171
1172
1173
1174
1175
1176
}

extern "C" migraphx_status
migraphx_quantize_int8_options_add_op_name(migraphx_quantize_int8_options_t quantize_int8_options,
                                           const char* name)
{
1177
    auto api_error_result = migraphx::try_([&] {
Shucai Xiao's avatar
Shucai Xiao committed
1178
1179
1180
1181
1182
        if(quantize_int8_options == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param,
                           "Bad parameter quantize_int8_options: Null pointer");
        migraphx::add_op_name((quantize_int8_options->object), (name));
    });
1183
    return api_error_result;
Shucai Xiao's avatar
Shucai Xiao committed
1184
1185
1186
1187
1188
}

extern "C" migraphx_status migraphx_quantize_int8_options_add_calibration_data(
    migraphx_quantize_int8_options_t quantize_int8_options, migraphx_program_parameters_t data)
{
1189
    auto api_error_result = migraphx::try_([&] {
Shucai Xiao's avatar
Shucai Xiao committed
1190
1191
1192
1193
1194
1195
1196
        if(quantize_int8_options == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param,
                           "Bad parameter quantize_int8_options: Null pointer");
        if(data == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter data: Null pointer");
        migraphx::add_calibration_data((quantize_int8_options->object), (data->object));
    });
1197
    return api_error_result;
Shucai Xiao's avatar
Shucai Xiao committed
1198
1199
1200
1201
1202
1203
}

extern "C" migraphx_status migraphx_quantize_int8(migraphx_program_t prog,
                                                  migraphx_target_t target,
                                                  migraphx_quantize_int8_options_t options)
{
1204
    auto api_error_result = migraphx::try_([&] {
Shucai Xiao's avatar
Shucai Xiao committed
1205
1206
1207
1208
1209
1210
1211
1212
        if(prog == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter prog: Null pointer");
        if(target == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter target: Null pointer");
        if(options == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter options: Null pointer");
        migraphx::quantize_int8_wrap((prog->object), (target->object), (options->object));
    });
1213
    return api_error_result;
Shucai Xiao's avatar
Shucai Xiao committed
1214
}