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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. # -*- coding: utf-8 -*-
  2. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  3. #
  4. # Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
  5. #
  6. # Unless required by applicable law or agreed to in writing,
  7. # software distributed under the License is distributed on an
  8. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. import io
  10. from tempfile import mkstemp
  11. import numpy as np
  12. import pytest
  13. from megengine import tensor
  14. from megengine.core.ops import builtin as ops
  15. from megengine.core.tensor import megbrain_graph as G
  16. from megengine.core.tensor.core import apply
  17. from megengine.core.tensor.raw_tensor import as_raw_tensor
  18. from megengine.functional import exp, log
  19. from megengine.jit import exclude_from_trace, trace
  20. def test_trace():
  21. for symbolic in [False, True]:
  22. @trace(symbolic=symbolic)
  23. def f(x):
  24. op = ops.Elemwise(mode="negate")
  25. (y,) = apply(op, x)
  26. return y
  27. x = as_raw_tensor([1]).numpy()
  28. y = f.__wrapped__(as_raw_tensor(x)).numpy()
  29. for i in range(3):
  30. np.testing.assert_equal(f(as_raw_tensor(x)).numpy(), y)
  31. def test_exclude_from_trace():
  32. for symbolic in [False, True]:
  33. @trace(symbolic=symbolic)
  34. def f(x):
  35. neg = ops.Elemwise(mode="negate")
  36. (x,) = apply(neg, x)
  37. with exclude_from_trace():
  38. if i % 2:
  39. (x,) = apply(neg, x)
  40. (x,) = apply(neg, x)
  41. return x
  42. x = as_raw_tensor([1]).numpy()
  43. for i in range(3):
  44. y = f.__wrapped__(as_raw_tensor(x)).numpy()
  45. np.testing.assert_equal(f(as_raw_tensor(x)).numpy(), y)
  46. def test_print_in_trace():
  47. for symbolic in [False]: # cannot read value in symbolic mode
  48. @trace(symbolic=symbolic)
  49. def f(x):
  50. nonlocal buf
  51. neg = ops.Elemwise(mode="negate")
  52. (x,) = apply(neg, x)
  53. buf = x.numpy()
  54. (x,) = apply(neg, x)
  55. return x
  56. buf = None
  57. x = as_raw_tensor([1]).numpy()
  58. for i in range(3):
  59. y = f.__wrapped__(as_raw_tensor(x)).numpy()
  60. z = buf
  61. buf = None
  62. np.testing.assert_equal(f(as_raw_tensor(x)).numpy(), y)
  63. np.testing.assert_equal(z, buf)
  64. def test_dump():
  65. @trace(symbolic=True, capture_as_const=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. for i in range(3):
  73. np.testing.assert_equal(f(as_raw_tensor(x)).numpy(), y)
  74. file = io.BytesIO()
  75. f.dump(file)
  76. def test_trace_profiler():
  77. for symbolic in [False, True]:
  78. @trace(symbolic=symbolic, profiling=True)
  79. def f(x):
  80. op = ops.Elemwise(mode="negate")
  81. (y,) = apply(op, x)
  82. return y
  83. x = as_raw_tensor([1]).numpy()
  84. y = f.__wrapped__(as_raw_tensor(x)).numpy()
  85. f(as_raw_tensor(x))
  86. f(as_raw_tensor(x)) # XXX: has to run twice
  87. out = f.get_profile()
  88. assert out.get("profiler")
  89. @pytest.mark.skip(reason="eq_to_unit failed in inplace.cpp")
  90. def test_goptions_div_zero():
  91. @trace(symbolic=True, opt_level=0)
  92. def f(x):
  93. return x / x
  94. @trace(symbolic=True, opt_level=1)
  95. def g(x):
  96. return x / x
  97. out = f(tensor(0.0))
  98. if out == out:
  99. raise ValueError("actual result should be nan")
  100. out = g(tensor(0.0))
  101. if out != out:
  102. raise ValueError("actual result should be 1")
  103. @pytest.mark.skip(reason="cast to Elemwise failed in inplace.cpp")
  104. def test_goptions_log_exp():
  105. @trace(symbolic=True, opt_level=0, capture_as_const=True)
  106. def f(x):
  107. return log(exp(x))
  108. @trace(symbolic=True, opt_level=1, capture_as_const=True)
  109. def g(x):
  110. return log(exp(x))
  111. f(tensor(1.0))
  112. _, out = mkstemp()
  113. f.dump(out)
  114. *_, outputs = G.load_comp_graph_from_file(out)
  115. oprs_1 = cgtools.get_oprs_seq(outputs)
  116. g(tensor(1.0))
  117. g.dump(out)
  118. *_, outputs = G.load_comp_graph_from_file(out)
  119. oprs_2 = cgtools.get_oprs_seq(outputs)
  120. assert len(oprs_1) - len(oprs_2) == 2
  121. @pytest.mark.skip(reason="need cgtools to check final oprs")
  122. def test_goptions_log_sum_exp():
  123. @trace(symbolic=True, opt_level=0, capture_as_const=True)
  124. def f(x, y):
  125. return log(exp(x) + exp(y))
  126. @trace(symbolic=True, opt_level=1, capture_as_const=True)
  127. def g(x, y):
  128. return log(exp(x) + exp(y))
  129. f(tensor(1.0), tensor(2.0))
  130. _, out = mkstemp()
  131. f.dump(out)
  132. *_, outputs = G.load_comp_graph_from_file(out)
  133. oprs_1 = cgtools.get_oprs_seq(outputs)
  134. g(tensor(1.0), tensor(2.0))
  135. g.dump(out)
  136. *_, outputs = G.load_comp_graph_from_file(out)
  137. oprs_2 = cgtools.get_oprs_seq(outputs)
  138. assert len(oprs_1) - len(oprs_2) == 2
  139. @pytest.mark.skip(reason="need cgtools to check computing input dtype")
  140. def test_optimize_for_inference():
  141. @trace(symbolic=True, capture_as_const=True)
  142. def f(x):
  143. return exp(x)
  144. _, out = mkstemp()
  145. f(tensor(5.0))
  146. f.dump(out, optimize_for_inference=True, optimize_options={"enable_io16xc32": True})
  147. res = G.load_comp_graph_from_file(out)
  148. computing_input = res.output_vars_list[0].owner.inputs[0]
  149. assert computing_input.dtype == np.float16

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