Add checkpoint import and ONNX publish pipelines
Browse files- checkpoint_import_job.py +41 -0
checkpoint_import_job.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# /// script
|
| 2 |
+
# requires-python = ">=3.11"
|
| 3 |
+
# dependencies = ["huggingface-hub>=1.0", "psutil>=6"]
|
| 4 |
+
# ///
|
| 5 |
+
from __future__ import annotations
|
| 6 |
+
import argparse, json, os, shutil, time
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
from huggingface_hub import snapshot_download
|
| 9 |
+
|
| 10 |
+
def emit(p, stage, msg):
|
| 11 |
+
print(json.dumps({"event":"progress","percent":p,"stage":stage,"message":msg}), flush=True)
|
| 12 |
+
|
| 13 |
+
def main():
|
| 14 |
+
ap=argparse.ArgumentParser()
|
| 15 |
+
ap.add_argument('--repo-id', required=True)
|
| 16 |
+
ap.add_argument('--repo-type', default='model')
|
| 17 |
+
ap.add_argument('--source-prefix', default='')
|
| 18 |
+
ap.add_argument('--target-name', required=True)
|
| 19 |
+
ap.add_argument('--mode', choices=['copy','onnx-only','checkpoints-only'], default='copy')
|
| 20 |
+
ap.add_argument('--dry-run', action='store_true')
|
| 21 |
+
args=ap.parse_args()
|
| 22 |
+
token=os.environ['HF_TOKEN']
|
| 23 |
+
target=Path('/cache/checkpoints')/args.target_name
|
| 24 |
+
target.mkdir(parents=True, exist_ok=True)
|
| 25 |
+
emit(5,'inspect',f'Inspecting {args.repo_id}')
|
| 26 |
+
patterns=[]
|
| 27 |
+
if args.source_prefix: patterns.append(args.source_prefix.rstrip('/')+'/**')
|
| 28 |
+
if args.mode=='onnx-only': patterns += ['**/*.onnx','**/*.onnx_data','**/onnx/**','**/exports/**']
|
| 29 |
+
elif args.mode=='checkpoints-only': patterns += ['runs/**','checkpoints/**','checkpoint-*/**','**/*.safetensors','**/*.bin','**/config.json']
|
| 30 |
+
if args.dry_run:
|
| 31 |
+
(target/'import_plan.json').write_text(json.dumps(vars(args), indent=2))
|
| 32 |
+
emit(100,'completed',f'Dry-run import plan stored at {target}')
|
| 33 |
+
return
|
| 34 |
+
emit(20,'download','Streaming repository files into the cache bucket')
|
| 35 |
+
snapshot_download(repo_id=args.repo_id, repo_type=args.repo_type, token=token, local_dir=target, allow_patterns=patterns or None)
|
| 36 |
+
manifest=[]
|
| 37 |
+
for path in target.rglob('*'):
|
| 38 |
+
if path.is_file(): manifest.append({'path':str(path.relative_to(target)),'size':path.stat().st_size})
|
| 39 |
+
(target/'checkpoint_manifest.json').write_text(json.dumps({'source':args.repo_id,'files':manifest}, indent=2))
|
| 40 |
+
emit(100,'completed',f'Imported {len(manifest)} files into checkpoint {args.target_name}')
|
| 41 |
+
if __name__=='__main__': main()
|