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_observer.py 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import multiprocessing as mp
  2. import platform
  3. import numpy as np
  4. import pytest
  5. import megengine as mge
  6. import megengine.distributed as dist
  7. from megengine.distributed.helper import get_device_count_by_fork
  8. from megengine.quantization.observer import (
  9. ExponentialMovingAverageObserver,
  10. MinMaxObserver,
  11. Observer,
  12. PassiveObserver,
  13. SyncExponentialMovingAverageObserver,
  14. SyncMinMaxObserver,
  15. )
  16. def test_observer():
  17. with pytest.raises(TypeError):
  18. Observer("qint8")
  19. def test_min_max_observer():
  20. x = np.random.rand(3, 3, 3, 3).astype("float32")
  21. np_min, np_max = x.min(), x.max()
  22. x = mge.tensor(x)
  23. m = MinMaxObserver()
  24. m(x)
  25. np.testing.assert_allclose(m.min_val.numpy(), np_min)
  26. np.testing.assert_allclose(m.max_val.numpy(), np_max)
  27. def test_exponential_moving_average_observer():
  28. t = np.random.rand()
  29. x1 = np.random.rand(3, 3, 3, 3).astype("float32")
  30. x2 = np.random.rand(3, 3, 3, 3).astype("float32")
  31. expected_min = x1.min() * t + x2.min() * (1 - t)
  32. expected_max = x1.max() * t + x2.max() * (1 - t)
  33. m = ExponentialMovingAverageObserver(momentum=t)
  34. m(mge.tensor(x1, dtype=np.float32))
  35. m(mge.tensor(x2, dtype=np.float32))
  36. np.testing.assert_allclose(m.min_val.numpy(), expected_min)
  37. np.testing.assert_allclose(m.max_val.numpy(), expected_max)
  38. def test_passive_observer():
  39. q_dict = {"scale": mge.tensor(1.0)}
  40. m = PassiveObserver(q_dict, "qint8")
  41. assert m.orig_scale == 1.0
  42. assert m.scale == 1.0
  43. m.scale = 2.0
  44. assert m.scale == 2.0
  45. assert m.get_qparams() == {"scale": mge.tensor(2.0)}
  46. @pytest.mark.skipif(
  47. platform.system() == "Darwin", reason="do not imp GPU mode at macos now"
  48. )
  49. @pytest.mark.skipif(
  50. platform.system() == "Windows", reason="windows disable MGB_ENABLE_OPR_MM"
  51. )
  52. @pytest.mark.skipif(get_device_count_by_fork("gpu") < 2, reason="need more gpu device")
  53. @pytest.mark.isolated_distributed
  54. def test_sync_min_max_observer():
  55. word_size = get_device_count_by_fork("gpu")
  56. x = np.random.rand(3 * word_size, 3, 3, 3).astype("float32")
  57. np_min, np_max = x.min(), x.max()
  58. @dist.launcher
  59. def worker():
  60. rank = dist.get_rank()
  61. m = SyncMinMaxObserver()
  62. y = mge.tensor(x[rank * 3 : (rank + 1) * 3])
  63. m(y)
  64. assert m.min_val == np_min and m.max_val == np_max
  65. worker()
  66. @pytest.mark.skipif(
  67. platform.system() == "Darwin", reason="do not imp GPU mode at macos now"
  68. )
  69. @pytest.mark.skipif(
  70. platform.system() == "Windows", reason="windows disable MGB_ENABLE_OPR_MM"
  71. )
  72. @pytest.mark.skipif(get_device_count_by_fork("gpu") < 2, reason="need more gpu device")
  73. @pytest.mark.isolated_distributed
  74. def test_sync_exponential_moving_average_observer():
  75. word_size = get_device_count_by_fork("gpu")
  76. t = np.random.rand()
  77. x1 = np.random.rand(3 * word_size, 3, 3, 3).astype("float32")
  78. x2 = np.random.rand(3 * word_size, 3, 3, 3).astype("float32")
  79. expected_min = x1.min() * t + x2.min() * (1 - t)
  80. expected_max = x1.max() * t + x2.max() * (1 - t)
  81. @dist.launcher
  82. def worker():
  83. rank = dist.get_rank()
  84. m = SyncExponentialMovingAverageObserver(momentum=t)
  85. y1 = mge.tensor(x1[rank * 3 : (rank + 1) * 3])
  86. y2 = mge.tensor(x2[rank * 3 : (rank + 1) * 3])
  87. m(y1)
  88. m(y2)
  89. np.testing.assert_allclose(m.min_val.numpy(), expected_min, atol=1e-6)
  90. np.testing.assert_allclose(m.max_val.numpy(), expected_max, atol=1e-6)
  91. worker()

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