|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import argparse |
| 3 | +import os |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +from huggingface_hub import snapshot_download |
| 7 | +from transformers import AutoConfig, AutoTokenizer |
| 8 | + |
| 9 | +from fms_acceleration_moe.utils import recover_safetensors_from_dcp |
| 10 | + |
| 11 | + |
| 12 | +HF_CACHE = "/workspace/.hf" |
| 13 | +os.environ.setdefault("HF_HOME", HF_CACHE) |
| 14 | + |
| 15 | + |
| 16 | +def has_weights(p: Path) -> bool: |
| 17 | + return ( |
| 18 | + (p / "model.safetensors").exists() |
| 19 | + or (p / "model.safetensors.index.json").exists() |
| 20 | + or any(p.glob("model-*.safetensors")) |
| 21 | + ) |
| 22 | + |
| 23 | + |
| 24 | +def get_base_model(model_id_or_path: str, allow_download: bool) -> Path: |
| 25 | + p = Path(model_id_or_path) |
| 26 | + |
| 27 | + if p.exists(): |
| 28 | + if not has_weights(p): |
| 29 | + raise RuntimeError(f"No base weights found in {p}") |
| 30 | + return p.resolve() |
| 31 | + |
| 32 | + if not allow_download: |
| 33 | + raise RuntimeError("Base model not found locally and downloads disabled") |
| 34 | + |
| 35 | + local_dir = snapshot_download( |
| 36 | + repo_id=model_id_or_path, |
| 37 | + allow_patterns=[ |
| 38 | + "config.json", |
| 39 | + "model*.safetensors", |
| 40 | + "tokenizer*", |
| 41 | + "special_tokens_map.json", |
| 42 | + "generation_config.json", |
| 43 | + ], |
| 44 | + ) |
| 45 | + |
| 46 | + local_dir = Path(local_dir).resolve() |
| 47 | + if not has_weights(local_dir): |
| 48 | + raise RuntimeError(f"Downloaded base model but weights missing in {local_dir}") |
| 49 | + |
| 50 | + return local_dir |
| 51 | + |
| 52 | + |
| 53 | +def main(): |
| 54 | + ap = argparse.ArgumentParser() |
| 55 | + ap.add_argument("--dcp_checkpoint_dir", required=True, type=Path) |
| 56 | + ap.add_argument("--pretrained_model_name_or_path", required=True) |
| 57 | + ap.add_argument("--output_dir", required=True, type=Path) |
| 58 | + ap.add_argument("--allow_model_download", action="store_true") |
| 59 | + ap.add_argument( |
| 60 | + "--additional_special_tokens", |
| 61 | + nargs="*", |
| 62 | + default=[], |
| 63 | + ) |
| 64 | + ap.add_argument("--chat_template", type=str, default=None) |
| 65 | + args = ap.parse_args() |
| 66 | + |
| 67 | + args.output_dir.mkdir(parents=True, exist_ok=True) |
| 68 | + |
| 69 | + # base model (local snapshot) |
| 70 | + base_model_dir = get_base_model( |
| 71 | + args.pretrained_model_name_or_path, |
| 72 | + args.allow_model_download, |
| 73 | + ) |
| 74 | + |
| 75 | + # dcp to hf compatible |
| 76 | + recover_safetensors_from_dcp( |
| 77 | + str(args.dcp_checkpoint_dir), |
| 78 | + str(base_model_dir), |
| 79 | + str(args.output_dir), |
| 80 | + ) |
| 81 | + |
| 82 | + # tokenizer chat_template plus additional tokens |
| 83 | + tokenizer = AutoTokenizer.from_pretrained(args.pretrained_model_name_or_path) |
| 84 | + if args.chat_template is not None: |
| 85 | + tokenizer.chat_template = args.chat_template |
| 86 | + if args.additional_special_tokens: |
| 87 | + tokenizer.add_special_tokens( |
| 88 | + {"additional_special_tokens": args.additional_special_tokens} |
| 89 | + ) |
| 90 | + tokenizer.save_pretrained(args.output_dir) |
| 91 | + |
| 92 | + config = AutoConfig.from_pretrained(base_model_dir) |
| 93 | + config.vocab_size = len(tokenizer) |
| 94 | + config.save_pretrained(args.output_dir) |
| 95 | + |
| 96 | + print(f"[OK] HF checkpoint written to {args.output_dir}") |
| 97 | + print(f"[OK] vocab_size = {len(tokenizer)}") |
| 98 | + |
| 99 | + |
| 100 | +if __name__ == "__main__": |
| 101 | + main() |
| 102 | + |
0 commit comments