api.cpp 84.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
24
#include <migraphx/execution_environment.hpp>
Paul Fultz II's avatar
Paul Fultz II committed
25
26
#include <migraphx/migraphx.h>
#include <migraphx/rank.hpp>
27
#include <migraphx/ranges.hpp>
Paul Fultz II's avatar
Paul Fultz II committed
28
29
30
#include <migraphx/shape.hpp>
#include <migraphx/program.hpp>
#include <migraphx/onnx.hpp>
kahmed10's avatar
kahmed10 committed
31
#include <migraphx/tf.hpp>
32
#include <migraphx/instruction_ref.hpp>
33
#include <migraphx/register_target.hpp>
Paul Fultz II's avatar
Paul Fultz II committed
34
#include <migraphx/generate.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
35
#include <migraphx/quantization.hpp>
36
#include <migraphx/load_save.hpp>
37
#include <migraphx/make_op.hpp>
38
#include <migraphx/register_op.hpp>
39
40
#include <migraphx/json.hpp>
#include <migraphx/convert_to_json.hpp>
41
#include <array>
42
#include <algorithm>
43
#include <cstdarg>
Paul Fultz II's avatar
Paul Fultz II committed
44
45
namespace migraphx {

46
47
static thread_local bool disable_exception_catch = false; // NOLINT

48
extern "C" MIGRAPHX_C_EXPORT void migraphx_test_private_disable_exception_catch(bool b)
49
50
51
52
{
    disable_exception_catch = b;
}

Paul Fultz II's avatar
Paul Fultz II committed
53
54
55
template <class F>
migraphx_status try_(F f, bool output = true) // NOLINT
{
56
    if(disable_exception_catch)
Paul Fultz II's avatar
Paul Fultz II committed
57
58
59
    {
        f();
    }
60
    else
Paul Fultz II's avatar
Paul Fultz II committed
61
    {
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
        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;
Paul Fultz II's avatar
Paul Fultz II committed
79
            return migraphx_status_unknown_error;
80
81
82
83
84
        }
        catch(...)
        {
            return migraphx_status_unknown_error;
        }
Paul Fultz II's avatar
Paul Fultz II committed
85
86
87
88
89
90
91
92
    }
    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
93
    case migraphx_shape_tuple_type: return shape::tuple_type;
Paul Fultz II's avatar
Paul Fultz II committed
94
95
96
97
98
99
100
101
102
103
104
105
#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
106
    case shape::tuple_type: return migraphx_shape_tuple_type;
Paul Fultz II's avatar
Paul Fultz II committed
107
108
109
110
111
112
113
114
#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");
}

115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
template <class T>
auto to_obj_vector(const T* x, std::size_t n)
{
    std::vector<decltype((*x)->object)> result;
    std::transform(x, x + n, std::back_inserter(result), [&](auto&& y) { return y->object; });
    return result;
}

template <class T, class U>
auto to_objptr_vector(const U* x, std::size_t n)
{
    std::vector<T> result;
    std::transform(
        x, x + n, std::back_inserter(result), [&](auto&& y) { return std::addressof(y->object); });
    return result;
}

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

134
135
136
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
137

138
139
140
141
142
void set_exhaustive_tune_flag(compile_options& options, bool value)
{
    options.exhaustive_tune = value;
}

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

145
void set_default_dim_value(onnx_options& options, size_t value)
Paul Fultz II's avatar
Paul Fultz II committed
146
{
147
148
149
    options.default_dim_value = value;
}

150
151
152
153
154
void set_default_dyn_dim_value(onnx_options& options, const shape::dynamic_dimension& dd)
{
    options.default_dyn_dim_value = dd;
}

Shucai Xiao's avatar
Shucai Xiao committed
155
156
157
158
159
void set_default_loop_iterations(onnx_options& options, int64_t value)
{
    options.max_loop_iterations = value;
}

kahmed10's avatar
kahmed10 committed
160
161
162
163
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; }

164
165
void set_input_parameter_shape(onnx_options& options,
                               const char* name,
166
                               std::vector<std::size_t> dims)
167
{
168
    options.map_input_dims[std::string(name)] = std::move(dims);
Paul Fultz II's avatar
Paul Fultz II committed
169
170
}

171
172
173
174
175
176
177
void set_dyn_input_parameter_shape(onnx_options& options,
                                   const char* name,
                                   std::vector<shape::dynamic_dimension> dyn_dims)
{
    options.map_dyn_input_dims[std::string(name)] = std::move(dyn_dims);
}

kahmed10's avatar
kahmed10 committed
178
179
180
181
182
183
184
185
186
187
void set_input_parameter_shape(tf_options& options, const char* name, std::vector<std::size_t> dims)
{
    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());
}

188
189
190
191
192
193
194
std::vector<argument>
run_async(program& p, const parameter_map& params, void* s, std::string_view name)
{
    execution_environment exec_env{any_ptr(s, name), true};
    return p.eval(params, exec_env);
}

Paul Fultz II's avatar
Paul Fultz II committed
195
196
197
198
199
200
201
202
203
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;
}

204
205
206
207
208
209
template <class T>
std::set<T> make_set(const T* x, std::size_t n)
{
    return {x, x + n};
}

Shucai Xiao's avatar
Shucai Xiao committed
210
211
212
213
214
215
216
217
218
219
220
221
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
{
222
223
    std::vector<parameter_map> calibration = {};
    std::vector<std::string> op_names      = {};
Shucai Xiao's avatar
Shucai Xiao committed
224
225
226
227
228
229
230
};

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

231
void add_calibration_data(quantize_int8_options& options, parameter_map& data)
Shucai Xiao's avatar
Shucai Xiao committed
232
233
234
235
236
237
238
239
240
241
242
243
244
245
{
    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);
}

246
247
248
249
250
251
#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)
252
{
253
254
255
    std::string sattributes = attributes == nullptr ? "" : attributes;
    std::vector<char> buffer(sattributes.size() * 2);
    std::vsnprintf(buffer.data(), buffer.size(), sattributes.c_str(), vlist);
256
257
258
    value v = value::object{};
    if(attributes != nullptr)
    {
259
        v = from_json_string(convert_to_json(std::string(buffer.data())));
260
261
262
263
264
265
    }
    auto op = make_op(name, v);

    return op;
}

266
267
268
269
#ifdef __clang__
#pragma clang diagnostic pop
#endif

Paul Fultz II's avatar
Paul Fultz II committed
270
271
272
273
274
275
template <class T>
bool equal(const T& x, const T& y)
{
    return x == y;
}

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

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

Shucai Xiao's avatar
Shucai Xiao committed
280
281
282
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
283

284
285
286
287
288
migraphx::instruction_ref add_allocation(module& m, const migraphx::shape& s)
{
    return m.add_instruction(migraphx::make_op("allocate", {{"shape", migraphx::to_value(s)}}), {});
}

289
290
291
292
293
294
295
296
297
298
299
struct experimental_custom_op
{
    std::string name;
    experimental_custom_op() = default;

    experimental_custom_op(std::string pname) : name(std::move(pname)) {}
};

template <class CustomOp>
struct custom_operation
{
300

301
302
303
304
305
    template <class Self, class F>
    static auto reflect(Self&, F)
    {
        return pack();
    }
306
307
308
309
310
311

    value attributes() const
    {
        return {{"custom_op", true}, {"target", op.runs_on_offload_target() ? "gpu" : "cpu"}};
    }

312
313
314
315
316
317
318
319
    CustomOp op;
    std::string name() const { return op.xobject.name; }

    shape compute_shape(std::vector<shape> inputs) const
    {
        return op.compute_shape(std::move(inputs));
    }

320
321
322
323
324
325
    // TODO: Compute method with module_args
    argument
    compute(migraphx::context ctx, migraphx::shape output_shape, std::vector<argument> inputs) const
    {
        return op.compute(std::move(ctx), std::move(output_shape), std::move(inputs));
    }
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342

    std::ptrdiff_t output_alias(std::vector<shape> inputs) const
    {
        auto alias_vec = op.output_alias(std::move(inputs));
        // TODO: For now, only support one output alias
        if(alias_vec.empty())
        {
            return -1;
        }
        if(alias_vec.size() > 1)
        {
            MIGRAPHX_THROW("Currently, CustomOps in MIGraphX only supports one output_alias");
        }
        return alias_vec.front();
    }

    bool runs_on_offload_target() const { return op.runs_on_offload_target(); }
343
344
345
346
347
348
349
350
};

template <class CustomOp>
void register_custom_op(const CustomOp& op)
{
    register_op(custom_operation<CustomOp>{op});
}

kahmed10's avatar
kahmed10 committed
351
352
migraphx::context get_context(const program& p) { return p.get_context(); }

Paul Fultz II's avatar
Paul Fultz II committed
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
} // 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)
{
369
370
371
372
    if constexpr(std::is_aggregate<Target>{})
        return new Target{std::forward<Ts>(xs)...}; // NOLINT
    else
        return new Target(std::forward<Ts>(xs)...); // NOLINT
Paul Fultz II's avatar
Paul Fultz II committed
373
374
375
376
377
378
379
}

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

381
382
383
384
385
386
387
388
// TODO: Move to interface preamble
template <class C, class D>
struct manage_generic_ptr
{
    manage_generic_ptr() = default;

    manage_generic_ptr(std::nullptr_t) {}

389
390
    manage_generic_ptr(void* pdata, const char* obj_tname, C pcopier, D pdeleter)
        : data(nullptr), obj_typename(obj_tname), copier(pcopier), deleter(pdeleter)
391
392
393
394
395
    {
        copier(&data, pdata);
    }

    manage_generic_ptr(const manage_generic_ptr& rhs)
396
        : data(nullptr), obj_typename(rhs.obj_typename), copier(rhs.copier), deleter(rhs.deleter)
397
398
399
400
401
402
    {
        if(copier)
            copier(&data, rhs.data);
    }

