PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度
import copy
import shutil
from test.helpers import get_model_path, save_tiny_model
import pytest
import torch
import transformers
from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer
from auto_round import AutoRound
class TestAutoRound:
save_dir = "./saved"
@pytest.fixture(autouse=True, scope="class")
def setup_and_teardown_class(self):
# ===== SETUP (setup_class) =====
print("[Setup] Running before any test in class")
# Yield to hand control to the test methods
yield
# ===== TEARDOWN (teardown_class) =====
print("[Teardown] Running after all tests in class")
shutil.rmtree("./saved", ignore_errors=True)
shutil.rmtree("runs", ignore_errors=True)
def test_transform_mxfp4_quant_infer(self):
model_name = get_model_path("qwen/Qwen3-0.6B")
scheme = "MXFP4"
ar = AutoRound(
model=model_name,
iters=0,
seqlen=2,
scheme=scheme,
alg_configs=["rtn", "hadamard"],
)
compressed_model, quantized_model_path = ar.quantize_and_save(output_dir=self.save_dir, format="auto_round")
model = AutoModelForCausalLM.from_pretrained(quantized_model_path, torch_dtype="auto", device_map="cuda")
tokenizer = AutoTokenizer.from_pretrained(quantized_model_path)
from test.helpers import generate_prompt
generate_prompt(model, tokenizer)
@pytest.mark.skip_ci(
reason="Matrix: Default MXFP4 transform covers CUDA export/reload; MXFP4 tuning remains in the full test tier"
)
def test_transform_mxfp4_tuning_quant_infer(self):
model_name = get_model_path("qwen/Qwen3-0.6B")
scheme = "MXFP4"
ar = AutoRound(
model=model_name,
iters=2,
seqlen=2,
scheme=scheme,
alg_configs=["auto_round", "hadamard"],
)
compressed_model, quantized_model_path = ar.quantize_and_save(output_dir=self.save_dir, format="auto_round")
model = AutoModelForCausalLM.from_pretrained(quantized_model_path, torch_dtype="auto", device_map="cuda")
tokenizer = AutoTokenizer.from_pretrained(quantized_model_path)
from test.helpers import generate_prompt
generate_prompt(model, tokenizer)
@pytest.mark.skip_ci(
reason="Matrix: Random Hadamard behavior has dedicated unit coverage; keep the CUDA MXFP4 E2E smoke deterministic"
)
def test_random_transform_mxfp4_quant_infer(self):
model_name = get_model_path("qwen/Qwen3-0.6B")
scheme = "MXFP4"
ar = AutoRound(
model=model_name,
iters=0,
seqlen=2,
scheme=scheme,
alg_configs=["rtn", "random_hadamard"],
)
compressed_model, quantized_model_path = ar.quantize_and_save(output_dir=self.save_dir, format="auto_round")
model = AutoModelForCausalLM.from_pretrained(quantized_model_path, torch_dtype="auto", device_map="cuda")
tokenizer = AutoTokenizer.from_pretrained(quantized_model_path)
from test.helpers import generate_prompt
generate_prompt(model, tokenizer)