-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add audonnx.device_to_providers() (#77)
* Add audonnx.device_to_providers() * Fix docstring * Add link to docstring
- Loading branch information
Showing
4 changed files
with
56 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import re | ||
import typing | ||
|
||
|
||
def device_to_providers( | ||
device: typing.Union[ | ||
str, | ||
typing.Tuple[str, typing.Dict], | ||
typing.Sequence[typing.Union[str, typing.Tuple[str, typing.Dict]]], | ||
], | ||
) -> typing.Sequence[typing.Union[str, typing.Tuple[str, typing.Dict]]]: | ||
r"""Converts device into a list of providers. | ||
Args: | ||
device: ``'cpu'``, | ||
``'cuda'``, | ||
``'cuda:<id>'``, | ||
or a (list of) `ONNX Runtime Execution Providers`_ | ||
Returns: | ||
sequence of `ONNX Runtime Execution Providers`_ | ||
Examples: | ||
>>> device_to_providers('cpu') | ||
['CPUExecutionProvider'] | ||
.. _ONNX Runtime Execution Providers: https://onnxruntime.ai/docs/execution-providers/ | ||
""" | ||
if isinstance(device, str): | ||
if device == 'cpu': | ||
providers = ['CPUExecutionProvider'] | ||
elif device.startswith('cuda'): | ||
match = re.search(r'^cuda:(\d+)$', device) | ||
if match: | ||
device_id = match.group(1) | ||
providers = [ | ||
( | ||
'CUDAExecutionProvider', { | ||
'device_id': device_id, | ||
} | ||
), | ||
] | ||
else: | ||
providers = ['CUDAExecutionProvider'] | ||
else: | ||
providers = [device] | ||
elif isinstance(device, tuple): | ||
providers = [device] | ||
else: | ||
providers = device | ||
return providers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,5 @@ audonnx | |
Model | ||
InputNode | ||
OutputNode | ||
device_to_providers | ||
load |