    manage_generic_ptr(manage_generic_ptr&& other) noexcept
403
404
405
406
        : data(other.data),
          obj_typename(other.obj_typename),
          copier(other.copier),
          deleter(other.deleter)
407
    {
408
409
410
411
        other.data         = nullptr;
        other.obj_typename = "";
        other.copier       = nullptr;
        other.deleter      = nullptr;
412
413
414
415
416
    }

    manage_generic_ptr& operator=(manage_generic_ptr rhs)
    {
        std::swap(data, rhs.data);
417
        std::swap(obj_typename, rhs.obj_typename);
418
419
420
421
422
423
424
425
426
427
428
        std::swap(copier, rhs.copier);
        std::swap(deleter, rhs.deleter);
        return *this;
    }

    ~manage_generic_ptr()
    {
        if(data != nullptr)
            deleter(data);
    }

429
430
431
432
    void* data               = nullptr;
    const char* obj_typename = "";
    C copier                 = nullptr;
    D deleter                = nullptr;
433
};
Paul Fultz II's avatar
Paul Fultz II committed
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
extern "C" struct migraphx_optimals;
struct migraphx_optimals
{
    template <class... Ts>
    migraphx_optimals(Ts&&... xs)
        : object(std::forward<Ts>(xs)...) // NOLINT(readability-redundant-member-init)
    {
    }
    std::set<size_t> object;
};

extern "C" struct migraphx_dynamic_dimension;
struct migraphx_dynamic_dimension
{
    template <class... Ts>
    migraphx_dynamic_dimension(Ts&&... xs)
        : object(std::forward<Ts>(xs)...) // NOLINT(readability-redundant-member-init)
    {
    }
    migraphx::shape::dynamic_dimension object;
};

extern "C" struct migraphx_dynamic_dimensions;
struct migraphx_dynamic_dimensions
{
    template <class... Ts>
    migraphx_dynamic_dimensions(Ts&&... xs)
        : object(std::forward<Ts>(xs)...) // NOLINT(readability-redundant-member-init)
    {
    }
    std::vector<migraphx::shape::dynamic_dimension> object;
};

Paul Fultz II's avatar
Paul Fultz II committed
468
469
470
471
extern "C" struct migraphx_shape;
struct migraphx_shape
{
    template <class... Ts>
472
473
    migraphx_shape(Ts&&... xs)
        : object(std::forward<Ts>(xs)...) // NOLINT(readability-redundant-member-init)
Paul Fultz II's avatar
Paul Fultz II committed
474
475
476
477
478
479
480
481
482
    {
    }
    migraphx::shape object;
};

extern "C" struct migraphx_argument;
struct migraphx_argument
{
    template <class... Ts>
483
484
    migraphx_argument(Ts&&... xs)
        : object(std::forward<Ts>(xs)...) // NOLINT(readability-redundant-member-init)
Paul Fultz II's avatar
Paul Fultz II committed
485
486
487
488
489
490
491
492
493
    {
    }
    migraphx::argument object;
};

extern "C" struct migraphx_target;
struct migraphx_target
{
    template <class... Ts>
494
495
    migraphx_target(Ts&&... xs)
        : object(std::forward<Ts>(xs)...) // NOLINT(readability-redundant-member-init)
Paul Fultz II's avatar
Paul Fultz II committed
496
497
498
499
500
501
502
503
504
    {
    }
    migraphx::target object;
};

extern "C" struct migraphx_program_parameter_shapes;
struct migraphx_program_parameter_shapes
{
    template <class... Ts>
505
506
    migraphx_program_parameter_shapes(Ts&&... xs)
        : object(std::forward<Ts>(xs)...) // NOLINT(readability-redundant-member-init)
Paul Fultz II's avatar
Paul Fultz II committed
507
508
509
510
511
512
513
514
515
    {
    }
    std::unordered_map<std::string, migraphx::shape> object;
};

extern "C" struct migraphx_program_parameters;
struct migraphx_program_parameters
{
    template <class... Ts>
516
517
    migraphx_program_parameters(Ts&&... xs)
        : object(std::forward<Ts>(xs)...) // NOLINT(readability-redundant-member-init)
Paul Fultz II's avatar
Paul Fultz II committed
518
519
520
521
522
523
524
525
526
    {
    }
    std::unordered_map<std::string, migraphx::argument> object;
};

extern "C" struct migraphx_arguments;
struct migraphx_arguments
{
    template <class... Ts>
527
528
    migraphx_arguments(Ts&&... xs)
        : object(std::forward<Ts>(xs)...) // NOLINT(readability-redundant-member-init)
Paul Fultz II's avatar
Paul Fultz II committed
529
530
531
532
533
534
535
536
537
    {
    }
    std::vector<migraphx::argument> object;
};

extern "C" struct migraphx_shapes;
struct migraphx_shapes
{
    template <class... Ts>
538
539
    migraphx_shapes(Ts&&... xs)
        : object(std::forward<Ts>(xs)...) // NOLINT(readability-redundant-member-init)
Paul Fultz II's avatar
Paul Fultz II committed
540
541
542
543
544
    {
    }
    std::vector<migraphx::shape> object;
};

545
546
547
548
extern "C" struct migraphx_instruction;
struct migraphx_instruction
{
    template <class... Ts>
549
550
    migraphx_instruction(Ts&&... xs)
        : object(std::forward<Ts>(xs)...) // NOLINT(readability-redundant-member-init)
551
552
553
554
555
556
557
558
559
    {
    }
    migraphx::instruction_ref object;
};

extern "C" struct migraphx_instructions;
struct migraphx_instructions
{
    template <class... Ts>
560
561
    migraphx_instructions(Ts&&... xs)
        : object(std::forward<Ts>(xs)...) // NOLINT(readability-redundant-member-init)
562
563
564
565
566
567
568
569
570
    {
    }
    std::vector<migraphx::instruction_ref> object;
};

extern "C" struct migraphx_modules;
struct migraphx_modules
{
    template <class... Ts>
571
572
    migraphx_modules(Ts&&... xs)
        : object(std::forward<Ts>(xs)...) // NOLINT(readability-redundant-member-init)
573
574
575
576
577
    {
    }
    std::vector<migraphx::module*> object;
};

Shucai Xiao's avatar
Shucai Xiao committed
578
579
580
581
extern "C" struct migraphx_module;
struct migraphx_module
{
    template <class... Ts>
582
583
    migraphx_module(Ts&&... xs)
        : object(std::forward<Ts>(xs)...) // NOLINT(readability-redundant-member-init)
Shucai Xiao's avatar
Shucai Xiao committed
584
585
586
587
588
    {
    }
    migraphx::module object;
};

Paul Fultz II's avatar
Paul Fultz II committed
589
590
591
592
extern "C" struct migraphx_program;
struct migraphx_program
{
    template <class... Ts>
593
594
    migraphx_program(Ts&&... xs)
        : object(std::forward<Ts>(xs)...) // NOLINT(readability-redundant-member-init)
Paul Fultz II's avatar
Paul Fultz II committed
595
596
597
598
599
    {
    }
    migraphx::program object;
};

600
601
602
603
extern "C" struct migraphx_operation;
struct migraphx_operation
{
    template <class... Ts>
604
605
    migraphx_operation(Ts&&... xs)
        : object(std::forward<Ts>(xs)...) // NOLINT(readability-redundant-member-init)
606
607
608
609
610
    {
    }
    migraphx::operation object;
};

611
612
613
614
extern "C" struct migraphx_onnx_options;
struct migraphx_onnx_options
{
    template <class... Ts>
615
616
    migraphx_onnx_options(Ts&&... xs)
        : object(std::forward<Ts>(xs)...) // NOLINT(readability-redundant-member-init)
617
618
619
620
621
    {
    }
    migraphx::onnx_options object;
};

622
623
624
625
extern "C" struct migraphx_file_options;
struct migraphx_file_options
{
    template <class... Ts>
626
627
    migraphx_file_options(Ts&&... xs)
        : object(std::forward<Ts>(xs)...) // NOLINT(readability-redundant-member-init)
628
629
630
631
632
    {
    }
    migraphx::file_options object;
};

633
634
635
636
extern "C" struct migraphx_compile_options;
struct migraphx_compile_options
{
    template <class... Ts>
637
638
    migraphx_compile_options(Ts&&... xs)
        : object(std::forward<Ts>(xs)...) // NOLINT(readability-redundant-member-init)
639
640
641
642
643
    {
    }
    migraphx::compile_options object;
};

kahmed10's avatar
kahmed10 committed
644
645
646
647
extern "C" struct migraphx_tf_options;
struct migraphx_tf_options
{
    template <class... Ts>
648
649
    migraphx_tf_options(Ts&&... xs)
        : object(std::forward<Ts>(xs)...) // NOLINT(readability-redundant-member-init)
kahmed10's avatar
kahmed10 committed
650
651
652
653
654
    {
    }
    migraphx::tf_options object;
};

Shucai Xiao's avatar
Shucai Xiao committed
655
656
657
658
extern "C" struct migraphx_quantize_op_names;
struct migraphx_quantize_op_names
{
    template <class... Ts>
659
660
    migraphx_quantize_op_names(Ts&&... xs)
        : object(std::forward<Ts>(xs)...) // NOLINT(readability-redundant-member-init)
Shucai Xiao's avatar
Shucai Xiao committed
661
662
663
664
665
666
667
668
669
    {
    }
    std::vector<std::string> object;
};

extern "C" struct migraphx_quantize_int8_options;
struct migraphx_quantize_int8_options
{
    template <class... Ts>
670
671
    migraphx_quantize_int8_options(Ts&&... xs)
        : object(std::forward<Ts>(xs)...) // NOLINT(readability-redundant-member-init)
Shucai Xiao's avatar
Shucai Xiao committed
672
673
674
675
676
    {
    }
    migraphx::quantize_int8_options object;
};

