File: api_summary.rst

package info (click to toggle)
onnxruntime 1.21.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 333,732 kB
  • sloc: cpp: 3,153,079; python: 179,219; ansic: 109,131; asm: 37,791; cs: 34,424; perl: 13,070; java: 11,047; javascript: 6,330; pascal: 4,126; sh: 3,277; xml: 598; objc: 281; makefile: 59
file content (441 lines) | stat: -rw-r--r-- 12,884 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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
154
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
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
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441

===
API
===

API Overview
============

*ONNX Runtime* loads and runs inference on a model in ONNX graph format, or ORT format (for memory and disk constrained environments).

The data consumed and produced by the model can be specified and accessed in the way that best matches your scenario.

Load and run a model
--------------------

InferenceSession is the main class of ONNX Runtime. It is used to load and run an ONNX model,
as well as specify environment and application configuration options.

.. code-block:: python

	session = onnxruntime.InferenceSession('model.onnx')

	outputs = session.run([output names], inputs)

ONNX and ORT format models consist of a graph of computations, modeled as operators,
and implemented as optimized operator kernels for different hardware targets.
ONNX Runtime orchestrates the execution of operator kernels via `execution providers`.
An execution provider contains the set of kernels for a specific execution target (CPU, GPU, IoT etc).
Execution provides are configured using the `providers` parameter. Kernels from different execution
providers are chosen in the priority order given in the list of providers. In the example below
if there is a kernel in the CUDA execution provider ONNX Runtime executes that on GPU. If not
the kernel is executed on CPU.

.. code-block:: python

	session = onnxruntime.InferenceSession(
		model, providers=['CUDAExecutionProvider', 'CPUExecutionProvider']
	)

The list of available execution providers can be found here: `Execution Providers <https://onnxruntime.ai/docs/execution-providers>`_.

Since ONNX Runtime 1.10, you must explicitly specify the execution provider for your target.
Running on CPU is the only time the API allows no explicit setting of the `provider` parameter.
In the examples that follow, the `CUDAExecutionProvider` and `CPUExecutionProvider` are used, assuming the application is running on NVIDIA GPUs.
Replace these with the execution provider specific to your environment.

You can supply other session configurations via the `session options` parameter. For example, to enable
profiling on the session:

.. code-block:: python

	options = onnxruntime.SessionOptions()
	options.enable_profiling=True
	session = onnxruntime.InferenceSession(
		'model.onnx',
		sess_options=options,
		providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
	)


Data inputs and outputs
-----------------------

The ONNX Runtime Inference Session consumes and produces data using its OrtValue class.

Data on CPU
^^^^^^^^^^^

On CPU (the default), OrtValues can be mapped to and from native Python data structures: numpy arrays, dictionaries and lists of
numpy arrays.

.. code-block:: python

	# X is numpy array on cpu
	ortvalue = onnxruntime.OrtValue.ortvalue_from_numpy(X)
	ortvalue.device_name()  # 'cpu'
	ortvalue.shape()        # shape of the numpy array X
	ortvalue.data_type()    # 'tensor(float)'
	ortvalue.is_tensor()    # 'True'
	np.array_equal(ortvalue.numpy(), X)  # 'True'

	# ortvalue can be provided as part of the input feed to a model
	session = onnxruntime.InferenceSession(
		'model.onnx',
		providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
	)
	results = session.run(["Y"], {"X": ortvalue})

By default, *ONNX Runtime* always places input(s) and output(s) on CPU. Having the data on CPU
may not optimal if the input or output is consumed and produced on a device
other than CPU because it introduces data copy between CPU and the device.


Data on device
^^^^^^^^^^^^^^

*ONNX Runtime* supports a custom data structure that supports all ONNX data formats that allows users
to place the data backing these on a device, for example, on a CUDA supported device. In ONNX Runtime,
this called `IOBinding`.

To use the `IOBinding` feature, replace `InferenceSession.run()` with `InferenceSession.run_with_iobinding()`.

A graph is executed on a device other than CPU, for instance CUDA. Users can
use IOBinding to copy the data onto the GPU.

.. code-block:: python

	# X is numpy array on cpu
	session = onnxruntime.InferenceSession(
		'model.onnx',
		providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
	)
	io_binding = session.io_binding()
	# OnnxRuntime will copy the data over to the CUDA device if 'input' is consumed by nodes on the CUDA device
	io_binding.bind_cpu_input('input', X)
	io_binding.bind_output('output')
	session.run_with_iobinding(io_binding)
	Y = io_binding.copy_outputs_to_cpu()[0]

The input data is on a device, users directly use the input. The output data is on CPU.

