api.cpp 68 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.
 */
Paul Fultz II's avatar
Paul Fultz II committed
24
25
26
27
28
#include <migraphx/migraphx.h>
#include <migraphx/rank.hpp>
#include <migraphx/shape.hpp>
#include <migraphx/program.hpp>
#include <migraphx/onnx.hpp>
kahmed10's avatar
kahmed10 committed
29
#include <migraphx/tf.hpp>
30
#include <migraphx/instruction_ref.hpp>
31
#include <migraphx/register_target.hpp>
Paul Fultz II's avatar
Paul Fultz II committed
32
#include <migraphx/generate.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
33
#include <migraphx/quantization.hpp>
34
#include <migraphx/ref/target.hpp>
35
#include <migraphx/load_save.hpp>
36
#include <migraphx/make_op.hpp>
37
#include <migraphx/register_op.hpp>
38
39
40
#include <migraphx/json.hpp>
#include <migraphx/convert_to_json.hpp>
#include <algorithm>
41
#include <cstdarg>
Paul Fultz II's avatar
Paul Fultz II committed
42
43
namespace migraphx {

44
45
46
47
48
49
50
static thread_local bool disable_exception_catch = false; // NOLINT

extern "C" void migraphx_test_private_disable_exception_catch(bool b)
{
    disable_exception_catch = b;
}

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

113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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;
}

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

132
133
134
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
135

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

138
void set_default_dim_value(onnx_options& options, size_t value)
Paul Fultz II's avatar
Paul Fultz II committed
139
{
140
141
142
    options.default_dim_value = value;
}

Shucai Xiao's avatar
Shucai Xiao committed
143
144
145
146
147
void set_default_loop_iterations(onnx_options& options, int64_t value)
{
    options.max_loop_iterations = value;
}

kahmed10's avatar
kahmed10 committed
148
149
150
151
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; }

152
153
void set_input_parameter_shape(onnx_options& options,
                               const char* name,
154
                               std::vector<std::size_t> dims)
155
{
156
    options.map_input_dims[std::string(name)] = std::move(dims);
Paul Fultz II's avatar
Paul Fultz II committed
157
158
}

kahmed10's avatar
kahmed10 committed
159
160
161
162
163
164
165
166
167
168
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());
}

Paul Fultz II's avatar
Paul Fultz II committed
169
170
171
172
173
174
175
176
177
template <class Value>
std::vector<const char*> get_names(const std::unordered_map<std::string, Value>& m)
{
    std::vector<const char*> result;
    std::transform(
        m.begin(), m.end(), std::back_inserter(result), [](auto&& p) { return p.first.c_str(); });
    return result;
}

Shucai Xiao's avatar
Shucai Xiao committed
178
179
180
181
182
183
184
185
186
187
188
189
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
{
190
191
    std::vector<parameter_map> calibration = {};
    std::vector<std::string> op_names      = {};
Shucai Xiao's avatar
Shucai Xiao committed
192
193
194
195
196
197
198
};

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

199
void add_calibration_data(quantize_int8_options& options, parameter_map& data)
Shucai Xiao's avatar
Shucai Xiao committed
200
201
202
203
204
205
206
207
208
209
210
211
212
213
{
    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);
}

214
215
216
217
218
219
#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)
220
{
221
222
223
    std::string sattributes = attributes == nullptr ? "" : attributes;
    std::vector<char> buffer(sattributes.size() * 2);
    std::vsnprintf(buffer.data(), buffer.size(), sattributes.c_str(), vlist);
224
225
226
    value v = value::object{};
    if(attributes != nullptr)
    {
227
        v = from_json_string(convert_to_json(std::string(buffer.data())));
228
229
230
231
232
233
    }
    auto op = make_op(name, v);

    return op;
}

234
235
236
237
#ifdef __clang__
#pragma clang diagnostic pop
#endif

Paul Fultz II's avatar
Paul Fultz II committed
238
239
240
241
242
243
template <class T>
bool equal(const T& x, const T& y)
{
    return x == y;
}

244
std::vector<argument> run(program& p, const parameter_map& params) { return p.eval(params); }
245
246
247
248
249
std::vector<argument>
run_async(program& p, const parameter_map& params, const execution_environment& exec_env)
{
    return p.eval(params, exec_env);
}
Paul Fultz II's avatar
Paul Fultz II committed
250

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

Shucai Xiao's avatar
Shucai Xiao committed
253
254
255
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
256

257
258
259
260
261
migraphx::instruction_ref add_allocation(module& m, const migraphx::shape& s)
{
    return m.add_instruction(migraphx::make_op("allocate", {{"shape", migraphx::to_value(s)}}), {});
}

262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
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
{
    template <class Self, class F>
    static auto reflect(Self&, F)
    {
        return pack();
    }
    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));
    }

286
287
288
289
290
291
    // 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));
    }
292
293
294
295
296
297
298
299
};

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

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

Paul Fultz II's avatar
Paul Fultz II committed
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
} // namespace migraphx

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

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

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

327
328
329
330
331
332
333
334
// TODO: Move to interface preamble
template <class C, class D>
struct manage_generic_ptr
{
    manage_generic_ptr() = default;

    manage_generic_ptr(std::nullptr_t) {}

335
336
    manage_generic_ptr(void* pdata, const char* obj_tname, C pcopier, D pdeleter)
        : data(nullptr), obj_typename(obj_tname), copier(pcopier), deleter(pdeleter)
337
338
339
340
341
    {
        copier(&data, pdata);
    }

