py.rst 9.45 KB
Newer Older
1
2
3
4
5
6
7
8
.. py:module:: migraphx

Python Reference
================

shape
-----

9
.. py:class:: shape(type, lens, strides=None, dyn_dims)
10

11
    Describes the shape of a tensor. This includes size, layout, and data type. Can be a dynamic shape by using dyn_dims.
12
13
14

.. py:method:: type()

15
    An integer that represents the type.
16
17
18
19
20

    :rtype: int

.. py:method:: lens()

21
    A list of the lengths of the shape.
22
23
24
25
26

    :rtype: list[int]

.. py:method:: strides()

27
    A list of the strides of the shape.
28
29
30
31
32

    :rtype: list[int]

.. py:method:: elements()

33
    The number of elements in the shape.
34
35
36

    :rtype: int

37
38
39
40
41
42
.. py:method:: dyn_dims()

    The dynamic dimensions of the shape.

    :rtype: list[dynamic_dimension]

43
44
.. py:method:: bytes()

45
    The number of bytes the shape uses.
46
47
48
49
50
51
52
53
54

    :rtype: int

.. py:method:: type_size()

    The number of bytes one element uses

    :rtype: int

55
56
57
58
59
60
.. py:method:: ndim()

    The number of dimensions for the shape.

    :rtype: int

61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
.. py:method:: packed()

    Returns true if the shape is packed.

    :rtype: bool

.. py:method:: transposed()

    Returns true if the shape is transposed.

    :rtype: bool

.. py:method:: broadcasted()

    Returns true if the shape is broadcasted.

    :rtype: bool

79
80
81
82
83
84
.. py:method:: dynamic()

    Returns true if the shape is dynamic.

    :rtype: bool

85
86
87
88
89
90
91
92
93
94
95
96
.. py:method:: standard()

    Returns true if the shape is a standard shape. That is, the shape is both packed and not transposed.

    :rtype: bool

.. py:method:: scalar()

    Returns true if all strides are equal to 0 (scalar tensor).

    :rtype: bool

97
98
99
100
101
102
103
104
105
106
107
108
dynamic_dimension
--------

.. py:class:: dynamic_dimension(min, max, optimals)

    Construct a dynamic_dimension from a minimum, a maximum, and optionally a set of optimals.

.. py:method:: is_fixed()
    
    Returns true if the dynamic_dimension is fixed.

    :rtype : int
109
110
111
112
113
114
115
116

argument
--------

.. py:class:: argument(data)

    Construct an argument from a python buffer. This can include numpy arrays.

117
118
119
120
121
122
.. py:method:: data_ptr()

    Returns the address to the underlying argument data.

    :rtype: int

123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
.. py:method:: get_shape()

    Returns the shape of the argument.

    :rtype: shape

.. py:method:: tolist()

    Convert the elements of the argument to a python list.

    :rtype: list


.. py:function:: generate_argument(s, seed=0)

    Generate an argument with random data.

    :param shape s: Shape of argument to generate.
141
    :param int seed: The seed used for random number generation.
142
143
144

    :rtype: argument

145
.. py:function:: fill_argument(s, value)
146

147
    Fill argument of shape s with value.
148

149
150
    :param shape s: Shape of argument to fill.
    :param int value: Value to fill in the argument.
151

152
153
    :rtype: argument

154
155
156
157
158
159
160
161
162
.. py:function:: create_argument(s, values)

    Create an argument of shape s with a set of values.

    :param shape s: Shape of argument to create.
    :param list values: Values to put in the argument. Must be the same number of elements as the shape.

    :rtype: argument

163
164
165
166
167
168
169
170
.. py:function:: argument_from_pointer(shape, address)

    Create argument from data stored in given address without copy.

    :param shape shape: Shape of the data stored in address.
    :param long address: Memory address of data from another source

    :rtype: argument 
171
172

target
173
------
174
175
176

.. py:class:: target()

177
    This represents the compilation target.
178
179
180
181
182

.. py:function:: get_target(name)

    Constructs the target.

183
    :param str name: The name of the target to construct. This can either be 'gpu' or 'ref'.
184
185
186
187

    :rtype: target


188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
module
------
.. py:method:: print()

    Prints the contents of the module as list of instructions.

.. py:method:: add_instruction(op, args, mod_args=[])
    
    Adds instruction into the module.

    :param operation op: 'migraphx.op' to be added as instruction.
    :param list[instruction] args: list of inputs to the op.
    :param list[module] mod_args: optional list of module arguments to the operator.
    :rtype instruction

203
204
205
206
207
208
209
.. py:method:: add_literal(data)

    Adds constant or literal data of provided shape into the module from python buffer which includes numpy array.    

    :param py::buffer data: Python buffer or numpy array 
    :rtype instruction 

210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
.. py:method:: add_parameter(name, shape)
    
    Adds a parameter to the module with provided name and shape.

    :param str name: name of the parameter.
    :param shape shape: shape of the parameter.
    :rtype instruction

.. py:method:: add_return(args)

    Adds a return instruction into the module.

    :param list[instruction] args: instruction arguments which need to be returned from the module.
    :rtype instruction


226
227
228
229
230
program
-------

.. py:class:: program()

