.helpers

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

Call to convert endianness of raw data in tensor.

Args:

tensor: TensorProto to be converted.

experimental_experiment.helpers.dtype_to_tensor_dtype(dt: dtype) int[source]

Converts a torch dtype or numpy dtype into a onnx element type.

Parameters:

to – dtype

Returns:

onnx type

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

experimental_experiment.helpers.from_array_extended(tensor: ndarray, name: str | None = None) TensorProto[source]

Converts an array into a TensorProto.

Parameters:
  • tensor – numpy array

  • name – name

Returns:

TensorProto

experimental_experiment.helpers.from_array_ml_dtypes(arr: ndarray, 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.

experimental_experiment.helpers.get_onnx_signature(model: ModelProto) Tuple[Tuple[str, Any], ...][source]

Produces a tuple of tuples correspinding to the signatures.

Parameters:

model – model

Returns:

signature

experimental_experiment.helpers.make_hash(obj: Any) str[source]

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

experimental_experiment.helpers.np_dtype_to_tensor_dtype(dt: dtype) int[source]

Converts a tnumpy dtype into a onnx element type.

Parameters:

to – dtype

Returns:

onnx type

experimental_experiment.helpers.onnx_dtype_name(itype: int) str[source]

Returns the ONNX name for a specific element type.

experimental_experiment.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

experimental_experiment.helpers.pretty_onnx(onx: FunctionProto | GraphProto | ModelProto | ValueInfoProto | str, with_attributes: bool = False, highlight: Set[str] | None = None) 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

Returns:

text

experimental_experiment.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 name 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

experimental_experiment.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

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

Returns the element size for an element type.

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

Renders discrepancies return by onnx_diagnostic.helpers.max_diff() into one string.

experimental_experiment.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

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

Displays the signature of a functions.

experimental_experiment.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 experimental_experiment.helpers import string_type

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

>>>

    (int,#2[str,float])
experimental_experiment.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

experimental_experiment.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

experimental_experiment.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