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 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 os
  10. import platform
  11. import numpy as np
  12. import pytest
  13. from utils import opr_test
  14. import megengine.functional as F
  15. from megengine import tensor
  16. from megengine.core._trace_option import use_tensor_shape
  17. from megengine.core.tensor.utils import astensor1d
  18. from megengine.distributed.helper import get_device_count_by_fork
  19. from megengine.test import assertTensorClose
  20. def test_eye():
  21. dtype = np.float32
  22. cases = [{"input": [10, 20]}, {"input": [20, 30]}]
  23. for case in cases:
  24. assertTensorClose(
  25. F.eye(case["input"], dtype=dtype).numpy(),
  26. np.eye(*case["input"]).astype(dtype),
  27. )
  28. def test_concat():
  29. def get_data_shape(length: int):
  30. return (length, 2, 3)
  31. data1 = np.random.random(get_data_shape(5)).astype("float32")
  32. data2 = np.random.random(get_data_shape(6)).astype("float32")
  33. data3 = np.random.random(get_data_shape(7)).astype("float32")
  34. def run(data1, data2):
  35. return F.concat([data1, data2])
  36. cases = [{"input": [data1, data2]}, {"input": [data1, data3]}]
  37. opr_test(cases, run, ref_fn=lambda x, y: np.concatenate([x, y]))
  38. def test_concat_device():
  39. data1 = tensor(np.random.random((3, 2, 2)).astype("float32"), device="cpu0")
  40. data2 = tensor(np.random.random((2, 2, 2)).astype("float32"), device="cpu1")
  41. out = F.concat([data1, data2], device="cpu0")
  42. assert str(out.device).split(":")[0] == "cpu0"
  43. def test_stack():
  44. data1 = np.random.random((3, 2, 2)).astype("float32")
  45. data2 = np.random.random((3, 2, 2)).astype("float32")
  46. data3 = np.random.random((3, 2, 2)).astype("float32")
  47. cases = [{"input": [data1, data2]}, {"input": [data1, data3]}]
  48. for ai in range(3):
  49. def run(data1, data2):
  50. return F.stack([data1, data2], axis=ai)
  51. opr_test(cases, run, ref_fn=lambda x, y: np.stack([x, y], axis=ai))
  52. def test_split():
  53. data = np.random.random((2, 3, 4, 5)).astype(np.float32)
  54. mge_out1 = F.split(tensor(data), 2, axis=3)
  55. mge_out2 = F.split(tensor(data), [3, 5], axis=3)
  56. np_out = np.split(data, [3, 5], axis=3)
  57. np.testing.assert_equal(mge_out1[0].numpy(), mge_out2[0].numpy())
  58. np.testing.assert_equal(mge_out1[0].numpy(), np_out[0])
  59. def test_reshape():
  60. x = np.arange(6, dtype="float32")
  61. xx = tensor(x)
  62. y = x.reshape(1, 2, 3)
  63. for shape in [
  64. (1, 2, 3),
  65. (1, -1, 3),
  66. (1, tensor(-1), 3),
  67. np.array([1, -1, 3], dtype="int32"),
  68. tensor([1, -1, 3]),
  69. ]:
  70. yy = F.reshape(xx, shape)
  71. np.testing.assert_equal(yy.numpy(), y)
  72. def test_squeeze():
  73. x = np.arange(6, dtype="float32").reshape(1, 2, 3, 1)
  74. xx = tensor(x)
  75. for axis in [None, 3, -4, (3, -4)]:
  76. y = np.squeeze(x, axis)
  77. yy = F.remove_axis(xx, axis)
  78. np.testing.assert_equal(y, yy.numpy())
  79. def test_expand_dims():
  80. x = np.arange(6, dtype="float32").reshape(2, 3)
  81. xx = tensor(x)
  82. for axis in [2, -3, (3, -4), (1, -4)]:
  83. y = np.expand_dims(x, axis)
  84. yy = F.add_axis(xx, axis)
  85. np.testing.assert_equal(y, yy.numpy())
  86. def test_elemwise_dtype_promotion():
  87. x = np.random.rand(2, 3).astype("float32")
  88. y = np.random.rand(1, 3).astype("float16")
  89. xx = tensor(x)
  90. yy = tensor(y)
  91. z = xx * yy
  92. np.testing.assert_equal(z.numpy(), x * y)
  93. z = xx + y
  94. np.testing.assert_equal(z.numpy(), x + y)
  95. z = x - yy
  96. np.testing.assert_equal(z.numpy(), x - y)
  97. def test_linspace():
  98. cases = [
  99. {"input": [1, 9, 9]},
  100. {"input": [3, 10, 8]},
  101. ]
  102. opr_test(
  103. cases,
  104. F.linspace,
  105. ref_fn=lambda start, end, step: np.linspace(start, end, step, dtype=np.float32),
  106. )
  107. cases = [
  108. {"input": [9, 1, 9]},
  109. {"input": [10, 3, 8]},
  110. ]
  111. opr_test(
  112. cases,
  113. F.linspace,
  114. ref_fn=lambda start, end, step: np.linspace(start, end, step, dtype=np.float32),
  115. )
  116. def test_arange():
  117. cases = [
  118. {"input": [1, 9, 1]},
  119. {"input": [2, 10, 2]},
  120. ]
  121. opr_test(
  122. cases,
  123. F.arange,
  124. ref_fn=lambda start, end, step: np.arange(start, end, step, dtype=np.float32),
  125. )
  126. cases = [
  127. {"input": [9, 1, -1]},
  128. {"input": [10, 2, -2]},
  129. ]
  130. opr_test(
  131. cases,
  132. F.arange,
  133. ref_fn=lambda start, end, step: np.arange(start, end, step, dtype=np.float32),
  134. )
  135. cases = [
  136. {"input": [9.3, 1.2, -0.5]},
  137. {"input": [10.3, 2.1, -1.7]},
  138. ]
  139. opr_test(
  140. cases,
  141. F.arange,
  142. ref_fn=lambda start, end, step: np.arange(start, end, step, dtype=np.float32),
  143. )
  144. def test_round():
  145. data1_shape = (15,)
  146. data2_shape = (25,)
  147. data1 = np.random.random(data1_shape).astype(np.float32)
  148. data2 = np.random.random(data2_shape).astype(np.float32)
  149. cases = [{"input": data1}, {"input": data2}]
  150. opr_test(cases, F.round, ref_fn=np.round)
  151. def test_flatten():
  152. data0_shape = (2, 3, 4, 5)
  153. data1_shape = (4, 5, 6, 7)
  154. data0 = np.random.random(data0_shape).astype(np.float32)
  155. data1 = np.random.random(data1_shape).astype(np.float32)
  156. def compare_fn(x, y):
  157. assert x.shape[0] == y
  158. output0 = (2 * 3 * 4 * 5,)
  159. output1 = (4 * 5 * 6 * 7,)
  160. cases = [
  161. {"input": data0, "output": output0},
  162. {"input": data1, "output": output1},
  163. ]
  164. opr_test(cases, F.flatten, compare_fn=compare_fn)
  165. output0 = (2, 3 * 4 * 5)
  166. output1 = (4, 5 * 6 * 7)
  167. cases = [
  168. {"input": data0, "output": output0},
  169. {"input": data1, "output": output1},
  170. ]
  171. opr_test(cases, F.flatten, compare_fn=compare_fn, start_axis=1)
  172. output0 = (2, 3, 4 * 5)
  173. output1 = (4, 5, 6 * 7)
  174. cases = [
  175. {"input": data0, "output": output0},
  176. {"input": data1, "output": output1},
  177. ]
  178. opr_test(cases, F.flatten, compare_fn=compare_fn, start_axis=2)
  179. output0 = (2, 3 * 4, 5)
  180. output1 = (4, 5 * 6, 7)
  181. cases = [
  182. {"input": data0, "output": output0},
  183. {"input": data1, "output": output1},
  184. ]
  185. opr_test(cases, F.flatten, compare_fn=compare_fn, start_axis=1, end_axis=2)
  186. def test_broadcast():
  187. input1_shape = (20, 30)
  188. output1_shape = (30, 20, 30)
  189. data1 = np.random.random(input1_shape).astype(np.float32)
  190. input2_shape = (10, 20)
  191. output2_shape = (20, 10, 20)
  192. data2 = np.random.random(input2_shape).astype(np.float32)
  193. def compare_fn(x, y):
  194. assert x.shape[0] == y
  195. cases = [
  196. {"input": [data1, output1_shape], "output": output1_shape},
  197. {"input": [data2, output2_shape], "output": output2_shape},
  198. ]
  199. opr_test(cases, F.broadcast, compare_fn=compare_fn)
  200. def test_utils_astensor1d():
  201. reference = tensor(0)
  202. # literal
  203. x = [1, 2, 3]
  204. for dtype in [None, "float32"]:
  205. xx = astensor1d(x, reference, dtype=dtype)
  206. assert type(xx) is tensor
  207. np.testing.assert_equal(xx.numpy(), x)
  208. # numpy array
  209. x = np.asarray([1, 2, 3], dtype="int32")
  210. for dtype in [None, "float32"]:
  211. xx = astensor1d(x, reference, dtype=dtype)
  212. assert type(xx) is tensor
  213. np.testing.assert_equal(xx.numpy(), x.astype(dtype) if dtype else x)
  214. # tensor
  215. x = tensor([1, 2, 3], dtype="int32")
  216. for dtype in [None, "float32"]:
  217. xx = astensor1d(x, reference, dtype=dtype)
  218. assert type(xx) is tensor
  219. np.testing.assert_equal(xx.numpy(), x.numpy())
  220. # mixed
  221. x = [1, tensor(2), 3]
  222. for dtype in [None, "float32"]:
  223. xx = astensor1d(x, reference, dtype=dtype)
  224. assert type(xx) is tensor
  225. np.testing.assert_equal(xx.numpy(), [1, 2, 3])
  226. def test_device():
  227. x = tensor([1, 2, 3], dtype="float32")
  228. y1 = F.eye(x.shape, dtype="float32")
  229. y2 = F.eye(x.shape, dtype="float32", device=None)
  230. np.testing.assert_almost_equal(y1.numpy(), y2.numpy())
  231. y3 = F.eye(x.shape, dtype="float32", device="xpux")
  232. y4 = F.eye(x.shape, dtype="float32", device=x.device.to_c())
  233. np.testing.assert_almost_equal(y3.numpy(), y4.numpy())
  234. y5 = F.full((3, 2), 4, device=x.device)
  235. y6 = F.full((3, 2), 4, device="xpux")
  236. np.testing.assert_almost_equal(y5.numpy(), y6.numpy())
  237. def test_identity():
  238. x = tensor(np.random.random((5, 10)).astype(np.float32))
  239. y = F.identity(x)
  240. np.testing.assert_equal(y.numpy(), x)
  241. def copy_test(dst, src):
  242. data = np.random.random((2, 3)).astype(np.float32)
  243. x = tensor(data, device=src)
  244. y = F.copy(x, dst)
  245. assert np.allclose(data, y.numpy())
  246. z = x.to(dst)
  247. assert np.allclose(data, z.numpy())
  248. @pytest.mark.skipif(
  249. platform.system() == "Darwin", reason="do not imp GPU mode at macos now"
  250. )
  251. @pytest.mark.skipif(
  252. platform.system() == "Windows", reason="do not imp GPU mode at Windows now"
  253. )
  254. @pytest.mark.skipif(get_device_count_by_fork("gpu") == 0, reason="CUDA is disabled")
  255. def test_copy_h2d():
  256. copy_test("cpu0", "gpu0")
  257. @pytest.mark.skipif(
  258. platform.system() == "Darwin", reason="do not imp GPU mode at macos now"
  259. )
  260. @pytest.mark.skipif(
  261. platform.system() == "Windows", reason="do not imp GPU mode at Windows now"
  262. )
  263. @pytest.mark.skipif(get_device_count_by_fork("gpu") == 0, reason="CUDA is disabled")
  264. def test_copy_d2h():
  265. copy_test("gpu0", "cpu0")
  266. @pytest.mark.skipif(
  267. platform.system() == "Darwin", reason="do not imp GPU mode at macos now"
  268. )
  269. @pytest.mark.skipif(
  270. platform.system() == "Windows", reason="do not imp GPU mode at Windows now"
  271. )
  272. @pytest.mark.skipif(get_device_count_by_fork("gpu") < 2, reason="need more gpu device")
  273. def test_copy_d2d():
  274. copy_test("gpu0", "gpu1")
  275. copy_test("gpu0:0", "gpu0:1")
  276. def test_param_pack_split():
  277. a = tensor(np.ones((10,), np.int32))
  278. b, c = F.param_pack_split(a, [0, 1, 1, 10], [(1,), (3, 3)])
  279. assert np.allclose(b.numpy(), a.numpy()[1])
  280. assert np.allclose(c.numpy(), a.numpy()[1:].reshape(3, 3))
  281. def test_param_pack_concat():
  282. a = tensor(np.ones((1,), np.int32))
  283. b = tensor(np.ones((3, 3), np.int32))
  284. offsets_val = [0, 1, 1, 10]
  285. offsets = tensor(offsets_val, np.int32)
  286. c = F.param_pack_concat([a, b], offsets, offsets_val)
  287. assert np.allclose(np.concatenate([a.numpy(), b.numpy().flatten()]), c.numpy())

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