migraphx.py 16.8 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import api


def bad_param_error(msg):
    return 'MIGRAPHX_THROW(migraphx_status_bad_param, "{}")'.format(msg)


api.error_type = 'migraphx_status'
api.success_type = 'migraphx_status_success'
api.try_wrap = 'migraphx::try_'
api.bad_param_error = bad_param_error


@api.cwrap('migraphx::shape::type_t')
def shape_type_wrap(p):
    if p.returns:
        p.add_param('migraphx_shape_datatype_t *')
        p.bad_param('${name} == nullptr', 'Null pointer')
        p.write = ['*${name} = migraphx::to_shape_type(${result})']
    else:
        p.add_param('migraphx_shape_datatype_t')
        p.read = 'migraphx::to_shape_type(${name})'


48
49
50
51
def auto_handle(*args, **kwargs):
    def with_handle(f):
        return api.handle('migraphx_' + f.__name__, 'migraphx::' + f.__name__,
                          *args, **kwargs)(f)
52

53
    return with_handle
54

Paul Fultz II's avatar
Paul Fultz II committed
55

56
57
58
59
60
@api.handle('migraphx_optimals', 'std::set<size_t>')
def optimals(h):
    h.constructor('create',
                  api.params(ptr='const size_t*', size='size_t'),
                  fname='migraphx::make_set<size_t>')
Paul Fultz II's avatar
Paul Fultz II committed
61

kahmed10's avatar
kahmed10 committed
62

63
64
65
66
67
68
69
70
71
72
73
74
@api.handle('migraphx_dynamic_dimension', 'migraphx::shape::dynamic_dimension')
def dynamic_dimension(h):
    h.constructor('create_min_max', api.params(min='size_t', max='size_t'))
    h.constructor(
        'create_min_max_optimals',
        api.params(min='size_t', max='size_t', optimals='std::set<size_t>'))
    h.method('is_fixed', returns='bool', const=True)
    h.method('equal',
             api.params(x='const migraphx::shape::dynamic_dimension&'),
             invoke='migraphx::equal($@)',
             returns='bool',
             const=True)
kahmed10's avatar
kahmed10 committed
75

Paul Fultz II's avatar
Paul Fultz II committed
76

77
78
79
80
81
82
83
84
85
86
87
88
89
@api.handle('migraphx_dynamic_dimensions',
            'std::vector<migraphx::shape::dynamic_dimension>')
def dynamic_dimensions(h):
    h.constructor(
        'create',
        api.params(ptr='const_migraphx_dynamic_dimension_t*', size='size_t'),
        fname='migraphx::to_obj_vector<const_migraphx_dynamic_dimension_t>')
    h.method('size', returns='size_t')
    h.method('get',
             api.params(idx='size_t'),
             fname='at',
             cpp_name='operator[]',
             returns='const migraphx::shape::dynamic_dimension&')
Paul Fultz II's avatar
Paul Fultz II committed
90

91
92

@auto_handle()
Paul Fultz II's avatar
Paul Fultz II committed
93
94
95
96
97
def shape(h):
    h.constructor(
        'create',
        api.params(type='migraphx::shape::type_t',
                   lengths='std::vector<size_t>'))
98
99
100
101
102
    h.constructor(
        'create_with_strides',
        api.params(type='migraphx::shape::type_t',
                   lengths='std::vector<size_t>',
                   strides='std::vector<size_t>'))
103
    h.constructor('create_scalar', api.params(type='migraphx::shape::type_t'))
104
105
106
107
    h.constructor(
        'create_dynamic',
        api.params(type='migraphx::shape::type_t',
                   dims='std::vector<migraphx::shape::dynamic_dimension>'))
Paul Fultz II's avatar
Paul Fultz II committed
108
109
110
111
112
    h.method('lengths',
             fname='lens',
             returns='const std::vector<size_t>&',
             const=True)
    h.method('strides', returns='const std::vector<size_t>&', const=True)
113
114
115
    h.method('dyn_dims',
             returns='std::vector<migraphx::shape::dynamic_dimension>',
             const=True)
Paul Fultz II's avatar
Paul Fultz II committed
116
    h.method('type', returns='migraphx::shape::type_t', const=True)
117
    h.method('elements', returns='size_t', const=True)
Paul Fultz II's avatar
Paul Fultz II committed
118
    h.method('bytes', returns='size_t', const=True)
119
    h.method('ndim', returns='size_t', const=True)
Paul Fultz II's avatar
Paul Fultz II committed
120
121
122
123
124
    h.method('equal',
             api.params(x='const migraphx::shape&'),
             invoke='migraphx::equal($@)',
             returns='bool',
             const=True)