.. code-block:: python

	# X is numpy array on cpu
	X_ortvalue = onnxruntime.OrtValue.ortvalue_from_numpy(X, 'cuda', 0)
	session = onnxruntime.InferenceSession(
		'model.onnx',
		providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
	)
	io_binding = session.io_binding()
	io_binding.bind_input(name='input', device_type=X_ortvalue.device_name(), device_id=0, element_type=np.float32, shape=X_ortvalue.shape(), buffer_ptr=X_ortvalue.data_ptr())
	io_binding.bind_output('output')
	session.run_with_iobinding(io_binding)
	Y = io_binding.copy_outputs_to_cpu()[0]

The input data and output data are both on a device, users directly use the input and also place output on the device.

.. code-block:: python

	#X is numpy array on cpu
	X_ortvalue = onnxruntime.OrtValue.ortvalue_from_numpy(X, 'cuda', 0)
	Y_ortvalue = onnxruntime.OrtValue.ortvalue_from_shape_and_type([3, 2], np.float32, 'cuda', 0)  # Change the shape to the actual shape of the output being bound
	session = onnxruntime.InferenceSession(
		'model.onnx',
		providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
	)
	io_binding = session.io_binding()
	io_binding.bind_input(
		name='input',
		device_type=X_ortvalue.device_name(),
		device_id=0,
		element_type=np.float32,
		shape=X_ortvalue.shape(),
		buffer_ptr=X_ortvalue.data_ptr()
	)
	io_binding.bind_output(
		name='output',
		device_type=Y_ortvalue.device_name(),
		device_id=0,
		element_type=np.float32,
		shape=Y_ortvalue.shape(),
		buffer_ptr=Y_ortvalue.data_ptr()
	)
	session.run_with_iobinding(io_binding)


Users can request *ONNX Runtime* to allocate an output on a device. This is particularly useful for dynamic shaped outputs.
Users can use the *get_outputs()* API to get access to the *OrtValue* (s) corresponding to the allocated output(s).
Users can thus consume the *ONNX Runtime* allocated memory for the output as an *OrtValue*.

.. code-block:: python

	#X is numpy array on cpu
	X_ortvalue = onnxruntime.OrtValue.ortvalue_from_numpy(X, 'cuda', 0)
	session = onnxruntime.InferenceSession(
		'model.onnx',
		providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
	)
	io_binding = session.io_binding()
	io_binding.bind_input(
		name='input',
		device_type=X_ortvalue.device_name(),
		device_id=0,
		element_type=np.float32,
		shape=X_ortvalue.shape(),
		buffer_ptr=X_ortvalue.data_ptr()
	)
	#Request ONNX Runtime to bind and allocate memory on CUDA for 'output'
	io_binding.bind_output('output', 'cuda')
	session.run_with_iobinding(io_binding)
	# The following call returns an OrtValue which has data allocated by ONNX Runtime on CUDA
	ort_output = io_binding.get_outputs()[0]


In addition, *ONNX Runtime* supports directly working with *OrtValue* (s) while inferencing a model if provided as part of the input feed.

Users can bind *OrtValue* (s) directly.

.. code-block:: python

	#X is numpy array on cpu
	#X is numpy array on cpu
	X_ortvalue = onnxruntime.OrtValue.ortvalue_from_numpy(X, 'cuda', 0)
	Y_ortvalue = onnxruntime.OrtValue.ortvalue_from_shape_and_type([3, 2], np.float32, 'cuda', 0)  # Change the shape to the actual shape of the output being bound
	session = onnxruntime.InferenceSession(
		'model.onnx',
		providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
	)
	io_binding = session.io_binding()
	io_binding.bind_ortvalue_input('input', X_ortvalue)
	io_binding.bind_ortvalue_output('output', Y_ortvalue)
	session.run_with_iobinding(io_binding)


You can also bind inputs and outputs directly to a PyTorch tensor.

.. code-block:: python

    # X is a PyTorch tensor on device
    session = onnxruntime.InferenceSession('model.onnx', providers=['CUDAExecutionProvider', 'CPUExecutionProvider']))
    binding = session.io_binding()

    X_tensor = X.contiguous()

    binding.bind_input(
        name='X',
        device_type='cuda',
        device_id=0,
        element_type=np.float32,
        shape=tuple(x_tensor.shape),
        buffer_ptr=x_tensor.data_ptr(),
        )

    ## Allocate the PyTorch tensor for the model output
    Y_shape = ... # You need to specify the output PyTorch tensor shape
    Y_tensor = torch.empty(Y_shape, dtype=torch.float32, device='cuda:0').contiguous()
    binding.bind_output(
        name='Y',
        device_type='cuda',
        device_id=0,
        element_type=np.float32,
        shape=tuple(Y_tensor.shape),
        buffer_ptr=Y_tensor.data_ptr(),
    )

    session.run_with_iobinding(binding)