    manage_generic_ptr(const manage_generic_ptr& rhs)
342
        : data(nullptr), obj_typename(rhs.obj_typename), copier(rhs.copier), deleter(rhs.deleter)
343
344
345
346
347
348
    {
        if(copier)
            copier(&data, rhs.data);
    }

    manage_generic_ptr(manage_generic_ptr&& other) noexcept
349
350
351
352
        : data(other.data),
          obj_typename(other.obj_typename),
          copier(other.copier),
          deleter(other.deleter)
353
    {
354
355
356
357
        other.data         = nullptr;
        other.obj_typename = "";
        other.copier       = nullptr;
        other.deleter      = nullptr;
358
359
360
361
362
    }

    manage_generic_ptr& operator=(manage_generic_ptr rhs)
    {
        std::swap(data, rhs.data);
363
        std::swap(obj_typename, rhs.obj_typename);
364
365
366
367
368
369
370
371
372
373
374
        std::swap(copier, rhs.copier);
        std::swap(deleter, rhs.deleter);
        return *this;
    }

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

375
376
377
378
    void* data               = nullptr;
    const char* obj_typename = "";
    C copier                 = nullptr;
    D deleter                = nullptr;
379
};
Paul Fultz II's avatar
Paul Fultz II committed
380
381
382
383
384

extern "C" struct migraphx_shape;
struct migraphx_shape
{
    template <class... Ts>
385
386
    migraphx_shape(Ts&&... xs)
        : object(std::forward<Ts>(xs)...) // NOLINT(readability-redundant-member-init)
Paul Fultz II's avatar
Paul Fultz II committed
387
388
389
390
391
392
393
394
395
    {
    }
    migraphx::shape object;
};

extern "C" struct migraphx_argument;
struct migraphx_argument
{
    template <class... Ts>
396
397
    migraphx_argument(Ts&&... xs)
        : object(std::forward<Ts>(xs)...) // NOLINT(readability-redundant-member-init)
Paul Fultz II's avatar
Paul Fultz II committed
398
399
400
401
402
403
404
405
406
    {
    }
    migraphx::argument object;
};

extern "C" struct migraphx_target;
struct migraphx_target
{
    template <class... Ts>
407
408
    migraphx_target(Ts&&... xs)
        : object(std::forward<Ts>(xs)...) // NOLINT(readability-redundant-member-init)
Paul Fultz II's avatar
Paul Fultz II committed
409
410
411
412
413
414
415
416
417
    {
    }
    migraphx::target object;
};

extern "C" struct migraphx_program_parameter_shapes;
struct migraphx_program_parameter_shapes
{
    template <class... Ts>
418
419
    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
420
421
422
423
424
425
426
427
428
    {
    }
    std::unordered_map<std::string, migraphx::shape> object;
};

extern "C" struct migraphx_program_parameters;
struct migraphx_program_parameters
{
    template <class... Ts>
429
430
    migraphx_program_parameters(Ts&&... xs)
        : object(std::forward<Ts>(xs)...) // NOLINT(readability-redundant-member-init)
Paul Fultz II's avatar
Paul Fultz II committed
431
432
433
434
435
436
437
438
439
    {
    }
    std::unordered_map<std::string, migraphx::argument> object;
};

extern "C" struct migraphx_arguments;
struct migraphx_arguments
{
    template <class... Ts>
440
441
    migraphx_arguments(Ts&&... xs)
        : object(std::forward<Ts>(xs)...) // NOLINT(readability-redundant-member-init)
Paul Fultz II's avatar
Paul Fultz II committed
442
443
444
445
446
447
448
449
450
    {
    }
    std::vector<migraphx::argument> object;
};

extern "C" struct migraphx_shapes;
struct migraphx_shapes
{
    template <class... Ts>
451
452
    migraphx_shapes(Ts&&... xs)
        : object(std::forward<Ts>(xs)...) // NOLINT(readability-redundant-member-init)
Paul Fultz II's avatar
Paul Fultz II committed
453
454
455
456
457
    {
    }
    std::vector<migraphx::shape> object;
};

458
459
460
461
extern "C" struct migraphx_instruction;
struct migraphx_instruction
{
    template <class... Ts>
462
463
    migraphx_instruction(Ts&&... xs)
        : object(std::forward<Ts>(xs)...) // NOLINT(readability-redundant-member-init)
464
465
466
467
468
469
470
471
472
    {
    }
    migraphx::instruction_ref object;
};

extern "C" struct migraphx_instructions;
struct migraphx_instructions
{
    template <class... Ts>
473
474
    migraphx_instructions(Ts&&... xs)
        : object(std::forward<Ts>(xs)...) // NOLINT(readability-redundant-member-init)
475
476
477
478
479
480
481
482
483
    {
    }
    std::vector<migraphx::instruction_ref> object;
};

extern "C" struct migraphx_modules;
struct migraphx_modules
{
    template <class... Ts>
484
485
    migraphx_modules(Ts&&... xs)
        : object(std::forward<Ts>(xs)...) // NOLINT(readability-redundant-member-init)
486
487
488
489
490
    {
    }
    std::vector<migraphx::module*> object;
};

Shucai Xiao's avatar
Shucai Xiao committed
491
492
493
494
extern "C" struct migraphx_module;
struct migraphx_module
{
    template <class... Ts>
495
496
    migraphx_module(Ts&&... xs)
        : object(std::forward<Ts>(xs)...) // NOLINT(readability-redundant-member-init)
Shucai Xiao's avatar
Shucai Xiao committed
497
498
499
500
501
    {
    }
    migraphx::module object;
};

