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

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

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