kahmed10's avatar
kahmed10 committed
677
678
679
680
extern "C" struct migraphx_context;
struct migraphx_context
{
    template <class... Ts>
681
682
    migraphx_context(Ts&&... xs)
        : object(std::forward<Ts>(xs)...) // NOLINT(readability-redundant-member-init)
kahmed10's avatar
kahmed10 committed
683
684
685
686
687
    {
    }
    migraphx::context object;
};

688
689
690
691
692
693
694
extern "C" struct migraphx_experimental_custom_op;
struct migraphx_experimental_custom_op
{
    template <class... Ts>
    migraphx_experimental_custom_op(void* p,
                                    migraphx_experimental_custom_op_copy c,
                                    migraphx_experimental_custom_op_delete d,
695
                                    const char* obj_typename,
696
                                    Ts&&... xs)
697
        : object_ptr(p, obj_typename, c, d), xobject(std::forward<Ts>(xs)...)
698
699
700
701
702
    {
    }
    manage_generic_ptr<migraphx_experimental_custom_op_copy, migraphx_experimental_custom_op_delete>
        object_ptr = nullptr;
    migraphx::experimental_custom_op xobject;
703
704
705
706
707
708
709
    migraphx_experimental_custom_op_compute compute_f = nullptr;
    migraphx::argument compute(migraphx::context ctx,
                               migraphx::shape output,
                               std::vector<migraphx::argument> inputs) const
    {
        if(compute_f == nullptr)
            throw std::runtime_error("compute function is missing.");
710
        std::remove_pointer_t<migraphx_argument_t> out;
711
712
        std::array<char, 256> exception_msg;
        exception_msg.front() = '\0';
713
714
        auto api_error_result = compute_f(&out,
                                          object_ptr.data,
715
716
                                          exception_msg.data(),
                                          exception_msg.size(),
717
718
719
720
                                          object_cast<migraphx_context_t>(&(ctx)),
                                          object_cast<migraphx_shape_t>(&(output)),
                                          object_cast<migraphx_arguments_t>(&(inputs)));
        if(api_error_result != migraphx_status_success)
721
722
723
724
725
        {
            const std::string exception_str(exception_msg.data());
            throw std::runtime_error("Error in compute of: " +
                                     std::string(object_ptr.obj_typename) + ": " + exception_str);
        }
726
727
728
        return (&out)->object;
    }

729
730
731
732
733
    migraphx_experimental_custom_op_compute_shape compute_shape_f = nullptr;
    migraphx::shape compute_shape(std::vector<migraphx::shape> inputs) const
    {
        if(compute_shape_f == nullptr)
            throw std::runtime_error("compute_shape function is missing.");
734
        std::remove_pointer_t<migraphx_shape_t> out;
735
736
737
738
739
740
741
        std::array<char, 256> exception_msg;
        exception_msg.front() = '\0';
        auto api_error_result = compute_shape_f(&out,
                                                object_ptr.data,
                                                exception_msg.data(),
                                                exception_msg.size(),
                                                object_cast<migraphx_shapes_t>(&(inputs)));
742
        if(api_error_result != migraphx_status_success)
743
744
745
746
747
        {
            const std::string exception_str(exception_msg.data());
            throw std::runtime_error("Error in compute_shape of: " +
                                     std::string(object_ptr.obj_typename) + ": " + exception_str);
        }
748
749
        return (&out)->object;
    }
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792

    migraphx_experimental_custom_op_output_alias output_alias_f = nullptr;
    std::vector<size_t> output_alias(std::vector<migraphx::shape> inputs) const
    {
        if(output_alias_f == nullptr)
            throw std::runtime_error("output_alias function is missing.");
        std::array<size_t, 1024> out;
        std::remove_pointer_t<size_t*> out_size = 1024;
        std::array<char, 256> exception_msg;
        exception_msg.front() = '\0';
        auto api_error_result = output_alias_f(out.data(),
                                               &out_size,
                                               object_ptr.data,
                                               exception_msg.data(),
                                               exception_msg.size(),
                                               object_cast<migraphx_shapes_t>(&(inputs)));
        if(api_error_result != migraphx_status_success)
        {
            const std::string exception_str(exception_msg.data());
            throw std::runtime_error("Error in output_alias of: " +
                                     std::string(object_ptr.obj_typename) + ": " + exception_str);
        }
        return {out.begin(), out.begin() + out_size}; // cppcheck-suppress returnDanglingLifetime;
    }

    migraphx_experimental_custom_op_runs_on_offload_target runs_on_offload_target_f = nullptr;
    bool runs_on_offload_target() const
    {
        if(runs_on_offload_target_f == nullptr)
            throw std::runtime_error("runs_on_offload_target function is missing.");
        std::remove_pointer_t<bool*> out;
        std::array<char, 256> exception_msg;
        exception_msg.front() = '\0';
        auto api_error_result = runs_on_offload_target_f(
            &out, object_ptr.data, exception_msg.data(), exception_msg.size());
        if(api_error_result != migraphx_status_success)
        {
            const std::string exception_str(exception_msg.data());
            throw std::runtime_error("Error in runs_on_offload_target of: " +
                                     std::string(object_ptr.obj_typename) + ": " + exception_str);
        }
        return out;
    }
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
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
extern "C" migraphx_status migraphx_optimals_destroy(migraphx_optimals_t optimals)
{
    auto api_error_result = migraphx::try_([&] { destroy((optimals)); });
    return api_error_result;
}

extern "C" migraphx_status migraphx_optimals_assign_to(migraphx_optimals_t output,
                                                       const_migraphx_optimals_t input)
{
    auto api_error_result = migraphx::try_([&] { *output = *input; });
    return api_error_result;
}

extern "C" migraphx_status
migraphx_optimals_create(migraphx_optimals_t* optimals, const size_t* ptr, size_t size)
{
    auto api_error_result = migraphx::try_([&] {
        *optimals = object_cast<migraphx_optimals_t>(
            allocate<std::set<size_t>>(migraphx::make_set<size_t>((ptr), (size))));
    });
    return api_error_result;
}

extern "C" migraphx_status
migraphx_dynamic_dimension_destroy(migraphx_dynamic_dimension_t dynamic_dimension)
{
    auto api_error_result = migraphx::try_([&] { destroy((dynamic_dimension)); });
    return api_error_result;
}

extern "C" migraphx_status
migraphx_dynamic_dimension_assign_to(migraphx_dynamic_dimension_t output,
                                     const_migraphx_dynamic_dimension_t input)
{
    auto api_error_result = migraphx::try_([&] { *output = *input; });
    return api_error_result;
}

extern "C" migraphx_status migraphx_dynamic_dimension_create_min_max(
    migraphx_dynamic_dimension_t* dynamic_dimension, size_t min, size_t max)
{
    auto api_error_result = migraphx::try_([&] {
        *dynamic_dimension = object_cast<migraphx_dynamic_dimension_t>(
            allocate<migraphx::shape::dynamic_dimension>((min), (max)));
    });
    return api_error_result;
}

extern "C" migraphx_status
migraphx_dynamic_dimension_create_min_max_optimals(migraphx_dynamic_dimension_t* dynamic_dimension,
                                                   size_t min,
                                                   size_t max,
                                                   migraphx_optimals_t optimals)
{
    auto api_error_result = migraphx::try_([&] {
        if(optimals == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter optimals: Null pointer");
        *dynamic_dimension = object_cast<migraphx_dynamic_dimension_t>(
            allocate<migraphx::shape::dynamic_dimension>((min), (max), (optimals->object)));
    });
    return api_error_result;
}

extern "C" migraphx_status
migraphx_dynamic_dimension_is_fixed(bool* out, const_migraphx_dynamic_dimension_t dynamic_dimension)
{
    auto api_error_result = migraphx::try_([&] {
        if(dynamic_dimension == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param,
                           "Bad parameter dynamic_dimension: Null pointer");
        *out = (dynamic_dimension->object).is_fixed();
    });
    return api_error_result;
}

extern "C" migraphx_status
migraphx_dynamic_dimension_equal(bool* out,
                                 const_migraphx_dynamic_dimension_t dynamic_dimension,
                                 const_migraphx_dynamic_dimension_t x)
{
    auto api_error_result = migraphx::try_([&] {
        if(dynamic_dimension == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param,
                           "Bad parameter dynamic_dimension: Null pointer");
        if(x == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter x: Null pointer");
        *out = migraphx::equal((dynamic_dimension->object), (x->object));
    });
    return api_error_result;
}

extern "C" migraphx_status
migraphx_dynamic_dimensions_destroy(migraphx_dynamic_dimensions_t dynamic_dimensions)
{
    auto api_error_result = migraphx::try_([&] { destroy((dynamic_dimensions)); });
    return api_error_result;
}

extern "C" migraphx_status
migraphx_dynamic_dimensions_assign_to(migraphx_dynamic_dimensions_t output,
                                      const_migraphx_dynamic_dimensions_t input)
{
    auto api_error_result = migraphx::try_([&] { *output = *input; });
    return api_error_result;
}

extern "C" migraphx_status
migraphx_dynamic_dimensions_create(migraphx_dynamic_dimensions_t* dynamic_dimensions,
903
                                   const const_migraphx_dynamic_dimension_t* ptr,
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
                                   size_t size)
{
    auto api_error_result = migraphx::try_([&] {
        *dynamic_dimensions = object_cast<migraphx_dynamic_dimensions_t>(
            allocate<std::vector<migraphx::shape::dynamic_dimension>>(
                migraphx::to_obj_vector<const_migraphx_dynamic_dimension_t>((ptr), (size))));
    });
    return api_error_result;
}

extern "C" migraphx_status
migraphx_dynamic_dimensions_size(size_t* out, migraphx_dynamic_dimensions_t dynamic_dimensions)
{
    auto api_error_result = migraphx::try_([&] {
        if(dynamic_dimensions == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param,
                           "Bad parameter dynamic_dimensions: Null pointer");
        *out = (dynamic_dimensions->object).size();
    });
    return api_error_result;
}

extern "C" migraphx_status
migraphx_dynamic_dimensions_get(const_migraphx_dynamic_dimension_t* out,
                                migraphx_dynamic_dimensions_t dynamic_dimensions,
                                size_t idx)
{
    auto api_error_result = migraphx::try_([&] {
        if(dynamic_dimensions == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param,
                           "Bad parameter dynamic_dimensions: Null pointer");
        *out = object_cast<const_migraphx_dynamic_dimension_t>(
            &((dynamic_dimensions->object).at((idx))));
    });
    return api_error_result;
}

Paul Fultz II's avatar
Paul Fultz II committed
941
942
extern "C" migraphx_status migraphx_shape_destroy(migraphx_shape_t shape)
{
943
944
    auto api_error_result = migraphx::try_([&] { destroy((shape)); });
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
945
946
}

947
948
949
950
951
952
953
extern "C" migraphx_status migraphx_shape_assign_to(migraphx_shape_t output,
                                                    const_migraphx_shape_t input)
{
    auto api_error_result = migraphx::try_([&] { *output = *input; });
    return api_error_result;
}

Paul Fultz II's avatar
Paul Fultz II committed
954
955
956
957
958
extern "C" migraphx_status migraphx_shape_create(migraphx_shape_t* shape,
                                                 migraphx_shape_datatype_t type,
                                                 size_t* lengths,
                                                 size_t lengths_size)
{
959
    auto api_error_result = migraphx::try_([&] {
960
        if(lengths == nullptr and lengths_size != 0)
Paul Fultz II's avatar
Paul Fultz II committed
961
962
963
964
965
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter lengths: Null pointer");
        *shape = object_cast<migraphx_shape_t>(
            allocate<migraphx::shape>((migraphx::to_shape_type(type)),
                                      (std::vector<size_t>(lengths, lengths + lengths_size))));
    });
966
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
967
968
}

969
970
971
972
973
974
975
extern "C" migraphx_status migraphx_shape_create_with_strides(migraphx_shape_t* shape,
                                                              migraphx_shape_datatype_t type,
                                                              size_t* lengths,
                                                              size_t lengths_size,
                                                              size_t* strides,
                                                              size_t strides_size)
{
976
    auto api_error_result = migraphx::try_([&] {
977
978
979
980
981
982
983
984
985
        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)),
                                      (std::vector<size_t>(lengths, lengths + lengths_size)),
                                      (std::vector<size_t>(strides, strides + strides_size))));
    });
