Nx.Testing (Nx v0.13.0)

Copy Markdown View Source

Testing functions for Nx tensor assertions.

This module provides functions for asserting tensor equality and approximate equality within specified tolerances. Both helpers handle vectorized tensors and produce a numeric diagnostic (max absolute / relative difference) on failure so that bit-level disagreements hidden by truncated inspect output are still diagnosable.

Summary

Functions

Asserts that two tensors are approximately equal within the given tolerances.

Asserts that two tensors are exactly equal.

Functions

assert_all_close(left, right, opts \\ [])

Asserts that two tensors are approximately equal within the given tolerances.

Works with vectorized tensors — the comparison is per vectorized instance, then aggregated. Two tensors must share the same vectorized axes to be comparable.

See also:

  • Nx.all_close/2 - The underlying function that performs the comparison.

Options

  • :atol - The absolute tolerance. Defaults to 1.0e-4.
  • :rtol - The relative tolerance. Defaults to 1.0e-4.

assert_equal(left, right)

Asserts that two tensors are exactly equal.

This handles NaN values correctly by considering NaN == NaN as true. Works with vectorized tensors — two tensors must share the same vectorized axes to be considered equal.