Paul Fultz II's avatar
Paul Fultz II committed
502
503
504
505
extern "C" struct migraphx_program;
struct migraphx_program
{
    template <class... Ts>
506
507
    migraphx_program(Ts&&... xs)
        : object(std::forward<Ts>(xs)...) // NOLINT(readability-redundant-member-init)
Paul Fultz II's avatar
Paul Fultz II committed
508
509
510
511
512
    {
    }
    migraphx::program object;
};

513
514
515
516
extern "C" struct migraphx_operation;
struct migraphx_operation
{
    template <class... Ts>
517
518
    migraphx_operation(Ts&&... xs)
        : object(std::forward<Ts>(xs)...) // NOLINT(readability-redundant-member-init)
519
520
521
522
523
    {
    }
    migraphx::operation object;
};

524
525
526
527
extern "C" struct migraphx_onnx_options;
struct migraphx_onnx_options
{
    template <class... Ts>
528
529
    migraphx_onnx_options(Ts&&... xs)
        : object(std::forward<Ts>(xs)...) // NOLINT(readability-redundant-member-init)
530
531
532
533
534
    {
    }
    migraphx::onnx_options object;
};

535
536
537
538
extern "C" struct migraphx_file_options;
struct migraphx_file_options
{
    template <class... Ts>
539
540
    migraphx_file_options(Ts&&... xs)
        : object(std::forward<Ts>(xs)...) // NOLINT(readability-redundant-member-init)
541
542
543
544
545
    {
    }
    migraphx::file_options object;
};

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

kahmed10's avatar
kahmed10 committed
557
558
559
560
extern "C" struct migraphx_tf_options;
struct migraphx_tf_options
{
    template <class... Ts>
561
562
    migraphx_tf_options(Ts&&... xs)
        : object(std::forward<Ts>(xs)...) // NOLINT(readability-redundant-member-init)
kahmed10's avatar
kahmed10 committed
563
564
565
566
567
    {
    }
    migraphx::tf_options object;
};

Shucai Xiao's avatar
Shucai Xiao committed
568
569
570
571
extern "C" struct migraphx_quantize_op_names;
struct migraphx_quantize_op_names
{
    template <class... Ts>
572
573
    migraphx_quantize_op_names(Ts&&... xs)
        : object(std::forward<Ts>(xs)...) // NOLINT(readability-redundant-member-init)
Shucai Xiao's avatar
Shucai Xiao committed
574
575
576
577
578
579
580
581
582
    {
    }
    std::vector<std::string> object;
};

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

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

601
602
603
604
605
606
607
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,
608
                                    const char* obj_typename,
609
                                    Ts&&... xs)
610
        : object_ptr(p, obj_typename, c, d), xobject(std::forward<Ts>(xs)...)
611
612
613
614
615
    {
    }
    manage_generic_ptr<migraphx_experimental_custom_op_copy, migraphx_experimental_custom_op_delete>
        object_ptr = nullptr;
    migraphx::experimental_custom_op xobject;
616
617
618
619
620
621
622
623
    migraphx_experimental_custom_op_compute compute_f = nullptr;
    migraphx::argument compute(migraphx::context ctx,
                               migraphx::shape output,
                               std::vector<migraphx::argument> inputs) const
    {
        std::remove_pointer_t<migraphx_argument_t> out;
        if(compute_f == nullptr)
            throw std::runtime_error("compute function is missing.");
624
625
        std::array<char, 256> exception_msg;
        exception_msg.front() = '\0';
626
627
        auto api_error_result = compute_f(&out,
                                          object_ptr.data,
628
629
                                          exception_msg.data(),
                                          exception_msg.size(),
630
631
632
633
                                          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)
634
635
636
637
638
        {
            const std::string exception_str(exception_msg.data());
            throw std::runtime_error("Error in compute of: " +
                                     std::string(object_ptr.obj_typename) + ": " + exception_str);
        }
639
640
641
        return (&out)->object;
    }

642
643
644
645
646
647
    migraphx_experimental_custom_op_compute_shape compute_shape_f = nullptr;
    migraphx::shape compute_shape(std::vector<migraphx::shape> inputs) const
    {
        std::remove_pointer_t<migraphx_shape_t> out;
        if(compute_shape_f == nullptr)
            throw std::runtime_error("compute_shape function is missing.");
648
649
650
651
652
653
654
        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)));
655
        if(api_error_result != migraphx_status_success)
656
657
658
659
660
        {
            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);
        }
661
662
663
664
        return (&out)->object;
    }
};

Paul Fultz II's avatar
Paul Fultz II committed
665
666
extern "C" migraphx_status migraphx_shape_destroy(migraphx_shape_t shape)
{
667
668
    auto api_error_result = migraphx::try_([&] { destroy((shape)); });
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
669
670
}

671
672
673
674
675
676
677
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
678
679
680
681
682
extern "C" migraphx_status migraphx_shape_create(migraphx_shape_t* shape,
                                                 migraphx_shape_datatype_t type,
                                                 size_t* lengths,
                                                 size_t lengths_size)
{
683
    auto api_error_result = migraphx::try_([&] {
684
        if(lengths == nullptr and lengths_size != 0)
Paul Fultz II's avatar
Paul Fultz II committed
685
686
687
688
689
            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))));
    });
690
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
691
692
}

693
694
695
696
697
698
699
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)
{
700
    auto api_error_result = migraphx::try_([&] {
701
702
703
704
705
706
707
708
709
        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))));
    });
710
    return api_error_result;
711
712
}

