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 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import io
  2. import numpy as np
  3. import megengine.utils.comp_graph_tools as cgtools
  4. from megengine import tensor
  5. from megengine.jit import trace
  6. def _default_compare_fn(x, y):
  7. if isinstance(x, np.ndarray):
  8. np.testing.assert_allclose(x, y, rtol=1e-6)
  9. else:
  10. np.testing.assert_allclose(x.numpy(), y, rtol=1e-6)
  11. def opr_test(
  12. cases, func, compare_fn=_default_compare_fn, ref_fn=None, test_trace=True, **kwargs
  13. ):
  14. """
  15. :param cases: the list which have dict element, the list length should be 2 for dynamic shape test.
  16. and the dict should have input,
  17. and should have output if ref_fn is None.
  18. should use list for multiple inputs and outputs for each case.
  19. :param func: the function to run opr.
  20. :param compare_fn: the function to compare the result and expected, use
  21. ``np.testing.assert_allclose`` if None.
  22. :param ref_fn: the function to generate expected data, should assign output if None.
  23. Examples:
  24. .. code-block::
  25. dtype = np.float32
  26. cases = [{"input": [10, 20]}, {"input": [20, 30]}]
  27. opr_test(cases,
  28. F.eye,
  29. ref_fn=lambda n, m: np.eye(n, m).astype(dtype),
  30. dtype=dtype)
  31. """
  32. def check_results(results, expected):
  33. if not isinstance(results, (tuple, list)):
  34. results = (results,)
  35. for r, e in zip(results, expected):
  36. if not isinstance(r, tensor):
  37. r = tensor(r)
  38. compare_fn(r, e)
  39. def get_param(cases, idx):
  40. case = cases[idx]
  41. inp = case.get("input", None)
  42. outp = case.get("output", None)
  43. if inp is None:
  44. raise ValueError("the test case should have input")
  45. if not isinstance(inp, (tuple, list)):
  46. inp = (inp,)
  47. if ref_fn is not None and callable(ref_fn):
  48. outp = ref_fn(*inp)
  49. if outp is None:
  50. raise ValueError("the test case should have output or reference function")
  51. if not isinstance(outp, (tuple, list)):
  52. outp = (outp,)
  53. return inp, outp
  54. if len(cases) == 0:
  55. raise ValueError("should give one case at least")
  56. if not callable(func):
  57. raise ValueError("the input func should be callable")
  58. inp, outp = get_param(cases, 0)
  59. inp_tensor = [tensor(inpi) for inpi in inp]
  60. if test_trace:
  61. copied_inp = inp_tensor.copy()
  62. for symbolic in [False, True]:
  63. traced_func = trace(symbolic=symbolic)(func)
  64. for _ in range(3):
  65. traced_results = traced_func(*copied_inp, **kwargs)
  66. check_results(traced_results, outp)
  67. dumped_func = trace(symbolic=True, capture_as_const=True)(func)
  68. dumped_results = dumped_func(*copied_inp, **kwargs)
  69. check_results(dumped_results, outp)
  70. file = io.BytesIO()
  71. dump_info = dumped_func.dump(file)
  72. file.seek(0)
  73. # arg_name has pattern arg_xxx, xxx is int value
  74. def take_number(arg_name):
  75. return int(arg_name.split("_")[-1])
  76. input_names = dump_info[4]
  77. inps_np = [i.numpy() for i in copied_inp]
  78. input_names.sort(key=take_number)
  79. inp_dict = dict(zip(input_names, inps_np))
  80. infer_cg = cgtools.GraphInference(file)
  81. # assume #outputs == 1
  82. loaded_results = list(infer_cg.run(inp_dict=inp_dict).values())[0]
  83. check_results(loaded_results, outp)
  84. results = func(*inp_tensor, **kwargs)
  85. check_results(results, outp)

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