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

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

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