713
714
715
extern "C" migraphx_status migraphx_shape_create_scalar(migraphx_shape_t* shape,
                                                        migraphx_shape_datatype_t type)
{
716
    auto api_error_result = migraphx::try_([&] {
717
718
719
        *shape = object_cast<migraphx_shape_t>(
            allocate<migraphx::shape>((migraphx::to_shape_type(type))));
    });
720
    return api_error_result;
721
722
}

Paul Fultz II's avatar
Paul Fultz II committed
723
724
725
extern "C" migraphx_status
migraphx_shape_lengths(const size_t** out, size_t* out_size, const_migraphx_shape_t shape)
{
726
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
727
728
729
730
731
732
733
734
        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();
    });
735
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
736
737
738
739
740
}

extern "C" migraphx_status
migraphx_shape_strides(const size_t** out, size_t* out_size, const_migraphx_shape_t shape)
{
741
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
742
743
744
745
746
747
748
749
        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();
    });
750
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
751
752
753
754
755
}

extern "C" migraphx_status migraphx_shape_type(migraphx_shape_datatype_t* out,
                                               const_migraphx_shape_t shape)
{
756
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
757
758
759
760
761
762
        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());
    });
763
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
764
765
766
767
}

extern "C" migraphx_status migraphx_shape_bytes(size_t* out, const_migraphx_shape_t shape)
{
768
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
769
770
771
772
        if(shape == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter shape: Null pointer");
        *out = (shape->object).bytes();
    });
773
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
774
775
776
777
778
}

extern "C" migraphx_status
migraphx_shape_equal(bool* out, const_migraphx_shape_t shape, const_migraphx_shape_t x)
{
779
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
780
781
782
783
784
785
        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));
    });
786
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
787
788
}

789
790
791
792
793
794
795
796
797
798
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;
}

Paul Fultz II's avatar
Paul Fultz II committed
799
800
extern "C" migraphx_status migraphx_argument_destroy(migraphx_argument_t argument)
{
801
802
    auto api_error_result = migraphx::try_([&] { destroy((argument)); });
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
803
804
}

805
806
807
808
809
810
811
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
812
813
814
extern "C" migraphx_status
migraphx_argument_create(migraphx_argument_t* argument, const_migraphx_shape_t shape, void* buffer)
{
815
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
816
817
818
819
820
        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)));
    });
821
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
822
823
824
825
826
}

extern "C" migraphx_status migraphx_argument_shape(const_migraphx_shape_t* out,
                                                   const_migraphx_argument_t argument)
{
827
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
828
829
830
831
        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()));
    });
832
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
833
834
835
836
}

extern "C" migraphx_status migraphx_argument_buffer(char** out, const_migraphx_argument_t argument)
{
837
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
838
839
840
841
        if(argument == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter argument: Null pointer");
        *out = (argument->object).data();
    });
842
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
843
844
845
846
847
}

extern "C" migraphx_status
migraphx_argument_equal(bool* out, const_migraphx_argument_t argument, const_migraphx_argument_t x)
{
848
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
849
850
851
852
853
854
        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));
    });
855
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
856
857
858
859
860
}

extern "C" migraphx_status
migraphx_argument_generate(migraphx_argument_t* out, const_migraphx_shape_t s, size_t seed)
{
861
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
862
863
864
865
        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)));
    });
866
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
867
868
869
870
}

extern "C" migraphx_status migraphx_target_destroy(migraphx_target_t target)
{
871
872
    auto api_error_result = migraphx::try_([&] { destroy((target)); });
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
873
874
}

875
876
877
878
879
880
881
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
882
883
extern "C" migraphx_status migraphx_target_create(migraphx_target_t* target, const char* name)
{
884
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
885
886
887
        *target = object_cast<migraphx_target_t>(
            allocate<migraphx::target>(migraphx::get_target((name))));
    });
888
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
889
890
891
892
893
}

extern "C" migraphx_status migraphx_program_parameter_shapes_destroy(
    migraphx_program_parameter_shapes_t program_parameter_shapes)
{
894
895
    auto api_error_result = migraphx::try_([&] { destroy((program_parameter_shapes)); });
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
896
897
}

898
899
900
901
902
903
904
905
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
906
907
908
909
extern "C" migraphx_status
migraphx_program_parameter_shapes_size(size_t* out,
                                       migraphx_program_parameter_shapes_t program_parameter_shapes)
{
910
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
911
912
913
914
915
        if(program_parameter_shapes == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param,
                           "Bad parameter program_parameter_shapes: Null pointer");
        *out = (program_parameter_shapes->object).size();
    });
916
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
917
918
919
920
921
922
923
}

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)
{
924
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
925
926
927
928
929
930
        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))));
    });
931
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
932
933
934
935
936
}

extern "C" migraphx_status migraphx_program_parameter_shapes_names(
    const char** out, migraphx_program_parameter_shapes_t program_parameter_shapes)
{
937
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
938
939
940
941
942
943
944
945
        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);
    });
946
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
947
948
949
950
951
}

extern "C" migraphx_status
migraphx_program_parameters_destroy(migraphx_program_parameters_t program_parameters)
{
952
953
    auto api_error_result = migraphx::try_([&] { destroy((program_parameters)); });
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
954
955
}

956
957
958
959
960
961
962
963
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
964
965
966
extern "C" migraphx_status
migraphx_program_parameters_create(migraphx_program_parameters_t* program_parameters)
{
967
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
968
969
970
        *program_parameters = object_cast<migraphx_program_parameters_t>(
            allocate<std::unordered_map<std::string, migraphx::argument>>());
    });
971
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
972
973
974
975
976
977
978
}

