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.

compare_binary_iodump.py 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  2. #
  3. # Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
  4. #
  5. # Unless required by applicable law or agreed to in writing,
  6. # software distributed under the License is distributed on an
  7. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  8. import argparse
  9. import os
  10. import textwrap
  11. from pathlib import Path
  12. import numpy as np
  13. from megengine.utils import plugin
  14. def check(v0, v1, name, max_err):
  15. v0 = np.ascontiguousarray(v0, dtype=np.float32)
  16. v1 = np.ascontiguousarray(v1, dtype=np.float32)
  17. assert np.isfinite(v0.sum()) and np.isfinite(
  18. v1.sum()
  19. ), "{} not finite: sum={} vs sum={}".format(name, v0.sum(), v1.sum())
  20. assert v0.shape == v1.shape, "{} shape mismatch: {} vs {}".format(
  21. name, v0.shape, v1.shape
  22. )
  23. vdiv = np.max([np.abs(v0), np.abs(v1), np.ones_like(v0)], axis=0)
  24. err = np.abs(v0 - v1) / vdiv
  25. check = err > max_err
  26. if check.sum():
  27. idx = tuple(i[0] for i in np.nonzero(check))
  28. raise AssertionError(
  29. "{} not equal: "
  30. "shape={} nonequal_idx={} v0={} v1={} err={}".format(
  31. name, v0.shape, idx, v0[idx], v1[idx], err[idx]
  32. )
  33. )
  34. def main():
  35. parser = argparse.ArgumentParser(
  36. description=(
  37. "compare tensor dumps generated BinaryOprIODump plugin, "
  38. "it can compare two dirs or two single files"
  39. ),
  40. formatter_class=argparse.ArgumentDefaultsHelpFormatter,
  41. )
  42. parser.add_argument("input0", help="dirname or filename")
  43. parser.add_argument("input1", help="dirname or filename")
  44. parser.add_argument(
  45. "-e", "--max-err", type=float, default=1e-3, help="max allowed error"
  46. )
  47. parser.add_argument(
  48. "-s", "--stop-on-error", action="store_true", help="do not compare "
  49. )
  50. args = parser.parse_args()
  51. files0 = set()
  52. files1 = set()
  53. if os.path.isdir(args.input0):
  54. assert os.path.isdir(args.input1)
  55. name0 = set()
  56. name1 = set()
  57. for i in os.listdir(args.input0):
  58. files0.add(str(Path(args.input0) / i))
  59. name0.add(i)
  60. for i in os.listdir(args.input1):
  61. files1.add(str(Path(args.input1) / i))
  62. name1.add(i)
  63. assert name0 == name1, "dir files mismatch: a-b={} b-a={}".format(
  64. name0 - name1, name1 - name0
  65. )
  66. else:
  67. files0.add(args.input0)
  68. files1.add(args.input1)
  69. files0 = sorted(files0)
  70. files1 = sorted(files1)
  71. for i, j in zip(files0, files1):
  72. val0, name0 = plugin.load_tensor_binary(i)
  73. val1, name1 = plugin.load_tensor_binary(j)
  74. name = "{}: \n{}\n{}\n".format(
  75. i, "\n ".join(textwrap.wrap(name0)), "\n ".join(textwrap.wrap(name1))
  76. )
  77. try:
  78. check(val0, val1, name, args.max_err)
  79. except Exception as exc:
  80. if args.stop_on_error:
  81. raise exc
  82. print(exc)
  83. if __name__ == "__main__":
  84. main()

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