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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 copy
  10. import numpy as np
  11. import pytest
  12. import megengine as mge
  13. import megengine.functional as F
  14. from megengine.core import Buffer, Graph, Parameter
  15. from megengine.module import Conv2d
  16. from megengine.test import assertTensorClose
  17. def test_set_value():
  18. v0 = np.random.random((2, 3)).astype(np.float32)
  19. param = Parameter(v0)
  20. v1 = np.random.random((2, 3)).astype(np.float32)
  21. param.set_value(v1)
  22. assertTensorClose(param.numpy(), v1, max_err=5e-6)
  23. v2 = np.random.random((3, 3)).astype(np.float32)
  24. # TODO: add this
  25. # with pytest.raises(ValueError):
  26. # param.set_value(v2)
  27. assertTensorClose(param.numpy(), v1, max_err=5e-6)
  28. def test_fill():
  29. a = Buffer(np.zeros((2, 3), dtype=np.float32))
  30. a.fill(3)
  31. assertTensorClose(a.numpy(), np.full((2, 3), 3, dtype=np.float32))
  32. a.fill(124.568)
  33. assertTensorClose(a.numpy(), np.full((2, 3), 124.568, dtype=np.float32))
  34. # TODO: remove or rewrite following test
  35. # def test_attach():
  36. # p_ = np.random.random((2, 3)).astype(np.float32)
  37. # with Graph() as g:
  38. # g.set_option('eager_evaluation', False)
  39. # p = Parameter(p_)
  40. # v = p * 2
  41. # f = compile(v, None)
  42. # out, = f()
  43. # assertTensorClose(out, p_ * 2)
  44. # F.add_update(p, p)
  45. # out, = f()
  46. # assertTensorClose(out, p_ * 4)
  47. # TODO: remove or rewrite following test
  48. # def test_module_attach():
  49. # v = np.random.random((1, 3, 64, 64)).astype(np.float32)
  50. # net = Conv2d(3, 16, 3)
  51. # with Graph() as g:
  52. # g.set_option('eager_evaluation', False)
  53. # data0 = Input("data")
  54. # f = compile(net(data0), None)
  55. # out0, = f(data=v)
  56. # data1 = Input("data", value=v)
  57. # out1 = net(data1)
  58. # assertTensorClose(out0, out1.numpy())
  59. def test_shape_warning():
  60. with Graph() as cg:
  61. cg.set_option("eager_evaluation", False)
  62. b = Buffer(np.ones((2, 3)).astype(np.float32))
  63. with pytest.warns(None) as record:
  64. print(b.shape)
  65. if len(record) != 0:
  66. raise ValueError(
  67. "Getting the shape of a constant Tensor should throw no Warning"
  68. )

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