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_node.py 20 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. import io
  2. import os
  3. import platform
  4. import numpy as np
  5. import pytest
  6. import megengine.core.tensor.dtype as dtype
  7. import megengine.core.tensor.megbrain_graph as G
  8. import megengine.functional as F
  9. import megengine.module as M
  10. import megengine.random as rand
  11. from megengine.core._imperative_rt.core2 import apply
  12. from megengine.core._wrap import Device
  13. from megengine.core.ops import builtin
  14. from megengine.device import is_cuda_available
  15. from megengine.functional.external import tensorrt_runtime_opr
  16. from megengine.jit.tracing import trace
  17. from megengine.tensor import Tensor
  18. from megengine.utils.comp_graph_tools import GraphInference
  19. from megengine.utils.network import Network as Net
  20. def check_pygraph_dump(trace_func, inp_data, expect_results):
  21. orig_model = io.BytesIO()
  22. inp_size = len(inp_data)
  23. out_size = len(expect_results)
  24. arg_names = ["arg_{}".format(i) for i in range(inp_size)]
  25. output_names = ["out_{}".format(i) for i in range(out_size)]
  26. trace_func.dump(
  27. orig_model,
  28. arg_names=arg_names,
  29. output_names=output_names,
  30. optimize_for_inference=False,
  31. )
  32. orig_model.seek(0)
  33. net = Net.load(orig_model)
  34. file = io.BytesIO()
  35. net.dump(file, optimize_for_inference=False)
  36. file.seek(0)
  37. graph = GraphInference(file)
  38. inp_dict = dict([(arg_names[i], inp_data[i].numpy()) for i in range(inp_size)])
  39. results = graph.run(inp_dict=inp_dict)
  40. for ind, tensor in enumerate(expect_results):
  41. np.testing.assert_equal(tensor.numpy(), results[output_names[ind]])
  42. assert tensor.dtype == results[output_names[ind]].dtype
  43. def test_elemwise():
  44. @trace(symbolic=True, capture_as_const=True)
  45. def fwd(x, y):
  46. z1 = x * y
  47. z2 = x + y
  48. z3 = z1 / z2
  49. z3 = z3 ** 3
  50. return z3
  51. x = Tensor([1.0, 2.0])
  52. y = Tensor([3.0, 5.0])
  53. result = fwd(x, y)
  54. check_pygraph_dump(fwd, [x, y], [result])
  55. def test_reduce():
  56. @trace(symbolic=True, capture_as_const=True)
  57. def fwd(data):
  58. x = data.sum(axis=2)
  59. x = x.mean(axis=1)
  60. return x
  61. data = Tensor(np.random.random((1, 32, 32)))
  62. result = fwd(data)
  63. check_pygraph_dump(fwd, [data], [result])
  64. def test_typecvt():
  65. @trace(symbolic=True, capture_as_const=True)
  66. def fwd(data):
  67. return data.astype(dtype.qint8(0.8))
  68. x = Tensor(np.random.random((2, 3)) * 255)
  69. result = fwd(x)
  70. check_pygraph_dump(fwd, [x], [result])
  71. def test_matinv():
  72. @trace(symbolic=True, capture_as_const=True)
  73. def fwd(data):
  74. return F.matinv(data)
  75. data = Tensor(np.random.random((5, 5)))
  76. result = fwd(data)
  77. check_pygraph_dump(fwd, [data], [result])
  78. def test_matmul():
  79. @trace(symbolic=True, capture_as_const=True)
  80. def fwd(data1, data2):
  81. return F.matmul(data1, data2)
  82. data1 = Tensor(np.random.random((32, 64)))
  83. data2 = Tensor(np.random.random((64, 16)))
  84. result = fwd(data1, data2)
  85. check_pygraph_dump(fwd, [data1, data2], [result])
  86. def test_batchmatmul():
  87. @trace(symbolic=True, capture_as_const=True)
  88. def fwd(x, y):
  89. return F.matmul(x, y)
  90. x = Tensor(np.random.random((3, 3, 5)))
  91. y = Tensor(np.random.random((3, 5, 3)))
  92. result = fwd(x, y)
  93. check_pygraph_dump(fwd, [x, y], [result])
  94. def test_dot():
  95. @trace(symbolic=True, capture_as_const=True)
  96. def fwd(x, y):
  97. return F.dot(x, y)
  98. x = Tensor([1.0, 2.0, 3.0])
  99. y = Tensor([3.0, 4.0, 5.0])
  100. result = fwd(x, y)
  101. check_pygraph_dump(fwd, [x, y], [result])
  102. def test_svd():
  103. @trace(symbolic=True, capture_as_const=True)
  104. def fwd(data):
  105. _, out, _ = F.svd(data)
  106. return out
  107. input = Tensor(np.random.random((1, 1, 3, 3)))
  108. result = fwd(input)
  109. check_pygraph_dump(fwd, [input], [result])
  110. def test_conv():
  111. conv = M.Conv2d(3, 32, 3)
  112. @trace(symbolic=True, capture_as_const=True)
  113. def fwd(data):
  114. return conv(data)
  115. data = Tensor(np.random.random((1, 3, 32, 32)))
  116. result = fwd(data)
  117. check_pygraph_dump(fwd, [data], [result])
  118. def test_deformable_conv():
  119. if not is_cuda_available():
  120. return
  121. conv = M.DeformableConv2d(3, 32, 3)
  122. @trace(symbolic=True, capture_as_const=True)
  123. def fwd(data, offset, mask):
  124. return conv(data, offset, mask)
  125. data = Tensor(np.random.random((1, 3, 32, 32)))
  126. offset = Tensor(np.ones((32, 3 * 3 * 2, 30, 30)).astype("int32") * 5)
  127. mask = Tensor(np.ones((32, 3 * 3, 30, 30)).astype("int32"))
  128. out = fwd(data, offset, mask)
  129. check_pygraph_dump(fwd, [data, offset, mask], [out])
  130. def test_convtranspose():
  131. deconv = M.ConvTranspose2d(32, 32, 3)
  132. @trace(symbolic=True, capture_as_const=True)
  133. def fwd(data):
  134. return deconv(data)
  135. data = Tensor(np.random.random((1, 32, 32, 32)))
  136. result = fwd(data)
  137. check_pygraph_dump(fwd, [data], [result])
  138. @pytest.mark.skip(reason="pytest aborted")
  139. def test_grouplocal():
  140. n = M.LocalConv2d(3, 32, 32, 32, 3)
  141. @trace(symbolic=True, capture_as_const=True)
  142. def fwd(data):
  143. return n(data)
  144. input = Tensor(np.random.random((1, 3, 32, 32)))
  145. result = fwd(input)
  146. check_pygraph_dump(fwd, [input], [result])
  147. def test_pooling():
  148. @trace(symbolic=True, capture_as_const=True)
  149. def fwd(data):
  150. out = F.max_pool2d(data, 2, 2)
  151. out = F.avg_pool2d(out, 2, 2)
  152. return out
  153. data = Tensor(np.random.random((1, 3, 64, 64)))
  154. result = fwd(data)
  155. check_pygraph_dump(fwd, [data], [result])
  156. def test_adaptivepooling():
  157. pool1 = M.AdaptiveMaxPool2d((2, 2))
  158. pool2 = M.AdaptiveAvgPool2d((2, 2))
  159. @trace(symbolic=True, capture_as_const=True)
  160. def fwd(data):
  161. out = pool1(data)
  162. out = pool2(out)
  163. return out
  164. input = Tensor(np.random.random((1, 3, 32, 32)))
  165. result = fwd(input)
  166. check_pygraph_dump(fwd, [input], [result])
  167. def test_roipooling():
  168. inp = Tensor(np.random.random((1, 1, 128, 128)))
  169. rois = Tensor(np.random.random((4, 5)))
  170. @trace(symbolic=True, capture_as_const=True)
  171. def fwd(inp, rois):
  172. return F.vision.roi_pooling(inp, rois, (2, 2), scale=2.0)
  173. output = fwd(inp, rois)
  174. check_pygraph_dump(fwd, [inp, rois], [output])
  175. def test_deformable_ps_roi_pooling():
  176. inp = Tensor(np.random.random((1, 256, 64, 64)).astype("float32"))
  177. rois = Tensor(np.random.random((1, 5)).astype("float32"))
  178. trans = Tensor(np.random.random((24, 2, 7, 7)).astype("float32"))
  179. pooled_h = 7
  180. pooled_w = 7
  181. sample_per_part = 4
  182. no_trans = False
  183. part_size = 7
  184. spatial_scale = 1.0 / 64
  185. trans_std = 0.1
  186. @trace(symbolic=True, capture_as_const=True)
  187. def fwd(inp, rois, trans):
  188. y = F.deformable_psroi_pooling(
  189. inp,
  190. rois,
  191. trans,
  192. no_trans,
  193. part_size,
  194. pooled_h,
  195. pooled_w,
  196. sample_per_part,
  197. spatial_scale,
  198. trans_std,
  199. )
  200. return y
  201. result = fwd(inp, rois, trans)
  202. check_pygraph_dump(fwd, [inp, rois, trans], [result])
  203. def test_convbias():
  204. @trace(symbolic=True, capture_as_const=True)
  205. def fwd(inp, weight, bias):
  206. return F.quantized.conv_bias_activation(
  207. inp, weight, bias, dtype=dtype.qint8(scale=1.0), nonlinear_mode="RELU"
  208. )
  209. inp = Tensor(np.random.random((1, 3, 64, 64)), dtype=dtype.qint8(scale=1.0))
  210. weight = Tensor(np.random.random((32, 3, 3, 3)), dtype=dtype.qint8(scale=1.0))
  211. bias = Tensor(np.random.random((1, 32, 1, 1)), dtype=dtype.qint32(scale=1.0))
  212. result = fwd(inp, weight, bias)
  213. check_pygraph_dump(fwd, [inp, weight, bias], [result])
  214. def test_batch_convbias():
  215. if is_cuda_available():
  216. return
  217. @trace(symbolic=True, capture_as_const=True)
  218. def fwd(inp, weight, bias):
  219. return F.quantized.batch_conv_bias_activation(
  220. inp, weight, bias, dtype=dtype.qint8(scale=1.0), nonlinear_mode="RELU"
  221. )
  222. inp = Tensor(np.random.random((1, 3, 64, 64)), dtype=dtype.qint8(scale=1.0))
  223. weight = Tensor(np.random.random((1, 32, 3, 3, 3)), dtype=dtype.qint8(scale=1.0))
  224. bias = Tensor(np.random.random((1, 32, 1, 1)), dtype=dtype.qint32(scale=1.0))
  225. result = fwd(inp, weight, bias)
  226. check_pygraph_dump(fwd, [inp, weight, bias], [result])
  227. def test_batchnorm():
  228. bn = M.BatchNorm2d(32)
  229. bn.eval()
  230. @trace(symbolic=True, capture_as_const=True)
  231. def fwd(data):
  232. return bn(data)
  233. data = Tensor(np.random.random((1, 32, 32, 32)))
  234. result = fwd(data)
  235. check_pygraph_dump(fwd, [data], [result])
  236. def test_roialign():
  237. inp = Tensor(np.random.randn(1, 1, 128, 128))
  238. rois = Tensor(np.random.random((4, 5)))
  239. @trace(symbolic=True, capture_as_const=True)
  240. def fwd(inp, rois):
  241. return F.vision.roi_align(inp, rois, (2, 2))
  242. output = fwd(inp, rois)
  243. check_pygraph_dump(fwd, [inp, rois], [output])
  244. def test_warpperspective():
  245. inp_shape = (1, 1, 4, 4)
  246. x = Tensor(np.arange(16, dtype=np.float32).reshape(inp_shape))
  247. M_shape = (1, 3, 3)
  248. # M defines a translation: dst(1, 1, h, w) = rst(1, 1, h+1, w+1)
  249. M = Tensor(
  250. np.array(
  251. [[1.0, 0.0, 1.0], [0.0, 1.0, 1.0], [0.0, 0.0, 1.0]], dtype=np.float32
  252. ).reshape(M_shape)
  253. )
  254. @trace(symbolic=True, capture_as_const=True)
  255. def fwd(x, M):
  256. return F.vision.warp_perspective(x, M, (2, 2))
  257. result = fwd(x, M)
  258. check_pygraph_dump(fwd, [x, M], [result])
  259. def test_warpaffine():
  260. inp_shape = (1, 3, 3, 3)
  261. x = Tensor(np.arange(27, dtype=np.float32).reshape(inp_shape))
  262. weightv = Tensor([[[1.26666667, 0.6, -83.33333333], [-0.33333333, 1, 66.66666667]]])
  263. @trace(symbolic=True, capture_as_const=True)
  264. def fwd(x, weightv):
  265. return F.vision.warp_affine(x, weightv, (2, 2), border_mode="WRAP")
  266. outp = fwd(x, weightv)
  267. check_pygraph_dump(fwd, [x, weightv], [outp])
  268. def test_remap():
  269. inp_shape = (1, 1, 4, 4)
  270. inp = Tensor(np.arange(16, dtype=np.float32).reshape(inp_shape))
  271. map_xy_shape = (1, 2, 2, 2)
  272. map_xy = Tensor(
  273. np.array(
  274. [[[1.0, 0.0], [0.0, 1.0]], [[0.0, 1.0], [0.0, 1.0]]], dtype=np.float32
  275. ).reshape(map_xy_shape)
  276. )
  277. @trace(symbolic=True, capture_as_const=True)
  278. def fwd(inp, map_xy):
  279. return F.vision.remap(inp, map_xy)
  280. out = fwd(inp, map_xy)
  281. check_pygraph_dump(fwd, [inp, map_xy], [out])
  282. def test_resize():
  283. x = Tensor(np.random.randn(10, 3, 32, 32))
  284. @trace(symbolic=True, capture_as_const=True)
  285. def fwd(x):
  286. return F.vision.interpolate(x, size=(16, 16), mode="BILINEAR")
  287. out = fwd(x)
  288. check_pygraph_dump(fwd, [x], [out])
  289. def test_index_onehot():
  290. src = Tensor([[1.0, 2.0]])
  291. index = Tensor([0])
  292. @trace(symbolic=True, capture_as_const=True)
  293. def fwd(src, index):
  294. return F.indexing_one_hot(src, index)
  295. out = fwd(src, index)
  296. check_pygraph_dump(fwd, [src, index], [out])
  297. def test_set_onehot():
  298. x = Tensor(np.arange(1, 4, dtype=np.int32))
  299. @trace(symbolic=True, capture_as_const=True)
  300. def fwd(x):
  301. return F.one_hot(x, num_classes=4)
  302. out = fwd(x)
  303. check_pygraph_dump(fwd, [x], [out])
  304. def test_copy():
  305. x = Tensor([1, 2, 3])
  306. @trace(symbolic=True, capture_as_const=True)
  307. def fwd(x):
  308. return x.to("cpu0:0")
  309. o = fwd(x)
  310. check_pygraph_dump(fwd, [x], [o])
  311. def test_argsort():
  312. @trace(symbolic=True, capture_as_const=True)
  313. def fwd(data):
  314. return F.argsort(data, True)
  315. data = Tensor([1.0, 2.0, 3.0, 5.0])
  316. result = fwd(data)
  317. check_pygraph_dump(fwd, [data], [result])
  318. def test_argmax_min():
  319. @trace(symbolic=True, capture_as_const=True)
  320. def fwd(data):
  321. return F.argmax(data), F.argmin(data)
  322. data = Tensor(np.random.random((10, 10)))
  323. result = fwd(data)
  324. check_pygraph_dump(fwd, [data], result)
  325. def test_condtake():
  326. mask = Tensor(np.array([[True, False], [False, True]], dtype=np.bool_))
  327. x = Tensor(np.array([[1, np.inf], [np.nan, 4]], dtype=np.float32))
  328. @trace(symbolic=True, capture_as_const=True)
  329. def fwd(mask, x):
  330. v, index = F.cond_take(mask, x)
  331. return v, index
  332. v, index = fwd(mask, x)
  333. check_pygraph_dump(fwd, [mask, x], [v, index])
  334. def test_topk():
  335. x = Tensor(np.array([2, 4, 6, 8, 7, 5, 3, 1], dtype=np.float32))
  336. @trace(symbolic=True, capture_as_const=True)
  337. def fwd(x):
  338. top, indices = F.topk(x, 5)
  339. return top, indices
  340. top, indices = fwd(x)
  341. check_pygraph_dump(fwd, [x], [top, indices])
  342. def test_random():
  343. @trace(symbolic=True, capture_as_const=True)
  344. def fwd():
  345. x = rand.uniform(size=(2, 2))
  346. y = rand.normal(size=(1, 3, 3, 3))
  347. return x, y
  348. x, y = fwd()
  349. check_pygraph_dump(fwd, [], [x, y])
  350. def test_tensor_gen():
  351. @trace(symbolic=True, capture_as_const=True)
  352. def fwd():
  353. a = F.linspace(3, 10, 3, device=Device("xpux").to_c())
  354. b = F.eye(3, device=Device("xpux").to_c())
  355. return a, b
  356. a, b = fwd()
  357. check_pygraph_dump(fwd, [], [a, b])
  358. def test_getvarshape():
  359. op = builtin.GetVarShape(axis=1)
  360. @trace(symbolic=True, capture_as_const=True)
  361. def fwd(data):
  362. return apply(op, data)[0]
  363. data = Tensor(np.random.random((1, 2, 3, 4)))
  364. result = fwd(data)
  365. check_pygraph_dump(fwd, [data], [result])
  366. def test_concat():
  367. @trace(symbolic=True, capture_as_const=True)
  368. def fwd(data1, data2):
  369. return F.concat([data1, data2], axis=1)
  370. x = Tensor(np.random.random((2, 3)))
  371. y = Tensor(np.random.random((2, 5)))
  372. result = fwd(x, y)
  373. check_pygraph_dump(fwd, [x, y], [result])
  374. def test_broadcast():
  375. inp = Tensor([[1], [2], [3], [4]])
  376. @trace(symbolic=True, capture_as_const=True)
  377. def fwd(inp):
  378. return F.broadcast_to(inp, (4, 4))
  379. out = fwd(inp)
  380. check_pygraph_dump(fwd, [inp], [out])
  381. def test_identity():
  382. @trace(symbolic=True, capture_as_const=True)
  383. def fwd(data):
  384. return F.copy(data)
  385. data = Tensor([1.0, 2.0])
  386. result = fwd(data)
  387. check_pygraph_dump(fwd, [data], [result])
  388. @pytest.mark.skip(reason="advance indexing trace error")
  389. def test_nms():
  390. x = np.zeros((100, 4))
  391. np.random.seed(42)
  392. x[:, :2] = np.random.rand(100, 2) * 20
  393. x[:, 2:] = np.random.rand(100, 2) * 20 + 100
  394. scores = Tensor(np.random.rand(100))
  395. inp = Tensor(x)
  396. @trace(symbolic=True, capture_as_const=True)
  397. def fwd(inp, scores):
  398. return F.nn.nms(inp, scores, iou_thresh=0.7, max_output=3)
  399. result = fwd(inp, scores)
  400. check_pygraph_dump(fwd, [inp, scores], [result])
  401. def test_dimshuffle():
  402. inp = Tensor([1, 2, 3, 4])
  403. @trace(symbolic=True, capture_as_const=True)
  404. def fwd(inp):
  405. return inp.T
  406. out = fwd(inp)
  407. check_pygraph_dump(fwd, [inp], [out])
  408. def test_reshape():
  409. @trace(symbolic=True, capture_as_const=True)
  410. def fwd(data):
  411. return data.reshape((1, 8))
  412. data = Tensor(np.random.random((1, 2, 2, 2)))
  413. result = fwd(data)
  414. check_pygraph_dump(fwd, [data], [result])
  415. def test_add_remove_axis():
  416. @trace(symbolic=True, capture_as_const=True)
  417. def fwd(data):
  418. x = F.expand_dims(data, [0, 0])
  419. y = F.squeeze(x, 0)
  420. return y
  421. data = Tensor([1.0, 2.0])
  422. result = fwd(data)
  423. check_pygraph_dump(fwd, [data], [result])
  424. @pytest.mark.parametrize("mode", ["get", "set", "inc"])
  425. def test_subtensor(mode):
  426. items = [[0, True, True, True, False], [1, False, False, False, True]]
  427. data = [Tensor(np.random.random((5, 5))), Tensor(np.random.random(2))]
  428. if mode == "get":
  429. op = builtin.Subtensor(items)
  430. data = data[:1]
  431. if mode == "set":
  432. op = builtin.SetSubtensor(items)
  433. if mode == "inc":
  434. op = builtin.IncrSubtensor(items)
  435. tensors = [Tensor(0), Tensor(4), Tensor(2), Tensor(3)]
  436. @trace(symbolic=True, capture_as_const=True)
  437. def fwd(*tensors):
  438. return apply(op, *tensors)[0]
  439. result = fwd(*data, *tensors)
  440. check_pygraph_dump(fwd, data + tensors, [result])
  441. @pytest.mark.parametrize("mode", ["get", "set", "inc"])
  442. def test_advance_indexing(mode):
  443. items = [[0, False, False, False, True]]
  444. tensors = [Tensor([0, 4, 2])]
  445. data = [Tensor(np.random.random((5, 5))), Tensor(np.random.random((3, 5)))]
  446. if mode == "get":
  447. op = builtin.IndexingMultiAxisVec(items)
  448. data = data[:1]
  449. if mode == "set":
  450. op = builtin.IndexingSetMultiAxisVec(items)
  451. if mode == "inc":
  452. op = builtin.IndexingIncrMultiAxisVec(items)
  453. @trace(symbolic=True, capture_as_const=True)
  454. def fwd(*tensors):
  455. return apply(op, *tensors)[0]
  456. result = fwd(*data, *tensors)
  457. check_pygraph_dump(fwd, data + tensors, [result])
  458. @pytest.mark.parametrize("mode", ["get", "set", "inc"])
  459. def test_mesh_indexing(mode):
  460. items = [[0, True, True, True, False], [1, False, False, False, True]]
  461. tensors = [Tensor(0), Tensor(5), Tensor(2), Tensor([1, 3])]
  462. data = [Tensor(np.random.random((5, 5))), Tensor(np.random.random((3, 2)))]
  463. if mode == "get":
  464. op = builtin.IndexingMultiAxisVec(items)
  465. data = data[:1]
  466. if mode == "set":
  467. op = builtin.IndexingSetMultiAxisVec(items)
  468. if mode == "inc":
  469. op = builtin.IndexingIncrMultiAxisVec(items)
  470. @trace(symbolic=True, capture_as_const=True)
  471. def fwd(*tensors):
  472. return apply(op, *tensors)[0]
  473. result = fwd(*data, *tensors)
  474. check_pygraph_dump(fwd, data + tensors, [result])
  475. @pytest.mark.parametrize("mode", ["get", "set", "inc"])
  476. def test_batch_mesh_indexing(mode):
  477. items = [[1, False, False, False, True], [2, False, False, False, True]]
  478. tensors = [Tensor([[0, 2], [0, 2]]), Tensor([[0, 1, 2], [1, 2, 3]])]
  479. data = [Tensor(np.random.random((2, 3, 4))), Tensor(np.random.random((2, 2, 3)))]
  480. if mode == "get":
  481. op = builtin.BatchedMeshIndexing(items)
  482. data = data[:1]
  483. if mode == "set":
  484. op = builtin.BatchedSetMeshIndexing(items)
  485. if mode == "inc":
  486. op = builtin.BatchedIncrMeshIndexing(items)
  487. @trace(symbolic=True, capture_as_const=True)
  488. def fwd(*tensors):
  489. return apply(op, *tensors)[0]
  490. result = fwd(*data, *tensors)
  491. check_pygraph_dump(fwd, data + tensors, [result])
  492. @pytest.mark.skip(reason="tmp skip")
  493. def test_assert_equal():
  494. g = G.Graph()
  495. inp1 = g.make_h2d(dtype=np.float32, device="xpux")
  496. inp2 = g.make_h2d(dtype=np.float32, device="xpux")
  497. op = builtin.AssertEqual(maxerr=1e-5)
  498. out = G.apply_normal_varnode(op, inp1._node, inp2._node)[0]
  499. print(out)
  500. g.compile(out)
  501. file = io.BytesIO()
  502. out_model = G.dump_graph([out])
  503. file.write(out_model[0])
  504. file.seek(0)
  505. net = Net.load(file)
  506. dump_file = io.BytesIO()
  507. net.dump(dump_file)
  508. dump_file.seek(0)
  509. g = GraphInference(dump_file)
  510. g.run(np.array([1.0, 2.0]), np.array([1.0, 2.0]))
  511. def test_elemwise_multitype():
  512. op = builtin.ElemwiseMultiType(mode="QADD", dtype=dtype.qint32(2.0))
  513. @trace(symbolic=True, capture_as_const=True)
  514. def fwd(x, y):
  515. return apply(op, x, y)[0]
  516. x = Tensor(np.random.random(10) * 10, dtype=dtype.qint8(2.0))
  517. y = Tensor(np.random.random(10) * 10, dtype=dtype.qint8(2.0))
  518. result = fwd(x, y)
  519. check_pygraph_dump(fwd, [x, y], [result])
  520. def test_cvtcolor():
  521. inp = np.random.randn(3, 3, 3, 3).astype(np.float32)
  522. x = Tensor(inp)
  523. @trace(symbolic=True, capture_as_const=True)
  524. def fwd(inp):
  525. return F.vision.cvt_color(inp, mode="RGB2GRAY")
  526. result = fwd(x)
  527. check_pygraph_dump(fwd, [x], [result])

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