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_tensor.py 13 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. # -*- coding: utf-8 -*-
  2. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  3. #
  4. # Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
  5. #
  6. # Unless required by applicable law or agreed to in writing,
  7. # software distributed under the License is distributed on an
  8. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. import platform
  10. import numpy as np
  11. import pytest
  12. import megengine.functional as F
  13. from megengine import tensor
  14. from megengine.core._trace_option import use_tensor_shape
  15. from megengine.core.tensor.utils import astensor1d
  16. from megengine.distributed.helper import get_device_count_by_fork
  17. from megengine.test import assertTensorClose
  18. def _default_compare_fn(x, y):
  19. assertTensorClose(x.numpy(), y)
  20. def opr_test(cases, func, compare_fn=_default_compare_fn, ref_fn=None, **kwargs):
  21. """
  22. func: the function to run opr.
  23. compare_fn: the function to compare the result and expected, use assertTensorClose if None.
  24. ref_fn: the function to generate expected data, should assign output if None.
  25. cases: the list which have dict element, the list length should be 2 for dynamic shape test.
  26. and the dict should have input,
  27. and should have output if ref_fn is None.
  28. should use list for multiple inputs and outputs for each case.
  29. kwargs: The additional kwargs for opr func.
  30. simple examples:
  31. dtype = np.float32
  32. cases = [{"input": [10, 20]}, {"input": [20, 30]}]
  33. opr_test(cases,
  34. F.eye,
  35. ref_fn=lambda n, m: np.eye(n, m).astype(dtype),
  36. dtype=dtype)
  37. """
  38. def check_results(results, expected):
  39. if not isinstance(results, tuple):
  40. results = (results,)
  41. for r, e in zip(results, expected):
  42. compare_fn(r, e)
  43. def get_param(cases, idx):
  44. case = cases[idx]
  45. inp = case.get("input", None)
  46. outp = case.get("output", None)
  47. if inp is None:
  48. raise ValueError("the test case should have input")
  49. if not isinstance(inp, list):
  50. inp = (inp,)
  51. else:
  52. inp = tuple(inp)
  53. if ref_fn is not None and callable(ref_fn):
  54. outp = ref_fn(*inp)
  55. if outp is None:
  56. raise ValueError("the test case should have output or reference function")
  57. if not isinstance(outp, list):
  58. outp = (outp,)
  59. else:
  60. outp = tuple(outp)
  61. return inp, outp
  62. if len(cases) == 0:
  63. raise ValueError("should give one case at least")
  64. if not callable(func):
  65. raise ValueError("the input func should be callable")
  66. inp, outp = get_param(cases, 0)
  67. inp_tensor = [tensor(inpi) for inpi in inp]
  68. results = func(*inp_tensor, **kwargs)
  69. check_results(results, outp)
  70. def test_eye():
  71. dtype = np.float32
  72. cases = [{"input": [10, 20]}, {"input": [20, 30]}]
  73. for case in cases:
  74. assertTensorClose(
  75. F.eye(case["input"], dtype=dtype).numpy(),
  76. np.eye(*case["input"]).astype(dtype),
  77. )
  78. def test_concat():
  79. def get_data_shape(length: int):
  80. return (length, 2, 3)
  81. data1 = np.random.random(get_data_shape(5)).astype("float32")
  82. data2 = np.random.random(get_data_shape(6)).astype("float32")
  83. data3 = np.random.random(get_data_shape(7)).astype("float32")
  84. def run(data1, data2):
  85. return F.concat([data1, data2])
  86. cases = [{"input": [data1, data2]}, {"input": [data1, data3]}]
  87. opr_test(cases, run, ref_fn=lambda x, y: np.concatenate([x, y]))
  88. def test_concat_device():
  89. data1 = tensor(np.random.random((3, 2, 2)).astype("float32"), device="cpu0")
  90. data2 = tensor(np.random.random((2, 2, 2)).astype("float32"), device="cpu1")
  91. out = F.concat([data1, data2], device="cpu0")
  92. assert str(out.device).split(":")[0] == "cpu0"
  93. def test_stack():
  94. data1 = np.random.random((3, 2, 2)).astype("float32")
  95. data2 = np.random.random((3, 2, 2)).astype("float32")
  96. data3 = np.random.random((3, 2, 2)).astype("float32")
  97. cases = [{"input": [data1, data2]}, {"input": [data1, data3]}]
  98. for ai in range(3):
  99. def run(data1, data2):
  100. return F.stack([data1, data2], axis=ai)
  101. opr_test(cases, run, ref_fn=lambda x, y: np.stack([x, y], axis=ai))
  102. def test_split():
  103. data = np.random.random((2, 3, 4, 5)).astype(np.float32)
  104. mge_out1 = F.split(tensor(data), 2, axis=3)
  105. mge_out2 = F.split(tensor(data), [3, 5], axis=3)
  106. np_out = np.split(data, [3, 5], axis=3)
  107. np.testing.assert_equal(mge_out1[0].numpy(), mge_out2[0].numpy())
  108. np.testing.assert_equal(mge_out1[0].numpy(), np_out[0])
  109. def test_reshape():
  110. x = np.arange(6, dtype="float32")
  111. xx = tensor(x)
  112. y = x.reshape(1, 2, 3)
  113. for shape in [
  114. (1, 2, 3),
  115. (1, -1, 3),
  116. (1, tensor(-1), 3),
  117. np.array([1, -1, 3], dtype="int32"),
  118. tensor([1, -1, 3]),
  119. ]:
  120. yy = F.reshape(xx, shape)
  121. np.testing.assert_equal(yy.numpy(), y)
  122. def test_squeeze():
  123. x = np.arange(6, dtype="float32").reshape(1, 2, 3, 1)
  124. xx = tensor(x)
  125. for axis in [None, 3, -4, (3, -4)]:
  126. y = np.squeeze(x, axis)
  127. yy = F.remove_axis(xx, axis)
  128. np.testing.assert_equal(y, yy.numpy())
  129. def test_expand_dims():
  130. x = np.arange(6, dtype="float32").reshape(2, 3)
  131. xx = tensor(x)
  132. for axis in [2, -3, (3, -4), (1, -4)]:
  133. y = np.expand_dims(x, axis)
  134. yy = F.add_axis(xx, axis)
  135. np.testing.assert_equal(y, yy.numpy())
  136. def test_elemwise_dtype_promotion():
  137. x = np.random.rand(2, 3).astype("float32")
  138. y = np.random.rand(1, 3).astype("float16")
  139. xx = tensor(x)
  140. yy = tensor(y)
  141. z = xx * yy
  142. np.testing.assert_equal(z.numpy(), x * y)
  143. z = xx + y
  144. np.testing.assert_equal(z.numpy(), x + y)
  145. z = x - yy
  146. np.testing.assert_equal(z.numpy(), x - y)
  147. def test_linspace():
  148. cases = [
  149. {"input": [1, 9, 9]},
  150. {"input": [3, 10, 8]},
  151. ]
  152. opr_test(
  153. cases,
  154. F.linspace,
  155. ref_fn=lambda start, end, step: np.linspace(start, end, step, dtype=np.float32),
  156. )
  157. cases = [
  158. {"input": [9, 1, 9]},
  159. {"input": [10, 3, 8]},
  160. ]
  161. opr_test(
  162. cases,
  163. F.linspace,
  164. ref_fn=lambda start, end, step: np.linspace(start, end, step, dtype=np.float32),
  165. )
  166. def test_arange():
  167. cases = [
  168. {"input": [1, 9, 1]},
  169. {"input": [2, 10, 2]},
  170. ]
  171. opr_test(
  172. cases,
  173. F.arange,
  174. ref_fn=lambda start, end, step: np.arange(start, end, step, dtype=np.float32),
  175. )
  176. cases = [
  177. {"input": [9, 1, -1]},
  178. {"input": [10, 2, -2]},
  179. ]
  180. opr_test(
  181. cases,
  182. F.arange,
  183. ref_fn=lambda start, end, step: np.arange(start, end, step, dtype=np.float32),
  184. )
  185. cases = [
  186. {"input": [9.3, 1.2, -0.5]},
  187. {"input": [10.3, 2.1, -1.7]},
  188. ]
  189. opr_test(
  190. cases,
  191. F.arange,
  192. ref_fn=lambda start, end, step: np.arange(start, end, step, dtype=np.float32),
  193. )
  194. def test_round():
  195. data1_shape = (15,)
  196. data2_shape = (25,)
  197. data1 = np.random.random(data1_shape).astype(np.float32)
  198. data2 = np.random.random(data2_shape).astype(np.float32)
  199. cases = [{"input": data1}, {"input": data2}]
  200. opr_test(cases, F.round, ref_fn=np.round)
  201. def test_flatten():
  202. data0_shape = (2, 3, 4, 5)
  203. data1_shape = (4, 5, 6, 7)
  204. data0 = np.random.random(data0_shape).astype(np.float32)
  205. data1 = np.random.random(data1_shape).astype(np.float32)
  206. def compare_fn(x, y):
  207. assert x.numpy().shape == y[0]
  208. output0 = (2 * 3 * 4 * 5,)
  209. output1 = (4 * 5 * 6 * 7,)
  210. cases = [
  211. {"input": data0, "output": (output0,)},
  212. {"input": data1, "output": (output1,)},
  213. ]
  214. opr_test(cases, F.flatten, compare_fn=compare_fn)
  215. output0 = (2, 3 * 4 * 5)
  216. output1 = (4, 5 * 6 * 7)
  217. cases = [
  218. {"input": data0, "output": (output0,)},
  219. {"input": data1, "output": (output1,)},
  220. ]
  221. opr_test(cases, F.flatten, compare_fn=compare_fn, start_axis=1)
  222. output0 = (2, 3, 4 * 5)
  223. output1 = (4, 5, 6 * 7)
  224. cases = [
  225. {"input": data0, "output": (output0,)},
  226. {"input": data1, "output": (output1,)},
  227. ]
  228. opr_test(cases, F.flatten, compare_fn=compare_fn, start_axis=2)
  229. output0 = (2, 3 * 4, 5)
  230. output1 = (4, 5 * 6, 7)
  231. cases = [
  232. {"input": data0, "output": (output0,)},
  233. {"input": data1, "output": (output1,)},
  234. ]
  235. opr_test(cases, F.flatten, compare_fn=compare_fn, start_axis=1, end_axis=2)
  236. def test_broadcast():
  237. input1_shape = (20, 30)
  238. output1_shape = (30, 20, 30)
  239. data1 = np.random.random(input1_shape).astype(np.float32)
  240. input2_shape = (10, 20)
  241. output2_shape = (20, 10, 20)
  242. data2 = np.random.random(input2_shape).astype(np.float32)
  243. def compare_fn(x, y):
  244. assert x.numpy().shape == y
  245. cases = [
  246. {"input": [data1, output1_shape], "output": output1_shape},
  247. {"input": [data2, output2_shape], "output": output2_shape},
  248. ]
  249. opr_test(cases, F.broadcast, compare_fn=compare_fn)
  250. def test_utils_astensor1d():
  251. reference = tensor(0)
  252. # literal
  253. x = [1, 2, 3]
  254. for dtype in [None, "float32"]:
  255. xx = astensor1d(x, reference, dtype=dtype)
  256. assert type(xx) is tensor
  257. np.testing.assert_equal(xx.numpy(), x)
  258. # numpy array
  259. x = np.asarray([1, 2, 3], dtype="int32")
  260. for dtype in [None, "float32"]:
  261. xx = astensor1d(x, reference, dtype=dtype)
  262. assert type(xx) is tensor
  263. np.testing.assert_equal(xx.numpy(), x.astype(dtype) if dtype else x)
  264. # tensor
  265. x = tensor([1, 2, 3], dtype="int32")
  266. for dtype in [None, "float32"]:
  267. xx = astensor1d(x, reference, dtype=dtype)
  268. assert type(xx) is tensor
  269. np.testing.assert_equal(xx.numpy(), x.numpy())
  270. # mixed
  271. x = [1, tensor(2), 3]
  272. for dtype in [None, "float32"]:
  273. xx = astensor1d(x, reference, dtype=dtype)
  274. assert type(xx) is tensor
  275. np.testing.assert_equal(xx.numpy(), [1, 2, 3])
  276. def test_device():
  277. x = tensor([1, 2, 3], dtype="float32")
  278. y1 = F.eye(x.shape, dtype="float32")
  279. y2 = F.eye(x.shape, dtype="float32", device=None)
  280. np.testing.assert_almost_equal(y1.numpy(), y2.numpy())
  281. y3 = F.eye(x.shape, dtype="float32", device="xpux")
  282. y4 = F.eye(x.shape, dtype="float32", device=x.device.to_c())
  283. np.testing.assert_almost_equal(y3.numpy(), y4.numpy())
  284. y5 = F.full((3, 2), 4, device=x.device)
  285. y6 = F.full((3, 2), 4, device="xpux")
  286. np.testing.assert_almost_equal(y5.numpy(), y6.numpy())
  287. def copy_test(dst, src):
  288. data = np.random.random((2, 3)).astype(np.float32)
  289. x = tensor(data, device=src)
  290. y = F.copy(x, dst)
  291. assert np.allclose(data, y.numpy())
  292. z = x.to(dst)
  293. assert np.allclose(data, z.numpy())
  294. @pytest.mark.skipif(
  295. platform.system() == "Darwin", reason="do not imp GPU mode at macos now"
  296. )
  297. @pytest.mark.skipif(
  298. platform.system() == "Windows", reason="do not imp GPU mode at Windows now"
  299. )
  300. @pytest.mark.skipif(get_device_count_by_fork("gpu") == 0, reason="CUDA is disabled")
  301. def test_copy_h2d():
  302. copy_test("cpu0", "gpu0")
  303. @pytest.mark.skipif(
  304. platform.system() == "Darwin", reason="do not imp GPU mode at macos now"
  305. )
  306. @pytest.mark.skipif(
  307. platform.system() == "Windows", reason="do not imp GPU mode at Windows now"
  308. )
  309. @pytest.mark.skipif(get_device_count_by_fork("gpu") == 0, reason="CUDA is disabled")
  310. def test_copy_d2h():
  311. copy_test("gpu0", "cpu0")
  312. @pytest.mark.skipif(
  313. platform.system() == "Darwin", reason="do not imp GPU mode at macos now"
  314. )
  315. @pytest.mark.skipif(
  316. platform.system() == "Windows", reason="do not imp GPU mode at Windows now"
  317. )
  318. @pytest.mark.skipif(get_device_count_by_fork("gpu") < 2, reason="need more gpu device")
  319. def test_copy_d2d():
  320. copy_test("gpu0", "gpu1")
  321. copy_test("gpu0:0", "gpu0:1")
  322. def test_param_pack_split():
  323. a = tensor(np.ones((10,), np.int32))
  324. b, c = F.param_pack_split(a, [0, 1, 1, 10], [(1,), (3, 3)])
  325. assert np.allclose(b.numpy(), a.numpy()[1])
  326. assert np.allclose(c.numpy(), a.numpy()[1:].reshape(3, 3))
  327. def test_param_pack_concat():
  328. a = tensor(np.ones((1,), np.int32))
  329. b = tensor(np.ones((3, 3), np.int32))
  330. offsets_val = [0, 1, 1, 10]
  331. offsets = tensor(offsets_val, np.int32)
  332. c = F.param_pack_concat([a, b], offsets, offsets_val)
  333. assert np.allclose(np.concatenate([a.numpy(), b.numpy().flatten()]), c.numpy())

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