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.

elemwise.py 947 B

12345678910111213141516171819202122232425262728
  1. from ...functional.elemwise import _elemwise_multi_type
  2. from ...tensor import Tensor
  3. from ..qat import elemwise as QAT
  4. from .module import QuantizedModule
  5. class Elemwise(QuantizedModule):
  6. r"""Quantized version of :class:`~.qat.Elemwise`."""
  7. def __init__(self, method, dtype=None, **kwargs):
  8. super().__init__(**kwargs)
  9. self.method = "q" + method
  10. self.output_dtype = dtype
  11. def forward(self, *inps):
  12. if self.training:
  13. raise ValueError("quantized module only support inference.")
  14. return _elemwise_multi_type(*inps, mode=self.method, dtype=self.output_dtype)
  15. @classmethod
  16. def from_qat_module(cls, qat_module: QAT.Elemwise):
  17. r"""
  18. Return a :class:`~.QuantizedModule` instance converted from a
  19. :class:`~.QATModule` instance.
  20. """
  21. return cls(
  22. qat_module.method, qat_module.get_activation_dtype(), name=qat_module.name
  23. )