Download train/local/grow_osr.py from AdminHugging/ppai-code: direct link, hf CLI and curl.
- Browser
- Download file 2.86 kB
-
https://huggingface.co/datasets/AdminHugging/ppai-code/resolve/main/train/local/grow_osr.py
- Command line
-
hf download hf://datasets/AdminHugging/ppai-code/train/local/grow_osr.py
-
curl -L -o grow_osr.py https://huggingface.co/datasets/AdminHugging/ppai-code/resolve/main/train/local/grow_osr.py
2.86 kB
| """Вырастить PP-OSR-H с 50M до ~75M: 10 слоёв -> 17. | |
| Формы матриц не меняются, поэтому веса первых 10 слоёв, эмбеддинги, ViT и | |
| проектор переносятся как есть; 7 новых слоёв инициализируются штатно. | |
| Получается «тёплый старт» вместо обучения с нуля. | |
| Запуск: python local/grow_osr.py | |
| """ | |
| from __future__ import annotations | |
| import os | |
| import sys | |
| import torch | |
| WS = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
| PKG = os.path.join(WS, "train", "ppai") | |
| sys.path.insert(0, PKG) | |
| OLD = os.path.join(WS, "pposr_local", "export") | |
| NEW = os.path.join(WS, "pposr_local75", "model", "base") | |
| NEW_LAYERS = 17 | |
| def main() -> None: | |
| from transformers import AutoModelForCausalLM | |
| print(f"[grow] загружаю 50M из {OLD}", flush=True) | |
| old = AutoModelForCausalLM.from_pretrained(OLD, trust_remote_code=True, torch_dtype=torch.float32) | |
| cfg = old.config | |
| old_layers = int(cfg.n_layers) | |
| print(f"[grow] было слоёв: {old_layers}, параметров " | |
| f"{sum(p.numel() for p in old.parameters())/1e6:.2f}M", flush=True) | |
| cfg.n_layers = NEW_LAYERS | |
| from modeling_ppai_vl import PPAIVLForCausalLM | |
| new = PPAIVLForCausalLM(cfg) | |
| print(f"[grow] стало слоёв: {NEW_LAYERS}, параметров " | |
| f"{sum(p.numel() for p in new.parameters())/1e6:.2f}M", flush=True) | |
| sd_old = old.state_dict() | |
| sd_new = new.state_dict() | |
| copied = 0 | |
| for k, v in sd_new.items(): | |
| if k in sd_old and sd_old[k].shape == v.shape: | |
| sd_new[k] = sd_old[k] | |
| copied += 1 | |
| new.load_state_dict(sd_new) | |
| print(f"[grow] перенесено тензоров: {copied} из {len(sd_new)}", flush=True) | |
| os.makedirs(NEW, exist_ok=True) | |
| new.save_pretrained(NEW, safe_serialization=True) | |
| # auto_map и токенизатор | |
| import json | |
| import shutil | |
| cpath = os.path.join(NEW, "config.json") | |
| conf = json.load(open(cpath, encoding="utf-8")) | |
| conf.update({"architectures": ["PPAIVLForCausalLM"], "model_type": "ppai", | |
| "auto_map": {"AutoConfig": "configuration_ppai.PPAIConfig", | |
| "AutoModelForCausalLM": "modeling_ppai_vl.PPAIVLForCausalLM"}}) | |
| json.dump(conf, open(cpath, "w", encoding="utf-8"), ensure_ascii=False, indent=2) | |
| for f in ("configuration_ppai.py", "modeling_ppai.py", "modeling_ppai_vl.py"): | |
| shutil.copy2(os.path.join(PKG, f), os.path.join(NEW, f)) | |
| for f in os.listdir(OLD): | |
| if f.startswith("tokenizer"): | |
| shutil.copy2(os.path.join(OLD, f), os.path.join(NEW, f)) | |
| print(f"[grow] готово: {NEW}", flush=True) | |
| if __name__ == "__main__": | |
| main() | |