986
    return api_error_result;
987
988
}

989
990
991
extern "C" migraphx_status migraphx_shape_create_scalar(migraphx_shape_t* shape,
                                                        migraphx_shape_datatype_t type)
{
992
    auto api_error_result = migraphx::try_([&] {
993
994
995
        *shape = object_cast<migraphx_shape_t>(
            allocate<migraphx::shape>((migraphx::to_shape_type(type))));
    });
996
    return api_error_result;
997
998
}

999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
extern "C" migraphx_status migraphx_shape_create_dynamic(migraphx_shape_t* shape,
                                                         migraphx_shape_datatype_t type,
                                                         migraphx_dynamic_dimensions_t dims)
{
    auto api_error_result = migraphx::try_([&] {
        if(dims == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter dims: Null pointer");
        *shape = object_cast<migraphx_shape_t>(
            allocate<migraphx::shape>((migraphx::to_shape_type(type)), (dims->object)));
    });
    return api_error_result;
}

Paul Fultz II's avatar
Paul Fultz II committed
1012
1013
1014
extern "C" migraphx_status
migraphx_shape_lengths(const size_t** out, size_t* out_size, const_migraphx_shape_t shape)
{
1015
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
1016
1017
1018
1019
1020
1021
1022
1023
        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();
    });
1024
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1025
1026
1027
1028
1029
}

extern "C" migraphx_status
migraphx_shape_strides(const size_t** out, size_t* out_size, const_migraphx_shape_t shape)
{
1030
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
1031
1032
1033
1034
1035
1036
1037
1038
        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();
    });
1039
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1040
1041
}

1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
extern "C" migraphx_status migraphx_shape_dyn_dims(migraphx_dynamic_dimensions_t* out,
                                                   const_migraphx_shape_t shape)
{
    auto api_error_result = migraphx::try_([&] {
        if(shape == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter shape: Null pointer");
        *out = allocate<migraphx_dynamic_dimensions_t>((shape->object).dyn_dims());
    });
    return api_error_result;
}

Paul Fultz II's avatar
Paul Fultz II committed
1053
1054
1055
extern "C" migraphx_status migraphx_shape_type(migraphx_shape_datatype_t* out,
                                               const_migraphx_shape_t shape)
{
1056
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
1057
1058
1059
1060
1061
1062
        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());
    });
1063
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1064
1065
}

1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
extern "C" migraphx_status migraphx_shape_elements(size_t* out, const_migraphx_shape_t shape)
{
    auto api_error_result = migraphx::try_([&] {
        if(shape == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter shape: Null pointer");
        *out = (shape->object).elements();
    });
    return api_error_result;
}

Paul Fultz II's avatar
Paul Fultz II committed
1076
1077
extern "C" migraphx_status migraphx_shape_bytes(size_t* out, const_migraphx_shape_t shape)
{
1078
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
1079
1080
1081
1082
        if(shape == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter shape: Null pointer");
        *out = (shape->object).bytes();
    });
1083
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1084
1085
}

1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
extern "C" migraphx_status migraphx_shape_ndim(size_t* out, const_migraphx_shape_t shape)
{
    auto api_error_result = migraphx::try_([&] {
        if(shape == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter shape: Null pointer");
        *out = (shape->object).ndim();
    });
    return api_error_result;
}

Paul Fultz II's avatar
Paul Fultz II committed
1096
1097
1098
extern "C" migraphx_status
migraphx_shape_equal(bool* out, const_migraphx_shape_t shape, const_migraphx_shape_t x)
{
1099
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
1100
1101
1102
1103
1104
1105
        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));
    });
1106
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1107
1108
}

1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
extern "C" migraphx_status migraphx_shape_standard(bool* out, const_migraphx_shape_t shape)
{
    auto api_error_result = migraphx::try_([&] {
        if(shape == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter shape: Null pointer");
        *out = (shape->object).standard();
    });
    return api_error_result;
}

1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
extern "C" migraphx_status migraphx_shape_dynamic(bool* out, const_migraphx_shape_t shape)
{
    auto api_error_result = migraphx::try_([&] {
        if(shape == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter shape: Null pointer");
        *out = (shape->object).dynamic();
    });
    return api_error_result;
}

1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
extern "C" migraphx_status migraphx_shape_index(size_t* out, const_migraphx_shape_t shape, size_t i)
{
    auto api_error_result = migraphx::try_([&] {
        if(shape == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter shape: Null pointer");
        *out = (shape->object).index((i));
    });
    return api_error_result;
}

Paul Fultz II's avatar
Paul Fultz II committed
1139
1140
extern "C" migraphx_status migraphx_argument_destroy(migraphx_argument_t argument)
{
1141
1142
    auto api_error_result = migraphx::try_([&] { destroy((argument)); });
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1143
1144
}

1145
1146
1147
1148
1149
1150
1151
extern "C" migraphx_status migraphx_argument_assign_to(migraphx_argument_t output,
                                                       const_migraphx_argument_t input)
{
    auto api_error_result = migraphx::try_([&] { *output = *input; });
    return api_error_result;
}

Paul Fultz II's avatar
Paul Fultz II committed
1152
1153
1154
extern "C" migraphx_status
migraphx_argument_create(migraphx_argument_t* argument, const_migraphx_shape_t shape, void* buffer)
{
1155
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
1156
1157
1158
1159
1160
        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)));
    });
1161
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1162
1163
}

1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
extern "C" migraphx_status migraphx_argument_create_empty(migraphx_argument_t* argument,
                                                          const_migraphx_shape_t shape)
{
    auto api_error_result = migraphx::try_([&] {
        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)));
    });
    return api_error_result;
}

Paul Fultz II's avatar
Paul Fultz II committed
1175
1176
1177
extern "C" migraphx_status migraphx_argument_shape(const_migraphx_shape_t* out,
                                                   const_migraphx_argument_t argument)
{
1178
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
1179
1180
1181
1182
        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()));
    });
1183
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1184
1185
1186
1187
}

extern "C" migraphx_status migraphx_argument_buffer(char** out, const_migraphx_argument_t argument)
{
1188
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
1189
1190
1191
1192
        if(argument == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter argument: Null pointer");
        *out = (argument->object).data();
    });
1193
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1194
1195
1196
1197
1198
}

extern "C" migraphx_status
migraphx_argument_equal(bool* out, const_migraphx_argument_t argument, const_migraphx_argument_t x)
{
1199
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
1200
1201
1202
1203
1204
1205
        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));
    });
1206
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1207
1208
1209
1210
1211
}

extern "C" migraphx_status
migraphx_argument_generate(migraphx_argument_t* out, const_migraphx_shape_t s, size_t seed)
{
1212
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
1213
1214
1215
1216
        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)));
    });
1217
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1218
1219
1220
1221
}

extern "C" migraphx_status migraphx_target_destroy(migraphx_target_t target)
{
1222
1223
    auto api_error_result = migraphx::try_([&] { destroy((target)); });
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1224
1225
}

1226
1227
1228
1229
1230
1231
1232
extern "C" migraphx_status migraphx_target_assign_to(migraphx_target_t output,
                                                     const_migraphx_target_t input)
{
    auto api_error_result = migraphx::try_([&] { *output = *input; });
    return api_error_result;
}

