# tensorplay.testing API Source: https://www.tensorplay.cn/docs/api/tensorplay.testing.html ## Functions 5 [#](#api-tensorplay.testing.assert_allclose) ### assert_allclose function[Full reference ↗](/docs/generated/tensorplay.testing.assert_allclose.html) ```python tensorplay.testing.assert_allclose(actual: Any, expected: Any, rtol: float | None = None, atol: float | None = None, equal_nan: bool = True, msg: str = '') → None ``` Legacy alias of [assert_close()](/docs/generated/tensorplay.testing.assert_close.html#tensorplay.testing.assert_close) with positional tolerances. [#](#api-tensorplay.testing.assert_close) ### assert_close function[Full reference ↗](/docs/generated/tensorplay.testing.assert_close.html) ```python 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) ``` Asserts that actual and expected are close. If actual and expected are strided and finite, they are considered close if $$\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](https://docs.python.org/3/builtins/functions.html#bool)) – If True (default) and other than exact type match, inputs that are subclasses of each other are considered close. - rtol (Optional[[float](https://docs.python.org/3/builtins/functions.html#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](https://docs.python.org/3/builtins/functions.html#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](https://docs.python.org/3/builtins/functions.html#bool)) – If True, two NaN values are considered equal. Defaults to False. - check_device ([bool](https://docs.python.org/3/builtins/functions.html#bool)) – If True (default), asserts that corresponding tensors are on the same device. - check_dtype ([bool](https://docs.python.org/3/builtins/functions.html#bool)) – If True (default), asserts that corresponding tensors have the same dtype. - check_layout ([bool](https://docs.python.org/3/builtins/functions.html#bool)) – If True (default), asserts that corresponding tensors have the same layout. - check_stride ([bool](https://docs.python.org/3/builtins/functions.html#bool)) – If True, asserts that corresponding strided tensors have the same stride. - msg (Optional[Union[[str](https://docs.python.org/3/builtins/stdtypes.html#str), Callable[[[str](https://docs.python.org/3/builtins/stdtypes.html#str)], [str](https://docs.python.org/3/builtins/stdtypes.html#str)]]]) – Optional error message to use in case of failure. Raises: - [ValueError](https://docs.python.org/3/builtins/exceptions.html#ValueError) – If only rtol or only atol is specified. - [AssertionError](https://docs.python.org/3/builtins/exceptions.html#AssertionError) – If corresponding values are not close. 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. [#](#api-tensorplay.testing.default_tolerances) ### default_tolerances function[Full reference ↗](/docs/generated/tensorplay.testing.default_tolerances.html) ```python tensorplay.testing.default_tolerances(*inputs: Tensor | dtype, dtype_precisions: dict[dtype, tuple[float, float]] | None = None) → tuple[float, float] ``` Returns the default absolute and relative testing tolerances for a set of inputs based on the dtype. Returns: Loosest tolerances of all input dtypes. Return type: (Tuple[[float](https://docs.python.org/3/builtins/functions.html#float), [float](https://docs.python.org/3/builtins/functions.html#float)]) [#](#api-tensorplay.testing.get_tolerances) ### get_tolerances function[Full reference ↗](/docs/generated/tensorplay.testing.get_tolerances.html) ```python tensorplay.testing.get_tolerances(*inputs: Tensor | dtype, rtol: float | None, atol: float | None, id: tuple[Any, ...] = ()) → tuple[float, float] ``` Gets absolute and relative tolerances to be used for numeric comparisons. If both rtol and atol are specified, this is a no-op. If neither is specified, [default_tolerances()](/docs/generated/tensorplay.testing.default_tolerances.html#tensorplay.testing.default_tolerances) is used. Specifying only one raises a [ValueError](https://docs.python.org/3/builtins/exceptions.html#ValueError), since a single tolerance might lead to surprising results. [#](#api-tensorplay.testing.make_tensor) ### make_tensor function[Full reference ↗](/docs/generated/tensorplay.testing.make_tensor.html) ```python tensorplay.testing.make_tensor(*shape: int | Size | list[int] | tuple[int, ...], dtype: dtype, device: str | device, low: float | None = None, high: float | None = None, requires_grad: bool = False, noncontiguous: bool = False, exclude_zero: bool = False) → Tensor ``` Creates a tensor with the given shape, device, and dtype, filled with values drawn uniformly from [low, high). If low or high are outside the range of the dtype’s representable finite values, they are clamped to the lowest or highest representable finite value, respectively. If None, they default to -9 and 9 respectively (0 and 2 for bool). Parameters: - shape (Tuple[[int](https://docs.python.org/3/builtins/functions.html#int), ...]) – Single integer or a collection of integers defining the shape of the output tensor. - dtype ([tensorplay.dtype](/docs/generated/tensorplay.DType.html#tensorplay.DType)) – The data type of the returned tensor. - device (Union[[str](https://docs.python.org/3/builtins/stdtypes.html#str), [tensorplay.device](/docs/generated/tensorplay.Device.html#tensorplay.Device)]) – The device of the returned tensor. - low (Optional[Number]) – Sets the lower limit of the range of the values in the returned tensor. - high (Optional[Number]) – Sets the upper limit of the range of the values in the returned tensor. - requires_grad ([bool](https://docs.python.org/3/builtins/functions.html#bool)) – If True, the returned tensor is set to require gradient. - noncontiguous ([bool](https://docs.python.org/3/builtins/functions.html#bool)) – If True, the returned tensor is non-contiguous. - exclude_zero ([bool](https://docs.python.org/3/builtins/functions.html#bool)) – If True, zeros in the returned tensor are replaced by the smallest normal value of the dtype (1 for boolean and integral dtypes). Raises: - [ValueError](https://docs.python.org/3/builtins/exceptions.html#ValueError) – If low >= high, or if the interval [low, high) does not intersect the dtype’s representable range. - [TypeError](https://docs.python.org/3/builtins/exceptions.html#TypeError) – For unsupported dtypes. ## Classes 1 [#](#api-tensorplay.testing.FileCheck) ### FileCheck class[Full reference ↗](/docs/generated/tensorplay.testing.FileCheck.html) ```python class tensorplay.testing.FileCheck ``` ```python check(self: tensorplay._C.FileCheck, test_string: str) → tensorplay._C.FileCheck ``` ```python check_count(self: tensorplay._C.FileCheck, test_string: str, count: SupportsInt | SupportsIndex, exactly: bool = False) → tensorplay._C.FileCheck ``` ```python check_dag(self: tensorplay._C.FileCheck, test_string: str) → tensorplay._C.FileCheck ``` ```python check_next(self: tensorplay._C.FileCheck, test_string: str) → tensorplay._C.FileCheck ``` ```python check_not(self: tensorplay._C.FileCheck, test_string: str) → tensorplay._C.FileCheck ``` ```python check_regex(self: tensorplay._C.FileCheck, test_string: str) → tensorplay._C.FileCheck ``` ```python check_same(self: tensorplay._C.FileCheck, test_string: str) → tensorplay._C.FileCheck ``` ```python check_source_highlighted(self: tensorplay._C.FileCheck, test_string: str) → tensorplay._C.FileCheck ``` ```python run(self: tensorplay._C.FileCheck, test_string: str) → None ```