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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. import megengine.core.tensor.megbrain_graph as G
  14. from megengine import cgtools, tensor
  15. from megengine.core._trace_option import set_tensor_shape
  16. from megengine.core.ops import builtin as ops
  17. from megengine.core.tensor.core import apply
  18. from megengine.core.tensor.raw_tensor import as_raw_tensor
  19. from megengine.functional import exp, log
  20. from megengine.jit import exclude_from_trace, trace
  21. def test_trace():
  22. for symbolic in [False, True]:
  23. @trace(symbolic=symbolic)
  24. def f(x):
  25. op = ops.Elemwise(mode="negate")
  26. (y,) = apply(op, x)
  27. return y
  28. x = as_raw_tensor([1]).numpy()
  29. y = f.__wrapped__(as_raw_tensor(x)).numpy()
  30. for i in range(3):
  31. np.testing.assert_equal(f(as_raw_tensor(x)).numpy(), y)
  32. def test_exclude_from_trace():
  33. for symbolic in [False, True]:
  34. @trace(symbolic=symbolic)
  35. def f(x):
  36. neg = ops.Elemwise(mode="negate")
  37. (x,) = apply(neg, x)
  38. with exclude_from_trace():
  39. if i % 2:
  40. (x,) = apply(neg, x)
  41. (x,) = apply(neg, x)
  42. return x
  43. x = as_raw_tensor([1]).numpy()
  44. for i in range(3):
  45. y = f.__wrapped__(as_raw_tensor(x)).numpy()
  46. np.testing.assert_equal(f(as_raw_tensor(x)).numpy(), y)
  47. def test_print_in_trace():
  48. for symbolic in [False]: # cannot read value in symbolic mode
  49. @trace(symbolic=symbolic)
  50. def f(x):
  51. nonlocal buf
  52. neg = ops.Elemwise(mode="negate")
  53. (x,) = apply(neg, x)
  54. buf = x.numpy()
  55. (x,) = apply(neg, x)
  56. return x
  57. buf = None
  58. x = as_raw_tensor([1]).numpy()
  59. for i in range(3):
  60. y = f.__wrapped__(as_raw_tensor(x)).numpy()
  61. z = buf
  62. buf = None
  63. np.testing.assert_equal(f(as_raw_tensor(x)).numpy(), y)
  64. np.testing.assert_equal(z, buf)
  65. def test_dump():
  66. @trace(symbolic=True, capture_as_const=True)
  67. def f(a, b):
  68. op = ops.Elemwise(mode="add")
  69. (y,) = apply(op, a, b)
  70. return y
  71. a = as_raw_tensor([2]).numpy()
  72. b = as_raw_tensor([4]).numpy()
  73. y = f.__wrapped__(as_raw_tensor(a), as_raw_tensor(b)).numpy()
  74. for i in range(3):
  75. np.testing.assert_equal(f(as_raw_tensor(a), as_raw_tensor(b)).numpy(), y)
  76. file = io.BytesIO()
  77. dump_info = f.dump(file)
  78. assert dump_info.nr_opr == 3
  79. np.testing.assert_equal(dump_info.inputs, ["h2d[0]", "h2d[2]"])
  80. np.testing.assert_equal(dump_info.outputs, ["ADD(h2d[0],h2d[2])[4]"])
  81. file.seek(0)
  82. result = cgtools.load_and_inference(file, [a, b])
  83. np.testing.assert_equal(result[0], y)
  84. def test_capture_dump():
  85. a = as_raw_tensor([2])
  86. @trace(symbolic=True, capture_as_const=True)
  87. def f(x):
  88. op = ops.Elemwise(mode="mul")
  89. (y,) = apply(op, x, a)
  90. return y
  91. x = as_raw_tensor([3]).numpy()
  92. y = f.__wrapped__(as_raw_tensor(x)).numpy()
  93. for i in range(3):
  94. np.testing.assert_equal(f(as_raw_tensor(x)).numpy(), y)
  95. file = io.BytesIO()
  96. f.dump(file)
  97. file.seek(0)
  98. result = cgtools.load_and_inference(file, [x])
  99. np.testing.assert_equal(result[0], y)
  100. def test_dump_volatile():
  101. p = as_raw_tensor([2])
  102. @trace(symbolic=True, capture_as_const=True)
  103. def f(x):
  104. op = ops.Elemwise(mode="mul")
  105. (y,) = apply(op, x, p)
  106. return y
  107. x = as_raw_tensor([3]).numpy()
  108. y = f.__wrapped__(as_raw_tensor(x)).numpy()
  109. for i in range(3):
  110. np.testing.assert_equal(f(as_raw_tensor(x)).numpy(), y)
  111. file = io.BytesIO()
  112. f.dump(file, optimize_for_inference=False)
  113. file.seek(0)
  114. cg, _, outputs = G.load_graph(file)
  115. (out,) = outputs
  116. assert (
  117. cgtools.get_owner_opr_type(cgtools.get_owner_opr_inputs(out)[1])
  118. == "SharedDeviceTensor"
  119. )
  120. def test_trace_profiler():
  121. for symbolic in [False, True]:
  122. @trace(symbolic=symbolic, profiling=True)
  123. def f(x):
  124. op = ops.Elemwise(mode="negate")
  125. (y,) = apply(op, x)
  126. return y
  127. x = as_raw_tensor([1]).numpy()
  128. y = f.__wrapped__(as_raw_tensor(x)).numpy()
  129. f(as_raw_tensor(x))
  130. f(as_raw_tensor(x)) # XXX: has to run twice
  131. out = f.get_profile()
  132. assert out.get("profiler")
  133. @pytest.mark.skip(reason="could not disable opt_level")
  134. def test_goptions_log_exp():
  135. @trace(symbolic=True, opt_level=0, capture_as_const=True)
  136. def f(x):
  137. return log(exp(x))
  138. @trace(symbolic=True, opt_level=1, capture_as_const=True)
  139. def g(x):
  140. return log(exp(x))
  141. f(tensor(1.0))
  142. _, out = mkstemp()
  143. f.dump(out, optimize_for_inference=False)
  144. *_, outputs = G.load_graph(out)
  145. oprs_1 = cgtools.get_oprs_seq(outputs)
  146. g(tensor(1.0))
  147. g.dump(out, optimize_for_inference=False)
  148. *_, outputs = G.load_graph(out)
  149. oprs_2 = cgtools.get_oprs_seq(outputs)
  150. assert len(oprs_1) - len(oprs_2) == 2
  151. @pytest.mark.skip(reason="could not disable opt_level")
  152. def test_goptions_log_sum_exp():
  153. @trace(symbolic=True, opt_level=0, capture_as_const=True)
  154. def f(x, y):
  155. return log(exp(x) + exp(y))
  156. @trace(symbolic=True, opt_level=1, capture_as_const=True)
  157. def g(x, y):
  158. return log(exp(x) + exp(y))
  159. f(tensor(1.0), tensor(2.0))
  160. _, out = mkstemp()
  161. f.dump(out, optimize_for_inference=False)
  162. *_, outputs = G.load_graph(out)
  163. oprs_1 = cgtools.get_oprs_seq(outputs)
  164. g(tensor(1.0), tensor(2.0))
  165. g.dump(out, optimize_for_inference=False)
  166. *_, outputs = G.load_graph(out)
  167. oprs_2 = cgtools.get_oprs_seq(outputs)
  168. assert len(oprs_1) - len(oprs_2) == 2
  169. def test_optimize_for_inference():
  170. @trace(symbolic=True, capture_as_const=True)
  171. def f(x):
  172. return exp(x)
  173. _, out = mkstemp()
  174. f(tensor(5.0))
  175. f.dump(out, enable_io16xc32=True)
  176. res = G.load_graph(out)
  177. computing_input = res.output_vars_list[0].owner.inputs[0]
  178. assert computing_input.dtype == np.float16
  179. def test_trace_cvt_bool():
  180. set_tensor_shape(True)
  181. x = tensor([0], dtype=np.int32)
  182. @trace(symbolic=True)
  183. def f(x):
  184. return x.shape[0] == 0
  185. for i in range(3):
  186. np.testing.assert_equal(f(x).numpy()[0], False)
  187. def test_trace_reshape():
  188. for symbolic in [False, True]:
  189. set_tensor_shape(True)
  190. x1 = tensor(np.random.randn(2, 10, 10))
  191. x2 = tensor(np.random.randn(4, 10, 10))
  192. x3 = tensor(np.random.randn(8, 10, 10))
  193. @trace(symbolic=symbolic, capture_as_const=True)
  194. def f(x):
  195. y = x.reshape(x.shape[0], 100)
  196. return y
  197. f(x1)
  198. f(x2)
  199. f(x3)

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