Paul Fultz II's avatar
Paul Fultz II committed
1233
1234
extern "C" migraphx_status migraphx_target_create(migraphx_target_t* target, const char* name)
{
1235
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
1236
1237
1238
        *target = object_cast<migraphx_target_t>(
            allocate<migraphx::target>(migraphx::get_target((name))));
    });
1239
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1240
1241
1242
1243
1244
}

extern "C" migraphx_status migraphx_program_parameter_shapes_destroy(
    migraphx_program_parameter_shapes_t program_parameter_shapes)
{
1245
1246
    auto api_error_result = migraphx::try_([&] { destroy((program_parameter_shapes)); });
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1247
1248
}

1249
1250
1251
1252
1253
1254
1255
1256
extern "C" migraphx_status
migraphx_program_parameter_shapes_assign_to(migraphx_program_parameter_shapes_t output,
                                            const_migraphx_program_parameter_shapes_t input)
{
    auto api_error_result = migraphx::try_([&] { *output = *input; });
    return api_error_result;
}

Paul Fultz II's avatar
Paul Fultz II committed
1257
1258
1259
1260
extern "C" migraphx_status
migraphx_program_parameter_shapes_size(size_t* out,
                                       migraphx_program_parameter_shapes_t program_parameter_shapes)
{
1261
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
1262
1263
1264
1265
1266
        if(program_parameter_shapes == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param,
                           "Bad parameter program_parameter_shapes: Null pointer");
        *out = (program_parameter_shapes->object).size();
    });
1267
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1268
1269
1270
1271
1272
1273
1274
}

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)
{
1275
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
1276
1277
1278
1279
1280
1281
        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))));
    });
1282
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1283
1284
1285
1286
1287
}

extern "C" migraphx_status migraphx_program_parameter_shapes_names(
    const char** out, migraphx_program_parameter_shapes_t program_parameter_shapes)
{
1288
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
1289
1290
1291
1292
1293
1294
1295
1296
        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);
    });
1297
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1298
1299
1300
1301
1302
}

extern "C" migraphx_status
migraphx_program_parameters_destroy(migraphx_program_parameters_t program_parameters)
{
1303
1304
    auto api_error_result = migraphx::try_([&] { destroy((program_parameters)); });
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1305
1306
}

1307
1308
1309
1310
1311
1312
1313
1314
extern "C" migraphx_status
migraphx_program_parameters_assign_to(migraphx_program_parameters_t output,
                                      const_migraphx_program_parameters_t input)
{
    auto api_error_result = migraphx::try_([&] { *output = *input; });
    return api_error_result;
}

Paul Fultz II's avatar
Paul Fultz II committed
1315
1316
1317
extern "C" migraphx_status
migraphx_program_parameters_create(migraphx_program_parameters_t* program_parameters)
{
1318
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
1319
1320
1321
        *program_parameters = object_cast<migraphx_program_parameters_t>(
            allocate<std::unordered_map<std::string, migraphx::argument>>());
    });
1322
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1323
1324
1325
1326
1327
1328
1329
}

extern "C" migraphx_status
migraphx_program_parameters_add(migraphx_program_parameters_t program_parameters,
                                const char* name,
                                const_migraphx_argument_t argument)
{
1330
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
1331
1332
1333
1334
1335
1336
1337
        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);
    });
1338
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1339
1340
1341
1342
}

extern "C" migraphx_status migraphx_arguments_destroy(migraphx_arguments_t arguments)
{
1343
1344
    auto api_error_result = migraphx::try_([&] { destroy((arguments)); });
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1345
1346
}

1347
1348
1349
1350
1351
1352
1353
extern "C" migraphx_status migraphx_arguments_assign_to(migraphx_arguments_t output,
                                                        const_migraphx_arguments_t input)
{
    auto api_error_result = migraphx::try_([&] { *output = *input; });
    return api_error_result;
}

Paul Fultz II's avatar
Paul Fultz II committed
1354
1355
extern "C" migraphx_status migraphx_arguments_size(size_t* out, migraphx_arguments_t arguments)
{
1356
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
1357
1358
1359
1360
        if(arguments == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter arguments: Null pointer");
        *out = (arguments->object).size();
    });
1361
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1362
1363
1364
1365
1366
}

extern "C" migraphx_status
migraphx_arguments_get(const_migraphx_argument_t* out, migraphx_arguments_t arguments, size_t idx)
{
1367
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
1368
1369
1370
1371
        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))));
    });
1372
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1373
1374
1375
1376
}

extern "C" migraphx_status migraphx_shapes_destroy(migraphx_shapes_t shapes)
{
1377
1378
    auto api_error_result = migraphx::try_([&] { destroy((shapes)); });
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1379
1380
}

1381
1382
1383
1384
1385
1386
1387
extern "C" migraphx_status migraphx_shapes_assign_to(migraphx_shapes_t output,
                                                     const_migraphx_shapes_t input)
{
    auto api_error_result = migraphx::try_([&] { *output = *input; });
    return api_error_result;
}

Paul Fultz II's avatar
Paul Fultz II committed
1388
1389
extern "C" migraphx_status migraphx_shapes_size(size_t* out, migraphx_shapes_t shapes)
{
1390
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
1391
1392
1393
1394
        if(shapes == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter shapes: Null pointer");
        *out = (shapes->object).size();
    });
1395
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1396
1397
1398
1399
1400
}

extern "C" migraphx_status
migraphx_shapes_get(const_migraphx_shape_t* out, migraphx_shapes_t shapes, size_t idx)
{
1401
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
1402
1403
1404
1405
        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))));
    });
1406
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1407
1408
}

1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
extern "C" migraphx_status migraphx_instruction_destroy(migraphx_instruction_t instruction)
{
    auto api_error_result = migraphx::try_([&] { destroy((instruction)); });
    return api_error_result;
}

extern "C" migraphx_status migraphx_instruction_assign_to(migraphx_instruction_t output,
                                                          const_migraphx_instruction_t input)
{
    auto api_error_result = migraphx::try_([&] { *output = *input; });
    return api_error_result;
}

extern "C" migraphx_status migraphx_instructions_destroy(migraphx_instructions_t instructions)
{
    auto api_error_result = migraphx::try_([&] { destroy((instructions)); });
    return api_error_result;
}

extern "C" migraphx_status migraphx_instructions_assign_to(migraphx_instructions_t output,
                                                           const_migraphx_instructions_t input)
{
    auto api_error_result = migraphx::try_([&] { *output = *input; });
    return api_error_result;
}

extern "C" migraphx_status migraphx_instructions_create(migraphx_instructions_t* instructions,
1436
                                                        const const_migraphx_instruction_t* ptr,
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
                                                        size_t size)
{
    auto api_error_result = migraphx::try_([&] {
        *instructions =
            object_cast<migraphx_instructions_t>(allocate<std::vector<migraphx::instruction_ref>>(
                migraphx::to_obj_vector<const_migraphx_instruction_t>((ptr), (size))));
    });
    return api_error_result;
}

extern "C" migraphx_status migraphx_modules_destroy(migraphx_modules_t modules)
{
    auto api_error_result = migraphx::try_([&] { destroy((modules)); });
    return api_error_result;
}

extern "C" migraphx_status migraphx_modules_assign_to(migraphx_modules_t output,
                                                      const_migraphx_modules_t input)
{
    auto api_error_result = migraphx::try_([&] { *output = *input; });
    return api_error_result;
}

extern "C" migraphx_status
migraphx_modules_create(migraphx_modules_t* modules, migraphx_module_t* ptr, size_t size)
{
    auto api_error_result = migraphx::try_([&] {
        *modules = object_cast<migraphx_modules_t>(allocate<std::vector<migraphx::module*>>(
            migraphx::to_objptr_vector<migraphx::module*>((ptr), (size))));
    });
    return api_error_result;
}

