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.

test_cgtools.py 2.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 io
  9. import numpy as np
  10. import megengine
  11. import megengine.functional as F
  12. import megengine.module as M
  13. from megengine import cgtools
  14. from megengine.core.tensor import megbrain_graph as mgb_graph
  15. from megengine.core.tensor.raw_tensor import as_raw_tensor
  16. from megengine.jit import trace
  17. def make_dev_tensor(value, dtype=None, device=None):
  18. return as_raw_tensor(value, dtype=dtype, device=device)._dev_tensor()
  19. def test_replace_vars():
  20. g = mgb_graph.Graph()
  21. g.options.async_exec_level = 0b100
  22. device = "xpux"
  23. dtype = np.float32
  24. a = mgb_graph.InputNode(device=device, dtype=dtype, graph=g)
  25. const = g.make_const(1.234)
  26. a_plus_a = F.add(a.outputs[0], a.outputs[0])
  27. a_plus_a_mul_const = F.mul(a_plus_a, const)
  28. rst = F.add(a_plus_a_mul_const, a.outputs[0])
  29. (new,) = cgtools.replace_vars([rst._node], {const._node: a_plus_a._node})
  30. out = mgb_graph.OutputNode(mgb_graph.VarNode(new))
  31. func = g.compile(out.outputs[0])
  32. func.execute()
  33. x = make_dev_tensor(5.0, device=device)
  34. a.set_value(x)
  35. res = out.get_value().numpy()
  36. np.testing.assert_equal(res, np.array([105.0]))
  37. def test_replace_oprs():
  38. g = mgb_graph.Graph()
  39. g.options.async_exec_level = 0b100
  40. device = "xpux"
  41. dtype = np.float32
  42. a = mgb_graph.InputNode(device=device, dtype=dtype, graph=g)
  43. const = g.make_const(1.25)
  44. a_plus_a = F.add(a.outputs[0], a.outputs[0])
  45. old_opr = a_plus_a.op
  46. a_plus_a_mul_const = F.mul(a_plus_a, const)
  47. a_mul_a = F.mul(a.outputs[0], a.outputs[0])
  48. new_opr = a_mul_a.op
  49. (new,) = cgtools.replace_oprs(
  50. [a_plus_a_mul_const._node], {old_opr._node: new_opr._node}
  51. )
  52. out = mgb_graph.OutputNode(mgb_graph.VarNode(new))
  53. func = g.compile(out.outputs[0])
  54. func.execute()
  55. x = make_dev_tensor(5.0, device=device)
  56. a.set_value(x)
  57. res = out.get_value().numpy()
  58. np.testing.assert_equal(res, np.array([5.0 * 5.0 * 1.25]))
  59. def test_graph_traversal():
  60. net = M.Conv2d(3, 32, 3)
  61. @trace(symbolic=True, capture_as_const=True)
  62. def fun(data):
  63. x = net(data)
  64. return x
  65. data = np.random.random([1, 3, 224, 224]).astype(np.float32)
  66. for i in range(3):
  67. fun(megengine.tensor(data))
  68. file = io.BytesIO()
  69. fun.dump(file)
  70. file.seek(0)
  71. cg, _, outputs = mgb_graph.load_graph(file)
  72. _, map_vars, var2oprs, *_ = cgtools.graph_traversal(outputs)
  73. input_var = map_vars[1]
  74. _, var_idx = var2oprs[input_var.id][0]
  75. assert var_idx == 0

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