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.

quantize.py 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  2. #
  3. # Copyright (c) 2014-2020 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 numpy as np
  9. import pytest
  10. from megengine import module as Float
  11. from megengine import tensor
  12. from megengine.module import qat as QAT
  13. from megengine.quantization import min_max_fakequant_qconfig
  14. from megengine.quantization.quantize import (
  15. _get_quantable_module_names,
  16. disable_fake_quant,
  17. quantize_qat,
  18. )
  19. def test_get_quantable_module_names():
  20. # need to make sure names from Quantized and QAT are the same
  21. def _get_qat_module_names():
  22. def is_qat(key: str):
  23. value = getattr(QAT, key)
  24. return (
  25. isinstance(value, type)
  26. and issubclass(value, QAT.QATModule)
  27. and value != QAT.QATModule
  28. )
  29. # source should have all quantable modules' names
  30. quantable_module_names = [key for key in dir(QAT) if is_qat(key)]
  31. return quantable_module_names
  32. qat_module_names = _get_qat_module_names()
  33. quantized_module_names = _get_quantable_module_names()
  34. assert set(qat_module_names) == set(quantized_module_names)
  35. for key in qat_module_names:
  36. value = getattr(Float, key)
  37. assert (
  38. isinstance(value, type)
  39. and issubclass(value, Float.Module)
  40. and value != Float.Module
  41. )
  42. def test_disable_quantize():
  43. class Net(Float.Module):
  44. def __init__(self):
  45. super().__init__()
  46. self.conv = Float.ConvBnRelu2d(3, 3, 3)
  47. self.conv.disable_quantize()
  48. def forward(self, x):
  49. return self.conv(x)
  50. net = Net()
  51. qat_net = quantize_qat(net, inplace=False)
  52. assert isinstance(qat_net.conv, Float.ConvBnRelu2d)
  53. assert isinstance(qat_net.conv.conv, Float.Conv2d)
  54. def test_convert_with_custom_mapping():
  55. class FloatExample(Float.Module):
  56. def forward(self, x):
  57. return x
  58. class QATExample(QAT.QATModule):
  59. def forward(self, x):
  60. return x
  61. @classmethod
  62. def from_float_module(cls, float_module):
  63. return cls()
  64. class Net(Float.Module):
  65. def __init__(self):
  66. super().__init__()
  67. self.example = FloatExample()
  68. def forward(self, x):
  69. return self.example(x)
  70. net = Net()
  71. qat_net = quantize_qat(net, inplace=False, mapping={FloatExample: QATExample})
  72. assert isinstance(qat_net.example, QATExample)
  73. def test_disable_fake_quant():
  74. class Net(Float.Module):
  75. def __init__(self):
  76. super().__init__()
  77. self.quant = Float.QuantStub()
  78. self.linear = Float.Linear(3, 3)
  79. self.dequant = Float.DequantStub()
  80. self.linear.bias.set_value(np.random.rand(3))
  81. def forward(self, x):
  82. x = self.quant(x)
  83. x = self.linear(x)
  84. x = self.dequant(x)
  85. return x
  86. x = tensor(np.random.randint(1, 10, size=(3, 3)).astype(np.float32))
  87. net = Net()
  88. y1 = net(x).numpy()
  89. net = quantize_qat(net, min_max_fakequant_qconfig)
  90. y2 = net(x).numpy()
  91. disable_fake_quant(net)
  92. y3 = net(x).numpy()
  93. np.testing.assert_allclose(y1, y3)
  94. with pytest.raises(AssertionError):
  95. np.testing.assert_allclose(y2, y3)

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