125
    h.method('standard', returns='bool', const=True)
126
    h.method('dynamic', returns='bool', const=True)
127
    h.method('index', api.params(i='size_t'), returns='size_t', const=True)
Paul Fultz II's avatar
Paul Fultz II committed
128
129


130
@auto_handle()
Paul Fultz II's avatar
Paul Fultz II committed
131
132
133
def argument(h):
    h.constructor('create',
                  api.params(shape='const migraphx::shape&', buffer='void*'))
134
    h.constructor('create_empty', api.params(shape='const migraphx::shape&'))
Paul Fultz II's avatar
Paul Fultz II committed
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
    h.method('shape',
             fname='get_shape',
             cpp_name='get_shape',
             returns='const migraphx::shape&',
             const=True)
    h.method('buffer',
             fname='data',
             cpp_name='data',
             returns='char*',
             const=True)
    h.method('equal',
             api.params(x='const migraphx::argument&'),
             invoke='migraphx::equal($@)',
             returns='bool',
             const=True)


api.add_function('migraphx_argument_generate',
                 api.params(s='const migraphx::shape&', seed='size_t'),
                 fname='migraphx::generate_argument',
                 returns='migraphx::argument')


158
@auto_handle()
Paul Fultz II's avatar
Paul Fultz II committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
def target(h):
    h.constructor('create',
                  api.params(name='const char*'),
                  fname='migraphx::get_target')


@api.handle('migraphx_program_parameter_shapes',
            'std::unordered_map<std::string, migraphx::shape>')
def program_parameter_shapes(h):
    h.method('size', returns='size_t')
    h.method('get',
             api.params(name='const char*'),
             fname='at',
             cpp_name='operator[]',
             returns='const migraphx::shape&')
    h.method('names',
             invoke='migraphx::get_names(${program_parameter_shapes})',
             returns='std::vector<const char*>')


@api.handle('migraphx_program_parameters',
            'std::unordered_map<std::string, migraphx::argument>')
def program_parameters(h):
    h.constructor('create')
    h.method('add',
             api.params(name='const char*',
                        argument='const migraphx::argument&'),
             invoke='${program_parameters}[${name}] = ${argument}')


@api.handle('migraphx_arguments', 'std::vector<migraphx::argument>')
def arguments(h):
    h.method('size', returns='size_t')
    h.method('get',
             api.params(idx='size_t'),
             fname='at',
             cpp_name='operator[]',
             returns='const migraphx::argument&')


@api.handle('migraphx_shapes', 'std::vector<migraphx::shape>')
def shapes(h):
    h.method('size', returns='size_t')
    h.method('get',
             api.params(idx='size_t'),
             fname='at',
             cpp_name='operator[]',
             returns='const migraphx::shape&')


209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
@api.handle('migraphx_instruction', 'migraphx::instruction_ref')
def instruction(h):
    pass


@api.handle('migraphx_instructions', 'std::vector<migraphx::instruction_ref>')
def instructions(h):
    h.constructor(
        'create',
        api.params(ptr='const_migraphx_instruction_t*', size='size_t'),
        fname='migraphx::to_obj_vector<const_migraphx_instruction_t>')


@api.handle('migraphx_modules', 'std::vector<migraphx::module*>')
def modules(h):
    h.constructor('create',
                  api.params(ptr='migraphx_module_t*', size='size_t'),
                  fname='migraphx::to_objptr_vector<migraphx::module*>')


Shucai Xiao's avatar
Shucai Xiao committed
229
230
@auto_handle(ref=True)
def module(h):
231
    h.constructor('create', api.params(name='std::string'))
Shucai Xiao's avatar
Shucai Xiao committed
232
    h.method('print', invoke='migraphx::print_module($@)', const=True)
233
234
235
236
237
238
239
240
241
242
    h.method('add_instruction',
             api.params(op='migraphx::operation',
                        args='std::vector<migraphx::instruction_ref>'),
             returns='migraphx::instruction_ref')
    h.method('add_instruction_with_mod_args',
             api.params(op='migraphx::operation',
                        args='std::vector<migraphx::instruction_ref>',
                        module_refs='std::vector<migraphx::module*>'),
             fname='add_instruction',
             returns='migraphx::instruction_ref')
243
244
245
    h.method('add_literal',
             api.params(shape='const migraphx::shape&', buffer='const char*'),
             returns='migraphx::instruction_ref')
