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_math.py 2.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. from helpers import opr_test
  11. import megengine.functional as F
  12. def common_test_reduce(opr, ref_opr):
  13. data1_shape = (5, 6, 7)
  14. data2_shape = (2, 9, 12)
  15. data1 = np.random.random(data1_shape).astype(np.float32)
  16. data2 = np.random.random(data2_shape).astype(np.float32)
  17. cases = [{"input": data1}, {"input": data2}]
  18. if opr not in (F.argmin, F.argmax):
  19. opr_test(cases, opr, ref_fn=ref_opr)
  20. axis = 2
  21. opr_test(cases, opr, ref_fn=lambda x: ref_opr(x, axis=axis), axis=axis)
  22. axis = 2
  23. keepdims = True
  24. opr_test(
  25. cases,
  26. opr,
  27. ref_fn=lambda x: ref_opr(x, axis=axis, keepdims=keepdims),
  28. axis=axis,
  29. keepdims=keepdims,
  30. )
  31. else:
  32. opr_test(cases, opr, ref_fn=lambda x: ref_opr(x).astype(np.int32))
  33. axis = 2
  34. opr_test(
  35. cases,
  36. opr,
  37. ref_fn=lambda x: ref_opr(x, axis=axis).astype(np.int32),
  38. axis=axis,
  39. )
  40. def test_sum():
  41. common_test_reduce(opr=F.sum, ref_opr=np.sum)
  42. def test_prod():
  43. common_test_reduce(opr=F.prod, ref_opr=np.prod)
  44. def test_mean():
  45. common_test_reduce(opr=F.mean, ref_opr=np.mean)
  46. def test_min():
  47. common_test_reduce(opr=F.min, ref_opr=np.min)
  48. def test_max():
  49. common_test_reduce(opr=F.max, ref_opr=np.max)
  50. def test_argmin():
  51. common_test_reduce(opr=F.argmin, ref_opr=np.argmin)
  52. def test_argmax():
  53. common_test_reduce(opr=F.argmax, ref_opr=np.argmax)
  54. def test_sqrt():
  55. d1_shape = (15,)
  56. d2_shape = (25,)
  57. d1 = np.random.random(d1_shape).astype(np.float32)
  58. d2 = np.random.random(d2_shape).astype(np.float32)
  59. cases = [{"input": d1}, {"input": d2}]
  60. opr_test(cases, F.sqrt, ref_fn=np.sqrt)

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