MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / _assert_equal

Function _assert_equal

imperative/python/megengine/functional/utils.py:13–46  ·  view source on GitHub ↗

r"""Asserts two tensors equal and returns expected value (first input). It is a variant of python assert which is symbolically traceable (similar to ``numpy.testing.assert_equal``). If we want to verify the correctness of model, just ``assert`` its states and outputs. While sometimes we

(
    expect: Tensor, actual: Tensor, *, maxerr: float = 0.0001, verbose: bool = False
)

Source from the content-addressed store, hash-verified

11
12
13def _assert_equal(
14 expect: Tensor, actual: Tensor, *, maxerr: float = 0.0001, verbose: bool = False
15):
16 r"""Asserts two tensors equal and returns expected value (first input).
17 It is a variant of python assert which is symbolically traceable (similar to ``numpy.testing.assert_equal``).
18 If we want to verify the correctness of model, just ``assert`` its states and outputs.
19 While sometimes we need to verify the correctness at different backends for *dumped* model
20 (or in :class:`~jit.trace` context), and no python code could be executed in that case.
21 Thus we have to use :func:`~functional.utils._assert_equal` instead.
22
23 Args:
24 expect: expected tensor value
25 actual: tensor to check value
26 maxerr: max allowed error; error is defined as the minimal of absolute and relative error
27 verbose: whether to print maxerr to stdout during opr exec
28
29 Examples:
30
31 >>> x = Tensor([1, 2, 3], dtype="float32")
32 >>> y = Tensor([1, 2, 3], dtype="float32")
33 >>> F.utils._assert_equal(x, y, maxerr=0)
34 Tensor([1. 2. 3.], device=xpux:0)
35
36 """
37 err = (
38 abs(expect - actual)
39 / maximum(
40 minimum(abs(expect), abs(actual)),
41 Tensor(1.0, dtype="float32", device=expect.device),
42 )
43 ).max()
44 result = apply(AssertEqual(maxerr=maxerr, verbose=verbose), expect, actual, err)[0]
45 _sync() # sync interpreter to get exception
46 return result
47
48
49def _simulate_error():

Callers

nothing calls this directly

Calls 7

maximumFunction · 0.85
minimumFunction · 0.85
AssertEqualClass · 0.85
absFunction · 0.70
TensorClass · 0.50
applyFunction · 0.50
maxMethod · 0.45

Tested by

no test coverage detected