extern "C" migraphx_status migraphx_module_create(migraphx_module_t* module, char* name)
{
    auto api_error_result = migraphx::try_([&] {
        if(name == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter name: Null pointer");
        *module = object_cast<migraphx_module_t>(allocate<migraphx::module>((std::string(name))));
    });
    return api_error_result;
}

Shucai Xiao's avatar
Shucai Xiao committed
1480
1481
extern "C" migraphx_status migraphx_module_print(const_migraphx_module_t module)
{
1482
    auto api_error_result = migraphx::try_([&] {
Shucai Xiao's avatar
Shucai Xiao committed
1483
1484
1485
1486
        if(module == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter module: Null pointer");
        migraphx::print_module((module->object));
    });
1487
    return api_error_result;
Shucai Xiao's avatar
Shucai Xiao committed
1488
1489
}

1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
extern "C" migraphx_status migraphx_module_add_instruction(migraphx_instruction_t* out,
                                                           migraphx_module_t module,
                                                           migraphx_operation_t op,
                                                           migraphx_instructions_t args)
{
    auto api_error_result = migraphx::try_([&] {
        if(module == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter module: Null pointer");
        if(op == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter op: Null pointer");
        if(args == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter args: Null pointer");
        *out = allocate<migraphx_instruction_t>(
            (module->object).add_instruction((op->object), (args->object)));
    });
    return api_error_result;
}

extern "C" migraphx_status
migraphx_module_add_instruction_with_mod_args(migraphx_instruction_t* out,
                                              migraphx_module_t module,
                                              migraphx_operation_t op,
                                              migraphx_instructions_t args,
                                              migraphx_modules_t module_refs)
{
    auto api_error_result = migraphx::try_([&] {
        if(module == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter module: Null pointer");
        if(op == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter op: Null pointer");
        if(args == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter args: Null pointer");
        if(module_refs == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter module_refs: Null pointer");
        *out = allocate<migraphx_instruction_t>(
            (module->object).add_instruction((op->object), (args->object), (module_refs->object)));
    });
    return api_error_result;
}

1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
extern "C" migraphx_status migraphx_module_add_literal(migraphx_instruction_t* out,
                                                       migraphx_module_t module,
                                                       const_migraphx_shape_t shape,
                                                       const char* buffer)
{
    auto api_error_result = migraphx::try_([&] {
        if(module == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter module: Null pointer");
        if(shape == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter shape: Null pointer");
        *out = allocate<migraphx_instruction_t>(
            (module->object).add_literal((shape->object), (buffer)));
    });
    return api_error_result;
}

1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
extern "C" migraphx_status migraphx_module_add_parameter(migraphx_instruction_t* out,
                                                         migraphx_module_t module,
                                                         const char* name,
                                                         const_migraphx_shape_t shape)
{
    auto api_error_result = migraphx::try_([&] {
        if(module == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter module: Null pointer");
        if(shape == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter shape: Null pointer");
        *out = allocate<migraphx_instruction_t>(
            (module->object).add_parameter((name), (shape->object)));
    });
    return api_error_result;
}

extern "C" migraphx_status migraphx_module_add_return(migraphx_instruction_t* out,
                                                      migraphx_module_t module,
                                                      migraphx_instructions_t args)
{
    auto api_error_result = migraphx::try_([&] {
        if(module == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter module: Null pointer");
        if(args == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter args: Null pointer");
        *out = allocate<migraphx_instruction_t>((module->object).add_return((args->object)));
    });
    return api_error_result;
}

1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
extern "C" migraphx_status migraphx_module_add_allocation(migraphx_instruction_t* out,
                                                          migraphx_module_t module,
                                                          const_migraphx_shape_t s)
{
    auto api_error_result = migraphx::try_([&] {
        if(module == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter module: Null pointer");
        if(s == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter s: Null pointer");
        *out = allocate<migraphx_instruction_t>(
            migraphx::add_allocation((module->object), (s->object)));
    });
    return api_error_result;
}

Paul Fultz II's avatar
Paul Fultz II committed
1591
1592
extern "C" migraphx_status migraphx_program_destroy(migraphx_program_t program)
{
1593
1594
    auto api_error_result = migraphx::try_([&] { destroy((program)); });
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1595
1596
}

1597
1598
1599
1600
1601
1602
1603
extern "C" migraphx_status migraphx_program_assign_to(migraphx_program_t output,
                                                      const_migraphx_program_t input)
{
    auto api_error_result = migraphx::try_([&] { *output = *input; });
    return api_error_result;
}

1604
1605
1606
1607
1608
1609
1610
extern "C" migraphx_status migraphx_program_create(migraphx_program_t* program)
{
    auto api_error_result = migraphx::try_(
        [&] { *program = object_cast<migraphx_program_t>(allocate<migraphx::program>()); });
    return api_error_result;
}

Shucai Xiao's avatar
Shucai Xiao committed
1611
1612
1613
extern "C" migraphx_status migraphx_program_get_main_module(migraphx_module_t* out,
                                                            migraphx_program_t program)
{
1614
    auto api_error_result = migraphx::try_([&] {
Shucai Xiao's avatar
Shucai Xiao committed
1615
1616
1617
1618
        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());
    });
1619
    return api_error_result;
Shucai Xiao's avatar
Shucai Xiao committed
1620
1621
}

1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
extern "C" migraphx_status
migraphx_program_create_module(migraphx_module_t* out, migraphx_program_t program, const char* name)
{
    auto api_error_result = migraphx::try_([&] {
        if(program == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter program: Null pointer");
        *out = object_cast<migraphx_module_t>((program->object).create_module((name)));
    });
    return api_error_result;
}

Paul Fultz II's avatar
Paul Fultz II committed
1633
1634
extern "C" migraphx_status migraphx_program_compile(migraphx_program_t program,
                                                    migraphx_target_t target,
1635
                                                    migraphx_compile_options_t options)
Paul Fultz II's avatar
Paul Fultz II committed
1636
{
1637
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
1638
1639
1640
1641
        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");
1642
1643
1644
        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
1645
    });
1646
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1647
1648
1649
1650
1651
1652
}

extern "C" migraphx_status
migraphx_program_get_parameter_shapes(migraphx_program_parameter_shapes_t* out,
                                      migraphx_program_t program)
{
1653
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
1654
1655
1656
1657
1658
        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());
    });
1659
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1660
1661
1662
1663
1664
}

extern "C" migraphx_status migraphx_program_get_output_shapes(migraphx_shapes_t* out,
                                                              migraphx_program_t program)
{
1665
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
1666
1667
1668
1669
        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)));
    });
1670
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1671
1672
1673
1674
}

extern "C" migraphx_status migraphx_program_print(const_migraphx_program_t program)
{
1675
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
1676
1677
        if(program == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter program: Null pointer");
Shucai Xiao's avatar
Shucai Xiao committed
1678
        migraphx::print_program((program->object));
Paul Fultz II's avatar
Paul Fultz II committed
1679
    });
1680
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1681
1682
}

1683
1684
extern "C" migraphx_status migraphx_program_sort(migraphx_program_t program)
{
1685
    auto api_error_result = migraphx::try_([&] {
1686
1687
1688
1689
        if(program == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter program: Null pointer");
        (program->object).sort();
    });
1690
    return api_error_result;
1691
1692
}

Paul Fultz II's avatar
Paul Fultz II committed
1693
1694
1695
1696
extern "C" migraphx_status migraphx_program_run(migraphx_arguments_t* out,
                                                migraphx_program_t program,
                                                migraphx_program_parameters_t params)
{
1697
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
1698
1699
1700
1701
1702
1703
        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)));
    });
1704
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1705
1706
}

1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
extern "C" migraphx_status migraphx_program_run_async(migraphx_arguments_t* out,
                                                      migraphx_program_t program,
                                                      migraphx_program_parameters_t params,
                                                      void* s,
                                                      const char* name)
{
    auto api_error_result = migraphx::try_([&] {
        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_async((program->object), (params->object), (s), (name)));
    });
    return api_error_result;
}

Paul Fultz II's avatar
Paul Fultz II committed
1724
1725
1726
extern "C" migraphx_status
migraphx_program_equal(bool* out, const_migraphx_program_t program, const_migraphx_program_t x)
{
1727
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
1728
1729
1730
1731
1732
1733
        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));
    });
1734
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1735
1736
}

