onnx_diagnostic.helpers.onnx_helper¶
- onnx_diagnostic.helpers.onnx_helper.check_model_ort(onx: ModelProto, providers: str | List[Any] | None = None, dump_file: str | None = None) onnxruntime.InferenceSession [source]¶
Loads a model with onnxruntime.
- Parameters:
onx – ModelProto
providers – list of providers, None fur CPU, cpu for CPU, cuda for CUDA
dump_file – if not empty, dumps the model into this file if an error happened
- Returns:
InferenceSession
- onnx_diagnostic.helpers.onnx_helper.convert_endian(tensor: TensorProto) None [source]¶
Call to convert endianness of raw data in tensor.
- Args:
tensor: TensorProto to be converted.
- onnx_diagnostic.helpers.onnx_helper.dtype_to_tensor_dtype(dt: dtype | torch.dtype) int [source]¶
Converts a torch dtype or numpy dtype into a onnx element type.
- Parameters:
to – dtype
- Returns:
onnx type
- onnx_diagnostic.helpers.onnx_helper.from_array_extended(tensor: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes], name: str | None = None) TensorProto [source]¶
Converts an array into a
onnx.TensorProto
.- Parameters:
tensor – numpy array or torch tensor
name – name
- Returns:
TensorProto
- onnx_diagnostic.helpers.onnx_helper.from_array_ml_dtypes(arr: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes], name: str | None = None) TensorProto [source]¶
Converts a numpy array to a tensor def assuming the dtype is defined in ml_dtypes.
- Args:
arr: a numpy array. name: (optional) the name of the tensor.
- Returns:
TensorProto: the converted tensor def.
- onnx_diagnostic.helpers.onnx_helper.get_onnx_signature(model: ModelProto) Tuple[Tuple[str, Any], ...] [source]¶
Produces a tuple of tuples corresponding to the signatures.
- Parameters:
model – model
- Returns:
signature
- onnx_diagnostic.helpers.onnx_helper.np_dtype_to_tensor_dtype(dt: dtype) int [source]¶
Converts a numpy dtype into a onnx element type.
- Parameters:
to – dtype
- Returns:
onnx type
- onnx_diagnostic.helpers.onnx_helper.onnx_dtype_name(itype: int) str [source]¶
Returns the ONNX name for a specific element type.
<<<
import onnx from onnx_diagnostic.helpers.onnx_helper import onnx_dtype_name itype = onnx.TensorProto.BFLOAT16 print(onnx_dtype_name(itype)) print(onnx_dtype_name(7))
>>>
BFLOAT16 INT64
- onnx_diagnostic.helpers.onnx_helper.onnx_dtype_to_np_dtype(itype: int) Any [source]¶
Converts an onnx type into a to numpy dtype. That includes ml_dtypes dtypes.
- Parameters:
to – onnx dtype
- Returns:
numpy dtype
- onnx_diagnostic.helpers.onnx_helper.onnx_dtype_to_torch_dtype(itype: int) torch.dtype [source]¶
Converts an onnx type into a torch dtype.
- Parameters:
to – onnx dtype
- Returns:
torch dtype
- onnx_diagnostic.helpers.onnx_helper.onnx_find(onx: str | ModelProto, verbose: int = 0, watch: Set[str] | None = None) List[NodeProto | TensorProto] [source]¶
Looks for node producing or consuming some results.
- Parameters:
onx – model
verbose – verbosity
watch – names to search for
- Returns:
list of nodes
- onnx_diagnostic.helpers.onnx_helper.onnx_lighten(onx: str | ModelProto, verbose: int = 0) Tuple[ModelProto, Dict[str, Dict[str, float]]] [source]¶
Creates a model without big initializers but stores statistics into dictionaries. The function can be reversed with
onnx_diagnostic.helpers.onnx_helper.onnx_unlighten()
. The model is modified inplace.- Parameters:
onx – model
verbose – verbosity
- Returns:
new model, statistics
- onnx_diagnostic.helpers.onnx_helper.onnx_unlighten(onx: str | ModelProto, stats: Dict[str, Dict[str, float]] | None = None, verbose: int = 0) ModelProto [source]¶
Function fixing the model produced by function
onnx_diagnostic.helpers.onnx_helper.onnx_lighten()
. The model is modified inplace.- Parameters:
onx – model
stats – statistics, can be None if onx is a file, then it loads the file
<filename>.stats
, it assumes it is json formatverbose – verbosity
- Returns:
new model, statistics
- onnx_diagnostic.helpers.onnx_helper.pretty_onnx(onx: FunctionProto | GraphProto | ModelProto | ValueInfoProto | str, with_attributes: bool = False, highlight: Set[str] | None = None, shape_inference: bool = False) str [source]¶
Displays an onnx prot in a better way.
- Parameters:
with_attributes – displays attributes as well, if only a node is printed
highlight – to highlight some names
shape_inference – run shape inference before printing the model
- Returns:
text
- onnx_diagnostic.helpers.onnx_helper.proto_from_tensor(arr: torch.Tensor, name: str | None = None, verbose: int = 0) TensorProto [source]¶
Converts a torch Tensor into a TensorProto.
- Parameters:
arr – tensor
verbose – display the type and shape
- Returns:
a TensorProto
- onnx_diagnostic.helpers.onnx_helper.tensor_dtype_to_np_dtype(tensor_dtype: int) dtype [source]¶
Converts a TensorProto’s data_type to corresponding numpy dtype. It can be used while making tensor.
- Parameters:
tensor_dtype – TensorProto’s data_type
- Returns:
numpy’s data_type
- onnx_diagnostic.helpers.onnx_helper.to_array_extended(proto: TensorProto) Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes] [source]¶
Converts
onnx.TensorProto
into a numpy array.
- onnx_diagnostic.helpers.onnx_helper.torch_dtype_to_onnx_dtype(to: torch.dtype) int [source]¶
Converts a torch dtype into a onnx element type.
- Parameters:
to – torch dtype
- Returns:
onnx type