extern "C" migraphx_status
migraphx_program_parameters_add(migraphx_program_parameters_t program_parameters,
                                const char* name,
                                const_migraphx_argument_t argument)
{
979
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
980
981
982
983
984
985
986
        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);
    });
987
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
988
989
990
991
}

extern "C" migraphx_status migraphx_arguments_destroy(migraphx_arguments_t arguments)
{
992
993
    auto api_error_result = migraphx::try_([&] { destroy((arguments)); });
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
994
995
}

996
997
998
999
1000
1001
1002
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
1003
1004
extern "C" migraphx_status migraphx_arguments_size(size_t* out, migraphx_arguments_t arguments)
{
1005
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
1006
1007
1008
1009
        if(arguments == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter arguments: Null pointer");
        *out = (arguments->object).size();
    });
1010
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1011
1012
1013
1014
1015
}

extern "C" migraphx_status
migraphx_arguments_get(const_migraphx_argument_t* out, migraphx_arguments_t arguments, size_t idx)
{
1016
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
1017
1018
1019
1020
        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))));
    });
1021
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1022
1023
1024
1025
}

extern "C" migraphx_status migraphx_shapes_destroy(migraphx_shapes_t shapes)
{
1026
1027
    auto api_error_result = migraphx::try_([&] { destroy((shapes)); });
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1028
1029
}

1030
1031
1032
1033
1034
1035
1036
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
1037
1038
extern "C" migraphx_status migraphx_shapes_size(size_t* out, migraphx_shapes_t shapes)
{
1039
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
1040
1041
1042
1043
        if(shapes == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter shapes: Null pointer");
        *out = (shapes->object).size();
    });
1044
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1045
1046
1047
1048
1049
}

extern "C" migraphx_status
migraphx_shapes_get(const_migraphx_shape_t* out, migraphx_shapes_t shapes, size_t idx)
{
1050
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
1051
1052
1053
1054
        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))));
    });
1055
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1056
1057
}

1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
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,
                                                        const_migraphx_instruction_t* ptr,
                                                        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
1129
1130
extern "C" migraphx_status migraphx_module_print(const_migraphx_module_t module)
{
1131
    auto api_error_result = migraphx::try_([&] {
Shucai Xiao's avatar
Shucai Xiao committed
1132
1133
1134
1135
        if(module == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter module: Null pointer");
        migraphx::print_module((module->object));
    });
1136
    return api_error_result;
Shucai Xiao's avatar
Shucai Xiao committed
1137
1138
}

1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
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;
}

1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
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;
}

1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
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;
}

1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
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
1240
1241
extern "C" migraphx_status migraphx_program_destroy(migraphx_program_t program)
{
1242
1243
    auto api_error_result = migraphx::try_([&] { destroy((program)); });
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1244
1245
}

1246
1247
1248
1249
1250
1251
1252
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;
}

1253
1254
1255
1256
1257
1258
1259
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
1260
1261
1262
extern "C" migraphx_status migraphx_program_get_main_module(migraphx_module_t* out,
                                                            migraphx_program_t program)
{
1263
    auto api_error_result = migraphx::try_([&] {
Shucai Xiao's avatar
Shucai Xiao committed
1264
1265
1266
1267
        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());
    });
1268
    return api_error_result;
Shucai Xiao's avatar
Shucai Xiao committed
1269
1270
}

1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
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
1282
1283
extern "C" migraphx_status migraphx_program_compile(migraphx_program_t program,
                                                    migraphx_target_t target,
1284
                                                    migraphx_compile_options_t options)
Paul Fultz II's avatar
Paul Fultz II committed
1285
{
1286
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
1287
1288
1289
1290
        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");
1291
1292
1293
        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
1294
    });
1295
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1296
1297
1298
1299
1300
1301
}

extern "C" migraphx_status
migraphx_program_get_parameter_shapes(migraphx_program_parameter_shapes_t* out,
                                      migraphx_program_t program)
{
1302
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
1303
1304
1305
1306
1307
        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());
    });
1308
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1309
1310
1311
1312
1313
}

extern "C" migraphx_status migraphx_program_get_output_shapes(migraphx_shapes_t* out,
                                                              migraphx_program_t program)
{
1314
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
1315
1316
1317
1318
        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)));
    });
1319
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1320
1321
1322
1323
}

extern "C" migraphx_status migraphx_program_print(const_migraphx_program_t program)
{
1324
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
1325
1326
        if(program == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter program: Null pointer");
Shucai Xiao's avatar
Shucai Xiao committed
1327
        migraphx::print_program((program->object));
Paul Fultz II's avatar
Paul Fultz II committed
1328
    });
1329
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1330
1331
}

1332
1333
extern "C" migraphx_status migraphx_program_sort(migraphx_program_t program)
{
1334
    auto api_error_result = migraphx::try_([&] {
1335
1336
1337
1338
        if(program == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter program: Null pointer");
        (program->object).sort();
    });
1339
    return api_error_result;
1340
1341
}

Paul Fultz II's avatar
Paul Fultz II committed
1342
1343
1344
1345
extern "C" migraphx_status migraphx_program_run(migraphx_arguments_t* out,
                                                migraphx_program_t program,
                                                migraphx_program_parameters_t params)
{
1346
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
1347
1348
1349
1350
1351
1352
        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)));
    });
1353
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1354
1355
1356
1357
1358
}

extern "C" migraphx_status
migraphx_program_equal(bool* out, const_migraphx_program_t program, const_migraphx_program_t x)
{
1359
    auto api_error_result = migraphx::try_([&] {
Paul Fultz II's avatar
Paul Fultz II committed
1360
1361
1362
1363
1364
1365
        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));
    });
