MCPcopy Create free account
hub / github.com/PaddlePaddle/Paddle / assert_close

Function assert_close

python/paddle/testing/_comparison.py:876–1062  ·  view source on GitHub ↗

r""" Asserts that ``actual`` and ``expected`` are close. If ``actual`` and ``expected`` are real-valued, and finite, they are considered close if .. math:: \lvert \text{actual} - \text{expected} \rvert \le \texttt{atol} + \texttt{rtol} \cdot \lvert \text{expected} \rvert

(
    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,
    msg: str | Callable[[str], str] | None = None,
)

Source from the content-addressed store, hash-verified

874
875
876def assert_close(
877 actual: Any,
878 expected: Any,
879 *,
880 allow_subclasses: bool = True,
881 rtol: float | None = None,
882 atol: float | None = None,
883 equal_nan: bool = False,
884 check_device: bool = True,
885 check_dtype: bool = True,
886 msg: str | Callable[[str], str] | None = None,
887) -> None:
888 r"""
889 Asserts that ``actual`` and ``expected`` are close.
890
891 If ``actual`` and ``expected`` are real-valued, and finite, they are considered close if
892
893 .. math::
894
895 \lvert \text{actual} - \text{expected} \rvert \le \texttt{atol} + \texttt{rtol} \cdot \lvert \text{expected} \rvert
896
897 Non-finite values (``-inf`` and ``inf``) are only considered close if and only if they are equal. ``NaN``'s are
898 only considered equal to each other if ``equal_nan`` is ``True``.
899
900 In addition, they are only considered close if they have the same
901
902 - :attr:`~paddle.Tensor.place` (if ``check_device`` is ``True``),
903 - ``dtype`` (if ``check_dtype`` is ``True``),
904
905 In static graph mode, only the check_dtype attribute verification will be performed.
906
907 ``actual`` and ``expected`` can be :class:`~paddle.Tensor`'s or any tensor-or-scalar-likes from which
908 :class:`paddle.Tensor`'s can be constructed with :func:`paddle.to_tensor`. Except for Python scalars the input types
909 have to be directly related. In addition, ``actual`` and ``expected`` can be :class:`~collections.abc.Sequence`'s
910 or :class:`~collections.abc.Mapping`'s in which case they are considered close if their structure matches and all
911 their elements are considered close according to the above definition.
912
913 .. note::
914
915 Python scalars are an exception to the type relation requirement, because their :func:`type`, i.e.
916 :class:`int`, :class:`float`, and :class:`complex`, is equivalent to the ``dtype`` of a tensor-like. Thus,
917 Python scalars of different types can be checked, but require ``check_dtype=False``.
918
919 Args:
920 actual (Any): Actual input.
921 expected (Any): Expected input.
922 allow_subclasses (bool): If ``True`` (default) and except for Python scalars, inputs of directly related types
923 are allowed. Otherwise type equality is required.
924 rtol (float, optional): Relative tolerance. If specified ``atol`` must also be specified. If omitted, default
925 values based on the :attr:`~paddle.Tensor.dtype` are selected with the below table.
926 atol (float, optional): Absolute tolerance. If specified ``rtol`` must also be specified. If omitted, default
927 values based on the :attr:`~paddle.Tensor.dtype` are selected with the below table.
928 equal_nan (bool|str, optional): If ``True``, two ``NaN`` values will be considered equal.
929 check_device (bool): If ``True`` (default), asserts that corresponding tensors are on the same
930 :attr:`~paddle.Tensor.place`. If this check is disabled, tensors on different
931 :attr:`~paddle.Tensor.place`'s are moved to the CPU before being compared.
932 check_dtype (bool): If ``True`` (default), asserts that corresponding tensors have the same ``dtype``. If this
933 check is disabled, tensors with different ``dtype``'s are promoted to a common ``dtype`` before being compared.

Callers 15

test_scalars_mismatchMethod · 0.90
test_numpy_scalarsMethod · 0.90
test_nan_handlingMethod · 0.90
test_sequencesMethod · 0.90
test_mappingsMethod · 0.90

Calls 2

not_close_error_metasFunction · 0.85
to_errorMethod · 0.80

Tested by 15

test_scalars_mismatchMethod · 0.72
test_numpy_scalarsMethod · 0.72
test_nan_handlingMethod · 0.72
test_sequencesMethod · 0.72
test_mappingsMethod · 0.72