Skip to content

Commit 951dc36

Browse files
committed
[model] fix: fall back to direct file copy when generation_config save fails
Some models (e.g. GLM-5) have a generation_config.json with fields that newer transformers versions reject as invalid (e.g. top_p set without do_sample=True). Instead of crashing the roundtrip, catch the ValueError in save_artifacts and copy the original file directly to preserve it. Signed-off-by: Yu Yao <yaoyu.094@gmail.com> Signed-off-by: yaoyu-33 <yaoyu.094@gmail.com>
1 parent f03048a commit 951dc36

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

  • src/megatron/bridge/models/hf_pretrained

‎src/megatron/bridge/models/hf_pretrained/base.py‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,22 @@ def save_artifacts(
189189
for name in self.OPTIONAL_ARTIFACTS:
190190
artifact = getattr(self, name, None)
191191
if artifact is not None and hasattr(artifact, "save_pretrained"):
192-
artifact.save_pretrained(save_path)
192+
try:
193+
artifact.save_pretrained(save_path)
194+
except ValueError as e:
195+
# Some models (e.g. GLM-5) have generation_config.json with fields that
196+
# newer transformers versions consider invalid (e.g. top_p without do_sample).
197+
# Fall back to copying the original file directly to preserve it as-is.
198+
logger.warning(
199+
f"Could not save {name} via save_pretrained ({e}). Attempting direct file copy from source."
200+
)
201+
src_dir = getattr(self, "model_name_or_path", None)
202+
if src_dir is not None:
203+
src_file = Path(str(src_dir)) / f"{name}.json"
204+
if src_file.exists():
205+
shutil.copy(src_file, Path(save_path) / f"{name}.json")
206+
else:
207+
logger.warning(f"Source file {src_file} not found; skipping {name}.")
193208

194209
# Download/copy additional files if specified
195210
if additional_files:

0 commit comments

Comments
 (0)