1366
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1367
1368
}

1369
1370
extern "C" migraphx_status
migraphx_program_experimental_get_context(migraphx_context_t* out, const_migraphx_program_t program)
kahmed10's avatar
kahmed10 committed
1371
1372
1373
1374
1375
1376
1377
1378
1379
{
    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;
}

1380
1381
extern "C" migraphx_status migraphx_operation_destroy(migraphx_operation_t operation)
{
1382
1383
    auto api_error_result = migraphx::try_([&] { destroy((operation)); });
    return api_error_result;
1384
1385
}

1386
1387
1388
1389
1390
1391
1392
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;
}

1393
1394
1395
1396
extern "C" migraphx_status migraphx_operation_create(migraphx_operation_t* operation,
                                                     const char* name,
                                                     const char* attributes,
                                                     ...)
1397
{
1398
1399
1400
    va_list vlist;
    va_start(vlist, attributes);
    auto api_error_result = migraphx::try_([&] {
1401
        *operation = object_cast<migraphx_operation_t>(
1402
            allocate<migraphx::operation>(migraphx::create_op((name), (attributes), (vlist))));
1403
    });
1404
1405
    va_end(vlist);
    return api_error_result;
1406
1407
1408
1409
1410
}

extern "C" migraphx_status
migraphx_operation_name(char* out, size_t out_size, migraphx_operation_t operation)
{
1411
    auto api_error_result = migraphx::try_([&] {
1412
1413
1414
1415
1416
1417
1418
1419
        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';
    });
1420
    return api_error_result;
1421
1422
}

1423
extern "C" migraphx_status
1424
migraphx_load(migraphx_program_t* out, const char* name, migraphx_file_options_t options)
1425
{
1426
    auto api_error_result = migraphx::try_([&] {
1427
1428
1429
        if(options == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter options: Null pointer");
        *out = allocate<migraphx_program_t>(migraphx::load((name), (options->object)));
1430
    });
1431
    return api_error_result;
1432
1433
1434
}

extern "C" migraphx_status
1435
migraphx_save(migraphx_program_t p, const char* name, migraphx_file_options_t options)
1436
{
1437
    auto api_error_result = migraphx::try_([&] {
1438
1439
        if(p == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter p: Null pointer");
1440
1441
1442
        if(options == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter options: Null pointer");
        migraphx::save((p->object), (name), (options->object));
1443
    });
1444
    return api_error_result;
1445
1446
}

1447
1448
extern "C" migraphx_status migraphx_onnx_options_destroy(migraphx_onnx_options_t onnx_options)
{
1449
1450
    auto api_error_result = migraphx::try_([&] { destroy((onnx_options)); });
    return api_error_result;
1451
1452
}

1453
1454
1455
1456
1457
1458
1459
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;
}

1460
1461
extern "C" migraphx_status migraphx_onnx_options_create(migraphx_onnx_options_t* onnx_options)
{
1462
    auto api_error_result = migraphx::try_([&] {
1463
1464
        *onnx_options = object_cast<migraphx_onnx_options_t>(allocate<migraphx::onnx_options>());
    });
1465
    return api_error_result;
1466
1467
}

1468
1469
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)
1470
{
1471
    auto api_error_result = migraphx::try_([&] {
1472
1473
        if(onnx_options == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter onnx_options: Null pointer");
1474
1475
1476
1477
        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)));
1478
    });
1479
    return api_error_result;
1480
1481
1482
1483
1484
}

extern "C" migraphx_status
migraphx_onnx_options_set_default_dim_value(migraphx_onnx_options_t onnx_options, size_t value)
{
1485
    auto api_error_result = migraphx::try_([&] {
1486
1487
1488
1489
        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));
    });
1490
    return api_error_result;
1491
1492
}

1493
1494
1495
1496
extern "C" migraphx_status
migraphx_onnx_options_set_default_loop_iterations(migraphx_onnx_options_t onnx_options,
                                                  int64_t value)
{
1497
    auto api_error_result = migraphx::try_([&] {
1498
1499
1500
1501
        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));
    });
1502
    return api_error_result;
1503
1504
}

1505
1506
extern "C" migraphx_status migraphx_file_options_destroy(migraphx_file_options_t file_options)
{
1507
1508
    auto api_error_result = migraphx::try_([&] { destroy((file_options)); });
    return api_error_result;
1509
1510
}

1511
1512
1513
1514
1515
1516
1517
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;
}

1518
1519
extern "C" migraphx_status migraphx_file_options_create(migraphx_file_options_t* file_options)
{
1520
    auto api_error_result = migraphx::try_([&] {
1521
1522
        *file_options = object_cast<migraphx_file_options_t>(allocate<migraphx::file_options>());
    });
1523
    return api_error_result;
1524
1525
1526
1527
1528
}

extern "C" migraphx_status
migraphx_file_options_set_file_format(migraphx_file_options_t file_options, const char* format)
{
1529
    auto api_error_result = migraphx::try_([&] {
1530
1531
1532
1533
        if(file_options == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter file_options: Null pointer");
        migraphx::set_file_format((file_options->object), (format));
    });
1534
    return api_error_result;
1535
1536
}

Shucai Xiao's avatar
Shucai Xiao committed
1537
extern "C" migraphx_status
1538
1539
migraphx_compile_options_destroy(migraphx_compile_options_t compile_options)
{
1540
1541
    auto api_error_result = migraphx::try_([&] { destroy((compile_options)); });
    return api_error_result;
1542
1543
}

1544
1545
1546
1547
1548
1549
1550
1551
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;
}

