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

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) str[source]

Displays an onnx prot in a better way.

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