1737
1738
extern "C" migraphx_status
migraphx_program_experimental_get_context(migraphx_context_t* out, const_migraphx_program_t program)
kahmed10's avatar
kahmed10 committed
1739
1740
1741
1742
1743
1744
1745
1746
1747
{
    auto api_error_result = migraphx::try_([&] {
        if(program == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter program: Null pointer");
        *out = allocate<migraphx_context_t>(migraphx::get_context((program->object)));
    });
    return api_error_result;
}

1748
1749
extern "C" migraphx_status migraphx_operation_destroy(migraphx_operation_t operation)
{
1750
1751
    auto api_error_result = migraphx::try_([&] { destroy((operation)); });
    return api_error_result;
1752
1753
}

1754
1755
1756
1757
1758
1759
1760
extern "C" migraphx_status migraphx_operation_assign_to(migraphx_operation_t output,
                                                        const_migraphx_operation_t input)
{
    auto api_error_result = migraphx::try_([&] { *output = *input; });
    return api_error_result;
}

1761
1762
1763
1764
extern "C" migraphx_status migraphx_operation_create(migraphx_operation_t* operation,
                                                     const char* name,
                                                     const char* attributes,
                                                     ...)
1765
{
1766
1767
1768
    va_list vlist;
    va_start(vlist, attributes);
    auto api_error_result = migraphx::try_([&] {
1769
        *operation = object_cast<migraphx_operation_t>(
1770
            allocate<migraphx::operation>(migraphx::create_op((name), (attributes), (vlist))));
1771
    });
1772
1773
    va_end(vlist);
    return api_error_result;
1774
1775
1776
1777
1778
}

extern "C" migraphx_status
migraphx_operation_name(char* out, size_t out_size, migraphx_operation_t operation)
{
1779
    auto api_error_result = migraphx::try_([&] {
1780
1781
1782
1783
1784
1785
1786
1787
        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();
        auto* it = std::copy_n(api_result.begin(), std::min(api_result.size(), out_size - 1), out);
        *it      = '\0';
    });
1788
    return api_error_result;
1789
1790
}

1791
extern "C" migraphx_status
1792
migraphx_load(migraphx_program_t* out, const char* name, migraphx_file_options_t options)
1793
{
1794
    auto api_error_result = migraphx::try_([&] {
1795
1796
1797
        if(options == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter options: Null pointer");
        *out = allocate<migraphx_program_t>(migraphx::load((name), (options->object)));
1798
    });
1799
    return api_error_result;
1800
1801
1802
}

extern "C" migraphx_status
1803
migraphx_save(migraphx_program_t p, const char* name, migraphx_file_options_t options)
1804
{
1805
    auto api_error_result = migraphx::try_([&] {
1806
1807
        if(p == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter p: Null pointer");
1808
1809
1810
        if(options == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter options: Null pointer");
        migraphx::save((p->object), (name), (options->object));
1811
    });
1812
    return api_error_result;
1813
1814
}

1815
1816
extern "C" migraphx_status migraphx_onnx_options_destroy(migraphx_onnx_options_t onnx_options)
{
1817
1818
    auto api_error_result = migraphx::try_([&] { destroy((onnx_options)); });
    return api_error_result;
1819
1820
}

1821
1822
1823
1824
1825
1826
1827
extern "C" migraphx_status migraphx_onnx_options_assign_to(migraphx_onnx_options_t output,
                                                           const_migraphx_onnx_options_t input)
{
    auto api_error_result = migraphx::try_([&] { *output = *input; });
    return api_error_result;
}

1828
1829
extern "C" migraphx_status migraphx_onnx_options_create(migraphx_onnx_options_t* onnx_options)
{
1830
    auto api_error_result = migraphx::try_([&] {
1831
1832
        *onnx_options = object_cast<migraphx_onnx_options_t>(allocate<migraphx::onnx_options>());
    });
1833
    return api_error_result;
1834
1835
}

1836
1837
extern "C" migraphx_status migraphx_onnx_options_set_input_parameter_shape(
    migraphx_onnx_options_t onnx_options, const char* name, size_t* dims, size_t dims_size)
1838
{
1839
    auto api_error_result = migraphx::try_([&] {
1840
1841
        if(onnx_options == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter onnx_options: Null pointer");
1842
1843
1844
1845
        if(dims == nullptr and dims_size != 0)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter dims: Null pointer");
        migraphx::set_input_parameter_shape(
            (onnx_options->object), (name), (std::vector<size_t>(dims, dims + dims_size)));
1846
    });
1847
    return api_error_result;
1848
1849
}

1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
extern "C" migraphx_status migraphx_onnx_options_set_dyn_input_parameter_shape(
    migraphx_onnx_options_t onnx_options, const char* name, migraphx_dynamic_dimensions_t dims)
{
    auto api_error_result = migraphx::try_([&] {
        if(onnx_options == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter onnx_options: Null pointer");
        if(dims == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter dims: Null pointer");
        migraphx::set_dyn_input_parameter_shape((onnx_options->object), (name), (dims->object));
    });
    return api_error_result;
}

1863
1864
1865
extern "C" migraphx_status
migraphx_onnx_options_set_default_dim_value(migraphx_onnx_options_t onnx_options, size_t value)
{
1866
    auto api_error_result = migraphx::try_([&] {
1867
1868
1869
1870
        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));
    });
1871
    return api_error_result;
1872
1873
}

1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
extern "C" migraphx_status
migraphx_onnx_options_set_default_dyn_dim_value(migraphx_onnx_options_t onnx_options,
                                                const_migraphx_dynamic_dimension_t dd)
{
    auto api_error_result = migraphx::try_([&] {
        if(onnx_options == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter onnx_options: Null pointer");
        if(dd == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter dd: Null pointer");
        migraphx::set_default_dyn_dim_value((onnx_options->object), (dd->object));
    });
    return api_error_result;
}

1888
1889
1890
1891
extern "C" migraphx_status
migraphx_onnx_options_set_default_loop_iterations(migraphx_onnx_options_t onnx_options,
                                                  int64_t value)
{
1892
    auto api_error_result = migraphx::try_([&] {
1893
1894
1895
1896
        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));
    });
1897
    return api_error_result;
1898
1899
}

1900
1901
extern "C" migraphx_status migraphx_file_options_destroy(migraphx_file_options_t file_options)
{
1902
1903
    auto api_error_result = migraphx::try_([&] { destroy((file_options)); });
    return api_error_result;
1904
1905
}

1906
1907
1908
1909
1910
1911
1912
extern "C" migraphx_status migraphx_file_options_assign_to(migraphx_file_options_t output,
                                                           const_migraphx_file_options_t input)
{
    auto api_error_result = migraphx::try_([&] { *output = *input; });
    return api_error_result;
}

1913
1914
extern "C" migraphx_status migraphx_file_options_create(migraphx_file_options_t* file_options)
{
1915
    auto api_error_result = migraphx::try_([&] {
1916
1917
        *file_options = object_cast<migraphx_file_options_t>(allocate<migraphx::file_options>());
    });
1918
    return api_error_result;
1919
1920
1921
1922
1923
}

extern "C" migraphx_status
migraphx_file_options_set_file_format(migraphx_file_options_t file_options, const char* format)
{
1924
    auto api_error_result = migraphx::try_([&] {
1925
1926
1927
1928
        if(file_options == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter file_options: Null pointer");
        migraphx::set_file_format((file_options->object), (format));
    });
1929
    return api_error_result;
1930
1931
}

Shucai Xiao's avatar
Shucai Xiao committed
1932
extern "C" migraphx_status
1933
1934
migraphx_compile_options_destroy(migraphx_compile_options_t compile_options)
{
1935
1936
    auto api_error_result = migraphx::try_([&] { destroy((compile_options)); });
    return api_error_result;
1937
1938
}

1939
1940
1941
1942
1943
1944
1945
1946
extern "C" migraphx_status
migraphx_compile_options_assign_to(migraphx_compile_options_t output,
                                   const_migraphx_compile_options_t input)
{
    auto api_error_result = migraphx::try_([&] { *output = *input; });
    return api_error_result;
}

1947
1948
extern "C" migraphx_status
migraphx_compile_options_create(migraphx_compile_options_t* compile_options)
Shucai Xiao's avatar
Shucai Xiao committed
1949
{
1950
    auto api_error_result = migraphx::try_([&] {
1951
1952
1953
        *compile_options =
            object_cast<migraphx_compile_options_t>(allocate<migraphx::compile_options>());
    });
1954
    return api_error_result;
1955
1956
1957
1958
1959
}

extern "C" migraphx_status
migraphx_compile_options_set_offload_copy(migraphx_compile_options_t compile_options, bool value)
{
1960
    auto api_error_result = migraphx::try_([&] {
1961
1962
1963
1964
1965
        if(compile_options == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param,
                           "Bad parameter compile_options: Null pointer");
        migraphx::set_offload_copy((compile_options->object), (value));
    });
1966
    return api_error_result;
1967
1968
1969
1970
1971
}

extern "C" migraphx_status
migraphx_compile_options_set_fast_math(migraphx_compile_options_t compile_options, bool value)
{
1972
    auto api_error_result = migraphx::try_([&] {
1973
1974
1975
1976
        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
1977
    });
1978
    return api_error_result;
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
}

extern "C" migraphx_status
migraphx_compile_options_set_exhaustive_tune_flag(migraphx_compile_options_t compile_options,
                                                  bool value)
{
    auto api_error_result = migraphx::try_([&] {
        if(compile_options == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param,
                           "Bad parameter compile_options: Null pointer");
        migraphx::set_exhaustive_tune_flag((compile_options->object), (value));
    });
    return api_error_result;
Shucai Xiao's avatar
Shucai Xiao committed
1992
1993
}

Paul Fultz II's avatar
Paul Fultz II committed
1994
extern "C" migraphx_status
1995
migraphx_parse_onnx(migraphx_program_t* out, const char* name, migraphx_onnx_options_t options)
Paul Fultz II's avatar
Paul Fultz II committed
1996
{
1997
    auto api_error_result = migraphx::try_([&] {
1998
1999
2000
        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
2001
    });
2002
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
2003
2004
2005
2006
2007
}

extern "C" migraphx_status migraphx_parse_onnx_buffer(migraphx_program_t* out,
                                                      const void* data,
                                                      size_t size,
2008
                                                      migraphx_onnx_options_t options)
Paul Fultz II's avatar
Paul Fultz II committed
2009
{
2010
    auto api_error_result = migraphx::try_([&] {
2011
2012
2013
2014
        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
2015
    });
2016
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
2017
}
Shucai Xiao's avatar
Shucai Xiao committed
2018

kahmed10's avatar
kahmed10 committed
2019
2020
extern "C" migraphx_status migraphx_tf_options_destroy(migraphx_tf_options_t tf_options)
{
2021
2022
    auto api_error_result = migraphx::try_([&] { destroy((tf_options)); });
    return api_error_result;
kahmed10's avatar
kahmed10 committed
2023
2024
}

2025
2026
2027
2028
2029
2030
2031
extern "C" migraphx_status migraphx_tf_options_assign_to(migraphx_tf_options_t output,
                                                         const_migraphx_tf_options_t input)
{
    auto api_error_result = migraphx::try_([&] { *output = *input; });
    return api_error_result;
}

kahmed10's avatar
kahmed10 committed
2032
2033
extern "C" migraphx_status migraphx_tf_options_create(migraphx_tf_options_t* tf_options)
{
2034
    auto api_error_result = migraphx::try_([&] {
kahmed10's avatar
kahmed10 committed
2035
2036
        *tf_options = object_cast<migraphx_tf_options_t>(allocate<migraphx::tf_options>());
    });
2037
    return api_error_result;
kahmed10's avatar
kahmed10 committed
2038
2039
2040
2041
2042
}

extern "C" migraphx_status migraphx_tf_options_set_nhwc(migraphx_tf_options_t tf_options,
                                                        bool is_nhwc)
{
2043
    auto api_error_result = migraphx::try_([&] {
kahmed10's avatar
kahmed10 committed
2044
2045
2046
2047
        if(tf_options == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter tf_options: Null pointer");
        migraphx::set_nhwc((tf_options->object), (is_nhwc));
    });
2048
    return api_error_result;
kahmed10's avatar
kahmed10 committed
2049
2050
2051
2052
2053
}

extern "C" migraphx_status migraphx_tf_options_set_input_parameter_shape(
    migraphx_tf_options_t tf_options, const char* name, size_t* dims, size_t dims_size)
{
2054
    auto api_error_result = migraphx::try_([&] {
kahmed10's avatar
kahmed10 committed
2055
2056
2057
2058
2059
2060
2061
        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(
            (tf_options->object), (name), (std::vector<size_t>(dims, dims + dims_size)));
    });
2062
    return api_error_result;
kahmed10's avatar
kahmed10 committed
2063
2064
2065
2066
2067
}

extern "C" migraphx_status
migraphx_tf_options_set_default_dim_value(migraphx_tf_options_t tf_options, size_t value)
{
2068
    auto api_error_result = migraphx::try_([&] {
kahmed10's avatar
kahmed10 committed
2069
2070
2071
2072
        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));
    });
2073
    return api_error_result;
kahmed10's avatar
kahmed10 committed
2074
2075
2076
2077
2078
2079
}

extern "C" migraphx_status migraphx_tf_options_set_output_names(migraphx_tf_options_t tf_options,
                                                                const char** names,
                                                                size_t names_size)
{
2080
    auto api_error_result = migraphx::try_([&] {
kahmed10's avatar
kahmed10 committed
2081
2082
2083
2084
2085
2086
2087
        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)));
    });
2088
    return api_error_result;
kahmed10's avatar
kahmed10 committed
2089
2090
2091
2092
2093
}

extern "C" migraphx_status
migraphx_parse_tf(migraphx_program_t* out, const char* name, migraphx_tf_options_t options)
{
2094
    auto api_error_result = migraphx::try_([&] {
kahmed10's avatar
kahmed10 committed
2095
2096
2097
2098
        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)));
    });
