migraphx.py 15.6 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
118
119
120
121
122
123
    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)
    h.method('bytes', returns='size_t', const=True)
    h.method('equal',
             api.params(x='const migraphx::shape&'),
             invoke='migraphx::equal($@)',
             returns='bool',
             const=True)
124
    h.method('standard', returns='bool', const=True)
Paul Fultz II's avatar
Paul Fultz II committed
125
126


127
@auto_handle()
Paul Fultz II's avatar
Paul Fultz II committed
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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')


154
@auto_handle()
Paul Fultz II's avatar
Paul Fultz II committed
155
156
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
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&')


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


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


288
@auto_handle()
289
290
def operation(h):
    h.constructor('create',
291
292
293
                  api.params(name='const char*',
                             attributes='const char*',
                             vlist='...'),
294
295
296
297
                  fname='migraphx::create_op')
    h.method('name', returns='std::string')


298
299
300
301
302
303
304
305
306
307
308
309
310
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')


311
@auto_handle()
312
313
314
315
def onnx_options(h):
    h.constructor('create')
    h.method(
        'set_input_parameter_shape',
316
        api.params(name='const char*', dims='std::vector<size_t>'),
317
318
319
320
321
322
323
        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
324
325
326
327
328
    h.method(
        'set_default_loop_iterations',
        api.params(value='int64_t'),
        invoke='migraphx::set_default_loop_iterations($@)',
    )
329
330


331
332
333
334
335
336
337
338
@auto_handle()
def file_options(h):
    h.constructor('create')
    h.method('set_file_format',
             api.params(format='const char*'),
             invoke='migraphx::set_file_format($@)')


339
340
341
342
343
344
345
346
347
348
349
@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
350
351
352
353
354
355
356
357
358
359
360
361
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
362
363


kahmed10's avatar
kahmed10 committed
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
@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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
@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')


412
@auto_handle()
Shucai Xiao's avatar
Shucai Xiao committed
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
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
432
433
434
435
436


@auto_handle(ref=True)
def context(h):
    h.method('finish', const=True)
437
    h.method('get_queue', returns='void*', fname='get_queue().unsafe_get')
438
439
440
441
442


@api.interface('migraphx_experimental_custom_op',
               'migraphx::experimental_custom_op')
def experimental_custom_op(h):
443
444
    h.constructor('create',
                  api.params(obj_typename='const char*', name='const char*'))
445
446
447
448
449
    h.virtual('compute',
              api.params(ctx='migraphx::context',
                         output='migraphx::shape',
                         inputs='std::vector<migraphx::argument>'),
              returns='migraphx::argument')
450
451
452
453
    h.virtual('compute_shape',
              api.params(inputs='std::vector<migraphx::shape>'),
              returns='migraphx::shape')
    h.method('register', invoke='migraphx::register_custom_op($@)')