migraphx.py 16.2 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
48
49
50
51
52
53
54
55
56
57
58
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})'


@api.cwrap('migraphx::compile_options')
def compile_options_type_wrap(p):
    if p.returns:
        p.add_param('migraphx_compile_options *')
        p.bad_param('${name} == nullptr', 'Null pointer')
        p.write = ['*${name} = migraphx::to_compile_options(${result})']
    else:
        p.add_param('migraphx_compile_options *')
        p.read = '${name} == nullptr ? migraphx::compile_options{} : migraphx::to_compile_options(*${name})'


59
60
61
62
63
64
65
66
67
68
69
@api.cwrap('migraphx::file_options')
def file_options_type_wrap(p):
    if p.returns:
        p.add_param('migraphx_file_options *')
        p.bad_param('${name} == nullptr', 'Null pointer')
        p.write = ['*${name} = migraphx::to_file_options(${result})']
    else:
        p.add_param('migraphx_file_options *')
        p.read = '${name} == nullptr ? migraphx::file_options{} : migraphx::to_file_options(*${name})'


Paul Fultz II's avatar
Paul Fultz II committed
70
71
72
73
74
75
76
77
78
79
80
@api.cwrap('migraphx::onnx_options')
def onnx_options_type_wrap(p):
    if p.returns:
        p.add_param('migraphx_onnx_options *')
        p.bad_param('${name} == nullptr', 'Null pointer')
        p.write = ['*${name} = migraphx::to_onnx_options(${result})']
    else:
        p.add_param('migraphx_onnx_options *')
        p.read = '${name} == nullptr ? migraphx::onnx_options{} : migraphx::to_onnx_options(*${name})'


kahmed10's avatar
kahmed10 committed
81
82
83
84
85
86
87
88
89
90
91
@api.cwrap('migraphx::tf_options')
def tf_options_type_wrap(p):
    if p.returns:
        p.add_param('migraphx_tf_options *')
        p.bad_param('${name} == nullptr', 'Null pointer')
        p.write = ['*${name} = migraphx::to_tf_options(${result})']
    else:
        p.add_param('migraphx_tf_options *')
        p.read = '${name} == nullptr ? migraphx::tf_options{} : migraphx::to_tf_options(*${name})'


92
93
94
95
def auto_handle(*args, **kwargs):
    def with_handle(f):
        return api.handle('migraphx_' + f.__name__, 'migraphx::' + f.__name__,
                          *args, **kwargs)(f)
Paul Fultz II's avatar
Paul Fultz II committed
96

97
    return with_handle
Paul Fultz II's avatar
Paul Fultz II committed
98

99
100

@auto_handle()
Paul Fultz II's avatar
Paul Fultz II committed
101
102
103
104
105
def shape(h):
    h.constructor(
        'create',
        api.params(type='migraphx::shape::type_t',
                   lengths='std::vector<size_t>'))
106
107
108
109
110
    h.constructor(
        'create_with_strides',
        api.params(type='migraphx::shape::type_t',
                   lengths='std::vector<size_t>',
                   strides='std::vector<size_t>'))
111
    h.constructor('create_scalar', api.params(type='migraphx::shape::type_t'))
Paul Fultz II's avatar
Paul Fultz II committed
112
113
114
115
116
117
    h.method('lengths',
             fname='lens',
             returns='const std::vector<size_t>&',
             const=True)
    h.method('strides', returns='const std::vector<size_t>&', const=True)
    h.method('type', returns='migraphx::shape::type_t', const=True)
118
    h.method('elements', returns='size_t', const=True)
Paul Fultz II's avatar
Paul Fultz II committed
119
120
121
122
123
124
    h.method('bytes', returns='size_t', const=True)
    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('index', api.params(i='size_t'), returns='size_t', const=True)
Paul Fultz II's avatar
Paul Fultz II committed
127
128


129
@auto_handle()
Paul Fultz II's avatar
Paul Fultz II committed
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
def argument(h):
    h.constructor('create',
                  api.params(shape='const migraphx::shape&', buffer='void*'))
    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')


156
@auto_handle()
Paul Fultz II's avatar
Paul Fultz II committed
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
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&')


207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
@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
227
228
@auto_handle(ref=True)
def module(h):
229
    h.constructor('create', api.params(name='std::string'))
Shucai Xiao's avatar
Shucai Xiao committed
230
    h.method('print', invoke='migraphx::print_module($@)', const=True)
231
232
233
234
235
236
237
238
239
240
    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')
241
242
243
    h.method('add_literal',
             api.params(shape='const migraphx::shape&', buffer='const char*'),
             returns='migraphx::instruction_ref')
244
245
246
247
248
249
    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')
250
251
252
253
    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
254
255


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


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


307
308
309
310
311
312
313
314
315
316
317
318
319
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')


320
@auto_handle()
321
322
323
324
def onnx_options(h):
    h.constructor('create')
    h.method(
        'set_input_parameter_shape',
325
        api.params(name='const char*', dims='std::vector<size_t>'),
326
327
328
329
330
331
332
        invoke='migraphx::set_input_parameter_shape($@)',
    )
    h.method(
        'set_default_dim_value',
        api.params(value='size_t'),
        invoke='migraphx::set_default_dim_value($@)',
    )
Shucai Xiao's avatar
Shucai Xiao committed
333
334
335
336
337
    h.method(
        'set_default_loop_iterations',
        api.params(value='int64_t'),
        invoke='migraphx::set_default_loop_iterations($@)',
    )
338
339


340
341
342
343
344
345
346
347
@auto_handle()
def file_options(h):
    h.constructor('create')
    h.method('set_file_format',
             api.params(format='const char*'),
             invoke='migraphx::set_file_format($@)')


348
349
350
351
352
353
354
355
356
357
358
@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($@)')


Paul Fultz II's avatar
Paul Fultz II committed
359
360
361
362
363
364
365
366
367
368
369
370
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
371
372


kahmed10's avatar
kahmed10 committed
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
@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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
@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')


421
@auto_handle()
Shucai Xiao's avatar
Shucai Xiao committed
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
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
441
442
443
444
445


@auto_handle(ref=True)
def context(h):
    h.method('finish', const=True)
446
    h.method('get_queue', returns='void*', fname='get_queue().unsafe_get')
447
448
449
450
451


@api.interface('migraphx_experimental_custom_op',
               'migraphx::experimental_custom_op')
def experimental_custom_op(h):
452
453
    h.constructor('create',
                  api.params(obj_typename='const char*', name='const char*'))
454
455
456
457
458
    h.virtual('compute',
              api.params(ctx='migraphx::context',
                         output='migraphx::shape',
                         inputs='std::vector<migraphx::argument>'),
              returns='migraphx::argument')
459
460
461
    h.virtual('compute_shape',
              api.params(inputs='std::vector<migraphx::shape>'),
              returns='migraphx::shape')
462
463
464
465
    h.virtual('output_alias',
              api.params(inputs='std::vector<migraphx::shape>'),
              returns='std::vector<size_t>')
    h.virtual('runs_on_offload_target', returns='bool')
466
    h.method('register', invoke='migraphx::register_custom_op($@)')