246
247
248
249
250
251
    h.method('add_parameter',
             api.params(name='const char*', shape='const migraphx::shape&'),
             returns='migraphx::instruction_ref')
    h.method('add_return',
             api.params(args='std::vector<migraphx::instruction_ref>'),
             returns='migraphx::instruction_ref')
252
253
254
255
    h.method('add_allocation',
             api.params(s='const migraphx::shape&'),
             invoke='migraphx::add_allocation($@)',
             returns='migraphx::instruction_ref')
Shucai Xiao's avatar
Shucai Xiao committed
256
257


258
@auto_handle()
Paul Fultz II's avatar
Paul Fultz II committed
259
def program(h):
260
    h.constructor('create')
Shucai Xiao's avatar
Shucai Xiao committed
261
    h.method('get_main_module', returns='migraphx::module*')
262
263
264
    h.method('create_module',
             api.params(name='const char*'),
             returns='migraphx::module*')
Paul Fultz II's avatar
Paul Fultz II committed
265
266
267
268
269
270
271
272
273
    h.method(
        'compile',
        api.params(target='migraphx::target',
                   options='migraphx::compile_options'))
    h.method('get_parameter_shapes',
             returns='std::unordered_map<std::string, migraphx::shape>')
    h.method('get_output_shapes',
             invoke='migraphx::get_output_shapes($@)',
             returns='std::vector<migraphx::shape>')
Shucai Xiao's avatar
Shucai Xiao committed
274
    h.method('print', invoke='migraphx::print_program($@)', const=True)
275
    h.method('sort')
Paul Fultz II's avatar
Paul Fultz II committed
276
277
278
279
280
    h.method('run',
             api.params(
                 params='std::unordered_map<std::string, migraphx::argument>'),
             invoke='migraphx::run($@)',
             returns='std::vector<migraphx::argument>')
281
282
283
284
285
286
287
    h.method('run_async',
             api.params(
                 params='std::unordered_map<std::string, migraphx::argument>',
                 s='void*',
                 name='const char *'),
             invoke='migraphx::run_async($@)',
             returns='std::vector<migraphx::argument>')
Paul Fultz II's avatar
Paul Fultz II committed
288
289
290
291
292
    h.method('equal',
             api.params(x='const migraphx::program&'),
             invoke='migraphx::equal($@)',
             returns='bool',
             const=True)
293
    h.method('experimental_get_context',
kahmed10's avatar
kahmed10 committed
294
295
296
             invoke='migraphx::get_context($@)',
             const=True,
             returns='migraphx::context')
Paul Fultz II's avatar
Paul Fultz II committed
297
298


299
@auto_handle()
300
301
def operation(h):
    h.constructor('create',
302
303
304
                  api.params(name='const char*',
                             attributes='const char*',
                             vlist='...'),
305
306
307
308
                  fname='migraphx::create_op')
    h.method('name', returns='std::string')


309
310
311
312
313
314
315
316
317
318
319
320
321
api.add_function('migraphx_load',
                 api.params(name='const char*',
                            options='migraphx::file_options'),
                 fname='migraphx::load',
                 returns='migraphx::program')

api.add_function('migraphx_save',
                 api.params(p='migraphx::program&',
                            name='const char*',
                            options='migraphx::file_options'),
                 fname='migraphx::save')


322
@auto_handle()
323
324
325
326
def onnx_options(h):
    h.constructor('create')
    h.method(
        'set_input_parameter_shape',
327
        api.params(name='const char*', dims='std::vector<size_t>'),
328
329
        invoke='migraphx::set_input_parameter_shape($@)',
    )
330
331
332
333
334
335
    h.method(
        'set_dyn_input_parameter_shape',
        api.params(name='const char*',
                   dims='std::vector<migraphx::shape::dynamic_dimension>'),
        invoke='migraphx::set_dyn_input_parameter_shape($@)',
    )
336
337
338
339
340
    h.method(
        'set_default_dim_value',
        api.params(value='size_t'),
        invoke='migraphx::set_default_dim_value($@)',
    )
341
342
343
344
345
    h.method(
        'set_default_dyn_dim_value',
        api.params(dd='const migraphx::shape::dynamic_dimension&'),
        invoke='migraphx::set_default_dyn_dim_value($@)',
    )
Shucai Xiao's avatar
Shucai Xiao committed
346
347
348
349
350
    h.method(
        'set_default_loop_iterations',
        api.params(value='int64_t'),
        invoke='migraphx::set_default_loop_iterations($@)',
    )
351
352


353
354
355
356
357
358
359
360
@auto_handle()
def file_options(h):
    h.constructor('create')
    h.method('set_file_format',
             api.params(format='const char*'),
             invoke='migraphx::set_file_format($@)')


