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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.require_ngpu(2)
  55. @pytest.mark.isolated_distributed
  56. def test_sync_min_max_observer():
  57. word_size = get_device_count_by_fork("gpu")
  58. x = np.random.rand(3 * word_size, 3, 3, 3).astype("float32")
  59. np_min, np_max = x.min(), x.max()
  60. @dist.launcher
  61. def worker():
  62. rank = dist.get_rank()
  63. m = SyncMinMaxObserver()
  64. y = mge.tensor(x[rank * 3 : (rank + 1) * 3])
  65. m(y)
  66. assert m.min_val == np_min and m.max_val == np_max
  67. worker()
  68. @pytest.mark.require_ngpu(2)
  69. @pytest.mark.isolated_distributed
  70. def test_sync_exponential_moving_average_observer():
  71. word_size = get_device_count_by_fork("gpu")
  72. t = np.random.rand()
  73. x1 = np.random.rand(3 * word_size, 3, 3, 3).astype("float32")
  74. x2 = np.random.rand(3 * word_size, 3, 3, 3).astype("float32")
  75. expected_min = x1.min() * t + x2.min() * (1 - t)
  76. expected_max = x1.max() * t + x2.max() * (1 - t)
  77. @dist.launcher
  78. def worker():
  79. rank = dist.get_rank()
  80. m = SyncExponentialMovingAverageObserver(momentum=t)
  81. y1 = mge.tensor(x1[rank * 3 : (rank + 1) * 3])
  82. y2 = mge.tensor(x2[rank * 3 : (rank + 1) * 3])
  83. m(y1)
  84. m(y2)
  85. np.testing.assert_allclose(m.min_val.numpy(), expected_min, atol=1e-6)
  86. np.testing.assert_allclose(m.max_val.numpy(), expected_max, atol=1e-6)
  87. worker()

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