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.

quant_dequant.py 1.2 kB

123456789101112131415161718192021222324252627282930313233343536373839
  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. from ..qat import quant_dequant as QAT
  9. from .module import QuantizedModule
  10. class QuantStub(QuantizedModule):
  11. r"""Quantized version of :class:`~.qat.QuantStub`,
  12. will convert input to quantized dtype.
  13. """
  14. def __init__(self, dtype=None, **kwargs):
  15. super().__init__(**kwargs)
  16. self.output_dtype = dtype
  17. def forward(self, inp):
  18. return inp.astype(self.output_dtype)
  19. @classmethod
  20. def from_qat_module(cls, qat_module: QAT.QuantStub):
  21. return cls(qat_module.get_activation_dtype(), name=qat_module.name)
  22. class DequantStub(QuantizedModule):
  23. r"""Quantized version of :class:`~.qat.DequantStub`,
  24. will restore quantized input to float32 dtype.
  25. """
  26. def forward(self, inp):
  27. return inp.astype("float32")
  28. @classmethod
  29. def from_qat_module(cls, qat_module: QAT.DequantStub):
  30. return cls(name=qat_module.name)