Skip to content

PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度

fix: enable imatrix calibration for MX and NV optimized RTN (#2494)

Wang, Chang94aa6d408dba
diff --git a/auto_round/autoround.py b/auto_round/autoround.py
index 47cde36d6..604a28ae3 100644
--- a/auto_round/autoround.py
+++ b/auto_round/autoround.py
@@ -193,7 +193,12 @@ def _select_rtn_compressor_base_cls(quant_config: RTNConfig, scheme, format, bas
# the plain min/max initialization ignores it
if getattr(quant_config, "enable_neuqi", False):
enable_imatrix = True
- elif (data_type == "int" and (bits is None or bits < 8)) or is_weight_scheme(scheme):
+ elif (
+ (data_type == "int" and (bits is None or bits < 8))
+ or (data_type in ("mx_fp", "mx_int") and bits is not None and bits <= 8)
+ or (data_type == "nv_fp" and bits == 4)
+ or is_weight_scheme(scheme)
+ ):
enable_imatrix = True
act_bits = resolved_attrs.get("act_bits")
diff --git a/test/unit/test_cpu/core/test_entry_contract.py b/test/unit/test_cpu/core/test_entry_contract.py
index 50e344fdb..3e8a4e114 100644
--- a/test/unit/test_cpu/core/test_entry_contract.py
+++ b/test/unit/test_cpu/core/test_entry_contract.py
@@ -12,10 +12,60 @@
# See the License for the specific language governing permissions and
# limitations under the License.
+import pytest
+
from auto_round import autoround
from auto_round.autoround import _split_entry_kwargs
[email protected]("scheme", ["MXFP4", "MXFP6", "MXFP8", "MXFP4_RCEIL", "MXFP8_RCEIL", "MXINT4", "NVFP4"])
[email protected]("disable_opt_rtn", [None, False, True])
+def test_mx_nv_opt_rtn_imatrix_routing(scheme, disable_opt_rtn):
+ from auto_round.algorithms.quantization.rtn.config import OptimizedRTNConfig, RTNConfig
+
+ config = RTNConfig(disable_opt_rtn=disable_opt_rtn)
+ autoround._select_rtn_compressor_base_cls(config, scheme, None, {})
+
+ enabled = disable_opt_rtn is not True
+ assert config.enable_imatrix is enabled
+ # NVFP4 still needs activation global-scale calibration when opt-RTN is disabled.
+ needs_calibration = enabled or scheme == "NVFP4"
+ assert isinstance(config, OptimizedRTNConfig) is needs_calibration
+ assert config.need_calib is needs_calibration
+
+
[email protected]("bits", [2, 4, 8])
+def test_custom_mx_int_opt_rtn_collects_imatrix(bits):
+ from auto_round.algorithms.quantization.rtn.config import OptimizedRTNConfig, RTNConfig
+ from auto_round.schemes import QuantizationScheme
+
+ scheme = QuantizationScheme(
+ bits=bits,
+ group_size=32,
+ data_type="mx_int",
+ act_bits=bits,
+ act_data_type="mx_int",
+ act_group_size=32,
+ act_sym=True,
+ act_dynamic=True,
+ )
+ config = RTNConfig()
+ autoround._select_rtn_compressor_base_cls(config, scheme, None, {})
+
+ assert config.enable_imatrix is True
+ assert isinstance(config, OptimizedRTNConfig)
+
+
[email protected]("scheme", ["FP8_BLOCK", "FPW8A16", "NVFP4_E5M3", "W8A16", "INT8_W8A8"])
+def test_other_rtn_datatypes_do_not_enable_imatrix_by_default(scheme):
+ from auto_round.algorithms.quantization.rtn.config import RTNConfig
+
+ config = RTNConfig()
+ autoround._select_rtn_compressor_base_cls(config, scheme, None, {})
+
+ assert config.enable_imatrix is False
+
+
def test_split_entry_kwargs_partitions_owned_fields():
processor = object()