Skip to content

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

Use shared tokenizer save helper in exporters (#1951)

Wang, Chang436b9db60f70
diff --git a/auto_round/export/export_to_autoround/export.py b/auto_round/export/export_to_autoround/export.py
index e93534857..32f688ab1 100644
--- a/auto_round/export/export_to_autoround/export.py
+++ b/auto_round/export/export_to_autoround/export.py
@@ -35,6 +35,7 @@ from auto_round.export.utils import (
release_layer_safely,
resolve_pipeline_export_layout,
save_model,
+ save_pretrained_artifact,
)
from auto_round.formats import AutoRoundExportFormat
from auto_round.logger import logger
@@ -347,8 +348,7 @@ def save_quantized_as_autoround(
if output_dir:
model_output_dir, processor_output_dir, _ = resolve_pipeline_export_layout(model, output_dir)
- if tokenizer is not None and hasattr(tokenizer, "save_pretrained"):
- tokenizer.save_pretrained(processor_output_dir)
+ save_pretrained_artifact(tokenizer, processor_output_dir, artifact_name="tokenizer")
if processor is not None:
processor.save_pretrained(processor_output_dir)
diff --git a/auto_round/export/export_to_autoround/export_to_fp8.py b/auto_round/export/export_to_autoround/export_to_fp8.py
index c40f5a33a..c0465dfcd 100644
--- a/auto_round/export/export_to_autoround/export_to_fp8.py
+++ b/auto_round/export/export_to_autoround/export_to_fp8.py
@@ -29,6 +29,7 @@ from auto_round.export.utils import (
is_immediate_saving_mode,
release_layer_safely,
save_model,
+ save_pretrained_artifact,
)
from auto_round.logger import logger
from auto_round.schemes import QuantizationScheme
@@ -280,10 +281,7 @@ def save_quantized_as_autoround(
immediate_saving = is_immediate_saving_mode(model, serialization_dict)
if os.path.exists(output_dir) and not immediate_saving:
logger.warning(f"{output_dir} already exists, this may cause model conflict")
- if tokenizer is not None and hasattr(tokenizer, "save_pretrained"):
- tokenizer.save_pretrained(output_dir)
- else:
- print(f"Skip tokenizer save, tokenizer={tokenizer}")
+ save_pretrained_artifact(tokenizer, output_dir, artifact_name="tokenizer")
if processor is not None:
processor.save_pretrained(output_dir)
diff --git a/auto_round/export/export_to_autoround/export_to_nvfp_mx.py b/auto_round/export/export_to_autoround/export_to_nvfp_mx.py
index 045f8f70f..e9be7d634 100644
--- a/auto_round/export/export_to_autoround/export_to_nvfp_mx.py
+++ b/auto_round/export/export_to_autoround/export_to_nvfp_mx.py
@@ -31,6 +31,7 @@ from auto_round.export.utils import (
is_immediate_saving_mode,
release_layer_safely,
save_model,
+ save_pretrained_artifact,
)
from auto_round.logger import logger
from auto_round.schemes import QuantizationScheme
@@ -269,10 +270,7 @@ def save_quantized_as_fp(
immediate_saving = is_immediate_saving_mode(model, serialization_dict)
if os.path.exists(output_dir) and not immediate_saving:
logger.warning(f"{output_dir} already exists, this may cause model conflict")
- if tokenizer is not None and hasattr(tokenizer, "save_pretrained"):
- tokenizer.save_pretrained(output_dir)
- else:
- print(f"Skip tokenizer save, tokenizer={tokenizer}")
+ save_pretrained_artifact(tokenizer, output_dir, artifact_name="tokenizer")
if processor is not None:
processor.save_pretrained(output_dir)
diff --git a/auto_round/export/export_to_awq/export.py b/auto_round/export/export_to_awq/export.py
index a6a7fff7a..fc91b3817 100644
--- a/auto_round/export/export_to_awq/export.py
+++ b/auto_round/export/export_to_awq/export.py
@@ -35,6 +35,7 @@ from auto_round.export.utils import (
is_immediate_saving_mode,
release_layer_safely,
save_model,
+ save_pretrained_artifact,
)
from auto_round.logger import logger
from auto_round.utils import (
@@ -164,8 +165,7 @@ def save_quantized_as_autoawq(
logger.warning(f"{output_dir} already exists, this may cause model conflict")
logger.info("Saving quantized model to auto_awq format")
- if tokenizer is not None and hasattr(tokenizer, "save_pretrained") and output_dir is not None:
- tokenizer.save_pretrained(output_dir)
+ save_pretrained_artifact(tokenizer, output_dir, artifact_name="tokenizer")
if processor is not None and output_dir is not None:
processor.save_pretrained(output_dir)
diff --git a/auto_round/export/export_to_llmcompressor/export.py b/auto_round/export/export_to_llmcompressor/export.py
index 3a5f5e609..cd0cc2186 100644
--- a/auto_round/export/export_to_llmcompressor/export.py
+++ b/auto_round/export/export_to_llmcompressor/export.py
@@ -18,7 +18,7 @@ from typing import Callable, Union
import torch
-from auto_round.export.utils import is_immediate_saving_mode, save_model
+from auto_round.export.utils import is_immediate_saving_mode, save_model, save_pretrained_artifact
from auto_round.logger import logger
from auto_round.utils import (
SUPPORTED_LAYER_TYPES,
@@ -209,8 +209,7 @@ def save_quantized_as_llmcompressor(
model = copy.deepcopy(model.to("cpu"))
# save tokenizer, processor
- if output_dir is not None and tokenizer is not None and hasattr(tokenizer, "save_pretrained"):
- tokenizer.save_pretrained(output_dir)
+ save_pretrained_artifact(tokenizer, output_dir, artifact_name="tokenizer")
if output_dir is not None and processor is not None:
processor.save_pretrained(output_dir)
diff --git a/auto_round/export/export_to_llmcompressor/export_to_fp.py b/auto_round/export/export_to_llmcompressor/export_to_fp.py
index a7e9f6df1..192a68443 100644
--- a/auto_round/export/export_to_llmcompressor/export_to_fp.py
+++ b/auto_round/export/export_to_llmcompressor/export_to_fp.py
@@ -33,6 +33,7 @@ from auto_round.export.utils import (
is_immediate_saving_mode,
release_layer_safely,
save_model,
+ save_pretrained_artifact,
)
from auto_round.logger import logger
from auto_round.utils import (
@@ -362,8 +363,7 @@ def save_quantized_as_fp(
immediate_saving = is_immediate_saving_mode(model, serialization_dict)
if os.path.exists(output_dir) and not immediate_saving:
logger.warning(f"{output_dir} already exists, this may cause model conflict")
- if tokenizer is not None:
- tokenizer.save_pretrained(output_dir)
+ save_pretrained_artifact(tokenizer, output_dir, artifact_name="tokenizer")
if processor is not None:
processor.save_pretrained(output_dir)
diff --git a/auto_round/export/export_to_llmcompressor/export_to_static_fp.py b/auto_round/export/export_to_llmcompressor/export_to_static_fp.py
index 0754f6df4..6bb055d57 100644
--- a/auto_round/export/export_to_llmcompressor/export_to_static_fp.py
+++ b/auto_round/export/export_to_llmcompressor/export_to_static_fp.py
@@ -25,7 +25,7 @@ from tqdm import tqdm
from auto_round.data_type.utils import reshape_pad_tensor_by_group_size, revert_tensor_by_pad
from auto_round.export.export_to_autoround.export_to_fp8 import FP8QLinear
from auto_round.export.export_to_llmcompressor.config import check_compressed_tensors_supported
-from auto_round.export.utils import is_immediate_saving_mode, save_model
+from auto_round.export.utils import is_immediate_saving_mode, save_model, save_pretrained_artifact
from auto_round.utils import (
SUPPORTED_LAYER_TYPES,
check_start_with_block_name,
@@ -265,8 +265,7 @@ def save_quantized_as_static_fp(
if os.path.exists(output_dir) and not immediate_saving:
logger.warning(f"{output_dir} already exists, this may cause model conflict")
- if tokenizer is not None:
- tokenizer.save_pretrained(output_dir)
+ save_pretrained_artifact(tokenizer, output_dir, artifact_name="tokenizer")
if processor is not None:
processor.save_pretrained(output_dir)
diff --git a/auto_round/export/export_to_mlx/export.py b/auto_round/export/export_to_mlx/export.py
index 6892980af..2681ca818 100644
--- a/auto_round/export/export_to_mlx/export.py
+++ b/auto_round/export/export_to_mlx/export.py
@@ -36,7 +36,7 @@ import torch
import torch.nn as nn
import transformers
-from auto_round.export.utils import unsupported_meta_device
+from auto_round.export.utils import save_pretrained_artifact, unsupported_meta_device
from auto_round.logger import logger
from auto_round.utils import (
check_to_quantized,
@@ -701,8 +701,7 @@ def save_quantized_as_mlx(
json.dump(config, f, indent=2)
# Save tokenizer
- if tokenizer is not None and hasattr(tokenizer, "save_pretrained"):
- tokenizer.save_pretrained(output_dir)
+ save_pretrained_artifact(tokenizer, output_dir, artifact_name="tokenizer")
# Copy processor if available
processor = kwargs.get("processor", None)
diff --git a/auto_round/export/utils.py b/auto_round/export/utils.py
index c809f0b0b..d62192999 100644
--- a/auto_round/export/utils.py
+++ b/auto_round/export/utils.py
@@ -27,6 +27,21 @@ from auto_round.utils import (
)
+def save_pretrained_artifact(artifact, output_dir: str, artifact_name: str = "artifact") -> bool:
+ """Save a Hugging Face style artifact and log explicit skip reasons."""
+ if output_dir is None:
+ logger.warning("Skipping %s save: output_dir is None.", artifact_name)
+ return False
+ if artifact is None:
+ logger.warning("Skipping %s save: object is None.", artifact_name)
+ return False
+ if not hasattr(artifact, "save_pretrained"):
+ logger.warning("Skipping %s save: object %r has no save_pretrained method.", artifact_name, artifact)
+ return False
+ artifact.save_pretrained(output_dir)
+ return True
+
+
def _save_model_configs(model: nn.Module, save_dir: str) -> None:
if hasattr(model, "config") and model.config is not None:
try: