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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 numpy as np
  10. import pytest
  11. import megengine.functional as F
  12. from megengine import Buffer, Parameter, is_cuda_available, tensor
  13. from megengine.core._trace_option import use_tensor_shape
  14. from megengine.core.tensor.utils import astensor1d
  15. from megengine.test import assertTensorClose
  16. def _default_compare_fn(x, y):
  17. assertTensorClose(x.numpy(), y)
  18. def opr_test(cases, func, compare_fn=_default_compare_fn, ref_fn=None, **kwargs):
  19. """
  20. func: the function to run opr.
  21. compare_fn: the function to compare the result and expected, use assertTensorClose if None.
  22. ref_fn: the function to generate expected data, should assign output if None.
  23. cases: the list which have dict element, the list length should be 2 for dynamic shape test.
  24. and the dict should have input,
  25. and should have output if ref_fn is None.
  26. should use list for multiple inputs and outputs for each case.
  27. kwargs: The additional kwargs for opr func.
  28. simple examples:
  29. dtype = np.float32
  30. cases = [{"input": [10, 20]}, {"input": [20, 30]}]
  31. opr_test(cases,
  32. F.eye,
  33. ref_fn=lambda n, m: np.eye(n, m).astype(dtype),
  34. dtype=dtype)
  35. """
  36. def check_results(results, expected):
  37. if not isinstance(results, tuple):
  38. results = (results,)
  39. for r, e in zip(results, expected):
  40. compare_fn(r, e)
  41. def get_param(cases, idx):
  42. case = cases[idx]
  43. inp = case.get("input", None)
  44. outp = case.get("output", None)
  45. if inp is None:
  46. raise ValueError("the test case should have input")
  47. if not isinstance(inp, list):
  48. inp = (inp,)
  49. else:
  50. inp = tuple(inp)
  51. if ref_fn is not None and callable(ref_fn):
  52. outp = ref_fn(*inp)
  53. if outp is None:
  54. raise ValueError("the test case should have output or reference function")
  55. if not isinstance(outp, list):
  56. outp = (outp,)
  57. else:
  58. outp = tuple(outp)
  59. return inp, outp
  60. if len(cases) == 0:
  61. raise ValueError("should give one case at least")
  62. if not callable(func):
  63. raise ValueError("the input func should be callable")
  64. inp, outp = get_param(cases, 0)
  65. inp_tensor = [tensor(inpi) for inpi in inp]
  66. results = func(*inp_tensor, **kwargs)
  67. check_results(results, outp)
  68. def test_eye():
  69. dtype = np.float32
  70. cases = [{"input": [10, 20]}, {"input": [20, 30]}]
  71. for case in cases:
  72. assertTensorClose(
  73. F.eye(case["input"], dtype=dtype).numpy(),
  74. np.eye(*case["input"]).astype(dtype),
  75. )
  76. def test_concat():
  77. def get_data_shape(length: int):
  78. return (length, 2, 3)
  79. data1 = np.random.random(get_data_shape(5)).astype("float32")
  80. data2 = np.random.random(get_data_shape(6)).astype("float32")
  81. data3 = np.random.random(get_data_shape(7)).astype("float32")
  82. def run(data1, data2):
  83. return F.concat([data1, data2])
  84. cases = [{"input": [data1, data2]}, {"input": [data1, data3]}]
  85. opr_test(cases, run, ref_fn=lambda x, y: np.concatenate([x, y]))
  86. def test_stack():
  87. data1 = np.random.random((3, 2, 2)).astype("float32")
  88. data2 = np.random.random((3, 2, 2)).astype("float32")
  89. data3 = np.random.random((3, 2, 2)).astype("float32")
  90. cases = [{"input": [data1, data2]}, {"input": [data1, data3]}]
  91. for ai in range(3):
  92. def run(data1, data2):
  93. return F.stack([data1, data2], axis=ai)
  94. opr_test(cases, run, ref_fn=lambda x, y: np.stack([x, y], axis=ai))
  95. def test_split():
  96. if use_tensor_shape(): # XXX: please fix me
  97. return
  98. data = np.random.random((2, 3, 4, 5)).astype(np.float32)
  99. mge_out1 = F.split(tensor(data), 2, axis=3)
  100. mge_out2 = F.split(tensor(data), [3, 5], axis=3)
  101. np_out = np.split(data, [3, 5], axis=3)
  102. np.testing.assert_equal(mge_out1[0].numpy(), mge_out2[0].numpy())
  103. np.testing.assert_equal(mge_out1[0].numpy(), np_out[0])
  104. def test_reshape():
  105. x = np.arange(6, dtype="float32")
  106. xx = tensor(x)
  107. y = x.reshape(1, 2, 3)
  108. for shape in [
  109. (1, 2, 3),
  110. (1, -1, 3),
  111. (1, tensor(-1), 3),
  112. np.array([1, -1, 3], dtype="int32"),
  113. tensor([1, -1, 3]),
  114. ]:
  115. yy = F.reshape(xx, shape)
  116. np.testing.assert_equal(yy.numpy(), y)
  117. def test_squeeze():
  118. x = np.arange(6, dtype="float32").reshape(1, 2, 3, 1)
  119. xx = tensor(x)
  120. for axis in [None, 3, -4, (3, -4)]:
  121. y = np.squeeze(x, axis)
  122. yy = F.squeeze(xx, axis)
  123. np.testing.assert_equal(y, yy.numpy())
  124. def test_expand_dims():
  125. x = np.arange(6, dtype="float32").reshape(2, 3)
  126. xx = tensor(x)
  127. for axis in [2, -3, (3, -4), (1, -4)]:
  128. y = np.expand_dims(x, axis)
  129. yy = F.expand_dims(xx, axis)
  130. np.testing.assert_equal(y, yy.numpy())
  131. def test_elemwise_dtype_promotion():
  132. x = np.random.rand(2, 3).astype("float32")
  133. y = np.random.rand(1, 3).astype("float16")
  134. xx = tensor(x)
  135. yy = tensor(y)
  136. z = xx * yy
  137. np.testing.assert_equal(z.numpy(), x * y)
  138. z = xx + y
  139. np.testing.assert_equal(z.numpy(), x + y)
  140. z = x - yy
  141. np.testing.assert_equal(z.numpy(), x - y)
  142. def test_linspace():
  143. cases = [
  144. {"input": [1, 9, 9]},
  145. {"input": [3, 10, 8]},
  146. ]
  147. opr_test(
  148. cases,
  149. F.linspace,
  150. ref_fn=lambda start, end, step: np.linspace(start, end, step, dtype=np.float32),
  151. )
  152. cases = [
  153. {"input": [9, 1, 9]},
  154. {"input": [10, 3, 8]},
  155. ]
  156. opr_test(
  157. cases,
  158. F.linspace,
  159. ref_fn=lambda start, end, step: np.linspace(start, end, step, dtype=np.float32),
  160. )
  161. def test_arange():
  162. cases = [
  163. {"input": [1, 9, 1]},
  164. {"input": [2, 10, 2]},
  165. ]
  166. opr_test(
  167. cases,
  168. F.arange,
  169. ref_fn=lambda start, end, step: np.arange(start, end, step, dtype=np.float32),
  170. )
  171. cases = [
  172. {"input": [9, 1, -1]},
  173. {"input": [10, 2, -2]},
  174. ]
  175. opr_test(
  176. cases,
  177. F.arange,
  178. ref_fn=lambda start, end, step: np.arange(start, end, step, dtype=np.float32),
  179. )
  180. cases = [
  181. {"input": [9.3, 1.2, -0.5]},
  182. {"input": [10.3, 2.1, -1.7]},
  183. ]
  184. opr_test(
  185. cases,
  186. F.arange,
  187. ref_fn=lambda start, end, step: np.arange(start, end, step, dtype=np.float32),
  188. )
  189. def test_round():
  190. data1_shape = (15,)
  191. data2_shape = (25,)
  192. data1 = np.random.random(data1_shape).astype(np.float32)
  193. data2 = np.random.random(data2_shape).astype(np.float32)
  194. cases = [{"input": data1}, {"input": data2}]
  195. opr_test(cases, F.round, ref_fn=np.round)
  196. def test_broadcast():
  197. input1_shape = (20, 30)
  198. output1_shape = (30, 20, 30)
  199. data1 = np.random.random(input1_shape).astype(np.float32)
  200. input2_shape = (10, 20)
  201. output2_shape = (20, 10, 20)
  202. data2 = np.random.random(input2_shape).astype(np.float32)
  203. def compare_fn(x, y):
  204. assert x.numpy().shape == y
  205. cases = [
  206. {"input": [data1, output1_shape], "output": output1_shape},
  207. {"input": [data2, output2_shape], "output": output2_shape},
  208. ]
  209. opr_test(cases, F.broadcast, compare_fn=compare_fn)
  210. def test_utils_astensor1d():
  211. reference = tensor(0)
  212. # literal
  213. x = [1, 2, 3]
  214. for dtype in [None, "float32"]:
  215. xx = astensor1d(x, reference, dtype=dtype)
  216. assert type(xx) is tensor
  217. np.testing.assert_equal(xx.numpy(), x)
  218. # numpy array
  219. x = np.asarray([1, 2, 3], dtype="int32")
  220. for dtype in [None, "float32"]:
  221. xx = astensor1d(x, reference, dtype=dtype)
  222. assert type(xx) is tensor
  223. np.testing.assert_equal(xx.numpy(), x.astype(dtype) if dtype else x)
  224. # tensor
  225. x = tensor([1, 2, 3], dtype="int32")
  226. for dtype in [None, "float32"]:
  227. xx = astensor1d(x, reference, dtype=dtype)
  228. assert type(xx) is tensor
  229. np.testing.assert_equal(xx.numpy(), x.numpy())
  230. # mixed
  231. x = [1, tensor(2), 3]
  232. for dtype in [None, "float32"]:
  233. xx = astensor1d(x, reference, dtype=dtype)
  234. assert type(xx) is tensor
  235. np.testing.assert_equal(xx.numpy(), [1, 2, 3])
  236. def test_device():
  237. x = tensor([1, 2, 3], dtype="float32")
  238. y1 = F.eye(x.shape, dtype="float32")
  239. y2 = F.eye(x.shape, dtype="float32", device=None)
  240. np.testing.assert_almost_equal(y1.numpy(), y2.numpy())
  241. y3 = F.eye(x.shape, dtype="float32", device="xpux")
  242. y4 = F.eye(x.shape, dtype="float32", device=x.device.to_c())
  243. np.testing.assert_almost_equal(y3.numpy(), y4.numpy())
  244. y5 = F.full((3, 2), 4, device=x.device)
  245. y6 = F.full((3, 2), 4, device="xpux")
  246. np.testing.assert_almost_equal(y5.numpy(), y6.numpy())

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