2099
    return api_error_result;
kahmed10's avatar
kahmed10 committed
2100
2101
}

Shucai Xiao's avatar
Shucai Xiao committed
2102
2103
2104
extern "C" migraphx_status
migraphx_quantize_op_names_destroy(migraphx_quantize_op_names_t quantize_op_names)
{
2105
2106
    auto api_error_result = migraphx::try_([&] { destroy((quantize_op_names)); });
    return api_error_result;
Shucai Xiao's avatar
Shucai Xiao committed
2107
2108
}

2109
2110
2111
2112
2113
2114
2115
2116
extern "C" migraphx_status
migraphx_quantize_op_names_assign_to(migraphx_quantize_op_names_t output,
                                     const_migraphx_quantize_op_names_t input)
{
    auto api_error_result = migraphx::try_([&] { *output = *input; });
    return api_error_result;
}

Shucai Xiao's avatar
Shucai Xiao committed
2117
2118
2119
extern "C" migraphx_status
migraphx_quantize_op_names_create(migraphx_quantize_op_names_t* quantize_op_names)
{
2120
    auto api_error_result = migraphx::try_([&] {
Shucai Xiao's avatar
Shucai Xiao committed
2121
2122
2123
        *quantize_op_names =
            object_cast<migraphx_quantize_op_names_t>(allocate<std::vector<std::string>>());
    });
2124
    return api_error_result;
Shucai Xiao's avatar
Shucai Xiao committed
2125
2126
2127
2128
2129
}

extern "C" migraphx_status
migraphx_quantize_op_names_add(migraphx_quantize_op_names_t quantize_op_names, const char* name)
{
2130
    auto api_error_result = migraphx::try_([&] {
Shucai Xiao's avatar
Shucai Xiao committed
2131
2132
2133
2134
2135
        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));
    });
2136
    return api_error_result;
Shucai Xiao's avatar
Shucai Xiao committed
2137
2138
2139
2140
2141
}

extern "C" migraphx_status migraphx_quantize_fp16_with_op_names(migraphx_program_t prog,
                                                                migraphx_quantize_op_names_t name)
{
2142
    auto api_error_result = migraphx::try_([&] {
Shucai Xiao's avatar
Shucai Xiao committed
2143
2144
2145
2146
2147
2148
        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));
    });
2149
    return api_error_result;
Shucai Xiao's avatar
Shucai Xiao committed
2150
2151
2152
2153
}

extern "C" migraphx_status migraphx_quantize_fp16(migraphx_program_t prog)
{
2154
    auto api_error_result = migraphx::try_([&] {
Shucai Xiao's avatar
Shucai Xiao committed
2155
2156
2157
2158
        if(prog == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter prog: Null pointer");
        migraphx::quantize_fp16((prog->object));
    });
2159
    return api_error_result;
Shucai Xiao's avatar
Shucai Xiao committed
2160
2161
2162
2163
2164
}

extern "C" migraphx_status
migraphx_quantize_int8_options_destroy(migraphx_quantize_int8_options_t quantize_int8_options)
{
2165
2166
    auto api_error_result = migraphx::try_([&] { destroy((quantize_int8_options)); });
    return api_error_result;
Shucai Xiao's avatar
Shucai Xiao committed
2167
2168
}

2169
2170
2171
2172
2173
2174
2175
2176
extern "C" migraphx_status
migraphx_quantize_int8_options_assign_to(migraphx_quantize_int8_options_t output,
                                         const_migraphx_quantize_int8_options_t input)
{
    auto api_error_result = migraphx::try_([&] { *output = *input; });
    return api_error_result;
}

Shucai Xiao's avatar
Shucai Xiao committed
2177
2178
2179
extern "C" migraphx_status
migraphx_quantize_int8_options_create(migraphx_quantize_int8_options_t* quantize_int8_options)
{
2180
    auto api_error_result = migraphx::try_([&] {
Shucai Xiao's avatar
Shucai Xiao committed
2181
2182
2183
        *quantize_int8_options = object_cast<migraphx_quantize_int8_options_t>(
            allocate<migraphx::quantize_int8_options>());
    });
2184
    return api_error_result;
Shucai Xiao's avatar
Shucai Xiao committed
2185
2186
2187
2188
2189
2190
}

extern "C" migraphx_status
migraphx_quantize_int8_options_add_op_name(migraphx_quantize_int8_options_t quantize_int8_options,
                                           const char* name)
{
2191
    auto api_error_result = migraphx::try_([&] {
Shucai Xiao's avatar
Shucai Xiao committed
2192
2193
2194
2195
2196
        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));
    });
2197
    return api_error_result;
Shucai Xiao's avatar
Shucai Xiao committed
2198
2199
2200
2201
2202
}

extern "C" migraphx_status migraphx_quantize_int8_options_add_calibration_data(
    migraphx_quantize_int8_options_t quantize_int8_options, migraphx_program_parameters_t data)
{
2203
    auto api_error_result = migraphx::try_([&] {
Shucai Xiao's avatar
Shucai Xiao committed
2204
2205
2206
2207
2208
2209
2210
        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));
    });
2211
    return api_error_result;
Shucai Xiao's avatar
Shucai Xiao committed
2212
2213
2214
2215
2216
2217
}

extern "C" migraphx_status migraphx_quantize_int8(migraphx_program_t prog,
                                                  migraphx_target_t target,
                                                  migraphx_quantize_int8_options_t options)
{
2218
    auto api_error_result = migraphx::try_([&] {
Shucai Xiao's avatar
Shucai Xiao committed
2219
2220
2221
2222
2223
2224
2225
2226
        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));
    });
2227
    return api_error_result;
Shucai Xiao's avatar
Shucai Xiao committed
2228
}
kahmed10's avatar
kahmed10 committed
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238

extern "C" migraphx_status migraphx_context_finish(const_migraphx_context_t context)
{
    auto api_error_result = migraphx::try_([&] {
        if(context == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter context: Null pointer");
        (context->object).finish();
    });
    return api_error_result;
}
2239

2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
extern "C" migraphx_status migraphx_context_get_queue(void** out, migraphx_context_t context)
{
    auto api_error_result = migraphx::try_([&] {
        if(context == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter context: Null pointer");
        *out = (context->object).get_queue().unsafe_get();
    });
    return api_error_result;
}

2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
extern "C" migraphx_status
migraphx_experimental_custom_op_destroy(migraphx_experimental_custom_op_t experimental_custom_op)
{
    auto api_error_result = migraphx::try_([&] { destroy((experimental_custom_op)); });
    return api_error_result;
}

extern "C" migraphx_status
migraphx_experimental_custom_op_assign_to(migraphx_experimental_custom_op_t output,
                                          const_migraphx_experimental_custom_op_t input)
{
    auto api_error_result = migraphx::try_([&] { *output = *input; });
    return api_error_result;
}

extern "C" migraphx_status
migraphx_experimental_custom_op_create(migraphx_experimental_custom_op_t* experimental_custom_op,
                                       void* obj,
                                       migraphx_experimental_custom_op_copy c,
                                       migraphx_experimental_custom_op_delete d,
2270
                                       const char* obj_typename,
2271
2272
2273
2274
                                       const char* name)
{
    auto api_error_result = migraphx::try_([&] {
        *experimental_custom_op =
2275
            allocate<migraphx_experimental_custom_op_t>((obj), (c), (d), (obj_typename), (name));
2276
2277
2278
2279
    });
    return api_error_result;
}

2280
2281
2282
2283
2284
2285
2286
2287
extern "C" migraphx_status
migraphx_experimental_custom_op_set_compute(migraphx_experimental_custom_op_t obj,
                                            migraphx_experimental_custom_op_compute input)
{
    auto api_error_result = migraphx::try_([&] { (obj)->compute_f = (input); });
    return api_error_result;
}

2288
2289
2290
2291
2292
2293
2294
extern "C" migraphx_status migraphx_experimental_custom_op_set_compute_shape(
    migraphx_experimental_custom_op_t obj, migraphx_experimental_custom_op_compute_shape input)
{
    auto api_error_result = migraphx::try_([&] { (obj)->compute_shape_f = (input); });
    return api_error_result;
}

2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
extern "C" migraphx_status
migraphx_experimental_custom_op_set_output_alias(migraphx_experimental_custom_op_t obj,
                                                 migraphx_experimental_custom_op_output_alias input)
{
    auto api_error_result = migraphx::try_([&] { (obj)->output_alias_f = (input); });
    return api_error_result;
}

extern "C" migraphx_status migraphx_experimental_custom_op_set_runs_on_offload_target(
    migraphx_experimental_custom_op_t obj,
    migraphx_experimental_custom_op_runs_on_offload_target input)
{
    auto api_error_result = migraphx::try_([&] { (obj)->runs_on_offload_target_f = (input); });
    return api_error_result;
}

2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
extern "C" migraphx_status
migraphx_experimental_custom_op_register(migraphx_experimental_custom_op_t experimental_custom_op)
{
    auto api_error_result = migraphx::try_([&] {
        if(experimental_custom_op == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param,
                           "Bad parameter experimental_custom_op: Null pointer");
        migraphx::register_custom_op((*experimental_custom_op));
    });
    return api_error_result;
}