361
362
363
364
365
366
367
368
369
@auto_handle()
def compile_options(h):
    h.constructor('create')
    h.method('set_offload_copy',
             api.params(value='bool'),
             invoke='migraphx::set_offload_copy($@)')
    h.method('set_fast_math',
             api.params(value='bool'),
             invoke='migraphx::set_fast_math($@)')
370
371
372
    h.method('set_exhaustive_tune_flag',
             api.params(value='bool'),
             invoke='migraphx::set_exhaustive_tune_flag($@)')
373
374


Paul Fultz II's avatar
Paul Fultz II committed
375
376
377
378
379
380
381
382
383
384
385
386
api.add_function('migraphx_parse_onnx',
                 api.params(name='const char*',
                            options='migraphx::onnx_options'),
                 fname='migraphx::parse_onnx',
                 returns='migraphx::program')

api.add_function('migraphx_parse_onnx_buffer',
                 api.params(data='const void*',
                            size='size_t',
                            options='migraphx::onnx_options'),
                 fname='migraphx::parse_onnx_buffer',
                 returns='migraphx::program')
Shucai Xiao's avatar
Shucai Xiao committed
387
388


kahmed10's avatar
kahmed10 committed
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
@auto_handle()
def tf_options(h):
    h.constructor('create')
    h.method(
        'set_nhwc',
        api.params(is_nhwc='bool'),
        invoke='migraphx::set_nhwc($@)',
    )
    h.method(
        'set_input_parameter_shape',
        api.params(name='const char*', dims='std::vector<size_t>'),
        invoke='migraphx::set_input_parameter_shape($@)',
    )
    h.method(
        'set_default_dim_value',
        api.params(value='size_t'),
        invoke='migraphx::set_default_dim_value($@)',
    )
    h.method(
        'set_output_names',
        api.params(names='std::vector<const char*>'),
        invoke='migraphx::set_output_names($@)',
    )


api.add_function('migraphx_parse_tf',
                 api.params(name='const char*',
                            options='migraphx::tf_options'),
                 fname='migraphx::parse_tf',
                 returns='migraphx::program')


Shucai Xiao's avatar
Shucai Xiao committed
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
@api.handle('migraphx_quantize_op_names', 'std::vector<std::string>')
def quantize_op_names(h):
    h.constructor('create')
    h.method('add', api.params(name='const char*'), fname='push_back')


api.add_function('migraphx_quantize_fp16_with_op_names',
                 api.params(prog='migraphx::program&',
                            name='std::vector<std::string>&'),
                 fname='migraphx::quantize_fp16_with_op_names')

api.add_function('migraphx_quantize_fp16',
                 api.params(prog='migraphx::program&'),
                 fname='migraphx::quantize_fp16')


437
@auto_handle()
Shucai Xiao's avatar
Shucai Xiao committed
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
def quantize_int8_options(h):
    h.constructor('create')
    h.method(
        'add_op_name',
        api.params(name='const char*'),
        invoke='migraphx::add_op_name($@)',
    )
    h.method(
        'add_calibration_data',
        api.params(data='std::unordered_map<std::string, migraphx::argument>'),
        invoke='migraphx::add_calibration_data($@)',
    )


api.add_function('migraphx_quantize_int8',
                 api.params(prog='migraphx::program&',
                            target='migraphx::target',
                            options='migraphx::quantize_int8_options'),
                 fname='migraphx::quantize_int8_wrap')
kahmed10's avatar
kahmed10 committed
457
458
459
460
461


@auto_handle(ref=True)
def context(h):
    h.method('finish', const=True)
462
    h.method('get_queue', returns='void*', fname='get_queue().unsafe_get')
463
464
465
466
467


@api.interface('migraphx_experimental_custom_op',
               'migraphx::experimental_custom_op')
def experimental_custom_op(h):
468
469
    h.constructor('create',
                  api.params(obj_typename='const char*', name='const char*'))
470
471
472
473
474
    h.virtual('compute',
              api.params(ctx='migraphx::context',
                         output='migraphx::shape',
                         inputs='std::vector<migraphx::argument>'),
              returns='migraphx::argument')
475
476
477
    h.virtual('compute_shape',
              api.params(inputs='std::vector<migraphx::shape>'),
              returns='migraphx::shape')
478
479
480
481
    h.virtual('output_alias',
              api.params(inputs='std::vector<migraphx::shape>'),
              returns='std::vector<size_t>')
    h.virtual('runs_on_offload_target', returns='bool')
482
    h.method('register', invoke='migraphx::register_custom_op($@)')