1552
1553
extern "C" migraphx_status
migraphx_compile_options_create(migraphx_compile_options_t* compile_options)
Shucai Xiao's avatar
Shucai Xiao committed
1554
{
1555
    auto api_error_result = migraphx::try_([&] {
1556
1557
1558
        *compile_options =
            object_cast<migraphx_compile_options_t>(allocate<migraphx::compile_options>());
    });
1559
    return api_error_result;
1560
1561
1562
1563
1564
}

extern "C" migraphx_status
migraphx_compile_options_set_offload_copy(migraphx_compile_options_t compile_options, bool value)
{
1565
    auto api_error_result = migraphx::try_([&] {
1566
1567
1568
1569
1570
        if(compile_options == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param,
                           "Bad parameter compile_options: Null pointer");
        migraphx::set_offload_copy((compile_options->object), (value));
    });
1571
    return api_error_result;
1572
1573
1574
1575
1576
}

extern "C" migraphx_status
migraphx_compile_options_set_fast_math(migraphx_compile_options_t compile_options, bool value)
{
1577
    auto api_error_result = migraphx::try_([&] {
1578
1579
1580
1581
        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
1582
    });
1583
    return api_error_result;
Shucai Xiao's avatar
Shucai Xiao committed
1584
1585
}

Paul Fultz II's avatar
Paul Fultz II committed
1586
extern "C" migraphx_status
1587
migraphx_parse_onnx(migraphx_program_t* out, const char* name, migraphx_onnx_options_t options)
Paul Fultz II's avatar
Paul Fultz II committed
1588
{
1589
    auto api_error_result = migraphx::try_([&] {
1590
1591
1592
        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
1593
    });
1594
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1595
1596
1597
1598
1599
}

extern "C" migraphx_status migraphx_parse_onnx_buffer(migraphx_program_t* out,
                                                      const void* data,
                                                      size_t size,
1600
                                                      migraphx_onnx_options_t options)
Paul Fultz II's avatar
Paul Fultz II committed
1601
{
1602
    auto api_error_result = migraphx::try_([&] {
1603
1604
1605
1606
        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
1607
    });
1608
    return api_error_result;
Paul Fultz II's avatar
Paul Fultz II committed
1609
}
Shucai Xiao's avatar
Shucai Xiao committed
1610

kahmed10's avatar
kahmed10 committed
1611
1612
extern "C" migraphx_status migraphx_tf_options_destroy(migraphx_tf_options_t tf_options)
{
1613
1614
    auto api_error_result = migraphx::try_([&] { destroy((tf_options)); });
    return api_error_result;
kahmed10's avatar
kahmed10 committed
1615
1616
}

1617
1618
1619
1620
1621
1622
1623
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
1624
1625
extern "C" migraphx_status migraphx_tf_options_create(migraphx_tf_options_t* tf_options)
{
1626
    auto api_error_result = migraphx::try_([&] {
kahmed10's avatar
kahmed10 committed
1627
1628
        *tf_options = object_cast<migraphx_tf_options_t>(allocate<migraphx::tf_options>());
    });
1629
    return api_error_result;
kahmed10's avatar
kahmed10 committed
1630
1631
1632
1633
1634
}

extern "C" migraphx_status migraphx_tf_options_set_nhwc(migraphx_tf_options_t tf_options,
                                                        bool is_nhwc)
{
1635
    auto api_error_result = migraphx::try_([&] {
kahmed10's avatar
kahmed10 committed
1636
1637
1638
1639
        if(tf_options == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter tf_options: Null pointer");
        migraphx::set_nhwc((tf_options->object), (is_nhwc));
    });
1640
    return api_error_result;
kahmed10's avatar
kahmed10 committed
1641
1642
1643
1644
1645
}

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)
{
1646
    auto api_error_result = migraphx::try_([&] {
kahmed10's avatar
kahmed10 committed
1647
1648
1649
1650
1651
1652
1653
        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)));
    });
1654
    return api_error_result;
kahmed10's avatar
kahmed10 committed
1655
1656
1657
1658
1659
}

extern "C" migraphx_status
migraphx_tf_options_set_default_dim_value(migraphx_tf_options_t tf_options, size_t value)
{
1660
    auto api_error_result = migraphx::try_([&] {
kahmed10's avatar
kahmed10 committed
1661
1662
1663
1664
        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));
    });
1665
    return api_error_result;
kahmed10's avatar
kahmed10 committed
1666
1667
1668
1669
1670
1671
}

extern "C" migraphx_status migraphx_tf_options_set_output_names(migraphx_tf_options_t tf_options,
                                                                const char** names,
                                                                size_t names_size)
{
1672
    auto api_error_result = migraphx::try_([&] {
kahmed10's avatar
kahmed10 committed
1673
1674
1675
1676
1677
1678
1679
        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)));
    });
1680
    return api_error_result;
kahmed10's avatar
kahmed10 committed
1681
1682
1683
1684
1685
}

extern "C" migraphx_status
migraphx_parse_tf(migraphx_program_t* out, const char* name, migraphx_tf_options_t options)
{
1686
    auto api_error_result = migraphx::try_([&] {
kahmed10's avatar
kahmed10 committed
1687
1688
1689
1690
        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)));
    });
1691
    return api_error_result;
kahmed10's avatar
kahmed10 committed
1692
1693
}

