PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度
import copy
import os
import re
import shutil
from test.helpers import get_model_path, transformers_version
import pytest
import requests
from packaging import version
from PIL import Image
from auto_round import AutoRound
from ...envs import multi_card, require_gptqmodel, require_optimum, require_vlm_env
class TestAutoRound:
@pytest.fixture(autouse=True)
def _save_dir(self, tmp_path):
self.save_dir = str(tmp_path / "saved")
yield
shutil.rmtree(self.save_dir, ignore_errors=True)
@pytest.fixture(autouse=True, scope="class")
def setup_and_teardown_class(self):
yield
shutil.rmtree("runs", ignore_errors=True)
def test_diffusion_rtn(self, tiny_flux_model_path):
from diffusers import AutoPipelineForText2Image
## load the model
pipe = AutoPipelineForText2Image.from_pretrained(tiny_flux_model_path)
# build tiny model for testing since the full model is too large to quantize and evaluate in CI
pipe.transformer.transformer_blocks = pipe.transformer.transformer_blocks[:2]
pipe.transformer.single_transformer_blocks = pipe.transformer.single_transformer_blocks[:2]
## quantize the model
autoround = AutoRound(
pipe,
tokenizer=None,
scheme="MXFP4",
iters=0,
disable_opt_rtn=True,
calib_num_inference_steps=2,
dataset="coco2014",
)
# skip model saving since it takes much time
autoround.quantize()
def test_z_image_tune(self, tiny_z_image_model_path, tmp_path):
autoround = AutoRound(
tiny_z_image_model_path,
iters=1,
nsamples=1,
calib_num_inference_steps=2,
dataset="coco2014",
)
# skip model saving since it takes much time
autoround.quantize_and_save(tmp_path)
@require_optimum
@pytest.mark.parametrize("low_gpu_mem_usage,cache_size", [(False, 0), (True, "auto"), (True, 1e-9)])
def test_diffusion_tune(self, tiny_flux_model_path, tmp_path, monkeypatch, low_gpu_mem_usage, cache_size):
from diffusers import AutoPipelineForText2Image
from auto_round.algorithms.tuning_cache import DiffusionTuningCache
caches = []
create_cache = DiffusionTuningCache.create
def capture_cache(*args, **kwargs):
cache = create_cache(*args, **kwargs)
caches.append(cache)
return cache
monkeypatch.setattr(DiffusionTuningCache, "create", capture_cache)
## load the model
pipe = AutoPipelineForText2Image.from_pretrained(tiny_flux_model_path)
model = pipe.transformer
# build tiny model for testing since the full model is too large to quantize and evaluate in CI
pipe.transformer.transformer_blocks = pipe.transformer.transformer_blocks[:2]
pipe.transformer.single_transformer_blocks = pipe.transformer.single_transformer_blocks[:2]
layer_config = {}
# skip some layers since it takes much time
for n, m in model.named_modules():
if m.__class__.__name__ != "Linear":
continue
match = re.search(r"blocks\.(\d+)", n)
if match and int(match.group(1)) > 0:
layer_config[n] = {"bits": 16, "act_bits": 16}
## quantize the model
# https://raw.githubusercontent.com/mlcommons/inference/refs/heads/master/text_to_image/coco2014/captions/captions_source.tsv
autoround = AutoRound(
pipe,
tokenizer=None,
scheme="MXFP4",
iters=2,
nsamples=1,
calib_num_inference_steps=2,
layer_config=layer_config,
dataset="coco2014",
low_gpu_mem_usage=low_gpu_mem_usage,
diffusion_tuning_cache_size=cache_size,
)
# skip model saving since it takes much time
autoround.quantize_and_save(tmp_path)
if cache_size == "auto":
assert any(cache is not None for cache in caches)
assert all(not cache.thread.is_alive() and not cache.slots for cache in caches if cache is not None)
elif cache_size:
assert caches and all(cache is None for cache in caches)
else:
assert not caches
@pytest.mark.skip_ci(reason="Architecture: Download large model; Time-consuming")
def test_diffusion_model_checker(self):
from auto_round.utils import is_diffusion_model
assert is_diffusion_model(get_model_path("black-forest-labs/FLUX.1-dev"))
assert is_diffusion_model(get_model_path("sd2-community/stable-diffusion-2-1"))
assert is_diffusion_model(get_model_path("stabilityai/stable-diffusion-xl-base-1.0"))
assert is_diffusion_model(get_model_path("Qwen/Qwen3-8B")) is False
@multi_card
@pytest.mark.skip_ci(reason="Resource: multiple card test")
def test_diffusion_tune_on_multi_cards(self, tiny_flux_model_path, tmp_path):
from diffusers import AutoPipelineForText2Image
## load the model
pipe = AutoPipelineForText2Image.from_pretrained(tiny_flux_model_path)
model = pipe.transformer
# build tiny model for testing since the full model is too large to quantize and evaluate in CI
pipe.transformer.transformer_blocks = pipe.transformer.transformer_blocks[:2]
pipe.transformer.single_transformer_blocks = pipe.transformer.single_transformer_blocks[:2]
layer_config = {}
# skip some layers since it takes much time
for n, m in model.named_modules():
if m.__class__.__name__ != "Linear":
continue
match = re.search(r"blocks\.(\d+)", n)
if match and int(match.group(1)) > 0:
layer_config[n] = {"bits": 16, "act_bits": 16}
## quantize the model
# https://raw.githubusercontent.com/mlcommons/inference/refs/heads/master/text_to_image/coco2014/captions/captions_source.tsv
autoround = AutoRound(
pipe,
tokenizer=None,
scheme="MXFP4",
iters=1,
nsamples=1,
calib_num_inference_steps=2,
layer_config=layer_config,
dataset="coco2014",
device_map="0,1",
)
# skip model saving since it takes much time
autoround.quantize_and_save(tmp_path)