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

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

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