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_network.py 8.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. import io
  2. import numpy as np
  3. import megengine.core.tensor.megbrain_graph as G
  4. import megengine.functional as F
  5. import megengine.module as M
  6. import megengine.utils.network_node as N
  7. from megengine.jit.tracing import trace
  8. from megengine.tensor import Tensor
  9. from megengine.utils.comp_graph_tools import GraphInference
  10. from megengine.utils.network import Network as Net
  11. from megengine.utils.network import as_oprnode
  12. from megengine.utils.network_node import Host2DeviceCopy, VarNode
  13. def test_replace_var():
  14. a = Tensor([1, 2])
  15. b = Tensor([3, 4])
  16. @trace(symbolic=True, capture_as_const=True)
  17. def fwd(a, b):
  18. return (a + b) * 2
  19. fwd(a, b)
  20. orig_model = io.BytesIO()
  21. fwd.dump(
  22. orig_model, arg_names=["a", "b"], output_names="o", optimize_for_inference=False
  23. )
  24. orig_model.seek(0)
  25. graph = Net.load(orig_model)
  26. vara = graph.var_filter.name("a").as_unique()
  27. varb = graph.var_filter.name("b").as_unique()
  28. out = F.mul(vara, varb)
  29. out = F.relu(out)
  30. opnode = list(graph.opr_filter.has_input(vara))
  31. repl_dict = {opnode[0].outputs[0]: out}
  32. graph.replace_vars(repl_dict)
  33. modified_model = io.BytesIO()
  34. graph.dump(modified_model)
  35. modified_model.seek(0)
  36. load_graph = GraphInference(modified_model)
  37. out = load_graph.run(a, b)
  38. np.testing.assert_equal(out["o"], [6, 16])
  39. def test_replace_opr():
  40. a = Tensor([1, 2])
  41. b = Tensor([3, 4])
  42. @trace(symbolic=True, capture_as_const=True)
  43. def fwd(a, b):
  44. return (a + b) * 2
  45. fwd(a, b)
  46. orig_model = io.BytesIO()
  47. fwd.dump(
  48. orig_model, arg_names=["a", "b"], output_names="o", optimize_for_inference=False
  49. )
  50. orig_model.seek(0)
  51. graph = Net.load(orig_model)
  52. vara = graph.var_filter.name("a").as_unique()
  53. varb = graph.var_filter.name("b").as_unique()
  54. out1 = F.sub(vara, varb)
  55. out1 = F.relu(out1)
  56. out1 = graph.add_dep_oprs(out1)
  57. orig_opr = graph.opr_filter.has_input(vara).as_unique()
  58. repl_dict = {orig_opr: out1[0].owner}
  59. graph.replace_oprs(repl_dict)
  60. modified_model1 = io.BytesIO()
  61. graph.dump(modified_model1)
  62. modified_model1.seek(0)
  63. load_graph = GraphInference(modified_model1)
  64. out = load_graph.run(a, b)
  65. np.testing.assert_equal(out["o"], [0, 0])
  66. def test_modify_params():
  67. a = Tensor([1, 2])
  68. b = Tensor([3, 4])
  69. @trace(symbolic=True, capture_as_const=True)
  70. def fwd(a, b):
  71. return (a + b) * 2
  72. fwd(a, b)
  73. orig_model = io.BytesIO()
  74. fwd.dump(
  75. orig_model, arg_names=["a", "b"], output_names="o", optimize_for_inference=False
  76. )
  77. orig_model.seek(0)
  78. graph = Net.load(orig_model)
  79. param_const = graph.params_filter.as_unique()
  80. param_const.set_value(3)
  81. modified_model = io.BytesIO()
  82. graph.dump(modified_model)
  83. modified_model.seek(0)
  84. load_graph = GraphInference(modified_model)
  85. out = load_graph.run(a, b)
  86. np.testing.assert_equal(out["o"], [12, 18])
  87. def test_make_const():
  88. a = Tensor([1, 2])
  89. b = Tensor([3, 4])
  90. @trace(symbolic=True, capture_as_const=True)
  91. def fwd(a, b):
  92. return (a + b) * 2
  93. fwd(a, b)
  94. orig_model = io.BytesIO()
  95. fwd.dump(
  96. orig_model, arg_names=["a", "b"], output_names="o", optimize_for_inference=False
  97. )
  98. orig_model.seek(0)
  99. graph = Net.load(orig_model)
  100. const_b = graph.make_const(np.array([0.0, 0.0]), name="b")
  101. varb = graph.var_filter.name("b").as_unique()
  102. repl_dict = {varb: const_b}
  103. graph.replace_vars(repl_dict)
  104. modified_model = io.BytesIO()
  105. graph.dump(modified_model)
  106. modified_model.seek(0)
  107. load_graph = GraphInference(modified_model)
  108. out = load_graph.run(a)
  109. np.testing.assert_equal(out["o"], [2, 4])
  110. def test_add_input():
  111. a = Tensor([1, 2])
  112. b = Tensor([3, 4])
  113. @trace(symbolic=True, capture_as_const=True)
  114. def fwd(a, b):
  115. return (a + b) * 2
  116. fwd(a, b)
  117. orig_model = io.BytesIO()
  118. fwd.dump(
  119. orig_model, arg_names=["a", "b"], output_names="o", optimize_for_inference=False
  120. )
  121. orig_model.seek(0)
  122. graph = Net.load(orig_model)
  123. inp_c = graph.make_input_node((2,), np.int32, name="c")
  124. varo = graph.var_filter.name("o").as_unique()
  125. out = F.add(varo, inp_c)
  126. out.name = "o1"
  127. graph.remove_output(varo)
  128. graph.add_output(out)
  129. modified_model = io.BytesIO()
  130. graph.dump(modified_model)
  131. modified_model.seek(0)
  132. load_graph = GraphInference(modified_model)
  133. out = load_graph.run(a, b, a)
  134. np.testing.assert_equal(out["o1"], ((a + b) * 2 + a).numpy())
  135. def test_add_output():
  136. a = Tensor([1.0, 2.0])
  137. b = Tensor([3.0, 4.0])
  138. @trace(symbolic=True, capture_as_const=True)
  139. def fwd(a, b):
  140. return (a + b) * 2
  141. fwd(a, b)
  142. orig_model = io.BytesIO()
  143. fwd.dump(
  144. orig_model, arg_names=["a", "b"], output_names="o", optimize_for_inference=False
  145. )
  146. orig_model.seek(0)
  147. net = Net.load(orig_model)
  148. var_a = net.var_filter.name("a").as_unique()
  149. var_b = net.var_filter.name("b").as_unique()
  150. y = F.add(var_a, var_b)
  151. y = F.sigmoid(y)
  152. y.name = "o1"
  153. net.add_output(y)
  154. modified_model = io.BytesIO()
  155. net.dump(modified_model)
  156. modified_model.seek(0)
  157. g = GraphInference(modified_model)
  158. out = g.run(a.numpy(), b.numpy())
  159. np.testing.assert_equal(out["o"], ((a + b) * 2).numpy())
  160. np.testing.assert_equal(out["o1"], (F.sigmoid((a + b))).numpy())
  161. def test_query():
  162. class Model(M.Module):
  163. def __init__(self):
  164. super().__init__()
  165. self.conv1 = M.Conv2d(3, 32, 3)
  166. self.conv2 = M.Conv2d(32, 32, 3)
  167. self.conv3 = M.Conv2d(32, 32, 3)
  168. def forward(self, data):
  169. x = self.conv1(data)
  170. x = self.conv2(x)
  171. x = self.conv3(x)
  172. return x
  173. n = Model()
  174. @trace(symbolic=True, capture_as_const=True)
  175. def fwd(data):
  176. return n(data)
  177. fwd(Tensor(np.random.random((1, 3, 224, 224))))
  178. orig_model = io.BytesIO()
  179. fwd.dump(
  180. orig_model,
  181. arg_names=["data"],
  182. output_names="o",
  183. keep_opr_name=True,
  184. keep_var_name=True,
  185. optimize_for_inference=False,
  186. )
  187. orig_model.seek(0)
  188. graph = Net.load(orig_model)
  189. r = graph.data_providers_filter.as_count()
  190. assert r == 1
  191. opr = graph.get_opr_by_type(Host2DeviceCopy)
  192. assert isinstance(opr, Host2DeviceCopy)
  193. r1 = graph.params_filter.as_count()
  194. assert r1 == 6
  195. r2 = graph.opr_filter.type(N.ConvolutionForward).as_count()
  196. assert r2 == 3
  197. r3 = graph.opr_filter.not_type(N.ConvolutionForward).as_count()
  198. assert r3 == len(graph.all_oprs) - r2
  199. var = graph.var_filter.name("data").as_unique()
  200. r4 = graph.opr_filter.has_input(var).as_count()
  201. assert r4 == 1
  202. r5 = graph.opr_filter.name("data").as_count()
  203. assert r5 == 1
  204. opr = graph.get_opr_by_name("data")
  205. assert isinstance(opr, Host2DeviceCopy)
  206. var = graph.get_var_by_name("data")
  207. assert isinstance(var, VarNode)
  208. r6 = graph.var_filter.name("*bias").as_count()
  209. assert r6 == 3
  210. def test_optimize_for_inference():
  211. @trace(symbolic=True, capture_as_const=True)
  212. def f(x):
  213. return F.exp(x)
  214. orig_model = io.BytesIO()
  215. f(Tensor(5.0))
  216. f.dump(orig_model, optimize_for_inference=False)
  217. orig_model.seek(0)
  218. optimize_model = io.BytesIO()
  219. net = Net.load(orig_model)
  220. net.dump(optimize_model, enable_io16xc32=True)
  221. optimize_model.seek(0)
  222. res = G.load_graph(optimize_model)
  223. computing_input = res.output_vars_list[0].owner.inputs[0]
  224. assert computing_input.dtype == np.float16
  225. def test_reset_batchsize():
  226. @trace(symbolic=True, capture_as_const=True)
  227. def f(x):
  228. return F.exp(x)
  229. orig_model = io.BytesIO()
  230. f(Tensor(np.random.random((3, 3, 224, 224))))
  231. f.dump(orig_model, optimize_for_inference=False)
  232. orig_model.seek(0)
  233. modified_model = io.BytesIO()
  234. net = Net.load(orig_model)
  235. net.reset_batch_size(1)
  236. net.dump(modified_model, optimize_for_inference=False)
  237. modified_model.seek(0)
  238. net1 = Net.load(modified_model)
  239. assert net1.data_providers_filter.as_unique().shape[0] == 1
  240. def test_modify_opr_name():
  241. @trace(symbolic=True, capture_as_const=True)
  242. def f(x):
  243. return F.exp(x)
  244. orig_model = io.BytesIO()
  245. f(Tensor(np.random.random((3, 3, 224, 224))))
  246. f.dump(orig_model, arg_names=["a"], optimize_for_inference=False)
  247. orig_model.seek(0)
  248. modified_model = io.BytesIO()
  249. net = Net.load(orig_model)
  250. net.modify_opr_names("net")
  251. net.modify_opr_names(lambda x: "net1." + x)
  252. net.dump(modified_model, optimize_for_inference=False)
  253. modified_model.seek(0)
  254. net1 = Net.load(modified_model)
  255. assert net1.data_providers_filter.as_unique().name == "net1.net.a"

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