PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度
import json
import shutil
import sys
from test.helpers import evaluate_accuracy, get_model_path
import pytest
import torch
from transformers import AutoModelForCausalLM, AutoRoundConfig, AutoTokenizer
from auto_round import AutoRound
AUTO_ROUND_PATH = __file__.split("/")
AUTO_ROUND_PATH = "/".join(AUTO_ROUND_PATH[: AUTO_ROUND_PATH.index("test")])
def _local_calibration_dataset(tmp_path):
dataset_path = tmp_path / "alg_ext_autoscheme_calibration.json"
dataset_path.write_text(
json.dumps(["Algorithm-extension local calibration text with enough tokens. " * 16]),
encoding="utf-8",
)
return str(dataset_path)
class TestAlgExt:
@pytest.fixture(autouse=True)
def _save_dir(self, tmp_path):
self.save_folder = str(tmp_path / "saved")
yield
shutil.rmtree(self.save_folder, ignore_errors=True)
@pytest.fixture(autouse=True, scope="class")
def setup_and_teardown_class(self):
yield
shutil.rmtree("runs", ignore_errors=True)
# def test_gguf_q2_k_s_uses_dq_wrapper_block(self, tiny_qwen_model_path):
# """Regression test: enable_alg_ext + gguf:q2_k_s must use DQWrapperLinear.
#
# gguf:q2_k_s overrides data_type to "int_asym_dq" at format-resolution
# time. The quantizer must be created *after* that override so that
# SignRoundV2 sees the final data_type and sets dq_wrapper_block
# (which wraps layers with DQWrapperLinear) instead of falling back to
# the plain wrapper_block (which produces WrapperLinear).
# """
# from auto_round.algorithms.quantization.sign_roundv2.quantizer import SignRoundDQWrapperLinear
#
# ar = AutoRound(
# tiny_qwen_model_path,
# bits=4,
# format="gguf:q2_k_s",
# iters=1,
# nsamples=1,
# seqlen=32,
# enable_alg_ext=True,
# )
# # post_init() runs the full pipeline (resolve_scheme → resolve_formats →
# # create_quantizer → ...). quantizer only exists afterwards.
# ar.post_init()
# quantizer = ar.alg_composer.block_quantizer
#
# assert quantizer.wrapper_block.keywords["wrapper_cls"] is SignRoundDQWrapperLinear, (
# f"Expected wrapper_block to use '{SignRoundDQWrapperLinear.__name__}', "
# f"got '{quantizer.wrapper_block.__name__}'. "
# "This likely means the quantizer was created before GGUF format "
# "overrides were applied (data_type was not yet 'int_asym_dq')."
# )
def test_int2_g64_asym_enable_alg_ext_keeps_config(self, tiny_qwen_model_path):
"""Regression test: asym int2/g64 keeps the requested tuning config."""
ar = AutoRound(
tiny_qwen_model_path,
bits=2,
group_size=64,
sym=False,
iters=1,
nsamples=1,
seqlen=32,
enable_alg_ext=True,
enable_minmax_tuning=False,
enable_norm_bias_tuning=True,
enable_quanted_input=False,
)
ar.post_init()
quantizer = ar.alg_composer.block_quantizer
assert quantizer.scheme.bits == 2
assert quantizer.scheme.group_size == 64
assert quantizer.scheme.sym is False
assert quantizer.enable_alg_ext is True
assert quantizer.enable_minmax_tuning is False
assert quantizer.enable_norm_bias_tuning is True
assert quantizer.enable_quanted_input is False
ar.quantize()
assert quantizer.enable_minmax_tuning is False
assert quantizer.enable_norm_bias_tuning is True
assert quantizer.enable_quanted_input is False
@pytest.mark.parametrize("scheme", ["MXFP4", "W2A16G64"])
def test_all_support_dtype(self, scheme, tiny_qwen_model_path, tmp_path, monkeypatch):
from auto_round.auto_scheme import AutoScheme
monkeypatch.setenv("AR_AUTO_SCHEME_NSAMPLES", "1")
monkeypatch.setenv("AR_AUTO_SCHEME_BATCH_SIZE", "1")
calibration_dataset = _local_calibration_dataset(tmp_path)
avg_bits = 2 if scheme == "W2A16G64" else 4
scheme = AutoScheme(options=scheme, avg_bits=avg_bits, ignore_scale_zp_bits=True)
ar = AutoRound(
tiny_qwen_model_path,
scheme=scheme,
iters=1,
nsamples=1,
seqlen=32,
dataset=calibration_dataset,
enable_alg_ext=True,
enable_torch_compile=True,
)
ar.quantize()
@pytest.mark.skip_ci(reason="Architecture: Only tiny model is suggested")
@pytest.mark.skipif(reason="Time-consuming for accuracy evaluation")
def test_2bits(self):
model_name = get_model_path("facebook/opt-125m")
ar = AutoRound(model=model_name, bits=2, group_size=64, enable_alg_ext=True)
_, quantized_model_path = ar.quantize_and_save(self.save_folder)
model = AutoModelForCausalLM.from_pretrained(
quantized_model_path,
device_map="auto",
)
tokenizer = AutoTokenizer.from_pretrained(self.save_folder)
evaluate_accuracy(model, tokenizer, threshold=0.22, batch_size=64)
@pytest.mark.skip_ci(reason="Matrix: Not necessary to test all case in CI")
def test_cli(self, tiny_opt_model_path):
import os
python_path = sys.executable
res = os.system(
f"PYTHONPATH='{AUTO_ROUND_PATH}:$PYTHONPATH' CUDA_VISIBLE_DEVICES=0 {python_path} -m auto_round --model {tiny_opt_model_path} --iters 1 --device auto --enable_alg_ext --disable_minmax_tuning --disable_quanted_input --bits 2 --schemes=W2A16,W4A16 --ignore_scale_zp_bits --nsamples 1 --seqlen 32"
)
if res > 0 or res == -1:
assert False, "cmd line test fail, please have a check"
res = os.system(
f"PYTHONPATH='{AUTO_ROUND_PATH}:$PYTHONPATH' CUDA_VISIBLE_DEVICES=0 {python_path} -m auto_round --model {tiny_opt_model_path} --iters 1 --device auto --enable_alg_ext --bits 5.5 --schemes=mxfp4,mxfp8 --ignore_scale_zp_bits --enable_torch_compile --nsamples 1 --seqlen 32"
)
if res > 0 or res == -1:
assert False, "cmd line test fail, please have a check"