You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.py 2.6 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # -*- coding: utf-8 -*-
  2. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  3. #
  4. # Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  5. #
  6. # Unless required by applicable law or agreed to in writing,
  7. # software distributed under the License is distributed on an
  8. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. from ..core._imperative_rt.core2 import apply
  10. from ..core._imperative_rt.core2 import sync as _sync
  11. from ..core.ops.builtin import AssertEqual
  12. from ..tensor import Tensor
  13. from ..utils.deprecation import deprecated_func
  14. from .elemwise import abs, maximum, minimum
  15. from .tensor import ones, zeros
  16. __all__ = ["topk_accuracy"]
  17. def _assert_equal(
  18. expect: Tensor, actual: Tensor, *, maxerr: float = 0.0001, verbose: bool = False
  19. ):
  20. r"""Asserts two tensors equal and returns expected value (first input).
  21. It is a variant of python assert which is symbolically traceable (similar to ``numpy.testing.assert_equal``).
  22. If we want to verify the correctness of model, just ``assert`` its states and outputs.
  23. While sometimes we need to verify the correctness at different backends for *dumped* model
  24. (or in :class:`~jit.trace` context), and no python code could be executed in that case.
  25. Thus we have to use :func:`~functional.utils._assert_equal` instead.
  26. Args:
  27. expect: expected tensor value
  28. actual: tensor to check value
  29. maxerr: max allowed error; error is defined as the minimal of absolute and relative error
  30. verbose: whether to print maxerr to stdout during opr exec
  31. Examples:
  32. .. testcode::
  33. import numpy as np
  34. from megengine import tensor
  35. import megengine.functional as F
  36. x = tensor([1, 2, 3], np.float32)
  37. y = tensor([1, 2, 3], np.float32)
  38. print(F.utils._assert_equal(x, y, maxerr=0).numpy())
  39. Outputs:
  40. .. testoutput::
  41. [1. 2. 3.]
  42. """
  43. err = (
  44. abs(expect - actual)
  45. / maximum(minimum(abs(expect), abs(actual)), Tensor(1.0, dtype="float32"))
  46. ).max()
  47. result = apply(AssertEqual(maxerr=maxerr, verbose=verbose), expect, actual, err)[0]
  48. _sync() # sync interpreter to get exception
  49. return result
  50. def _simulate_error():
  51. x1 = zeros(100)
  52. x2 = ones(100)
  53. (ret,) = apply(AssertEqual(maxerr=0, verbose=False), x1, x2, x2)
  54. return ret
  55. topk_accuracy = deprecated_func(
  56. "1.3", "megengine.functional.metric", "topk_accuracy", True
  57. )
  58. copy = deprecated_func("1.3", "megengine.functional.tensor", "copy", True)