onnx_light.ext_test_case#

class onnx_light.ext_test_case.ExtTestCase(methodName='runTest')#
assertAlmostEqual(expected: ndarray, value: ndarray, atol: float = 0, rtol: float = 0)#

Fail if the two objects are unequal as determined by their difference rounded to the given number of decimal places (default 7) and comparing to zero, or by comparing that the difference between the two objects is more than the given delta.

Note that decimal places (from zero) are usually not the same as significant digits (measured from the most significant digit).

If the two objects compare equal then they will automatically compare almost equal.

assertIns(sub: Tuple[Any, ...], s: str)#

Checks that one of the substrings in sub is part of s.

assertNotAlmostEqual(expected: ndarray, value: ndarray, atol: float = 0, rtol: float = 0)#

Fail if the two objects are equal as determined by their difference rounded to the given number of decimal places (default 7) and comparing to zero, or by comparing that the difference between the two objects is less than the given delta.

Note that decimal places (from zero) are usually not the same as significant digits (measured from the most significant digit).

Objects that are equal automatically fail.

capture(fct: Callable) Tuple[Any, str, str]#

Runs a function and capture standard output and error.

Parameters:

fct – function to run

Returns:

result of fct, output, error

dump_onnx(name: str, proto: Any, folder: str | None = None) str#

Dumps an onnx file.

get_dump_file(name: str, folder: str | None = None, clean: bool = False) str#

Returns a filename to dump a model.

shortDescription()#

Returns a one-line description of the test, or None if no description has been provided.

The default implementation of this method returns the first line of the specified test method’s docstring.

classmethod tearDownClass()#

Hook method for deconstructing the class fixture after running all tests in the class.

tryCall(fct: Callable, msg: str | None = None, none_if: str | None = None) Any | None#

Calls the function, catch any error.

Parameters:
  • fct – function to call

  • msg – error message to display if failing

  • none_if – returns None if this substring is found in the error message

Returns:

output of fct

class onnx_light.ext_test_case.InferenceSessionAllTypes(model: ModelProto, providers: List[str] | None = None)#

Wrapper around onnxruntime.InferenceSession that supports all ONNX dtypes.

Uses IOBinding with raw memory buffers to support dtypes that ORT doesn’t natively handle through NumPy conversion (FLOAT8, BFLOAT16, INT2, INT4, etc.). Creates an inference session.

Parameters:
  • model_bytes – ONNX model

  • providers – List of execution providers (defaults to [“CPUExecutionProvider”])

classmethod mapping_numpy_dtype_to_onnx()#

Returns mapping from NumPy dtype to ONNX TensorProto data type.

classmethod mapping_ort_type_name_to_numpy_dtype()#

Returns mapping for special dtypes that need IOBinding.

Maps ONNX dtype to (numpy view dtype, onnx tensor element type).

classmethod mapping_sub_byte_types()#

Returns the sub-byte ONNX dtypes ORT packs into bytes through IOBinding.

ORT stores these types packed (several values per byte), so the wrapper packs inputs and unpacks outputs around the raw memory buffers.

Returns:

A dictionary mapping the ORT type name (e.g. "tensor(int2)") to a tuple (bits, signed, onnx_element_type, numpy_dtype_name).

run(output_names: List[str] | None, input_feed: dict[str, ndarray]) List[ndarray]#

Runs the model with support for all ONNX dtypes.

Uses IOBinding for special dtypes that ORT doesn’t natively support.

Parameters:
  • output_names – Names of outputs to compute (None = all outputs)

  • input_feed – Dictionary mapping input names to numpy arrays

Returns:

List of output arrays from the model

onnx_light.ext_test_case.has_ir_py() bool#

Tells if ir-py is installed.

onnx_light.ext_test_case.has_onnxruntime() bool#

Tells if onnxruntime is installed.

onnx_light.ext_test_case.hide_stdout(f: Callable | None = None) Callable#

Catches warnings, hides standard output. The function may be disabled by setting UNHIDE=1 before running the unit test.

Parameters:

f – the function is called with the stdout as an argument

onnx_light.ext_test_case.ignore_errors(errors: Exception | Tuple[Exception]) Callable#

Catches exception, skip the test if the error is expected sometimes.

Parameters:

errors – errors to ignore

onnx_light.ext_test_case.ignore_warnings(warns: Sequence[type[Warning]]) Callable#

Catches warnings.

Parameters:

warns – warnings to ignore

onnx_light.ext_test_case.import_or_skip(module_name: str, attribute: str | None = None) Any#

Imports a module (or one of its attributes) or skips the test otherwise.

onnx-light can be built without the operator-kernel runtime and the backend-test registries (ONNX_LIGHT_BUILD_KERNELS=OFF). In that reduced build the kernel/backend Python modules raise ImportError. Tests that depend on them call this helper, at module level or inside a test, to skip themselves cleanly instead of failing.

Parameters:
  • module_name – The fully-qualified name of the module to import.

  • attribute – An optional attribute to return from the imported module.

Returns:

The imported module, or getattr(module, attribute) when attribute is provided.

Raises:

unittest.SkipTest – When the module (or attribute) cannot be imported.

onnx_light.ext_test_case.skipif_ci_apple(msg) Callable#

Skips a unit test if it runs on Macosx.

onnx_light.ext_test_case.skipif_ci_windows(msg) Callable#

Skips a unit test if it runs on Windows.

onnx_light.ext_test_case.skipif_unstable(msg) Callable#

Skips a unit test if the environment variable SKIP_UNSTABLE is set to 1.

onnx_light.ext_test_case.unit_test_going()#

Enables a flag telling the script is running while testing it. Avois unit tests to be very long.