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.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import platform
  2. import numpy as np
  3. import pytest
  4. import megengine as mge
  5. import megengine.distributed as dist
  6. from megengine.distributed.helper import get_device_count_by_fork
  7. from megengine.quantization.observer import (
  8. ExponentialMovingAverageObserver,
  9. HistogramObserver,
  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_histogram_observer():
  39. x = np.random.rand(3, 3, 3, 3).astype("float32")
  40. np_min, np_max = x.min(), x.max()
  41. x = mge.tensor(x)
  42. m = HistogramObserver()
  43. m(x)
  44. np.testing.assert_allclose(m.min_val.numpy(), np_min)
  45. np.testing.assert_allclose(m.max_val.numpy(), np_max)
  46. def test_passive_observer():
  47. q_dict = {"scale": mge.tensor(1.0)}
  48. m = PassiveObserver(q_dict, "qint8")
  49. assert m.orig_scale == 1.0
  50. assert m.scale == 1.0
  51. m.scale = 2.0
  52. assert m.scale == 2.0
  53. assert m.get_qparams() == {"scale": mge.tensor(2.0)}
  54. @pytest.mark.skipif(
  55. platform.system() == "Darwin", reason="do not imp GPU mode at macos now"
  56. )
  57. @pytest.mark.skipif(
  58. platform.system() == "Windows", reason="windows disable MGB_ENABLE_OPR_MM"
  59. )
  60. @pytest.mark.skipif(get_device_count_by_fork("gpu") < 2, reason="need more gpu device")
  61. @pytest.mark.isolated_distributed
  62. def test_sync_min_max_observer():
  63. word_size = get_device_count_by_fork("gpu")
  64. x = np.random.rand(3 * word_size, 3, 3, 3).astype("float32")
  65. np_min, np_max = x.min(), x.max()
  66. @dist.launcher
  67. def worker():
  68. rank = dist.get_rank()
  69. m = SyncMinMaxObserver()
  70. y = mge.tensor(x[rank * 3 : (rank + 1) * 3])
  71. m(y)
  72. assert m.min_val == np_min and m.max_val == np_max
  73. worker()
  74. @pytest.mark.skipif(
  75. platform.system() == "Darwin", reason="do not imp GPU mode at macos now"
  76. )
  77. @pytest.mark.skipif(
  78. platform.system() == "Windows", reason="windows disable MGB_ENABLE_OPR_MM"
  79. )
  80. @pytest.mark.skipif(get_device_count_by_fork("gpu") < 2, reason="need more gpu device")
  81. @pytest.mark.isolated_distributed
  82. def test_sync_exponential_moving_average_observer():
  83. word_size = get_device_count_by_fork("gpu")
  84. t = np.random.rand()
  85. x1 = np.random.rand(3 * word_size, 3, 3, 3).astype("float32")
  86. x2 = np.random.rand(3 * word_size, 3, 3, 3).astype("float32")
  87. expected_min = x1.min() * t + x2.min() * (1 - t)
  88. expected_max = x1.max() * t + x2.max() * (1 - t)
  89. @dist.launcher
  90. def worker():
  91. rank = dist.get_rank()
  92. m = SyncExponentialMovingAverageObserver(momentum=t)
  93. y1 = mge.tensor(x1[rank * 3 : (rank + 1) * 3])
  94. y2 = mge.tensor(x2[rank * 3 : (rank + 1) * 3])
  95. m(y1)
  96. m(y2)
  97. np.testing.assert_allclose(m.min_val.numpy(), expected_min, atol=1e-6)
  98. np.testing.assert_allclose(m.max_val.numpy(), expected_max, atol=1e-6)
  99. worker()

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