231
    Represents the computation graph to be compiled and run.
232
233
234

.. py:method:: clone()

235
    Make a copy of the program.
236
237
238

    :rtype: program

239
240
241
242
243
244
.. py:method:: get_parameter_names()
 
    Get all the input arguments' or parameters' names to the program as a list.

    :rtype list[str]

245
246
247
248
249
250
.. py:method:: get_parameter_shapes()

    Get the shapes of all the input parameters in the program.

    :rtype: dict[str, shape]

251
.. py:method:: get_output_shapes()
252

253
    Get the shapes of the final outputs of the program.
254

255
    :rtype: list[shape]
256

257
.. py:method:: compile(t, offload_copy=True, fast_math=True, exhaustive_tune=False)
258
259
260
261
262

    Compiles the program for the target and optimizes it.

    :param target t: This is the target to compile the program for.
    :param bool offload_copy: For targets with offloaded memory(such as the gpu), this will insert instructions during compilation to copy the input parameters to the offloaded memory and to copy the final result from the offloaded memory back to main memory.
kahmed10's avatar
kahmed10 committed
263
    :param bool fast_math: Optimize math functions to use faster approximate versions. There may be slight accuracy degredation when enabled.
264
    :param exhaustive_tune: Flag to enable exhaustive search to find the fastest version of generated kernels for selected backend.
265

266
267
268
269
270
271
272
273
274
275
276
277
278
.. py:method:: get_main_module()
    
    Get main module of the program.

    :rtype module

.. py:method:: create_module(name)
    
    Create and add a module of provided name into the program.

    :param str name : name of the new module.
    :rtype module

279
280
281
282
283
284
285
286
.. py:method:: run(params)

    Run the program.

    :param params: This is a map of the input parameters which will be used when running the program.
    :type params: dict[str, argument]

    :return: The result of the last instruction.
287
288
289
290
291
    :rtype: list[argument]

.. py:method:: sort()

    Sort the modules of the program such that instructions appear in topologically sorted order.
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313

.. py:function:: quantize_fp16(prog, ins_names=["all"])

    Quantize the program to use fp16.

    :param program prog: Program to quantize.
    :param ins_names: List of instructions to quantize.
    :type ins_names: list[str]


.. py:function:: quantize_int8(prog, t, calibration=[], ins_names=["dot", "convolution"])

    Quantize the program to use int8.

    :param program prog: Program to quantize.
    :param target t: Target that will be used to run the calibration data.
    :param calibration: Calibration data used to decide the parameters to the int8 optimization.
    :type calibration: list[dict[str, argument]]
    :param ins_names: List of instructions to quantize.
    :type ins_names: list[str]


314
315
316
317
318
319
320
321
322
323
324
325
op
--
.. py::class:: op(name, kwargs)

    Construct an operation with name and arguments.
    
    :param str name : name of the operation, must be supported by MIGraphX.
    :param dict[str, any] kwargs: arguments to the operation.
    :rtype operation



326
327
328
parse_onnx
----------

329
.. py:function:: parse_onnx(filename, default_dim_value=1, map_input_dims={}, skip_unknown_operators=false, print_program_on_error=false, max_loop_iterations=10, limit_max_iterations=65535)
330
331
332
333

    Load and parse an onnx file.

    :param str filename: Path to file.
334
335
    :param str default_dim_value: default dimension to use (if not specified in onnx file).
    :param dynamic_dimension default_dyn_dim_value: default dynamic_dimension value to use.
336
    :param str map_input_dims: Explicitly specify the dims of an input.
337
    :param list[dynamic_dimension] map_dyn_input_dims: Explicitly specify the dynamic_dimensions of an input.
338
339
    :param str skip_unknown_operators: Continue parsing onnx file if an unknown operator is found.
    :param str print_program_on_error: Print program if an error occurs.
340
341
    :param int max_loop_iterations: Maximum iteration number for the loop operator if trip count is not set.
    :param int limit_max_iterations: Maximum iteration limit for the loop operator.
342
343
344
    :rtype: program

parse_tf
345
--------
346

347
.. py:function:: parse_tf(filename, is_nhwc=True, batch_size=1, map_input_dims=dict(), output_names=[])
348
349
350
351
352
353

    Load and parse an tensorflow protobuf file file.

    :param str filename: Path to file.
    :param bool is_nhwc: Use nhwc as default format.
    :param str batch_size: default batch size to use (if not specified in protobuf).
354
355
    :param dict[str, list[int]] map_input_dims: Optional arg to explictly specify dimensions of the inputs.
    :param list[str] output_names:  Optional argument specify names of the output nodes.
356
357
    :rtype: program

358
359
360
361
362
load
----

.. py:function:: load(filename, format='msgpack')

363
    Load a MIGraphX program.
364
365
366
367
368
369
370
371
372
373
374

    :param str filename: Path to file.
    :param str format: Format of file. Valid options are msgpack or json.

    :rtype: program

save
----

.. py:function:: save(p, filename, format='msgpack')

375
    Save a MIGraphX program.
376
377
378
379
380

    :param program p: Program to save.
    :param str filename: Path to file.
    :param str format: Format of file. Valid options are msgpack or json.