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_tracing.py 2.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import io
  2. import numpy as np
  3. from megengine.core.ops import builtin as ops
  4. from megengine.core.tensor.core import apply
  5. from megengine.core.tensor.raw_tensor import as_raw_tensor
  6. from megengine.jit import exclude_from_trace, trace
  7. def test_trace():
  8. for symbolic in [False, True]:
  9. @trace(symbolic=symbolic)
  10. def f(x):
  11. op = ops.Elemwise(mode="negate")
  12. (y,) = apply(op, x)
  13. return y
  14. x = as_raw_tensor([1]).numpy()
  15. y = f.__wrapped__(as_raw_tensor(x)).numpy()
  16. for i in range(3):
  17. np.testing.assert_equal(f(as_raw_tensor(x)).numpy(), y)
  18. def test_exclude_from_trace():
  19. for symbolic in [False, True]:
  20. @trace(symbolic=symbolic)
  21. def f(x):
  22. neg = ops.Elemwise(mode="negate")
  23. (x,) = apply(neg, x)
  24. with exclude_from_trace():
  25. if i % 2:
  26. (x,) = apply(neg, x)
  27. (x,) = apply(neg, x)
  28. return x
  29. x = as_raw_tensor([1]).numpy()
  30. for i in range(3):
  31. y = f.__wrapped__(as_raw_tensor(x)).numpy()
  32. np.testing.assert_equal(f(as_raw_tensor(x)).numpy(), y)
  33. def test_print_in_trace():
  34. for symbolic in [False]: # cannot read value in symbolic mode
  35. @trace(symbolic=symbolic)
  36. def f(x):
  37. nonlocal buf
  38. neg = ops.Elemwise(mode="negate")
  39. (x,) = apply(neg, x)
  40. buf = x.numpy()
  41. (x,) = apply(neg, x)
  42. return x
  43. buf = None
  44. x = as_raw_tensor([1]).numpy()
  45. for i in range(3):
  46. y = f.__wrapped__(as_raw_tensor(x)).numpy()
  47. z = buf
  48. buf = None
  49. np.testing.assert_equal(f(as_raw_tensor(x)).numpy(), y)
  50. np.testing.assert_equal(z, buf)
  51. def test_dump():
  52. @trace(symbolic=True, capture_as_const=True)
  53. def f(x):
  54. op = ops.Elemwise(mode="negate")
  55. (y,) = apply(op, x)
  56. return y
  57. x = as_raw_tensor([1]).numpy()
  58. y = f.__wrapped__(as_raw_tensor(x)).numpy()
  59. for i in range(3):
  60. np.testing.assert_equal(f(as_raw_tensor(x)).numpy(), y)
  61. file = io.BytesIO()
  62. f.dump(file)
  63. def test_trace_profiler():
  64. for symbolic in [False, True]:
  65. @trace(symbolic=symbolic, profiling=True)
  66. def f(x):
  67. op = ops.Elemwise(mode="negate")
  68. (y,) = apply(op, x)
  69. return y
  70. x = as_raw_tensor([1]).numpy()
  71. y = f.__wrapped__(as_raw_tensor(x)).numpy()
  72. f(as_raw_tensor(x))
  73. f(as_raw_tensor(x)) # XXX: has to run twice
  74. out = f.get_profile()
  75. assert out.get("profiler")

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