You can also see code examples of this API in in the `ONNX Runtime inferences examples <https://github.com/microsoft/onnxruntime-inference-examples/blob/main/python/api/onnxruntime-python-api.py>`_.

Some onnx data type (like TensorProto.BFLOAT16, TensorProto.FLOAT8E4M3FN and TensorProto.FLOAT8E5M2) are not supported by Numpy. You can directly bind input or output with Torch tensor of corresponding data type
(like torch.bfloat16, torch.float8_e4m3fn and torch.float8_e5m2) in GPU memory.

.. code-block:: python

    x = torch.ones([3], dtype=torch.float8_e5m2, device='cuda:0')
    y = torch.empty([3], dtype=torch.bfloat16, device='cuda:0')

    binding = session.io_binding()
    binding.bind_input(
        name='X',
        device_type='cuda',
        device_id=0,
        element_type=TensorProto.FLOAT8E5M2,
        shape=tuple(x.shape),
        buffer_ptr=x.data_ptr(),
        )
    binding.bind_output(
        name='Y',
        device_type='cuda',
        device_id=0,
        element_type=TensorProto.BFLOAT16,
        shape=tuple(y.shape),
        buffer_ptr=y.data_ptr(),
        )
	session.run_with_iobinding(binding)


API Details
===========


InferenceSession
----------------

.. autoclass:: onnxruntime.InferenceSession
    :members:
    :inherited-members:

Options
-------

RunOptions
^^^^^^^^^^

.. autoclass:: onnxruntime.RunOptions
    :members:

SessionOptions
^^^^^^^^^^^^^^

.. autoclass:: onnxruntime.SessionOptions
    :members:

.. autoclass:: onnxruntime.ExecutionMode
    :members:

.. autoclass:: onnxruntime.ExecutionOrder
    :members:

.. autoclass:: onnxruntime.GraphOptimizationLevel
    :members:

.. autoclass:: onnxruntime.OrtAllocatorType
    :members:

.. autoclass:: onnxruntime.OrtArenaCfg
    :members:

.. autoclass:: onnxruntime.OrtMemoryInfo
    :members:

.. autoclass:: onnxruntime.OrtMemType
    :members:

Functions
---------

Allocators
^^^^^^^^^^

.. autofunction:: onnxruntime.create_and_register_allocator

.. autofunction:: onnxruntime.create_and_register_allocator_v2

Telemetry events
^^^^^^^^^^^^^^^^

.. autofunction:: onnxruntime.disable_telemetry_events

.. autofunction:: onnxruntime.enable_telemetry_events

Providers
^^^^^^^^^

.. autofunction:: onnxruntime.get_all_providers

.. autofunction:: onnxruntime.get_available_providers

Build, Version
^^^^^^^^^^^^^^

.. autofunction:: onnxruntime.get_build_info

.. autofunction:: onnxruntime.get_version_string

.. autofunction:: onnxruntime.has_collective_ops

Device
^^^^^^

.. autofunction:: onnxruntime.get_device

Logging
^^^^^^^

.. autofunction:: onnxruntime.set_default_logger_severity

.. autofunction:: onnxruntime.set_default_logger_verbosity

Random
^^^^^^

.. autofunction:: onnxruntime.set_seed

Data
----

OrtValue
^^^^^^^^

.. autoclass:: onnxruntime.OrtValue
    :members:

SparseTensor
^^^^^^^^^^^^

.. autoclass:: onnxruntime.SparseTensor
    :members:

Devices
-------

IOBinding
^^^^^^^^^

.. autoclass:: onnxruntime.IOBinding
    :members:

.. autoclass:: onnxruntime.SessionIOBinding
    :members:

OrtDevice
^^^^^^^^^

.. autoclass:: onnxruntime.OrtDevice
    :members:

Internal classes
----------------

These classes cannot be instantiated by users but they are returned
by methods or functions of this library.

ModelMetadata
^^^^^^^^^^^^^

.. autoclass:: onnxruntime.ModelMetadata
    :members:

NodeArg
^^^^^^^

.. autoclass:: onnxruntime.NodeArg
    :members:

Backend
=======

In addition to the regular API which is optimized for performance and usability, 
*ONNX Runtime* also implements the
`ONNX backend API <https://github.com/onnx/onnx/blob/main/docs/ImplementingAnOnnxBackend.md>`_
for verification of *ONNX* specification conformance.
The following functions are supported:

.. autofunction:: onnxruntime.backend.is_compatible

.. autofunction:: onnxruntime.backend.prepare

.. autofunction:: onnxruntime.backend.run

.. autofunction:: onnxruntime.backend.supports_device