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

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

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