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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  2. #
  3. # Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  4. #
  5. # Unless required by applicable law or agreed to in writing,
  6. # software distributed under the License is distributed on an
  7. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  8. import platform
  9. import numpy as np
  10. import pytest
  11. import megengine as mge
  12. import megengine.distributed as dist
  13. from megengine.device import get_device_count
  14. from megengine.quantization import QuantMode, create_qparams
  15. from megengine.quantization.observer import (
  16. ExponentialMovingAverageObserver,
  17. HistogramObserver,
  18. MinMaxObserver,
  19. Observer,
  20. PassiveObserver,
  21. SyncExponentialMovingAverageObserver,
  22. SyncMinMaxObserver,
  23. )
  24. def test_observer():
  25. with pytest.raises(TypeError):
  26. Observer("qint8")
  27. def test_min_max_observer():
  28. x = np.random.rand(3, 3, 3, 3).astype("float32")
  29. np_min, np_max = x.min(), x.max()
  30. x = mge.tensor(x)
  31. m = MinMaxObserver()
  32. m(x)
  33. np.testing.assert_allclose(m.min_val.numpy(), np_min)
  34. np.testing.assert_allclose(m.max_val.numpy(), np_max)
  35. def test_exponential_moving_average_observer():
  36. t = np.random.rand()
  37. x1 = np.random.rand(3, 3, 3, 3).astype("float32")
  38. x2 = np.random.rand(3, 3, 3, 3).astype("float32")
  39. expected_min = x1.min() * t + x2.min() * (1 - t)
  40. expected_max = x1.max() * t + x2.max() * (1 - t)
  41. m = ExponentialMovingAverageObserver(momentum=t)
  42. m(mge.tensor(x1, dtype=np.float32))
  43. m(mge.tensor(x2, dtype=np.float32))
  44. np.testing.assert_allclose(m.min_val.numpy(), expected_min, atol=1e-5)
  45. np.testing.assert_allclose(m.max_val.numpy(), expected_max, atol=1e-5)
  46. def test_histogram_observer():
  47. x = np.random.rand(3, 3, 3, 3).astype("float32")
  48. np_min, np_max = x.min(), x.max()
  49. x = mge.tensor(x)
  50. m = HistogramObserver()
  51. m(x)
  52. np.testing.assert_allclose(m.min_val.numpy(), np_min)
  53. np.testing.assert_allclose(m.max_val.numpy(), np_max)
  54. def test_passive_observer():
  55. qparams = create_qparams(QuantMode.SYMMERTIC, "qint8", mge.tensor(1.0))
  56. m = PassiveObserver("qint8")
  57. m.set_qparams(qparams)
  58. assert m.orig_scale == 1.0
  59. assert m.scale.numpy() == 1.0
  60. assert m.get_qparams().dtype_meta == qparams.dtype_meta
  61. assert m.get_qparams().scale == qparams.scale
  62. assert m.get_qparams() == qparams
  63. @pytest.mark.require_ngpu(2)
  64. @pytest.mark.isolated_distributed
  65. def test_sync_min_max_observer():
  66. word_size = get_device_count("gpu")
  67. x = np.random.rand(3 * word_size, 3, 3, 3).astype("float32")
  68. np_min, np_max = x.min(), x.max()
  69. @dist.launcher
  70. def worker():
  71. rank = dist.get_rank()
  72. m = SyncMinMaxObserver()
  73. y = mge.tensor(x[rank * 3 : (rank + 1) * 3])
  74. m(y)
  75. assert m.min_val == np_min and m.max_val == np_max
  76. worker()
  77. @pytest.mark.require_ngpu(2)
  78. @pytest.mark.isolated_distributed
  79. def test_sync_exponential_moving_average_observer():
  80. word_size = get_device_count("gpu")
  81. t = np.random.rand()
  82. x1 = np.random.rand(3 * word_size, 3, 3, 3).astype("float32")
  83. x2 = np.random.rand(3 * word_size, 3, 3, 3).astype("float32")
  84. expected_min = x1.min() * t + x2.min() * (1 - t)
  85. expected_max = x1.max() * t + x2.max() * (1 - t)
  86. @dist.launcher
  87. def worker():
  88. rank = dist.get_rank()
  89. m = SyncExponentialMovingAverageObserver(momentum=t)
  90. y1 = mge.tensor(x1[rank * 3 : (rank + 1) * 3])
  91. y2 = mge.tensor(x2[rank * 3 : (rank + 1) * 3])
  92. m(y1)
  93. m(y2)
  94. np.testing.assert_allclose(m.min_val.numpy(), expected_min, atol=1e-6)
  95. np.testing.assert_allclose(m.max_val.numpy(), expected_max, atol=1e-6)
  96. worker()