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

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

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