Shucai Xiao's avatar
Shucai Xiao committed
1694
1695
1696
extern "C" migraphx_status
migraphx_quantize_op_names_destroy(migraphx_quantize_op_names_t quantize_op_names)
{
1697
1698
    auto api_error_result = migraphx::try_([&] { destroy((quantize_op_names)); });
    return api_error_result;
Shucai Xiao's avatar
Shucai Xiao committed
1699
1700
}

1701
1702
1703
1704
1705
1706
1707
1708
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
1709
1710
1711
extern "C" migraphx_status
migraphx_quantize_op_names_create(migraphx_quantize_op_names_t* quantize_op_names)
{
1712
    auto api_error_result = migraphx::try_([&] {
Shucai Xiao's avatar
Shucai Xiao committed
1713
1714
1715
        *quantize_op_names =
            object_cast<migraphx_quantize_op_names_t>(allocate<std::vector<std::string>>());
    });
1716
    return api_error_result;
Shucai Xiao's avatar
Shucai Xiao committed
1717
1718
1719
1720
1721
}

extern "C" migraphx_status
migraphx_quantize_op_names_add(migraphx_quantize_op_names_t quantize_op_names, const char* name)
{
1722
    auto api_error_result = migraphx::try_([&] {
Shucai Xiao's avatar
Shucai Xiao committed
1723
1724
1725
1726
1727
        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));
    });
1728
    return api_error_result;
Shucai Xiao's avatar
Shucai Xiao committed
1729
1730
1731
1732
1733
}

extern "C" migraphx_status migraphx_quantize_fp16_with_op_names(migraphx_program_t prog,
                                                                migraphx_quantize_op_names_t name)
{
1734
    auto api_error_result = migraphx::try_([&] {
Shucai Xiao's avatar
Shucai Xiao committed
1735
1736
1737
1738
1739
1740
        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));
    });
1741
    return api_error_result;
Shucai Xiao's avatar
Shucai Xiao committed
1742
1743
1744
1745
}

extern "C" migraphx_status migraphx_quantize_fp16(migraphx_program_t prog)
{
1746
    auto api_error_result = migraphx::try_([&] {
Shucai Xiao's avatar
Shucai Xiao committed
1747
1748
1749
1750
        if(prog == nullptr)
            MIGRAPHX_THROW(migraphx_status_bad_param, "Bad parameter prog: Null pointer");
        migraphx::quantize_fp16((prog->object));
    });
1751
    return api_error_result;
Shucai Xiao's avatar
Shucai Xiao committed
1752
1753
1754
1755
1756
}

extern "C" migraphx_status
migraphx_quantize_int8_options_destroy(migraphx_quantize_int8_options_t quantize_int8_options)
{
1757
1758
    auto api_error_result = migraphx::try_([&] { destroy((quantize_int8_options)); });
    return api_error_result;
Shucai Xiao's avatar
Shucai Xiao committed
1759
1760
}

1761
1762
1763
1764
1765
1766
1767
1768
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
1769
1770
1771
extern "C" migraphx_status
migraphx_quantize_int8_options_create(migraphx_quantize_int8_options_t* quantize_int8_options)
{
1772
    auto api_error_result = migraphx::try_([&] {
Shucai Xiao's avatar
Shucai Xiao committed
1773
1774
1775
        *quantize_int8_options = object_cast<migraphx_quantize_int8_options_t>(
            allocate<migraphx::quantize_int8_options>());
    });
1776
    return api_error_result;
Shucai Xiao's avatar
Shucai Xiao committed
1777
1778
1779
1780
1781
1782
}

extern "C" migraphx_status
migraphx_quantize_int8_options_add_op_name(migraphx_quantize_int8_options_t quantize_int8_options,
                                           const char* name)
{
1783
    auto api_error_result = migraphx::try_([&] {
Shucai Xiao's avatar
Shucai Xiao committed
1784
1785
1786
1787
1788
        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));
    });
1789
    return api_error_result;
Shucai Xiao's avatar
Shucai Xiao committed
1790
1791
1792
1793
1794
}

extern "C" migraphx_status migraphx_quantize_int8_options_add_calibration_data(
    migraphx_quantize_int8_options_t quantize_int8_options, migraphx_program_parameters_t data)
{
1795
    auto api_error_result = migraphx::try_([&] {
Shucai Xiao's avatar
Shucai Xiao committed
1796
1797
1798
1799
1800
1801
1802
        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));
    });
1803
    return api_error_result;
Shucai Xiao's avatar
Shucai Xiao committed
1804
1805
1806
1807
1808
1809
}

extern "C" migraphx_status migraphx_quantize_int8(migraphx_program_t prog,
                                                  migraphx_target_t target,
                                                  migraphx_quantize_int8_options_t options)
{
1810
    auto api_error_result = migraphx::try_([&] {
Shucai Xiao's avatar
Shucai Xiao committed
1811
1812
1813
1814
1815
1816
1817
1818
        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));
    });
1819
    return api_error_result;
Shucai Xiao's avatar
Shucai Xiao committed
1820
}
kahmed10's avatar
kahmed10 committed
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830

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;
}
1831

1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
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;
}

1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
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,
1862
                                       const char* obj_typename,
1863
1864
1865
1866
                                       const char* name)
{
    auto api_error_result = migraphx::try_([&] {
        *experimental_custom_op =
1867
            allocate<migraphx_experimental_custom_op_t>((obj), (c), (d), (obj_typename), (name));
1868
1869
1870
1871
    });
    return api_error_result;
}

1872
1873
1874
1875
1876
1877
1878
1879
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;
}

1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
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;
}

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;
}