Skip to content

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

feat: enhance FP32 tensor handling in mixed-precision checkpoints and… (#2519)

Xin He1d623e2acc98
diff --git a/auto_round/context/model.py b/auto_round/context/model.py
index b5c1bb2a8..38ded59be 100644
--- a/auto_round/context/model.py
+++ b/auto_round/context/model.py
@@ -399,8 +399,10 @@ class ModelContext(BaseContext):
runs, in ``__init__``, via ``_materialize_disk_stream_non_block_params``.
"""
from auto_round.utils.disk_stream_util import build_meta_model
+ from auto_round.utils.missing_tensors import keep_special_fp32_params
model, tokenizer, index = build_meta_model(model_name, trust_remote_code=self.trust_remote_code)
+ keep_special_fp32_params(model, model_name)
model.path = model_name
# Stash the index on the model object
# itself so downstream code that only has a reference to the model
diff --git a/auto_round/utils/missing_tensors.py b/auto_round/utils/missing_tensors.py
index 7bd494e81..00365a20e 100644
--- a/auto_round/utils/missing_tensors.py
+++ b/auto_round/utils/missing_tensors.py
@@ -68,16 +68,24 @@ def _restore_special_fp32_tensors(
source_to_saved[source_name] = source_name
source_names_by_shard: dict[str, list[str]] = {}
- for tensor_name in source_to_saved:
- source_names_by_shard.setdefault(source_tensor_to_file[tensor_name], []).append(tensor_name)
+ for tensor_name, source_shard in source_tensor_to_file.items():
+ source_names_by_shard.setdefault(source_shard, []).append(tensor_name)
candidates: list[str] = []
+ has_low_precision_float = False
for source_shard, tensor_names in source_names_by_shard.items():
with safe_open(source_shard, framework="pt", device="cpu") as source_file:
for tensor_name in tensor_names:
- if source_file.get_slice(tensor_name).get_dtype() == "F32":
+ dtype = source_file.get_slice(tensor_name).get_dtype()
+ if dtype in {"F16", "BF16"} or dtype.startswith("F8"):
+ has_low_precision_float = True
+ elif dtype == "F32" and tensor_name in source_to_saved:
candidates.append(tensor_name)
+ # An all-FP32 source has no "special" FP32 tensors; the downcast to the export dtype is intended.
+ if not has_low_precision_float:
+ return
+
target_names_by_shard: dict[str, list[str]] = {}
for tensor_name in candidates:
target_name = source_to_saved[tensor_name]
@@ -91,7 +99,13 @@ def _restore_special_fp32_tensors(
if target_file.get_slice(target_name).get_dtype() in {"F16", "BF16"}:
source_shard = source_tensor_to_file[tensor_name]
with safe_open(source_shard, framework="pt", device="cpu") as source_file:
- tensors_to_restore[tensor_name] = source_file.get_tensor(tensor_name)
+ source_tensor = source_file.get_tensor(tensor_name)
+ target_tensor = target_file.get_tensor(target_name)
+ # Skip tensors changed beyond a plain downcast (e.g. fake-quantized weights).
+ if source_tensor.shape == target_tensor.shape and torch.equal(
+ source_tensor.to(target_tensor.dtype), target_tensor
+ ):
+ tensors_to_restore[tensor_name] = source_tensor
restore_names_by_shard: dict[str, list[str]] = {}
for tensor_name in tensors_to_restore:
@@ -129,26 +143,81 @@ def _restore_special_fp32_tensors(
)
-def _resolve_tensor_shard_maps(
- source_dir: str,
- target_dir: str,
-) -> tuple[dict[str, str], dict[str, str]] | None:
- """Resolve ``tensor_name -> shard_file`` maps for *source_dir* and *target_dir*.
+def keep_special_fp32_params(model: torch.nn.Module, source_dir: str) -> list[str]:
+ """Preserve checkpoint FP32 tensors after loading or building a meta skeleton.
- Returns ``None`` when either checkpoint cannot be resolved (missing config,
- unresolvable source repo, or no safetensors present), in which case callers
- should skip whatever tensor-level processing they were about to do.
+ Only applies to mixed-precision checkpoints (e.g. a BF16 model that stores
+ ``linear_attn.norm.weight`` in FP32); for an all-FP32 checkpoint the load dtype is
+ intentional. Restored names are appended to ``model._keep_in_fp32_modules`` so that
+ later ``cast_model_dtype`` calls and export keep them in FP32.
+
+ Meta tensors are promoted without reading their payload; materialization later
+ loads the original FP32 values. Returns the restored or marked tensor names.
"""
+ source_tensor_to_file = _resolve_source_tensor_map(source_dir)
+ if not source_tensor_to_file:
+ return []
+ from safetensors import safe_open
+
+ model_tensors = dict(model.named_parameters(remove_duplicate=False))
+ model_tensors.update(model.named_buffers(remove_duplicate=False))
+
+ names_by_shard: dict[str, list[str]] = {}
+ for tensor_name, shard in source_tensor_to_file.items():
+ names_by_shard.setdefault(shard, []).append(tensor_name)
+
+ has_low_precision_float = False
+ candidates_by_shard: dict[str, list[str]] = {}
+ for shard, tensor_names in names_by_shard.items():
+ with safe_open(shard, framework="pt", device="cpu") as f:
+ for tensor_name in tensor_names:
+ dtype = f.get_slice(tensor_name).get_dtype()
+ if dtype in {"F16", "BF16"} or dtype.startswith("F8"):
+ has_low_precision_float = True
+ elif dtype == "F32":
+ tensor = model_tensors.get(tensor_name)
+ if tensor is not None and tensor.dtype in (torch.float16, torch.bfloat16, torch.float32):
+ candidates_by_shard.setdefault(shard, []).append(tensor_name)
+
+ if not has_low_precision_float or not candidates_by_shard:
+ return []
+
+ restored: list[str] = []
+ for shard, tensor_names in candidates_by_shard.items():
+ with safe_open(shard, framework="pt", device="cpu") as f:
+ for tensor_name in tensor_names:
+ tensor = model_tensors[tensor_name]
+ if tensor.device.type == "meta":
+ if tuple(f.get_slice(tensor_name).get_shape()) != tuple(tensor.shape):
+ continue
+ tensor.data = tensor.to(dtype=torch.float32)
+ restored.append(tensor_name)
+ continue
+ value = f.get_tensor(tensor_name)
+ if value.shape != tensor.shape:
+ continue
+ tensor.data = value.to(tensor.device)
+ restored.append(tensor_name)
+
+ if restored:
+ keep = getattr(model, "_keep_in_fp32_modules", None) or []
+ keep = [keep] if isinstance(keep, str) else list(keep)
+ model._keep_in_fp32_modules = keep + [name for name in restored if name not in keep]
+ logger.info(
+ f"Kept {len(restored)} checkpoint FP32 tensor(s) in FP32 after loading: "
+ f"{compress_layer_names(restored)}."
+ )
+ return restored
+
+
+def _resolve_source_tensor_map(source_dir: str) -> dict[str, str] | None:
+ """Return ``tensor_name -> shard_file`` for the safetensors checkpoint in *source_dir*."""
try:
from safetensors import safe_open
except ImportError:
logger.warning("safetensors not available, skipping tensor comparison against source checkpoint")
return None
- config_path = os.path.join(target_dir, "config.json")
- if not os.path.exists(config_path):
- return None
-
if not os.path.isdir(source_dir):
try:
from auto_round.utils.model import download_hf_model
@@ -180,6 +249,27 @@ def _resolve_tensor_shard_maps(
source_tensor_to_file[key] = source_single_file
else:
return None
+ return source_tensor_to_file
+
+
+def _resolve_tensor_shard_maps(
+ source_dir: str,
+ target_dir: str,
+) -> tuple[dict[str, str], dict[str, str]] | None:
+ """Resolve ``tensor_name -> shard_file`` maps for *source_dir* and *target_dir*.
+
+ Returns ``None`` when either checkpoint cannot be resolved (missing config,
+ unresolvable source repo, or no safetensors present), in which case callers
+ should skip whatever tensor-level processing they were about to do.
+ """
+ config_path = os.path.join(target_dir, "config.json")
+ if not os.path.exists(config_path):
+ return None
+
+ source_tensor_to_file = _resolve_source_tensor_map(source_dir)
+ if source_tensor_to_file is None:
+ return None
+ from safetensors import safe_open
saved_tensor_to_file: dict[str, str] = {}
saved_index_file = os.path.join(target_dir, "model.safetensors.index.json")
diff --git a/auto_round/utils/model.py b/auto_round/utils/model.py
index c805fb714..dcaded51e 100644
--- a/auto_round/utils/model.py
+++ b/auto_round/utils/model.py
@@ -213,7 +213,7 @@ def check_start_with_block_name(name: str, block_name_to_quantize: list):
return False
-def download_or_get_path(repo_id: str, platform: str | None = None) -> str:
+def download_or_get_path(repo_id: str, platform: str | None = None, revision: str | None = None) -> str:
from auto_round import envs
if platform is None:
@@ -223,12 +223,14 @@ def download_or_get_path(repo_id: str, platform: str | None = None) -> str:
platform = "hf"
if platform == "model_scope":
- return download_modelscope_model(repo_id)
+ return download_modelscope_model(repo_id, revision=revision)
else:
- return download_hf_model(repo_id)
+ return download_hf_model(repo_id, revision=revision)
-def download_modelscope_model(repo_id: str, local_dir: str | None = None, cache_dir: str | None = None):
+def download_modelscope_model(
+ repo_id: str, local_dir: str | None = None, cache_dir: str | None = None, revision: str | None = None
+):
from modelscope.utils.file_utils import get_modelscope_cache_dir # pylint: disable=E0401
system_cache = cache_dir if cache_dir is not None else get_modelscope_cache_dir()
@@ -238,12 +240,12 @@ def download_modelscope_model(repo_id: str, local_dir: str | None = None, cache_
directory = os.path.join(system_cache, *repo_id.split("/"))
else:
directory = os.path.join(system_cache, "models", *repo_id.split("/"))
- if os.path.exists(directory):
+ if os.path.exists(directory) and revision is None:
return directory
else:
from modelscope.hub.snapshot_download import snapshot_download # pylint: disable=E0401
- return snapshot_download(repo_id)
+ return snapshot_download(repo_id, revision=revision)
def download_hf_model(repo_id, cache_dir=None, repo_type=None, revision=None):
@@ -273,7 +275,7 @@ def download_hf_model(repo_id, cache_dir=None, repo_type=None, revision=None):
else: # pragma: no cover
from huggingface_hub import snapshot_download
- model_path = snapshot_download(repo_id)
+ model_path = snapshot_download(repo_id, revision=revision)
return model_path
@@ -568,11 +570,28 @@ def llm_load_model(
monkey_patch_model(model)
check_and_mark_quantized_module(model)
handle_generation_config(model)
+ _keep_checkpoint_fp32_params(model, pretrained_model_name_or_path, platform, revision=load_kwargs.get("revision"))
model = _to_model_dtype(model, model_dtype)
return model, tokenizer
+def _keep_checkpoint_fp32_params(model, pretrained_model_name_or_path, platform, subfolder=None, revision=None):
+ """Keep FP32 tensors of a mixed-precision checkpoint in FP32 after ``from_pretrained``."""
+ from auto_round.utils.missing_tensors import keep_special_fp32_params
+
+ revision = getattr(getattr(model, "config", None), "_commit_hash", None) or revision
+ try:
+ source_dir = pretrained_model_name_or_path
+ if not os.path.isdir(source_dir):
+ source_dir = download_or_get_path(source_dir, platform=platform, revision=revision)
+ if subfolder is not None:
+ source_dir = os.path.join(source_dir, subfolder)
+ keep_special_fp32_params(model, source_dir)
+ except Exception as e:
+ logger.warning(f"Skipping restore of FP32 tensors from the checkpoint due to error: {e}")
+
+
def _find_pipeline_model_subfolder(model_dir_or_repo: str, file_list: list | None = None) -> tuple:
"""Find model/processor subfolders from a pipeline's model_index.json.
@@ -941,6 +960,9 @@ def mllm_load_model(
model = model.eval()
check_and_mark_quantized_module(model)
handle_generation_config(model)
+ _keep_checkpoint_fp32_params(
+ model, pretrained_model_name_or_path, platform, model_subfolder, revision=kwargs.get("revision")
+ )
model = _to_model_dtype(model, model_dtype)
if model_subfolder is not None:
diff --git a/test/unit/common/utils/test_missing_tensors.py b/test/unit/common/utils/test_missing_tensors.py
index 3601cde03..7e757991a 100644
--- a/test/unit/common/utils/test_missing_tensors.py
+++ b/test/unit/common/utils/test_missing_tensors.py
@@ -503,7 +503,7 @@ class TestCopyMissingTensorsFromSource:
os.makedirs(src)
os.makedirs(tgt)
original = torch.tensor([1.0001, -2.0002], dtype=torch.float32)
- unchanged = torch.tensor([3.0], dtype=torch.float32)
+ unchanged = torch.tensor([3.0], dtype=torch.bfloat16)
_save_safetensors(
{"model.special.weight": original, "model.unchanged.weight": unchanged},
os.path.join(src, "model.safetensors"),
@@ -533,13 +533,22 @@ class TestCopyMissingTensorsFromSource:
original = torch.tensor([1.0001, -2.0002], dtype=torch.float32)
source_shard = "model-00002-of-00002.safetensors"
target_shard = "model-00001-of-00002.safetensors"
- _save_safetensors({"model.special.weight": original}, os.path.join(src, source_shard))
+ _save_safetensors(
+ {"model.special.weight": original, "model.other.weight": torch.ones(2, dtype=torch.bfloat16)},
+ os.path.join(src, source_shard),
+ )
_save_safetensors(
{"model.special.weight": original.to(torch.bfloat16), "model.other.weight": torch.ones(2)},
os.path.join(tgt, target_shard),
)
with open(os.path.join(src, "model.safetensors.index.json"), "w") as f:
- json.dump({"metadata": {}, "weight_map": {"model.special.weight": source_shard}}, f)
+ json.dump(
+ {
+ "metadata": {},
+ "weight_map": {"model.special.weight": source_shard, "model.other.weight": source_shard},
+ },
+ f,
+ )
with open(os.path.join(tgt, "model.safetensors.index.json"), "w") as f:
json.dump(
{
@@ -897,7 +906,7 @@ class TestRestoreFp32DecoupledFromMtp:
os.makedirs(src, exist_ok=True)
os.makedirs(tgt, exist_ok=True)
norm_weight = torch.tensor([1.0001, -2.0002, 3.0003], dtype=torch.float32)
- mtp_weight = torch.randn(32, 64)
+ mtp_weight = torch.randn(32, 64, dtype=torch.bfloat16)
_save_safetensors(
{
"model.language_model.layers.0.linear_attn.norm.weight": norm_weight,
@@ -944,7 +953,7 @@ class TestRestoreFp32DecoupledFromMtp:
_save_safetensors(
{
"model.language_model.layers.0.linear_attn.norm.weight": norm_weight,
- "model.language_model.mtp.0.fc.weight": torch.randn(32, 64),
+ "model.language_model.mtp.0.fc.weight": torch.randn(32, 64, dtype=torch.bfloat16),
},
os.path.join(src, "model.safetensors"),
)
@@ -985,7 +994,7 @@ class TestRestoreFp32DecoupledFromMtp:
src = str(tmp_path / "src")
os.makedirs(src)
norm_weight = torch.tensor([1.0001, -2.0002, 3.0003], dtype=torch.float32)
- mtp_weight = torch.randn(32, 64)
+ mtp_weight = torch.randn(32, 64, dtype=torch.bfloat16)
_save_safetensors(
{
"model.language_model.layers.0.linear_attn.norm.weight": norm_weight,
@@ -1022,3 +1031,215 @@ class TestRestoreFp32DecoupledFromMtp:
extra = _load_safetensors(os.path.join(tmp_save_dir, "model_extra_tensors.safetensors"))
assert "model.language_model.mtp.0.fc.weight" in extra
torch.testing.assert_close(extra["model.language_model.mtp.0.fc.weight"], mtp_weight)
+
+ def test_all_fp32_source_is_not_restored(self, tmp_path):
+ """An all-FP32 source (e.g. fake format export to bf16) must keep the saved tensors."""
+ src, tgt = str(tmp_path / "src"), str(tmp_path / "tgt")
+ os.makedirs(src)
+ os.makedirs(tgt)
+ weight = torch.randn(32, 64)
+ _save_safetensors({"model.layers.0.mlp.up_proj.weight": weight}, os.path.join(src, "model.safetensors"))
+ _save_safetensors(
+ {"model.layers.0.mlp.up_proj.weight": weight.to(torch.bfloat16)}, os.path.join(tgt, "model.safetensors")
+ )
+ _write_config(tgt)
+
+ restore_fp32_tensors_from_source(src, tgt)
+
+ result = _load_safetensors(os.path.join(tgt, "model.safetensors"))
+ assert result["model.layers.0.mlp.up_proj.weight"].dtype == torch.bfloat16
+
+ def test_modified_fp32_tensor_is_not_restored(self, tmp_path):
+ """FP32 source tensors altered by quantization (fake format) must not be overwritten."""
+ src, tgt = str(tmp_path / "src"), str(tmp_path / "tgt")
+ os.makedirs(src)
+ os.makedirs(tgt)
+ norm_weight = torch.tensor([1.0001, -2.0002, 3.0003], dtype=torch.float32)
+ weight = torch.randn(32, 64)
+ fake_quantized = (weight * 4).round().div(4).to(torch.bfloat16) + 1
+ _save_safetensors(
+ {
+ "model.layers.0.norm.weight": norm_weight,
+ "model.layers.0.mlp.up_proj.weight": weight,
+ "model.embed_tokens.weight": torch.randn(8, 64, dtype=torch.bfloat16),
+ },
+ os.path.join(src, "model.safetensors"),
+ )
+ _save_safetensors(
+ {
+ "model.layers.0.norm.weight": norm_weight.to(torch.bfloat16),
+ "model.layers.0.mlp.up_proj.weight": fake_quantized,
+ },
+ os.path.join(tgt, "model.safetensors"),
+ )
+ _write_config(tgt)
+
+ restore_fp32_tensors_from_source(src, tgt)
+
+ result = _load_safetensors(os.path.join(tgt, "model.safetensors"))
+ assert torch.equal(result["model.layers.0.norm.weight"], norm_weight)
+ assert torch.equal(result["model.layers.0.mlp.up_proj.weight"], fake_quantized)
+
+
+class TestKeepSpecialFp32Params:
+
+ def test_meta_loader_marks_fp32_params_and_buffers(self, tmp_path):
+ from types import SimpleNamespace
+ from unittest.mock import patch
+
+ from auto_round.context.model import ModelContext
+ from auto_round.utils.disk_stream_util import SafetensorsIndex, materialize_module
+
+ norm_weight = torch.tensor([1.0001, -2.0002, 3.0003, 4.0004])
+ _save_safetensors(
+ {
+ "block.norm.weight": norm_weight,
+ "block.norm.bias": torch.zeros(4, dtype=torch.bfloat16),
+ "block.proj.weight": torch.randn(4, 4, dtype=torch.bfloat16),
+ "block.scale": norm_weight.clone(),
+ },
+ str(tmp_path / "model.safetensors"),
+ )
+ with torch.device("meta"):
+ model = torch.nn.Module()
+ model.block = self._make_model().to(torch.bfloat16)
+ model.block.register_buffer("scale", torch.empty(4))
+ index = SafetensorsIndex(str(tmp_path))
+ context = SimpleNamespace(trust_remote_code=False)
+ with patch("auto_round.utils.disk_stream_util.build_meta_model", return_value=(model, None, index)):
+ loaded, _, loaded_index = ModelContext._build_disk_stream_model(context, str(tmp_path))
+
+ assert loaded is model
+ assert loaded_index is index
+ assert set(model._keep_in_fp32_modules) == {"block.norm.weight", "block.scale"}
+ assert all(tensor.is_meta for tensor in model.parameters())
+ assert model.block.scale.is_meta
+ materialize_module(model.block, "block", index, device="cpu")
+ assert torch.equal(model.block.norm.weight, norm_weight)
+ assert model.block.norm.weight.dtype == torch.float32
+ assert torch.equal(model.block.scale, norm_weight)
+ assert model.block.scale.dtype == torch.float32
+
+ def test_all_fp32_meta_checkpoint_is_untouched(self, tmp_path):
+ from auto_round.utils.missing_tensors import keep_special_fp32_params
+
+ _save_safetensors(self._make_model().state_dict(), str(tmp_path / "model.safetensors"))
+ with torch.device("meta"):
+ model = self._make_model().to(torch.bfloat16)
+
+ assert keep_special_fp32_params(model, str(tmp_path)) == []
+ assert model.norm.weight.is_meta
+ assert model.norm.weight.dtype == torch.bfloat16
+
+ def test_meta_checkpoint_keeps_fp32_through_materialization(self, tmp_path):
+ from auto_round.utils.disk_stream_util import SafetensorsIndex, free_module, materialize_module
+ from auto_round.utils.missing_tensors import keep_special_fp32_params
+ from auto_round.utils.model import cast_model_dtype
+
+ norm_weight = torch.tensor([1.0001, -2.0002, 3.0003, 4.0004])
+ _save_safetensors(
+ {
+ "block.norm.weight": norm_weight,
+ "block.norm.bias": torch.zeros(4, dtype=torch.bfloat16),
+ "block.proj.weight": torch.randn(4, 4, dtype=torch.bfloat16),
+ },
+ str(tmp_path / "model.safetensors"),
+ )
+ model = torch.nn.Module()
+ with torch.device("meta"):
+ model.block = self._make_model().to(torch.bfloat16)
+
+ assert keep_special_fp32_params(model, str(tmp_path)) == ["block.norm.weight"]
+ assert all(tensor.is_meta for tensor in model.parameters())
+ cast_model_dtype(model, torch.bfloat16)
+ assert model.block.norm.weight.dtype == torch.float32
+
+ index = SafetensorsIndex(str(tmp_path))
+ for _ in range(2):
+ materialize_module(model.block, "block", index, device="cpu")
+ assert model.block.norm.weight.dtype == torch.float32
+ assert torch.equal(model.block.norm.weight, norm_weight)
+ assert model.block.proj.weight.dtype == torch.bfloat16
+ free_module(model.block)
+
+ @staticmethod
+ def _make_model():
+ model = torch.nn.Module()
+ model.norm = torch.nn.LayerNorm(4)
+ model.proj = torch.nn.Linear(4, 4, bias=False)
+ return model
+
+ def test_mixed_checkpoint_keeps_fp32_after_cast(self, tmp_path):
+ from auto_round.utils.missing_tensors import keep_special_fp32_params
+ from auto_round.utils.model import cast_model_dtype
+
+ norm_weight = torch.tensor([1.0001, -2.0002, 3.0003, 4.0004])
+ _save_safetensors(
+ {
+ "norm.weight": norm_weight,
+ "norm.bias": torch.zeros(4, dtype=torch.bfloat16),
+ "proj.weight": torch.randn(4, 4, dtype=torch.bfloat16),
+ },
+ str(tmp_path / "model.safetensors"),
+ )
+ model = self._make_model().to(torch.bfloat16)
+
+ assert keep_special_fp32_params(model, str(tmp_path)) == ["norm.weight"]
+ model = cast_model_dtype(model, torch.bfloat16)
+
+ assert model.norm.weight.dtype == torch.float32
+ assert torch.equal(model.norm.weight, norm_weight)
+ assert model.proj.weight.dtype == torch.bfloat16
+
+ def test_mixed_checkpoint_keeps_fp32_loaded_model_param_after_cast(self, tmp_path):
+ from auto_round.utils.missing_tensors import keep_special_fp32_params
+ from auto_round.utils.model import cast_model_dtype
+
+ norm_weight = torch.tensor([1.0001, -2.0002, 3.0003, 4.0004])
+ _save_safetensors(
+ {
+ "norm.weight": norm_weight,
+ "norm.bias": torch.zeros(4, dtype=torch.bfloat16),
+ "proj.weight": torch.randn(4, 4, dtype=torch.bfloat16),
+ },
+ str(tmp_path / "model.safetensors"),
+ )
+ model = self._make_model()
+
+ assert keep_special_fp32_params(model, str(tmp_path)) == ["norm.weight"]
+ model = cast_model_dtype(model, torch.bfloat16)
+
+ assert model.norm.weight.dtype == torch.float32
+ assert torch.equal(model.norm.weight, norm_weight)
+ assert model.proj.weight.dtype == torch.bfloat16
+
+ def test_checkpoint_fp32_restore_uses_resolved_revision(self, tmp_path, monkeypatch):
+ from types import SimpleNamespace
+
+ import auto_round.utils.missing_tensors as missing_tensors
+ import auto_round.utils.model as model_utils
+
+ calls = {}
+
+ def fake_download(repo_id, platform, revision):
+ calls.update(repo_id=repo_id, platform=platform, revision=revision)
+ return str(tmp_path)
+
+ monkeypatch.setattr(model_utils, "download_or_get_path", fake_download)
+ monkeypatch.setattr(missing_tensors, "keep_special_fp32_params", lambda model, source_dir: [])
+ model = self._make_model()
+ model.config = SimpleNamespace(_commit_hash="resolved-commit")
+
+ model_utils._keep_checkpoint_fp32_params(model, "org/model", "hf", revision="requested-branch")
+
+ assert calls == {"repo_id": "org/model", "platform": "hf", "revision": "resolved-commit"}
+
+ def test_all_fp32_checkpoint_is_untouched(self, tmp_path):
+ from auto_round.utils.missing_tensors import keep_special_fp32_params
+
+ model = self._make_model()
+ _save_safetensors(model.state_dict(), str(tmp_path / "model.safetensors"))
+ model = model.to(torch.bfloat16)
+
+ assert keep_special_fp32_params(model, str(tmp_path)) == []
+ assert model.norm.weight.dtype == torch.bfloat16
diff --git a/test/unit/test_cpu/export/test_fake_format.py b/test/unit/test_cpu/export/test_fake_format.py
index b1cee31b2..b47385b78 100644
--- a/test/unit/test_cpu/export/test_fake_format.py
+++ b/test/unit/test_cpu/export/test_fake_format.py
@@ -412,7 +412,7 @@ def test_fake_format_restores_fp32_and_copies_mtp_tensors(tmp_path, monkeypatch)
source_dir = str(tmp_path / "source")
os.makedirs(source_dir)
norm_weight = torch.tensor([1.0001, -2.0002, 3.0003, 4.0004], dtype=torch.float32)
- mtp_weight = torch.randn(8, 4)
+ mtp_weight = torch.randn(8, 4, dtype=torch.bfloat16)
save_file(
{
"model.language_model.layers.0.linear_attn.norm.weight": norm_weight,