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.

__init__.py 1.5 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # -*- coding: utf-8 -*-
  2. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  3. #
  4. # Copyright (c) 2014-2020 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. import numpy as np
  10. def assertTensorClose(v0, v1, *, max_err=1e-6, name=None):
  11. """
  12. max_err: relative error
  13. """
  14. __tracebackhide__ = True # pylint: disable=unused-variable
  15. assert (
  16. v0.dtype == v1.dtype
  17. ), "Two Tensor must have same dtype, but the inputs are {} and {}".format(
  18. v0.dtype, v1.dtype
  19. )
  20. v0 = np.ascontiguousarray(v0, dtype=np.float32)
  21. v1 = np.ascontiguousarray(v1, dtype=np.float32)
  22. assert np.isfinite(v0.sum()) and np.isfinite(v1.sum()), (v0, v1)
  23. assert v0.shape == v1.shape, "Two tensor must have same shape({} v.s. {})".format(
  24. v0.shape, v1.shape
  25. )
  26. vdiv = np.max([np.abs(v0), np.abs(v1), np.ones_like(v0)], axis=0)
  27. err = np.abs(v0 - v1) / vdiv
  28. check = err > max_err
  29. if check.sum():
  30. idx = tuple(i[0] for i in np.nonzero(check))
  31. if name is None:
  32. name = "tensor"
  33. else:
  34. name = "tensor {}".format(name)
  35. raise AssertionError(
  36. "{} not equal: "
  37. "shape={} nonequal_idx={} v0={} v1={} err={}".format(
  38. name, v0.shape, idx, v0[idx], v1[idx], err[idx]
  39. )
  40. )

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台