TensorPlay
latest (dev)
Copy
View Markdown

Latest development documentation · Updated 2026-10-08

tensorplay.testing.assert_close

tensorplay.testing.assert_close(actual: Any, expected: Any, *, allow_subclasses: bool = True, rtol: float | None = None, atol: float | None = None, equal_nan: bool = False, check_device: bool = True, check_dtype: bool = True, check_layout: bool = True, check_stride: bool = False, msg: str | Callable[[str], str] | None = None)[source]

Asserts that actual and expected are close.

If actual and expected are strided and finite, they are considered close if

∣actual−expected∣≤atol+rtol⋅∣expected∣\lvert \text{actual} - \text{expected} \rvert \le \texttt{atol} + \texttt{rtol} \cdot \lvert \text{expected} \rvert

Non-finite values (-inf and inf) are only considered close if and only if they are equal. NaN’s are only considered equal to each other if equal_nan is True.

In addition, they are only considered close if they have the same

  • device (if check_device is True),

  • dtype (if check_dtype is True),

  • layout (if check_layout is True), and

  • stride (if check_stride is True).

If either actual or expected is a scalar or a nested python container, the other side is converted to a tensor-like value before the comparison.

Parameters:
  • actual (Any) – Actual input.

  • expected (Any) – Expected input.

  • allow_subclasses (bool) – If True (default) and other than exact type match, inputs that are subclasses of each other are considered close.

  • rtol (Optional[float]) – Relative tolerance. If specified atol must also be specified. If omitted, default values based on the dtype are selected. See below for details.

  • atol (Optional[float]) – Absolute tolerance. If specified rtol must also be specified. If omitted, default values based on the dtype are selected. See below for details.

  • equal_nan (bool) – If True, two NaN values are considered equal. Defaults to False.

  • check_device (bool) – If True (default), asserts that corresponding tensors are on the same device.

  • check_dtype (bool) – If True (default), asserts that corresponding tensors have the same dtype.

  • check_layout (bool) – If True (default), asserts that corresponding tensors have the same layout.

  • check_stride (bool) – If True, asserts that corresponding strided tensors have the same stride.

  • msg (Optional[Union[str, Callable[[str], str]]]) – Optional error message to use in case of failure.

Raises:

Default tolerances by dtype:

dtype

rtol

atol

float16

1e-3

1e-5

bfloat16

1.6e-2

1e-5

float32

1.3e-6

1e-5

float64

1e-7

1e-7

complex64

1.3e-6

1e-5

complex128

1e-7

1e-7

Note

Tensors are compared elementwise, allowing for a relative and an absolute tolerance per element. If both tolerances are omitted, the loosest tolerance of the involved dtypes is selected.

On this page

Ask DeepWiki