onnx_diagnostic.helpers

onnx_diagnostic.helpers.convert_endian(tensor: TensorProto) None[source]

Call to convert endianness of raw data in tensor.

Args:

tensor: TensorProto to be converted.

onnx_diagnostic.helpers.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.flatten_object(x: Any, drop_keys: bool = False) Any[source]

Flattens the object. It accepts some common classes used in deep learning.

Parameters:
  • x – any object

  • drop_keys – drop the keys if a dictionary is flattened. Keeps the order defined by the dictionary if False, sort them if True.

Returns:

flattened object

onnx_diagnostic.helpers.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.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.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.make_hash(obj: Any) str[source]

Returns a simple hash of id(obj) in four letter.

onnx_diagnostic.helpers.max_diff(expected: Any, got: Any, verbose: int = 0, level: int = 0, flatten: bool = False, debug_info: List[str] | None = None, begin: int = 0, end: int = -1, _index: int = 0, allow_unique_tensor_with_list_of_one_element: bool = True) Dict[str, float][source]

Returns the maximum discrepancy.

Parameters:
  • expected – expected values

  • got – values

  • verbose – verbosity level

  • level – for embedded outputs, used for debug purpposes

  • flatten – flatten outputs

  • debug_info – debug information

  • begin – first output to considered

  • end – last output to considered (-1 for the last one)

  • _index – used with begin and end

  • allow_unique_tensor_with_list_of_one_element – allow a comparison between a single tensor and a list of one tensor

Returns:

dictionary with many values

  • abs: max absolute error

  • rel: max relative error

  • sum: sum of the errors

  • n: number of outputs values, if there is one

    output, this number will be the number of elements of this output

  • dnan: difference in the number of nan

You may use string_diff() to display the discrepancies in one string.

onnx_diagnostic.helpers.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_dtype_name(itype: int) str[source]

Returns the ONNX name for a specific element type.

<<<

import onnx
from onnx_diagnostic.helpers import onnx_dtype_name

itype = onnx.TensorProto.BFLOAT16
print(onnx_dtype_name(itype))
print(onnx_dtype_name(7))

>>>

    BFLOAT16
    INT64
onnx_diagnostic.helpers.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_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.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.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.rename_dynamic_dimensions(constraints: Dict[str, Set[str]], original: Set[str], ban_prefix: str = 'DYN') Dict[str, str][source]

Renames dynamic shapes as requested by the user. torch.export.export() uses many names for dynamic dimensions. When building the onnx model, some of them are redundant and can be replaced by the name provided by the user.

Parameters:
  • constraints – exhaustive list of used names and all the values equal to it

  • original – the names to use if possible

  • ban_prefix – avoid any rewriting by a constant starting with this prefix

Returns:

replacement dictionary

onnx_diagnostic.helpers.rename_dynamic_expression(expression: str, replacements: Dict[str, str])[source]

Renames variables of an expression.

Parameters:
  • expression – something like s15 + seq_length

  • replacements – replacements to make

Returns:

new string

onnx_diagnostic.helpers.size_type(dtype: Any) int[source]

Returns the element size for an element type.

onnx_diagnostic.helpers.string_diff(diff: Dict[str, Any]) str[source]

Renders discrepancies return by max_diff() into one string.

onnx_diagnostic.helpers.string_sig(f: Callable, kwargs: Dict[str, Any] | None = None) str[source]

Displays the signature of a functions if the default if the given value is different from

onnx_diagnostic.helpers.string_signature(sig: Any) str[source]

Displays the signature of a functions.

onnx_diagnostic.helpers.string_type(obj: Any, with_shape: bool = False, with_min_max: bool = False, with_device: bool = False, ignore: bool = False, limit: int = 10) str[source]

Displays the types of an object as a string.

Parameters:
  • obj – any

  • with_shape – displays shapes as well

  • with_min_max – displays information about the values

  • with_device – display the device

  • ignore – if True, just prints the type for unknown types

Returns:

str

<<<

from onnx_diagnostic.helpers import string_type

print(string_type((1, ["r", 6.6])))

>>>

    (int,#2[str,float])

With pytorch:

<<<

import torch
from onnx_diagnostic.helpers import string_type

inputs = (
    torch.rand((3, 4), dtype=torch.float16),
    [
        torch.rand((5, 6), dtype=torch.float16),
        torch.rand((5, 6, 7), dtype=torch.float16),
    ],
)

# with shapes
print(string_type(inputs, with_shape=True))

# with min max
print(string_type(inputs, with_shape=True, with_min_max=True))

>>>

    (T10s3x4,#2[T10s5x6,T10s5x6x7])
    (T10s3x4[0.03515625,0.97705078125:A0.4762776692708333],#2[T10s5x6[0.033203125,0.986328125:A0.574560546875],T10s5x6x7[0.00244140625,0.9970703125:A0.5127813430059524]])
onnx_diagnostic.helpers.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.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.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

onnx_diagnostic.helpers.type_info(itype: int, att: str)[source]

Returns the minimum or maximum value for a type.

Parameters:
  • itype – onnx type

  • att – ‘min’ or ‘max’

Returns:

value