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.

constant.py 3.6 kB

5 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """Constant module for compression"""
  16. import enum
  17. import re
  18. from types import DynamicClassAttribute
  19. __all__ = ["QuantDtype"]
  20. @enum.unique
  21. class QuantDtype(enum.Enum):
  22. """
  23. An enum for quant datatype, contains `INT2` ~ `INT8`, `UINT2` ~ `UINT8`.
  24. """
  25. INT2 = "INT2"
  26. INT3 = "INT3"
  27. INT4 = "INT4"
  28. INT5 = "INT5"
  29. INT6 = "INT6"
  30. INT7 = "INT7"
  31. INT8 = "INT8"
  32. UINT2 = "UINT2"
  33. UINT3 = "UINT3"
  34. UINT4 = "UINT4"
  35. UINT5 = "UINT5"
  36. UINT6 = "UINT6"
  37. UINT7 = "UINT7"
  38. UINT8 = "UINT8"
  39. def __str__(self):
  40. return f"{self.name}"
  41. @staticmethod
  42. def is_signed(dtype):
  43. """
  44. Get whether the quant datatype is signed.
  45. Args:
  46. dtype (QuantDtype): quant datatype.
  47. Returns:
  48. bool, whether the input quant datatype is signed.
  49. Examples:
  50. >>> quant_dtype = QuantDtype.INT8
  51. >>> is_signed = QuantDtype.is_signed(quant_dtype)
  52. """
  53. return dtype in [QuantDtype.INT2, QuantDtype.INT3, QuantDtype.INT4, QuantDtype.INT5,
  54. QuantDtype.INT6, QuantDtype.INT7, QuantDtype.INT8]
  55. @staticmethod
  56. def switch_signed(dtype):
  57. """
  58. Switch the signed state of the input quant datatype.
  59. Args:
  60. dtype (QuantDtype): quant datatype.
  61. Returns:
  62. QuantDtype, quant datatype with opposite signed state as the input.
  63. Examples:
  64. >>> quant_dtype = QuantDtype.INT8
  65. >>> quant_dtype = QuantDtype.switch_signed(quant_dtype)
  66. """
  67. type_map = {
  68. QuantDtype.INT2: QuantDtype.UINT2,
  69. QuantDtype.INT3: QuantDtype.UINT3,
  70. QuantDtype.INT4: QuantDtype.UINT4,
  71. QuantDtype.INT5: QuantDtype.UINT5,
  72. QuantDtype.INT6: QuantDtype.UINT6,
  73. QuantDtype.INT7: QuantDtype.UINT7,
  74. QuantDtype.INT8: QuantDtype.UINT8,
  75. QuantDtype.UINT2: QuantDtype.INT2,
  76. QuantDtype.UINT3: QuantDtype.INT3,
  77. QuantDtype.UINT4: QuantDtype.INT4,
  78. QuantDtype.UINT5: QuantDtype.INT5,
  79. QuantDtype.UINT6: QuantDtype.INT6,
  80. QuantDtype.UINT7: QuantDtype.INT7,
  81. QuantDtype.UINT8: QuantDtype.INT8
  82. }
  83. return type_map[dtype]
  84. @DynamicClassAttribute
  85. def _value(self):
  86. """The value of the Enum member."""
  87. return int(re.search(r"(\d+)", self._value_).group(1))
  88. @DynamicClassAttribute
  89. def num_bits(self):
  90. """
  91. Get the num bits of the QuantDtype member.
  92. Returns:
  93. int, the num bits of the QuantDtype member.
  94. Examples:
  95. >>> from mindspore.compression.common import QuantDtype
  96. >>> quant_dtype = QuantDtype.INT8
  97. >>> num_bits = quant_dtype.num_bits
  98. >>> print(num_bits)
  99. 8
  100. """
  101. return self._value