Download bundle.py from openmodelai/Terra-2.5: direct link, hf CLI and curl.
- Browser
- Download file 3.23 kB
-
https://huggingface.co/openmodelai/Terra-2.5/resolve/main/bundle.py
- Command line
-
hf download hf://openmodelai/Terra-2.5/bundle.py
-
curl -L -o bundle.py https://huggingface.co/openmodelai/Terra-2.5/resolve/main/bundle.py
3.23 kB
| """One self-contained HF ZIP; preserve weight bytes, omit optimizer statistics explicitly.""" | |
| import argparse | |
| import json | |
| from pathlib import Path | |
| import shutil | |
| import tempfile | |
| import torch | |
| from scripts.archive import pack, sha256 | |
| ROOT = Path(__file__).resolve().parents[1] | |
| HF_FILES = ('config.json', 'generation_config.json', 'model.safetensors', | |
| 'tokenizer.json', 'tokenizer_config.json', 'special_tokens_map.json', 'README.md') | |
| def make_bundle(source, destination, repo=ROOT): | |
| source, destination, repo = Path(source).resolve(), Path(destination).resolve(), Path(repo).resolve() | |
| if destination.exists() or destination.is_relative_to(source): | |
| raise ValueError('Choose a new ZIP outside the source checkpoint') | |
| meta = json.loads((source / 'training_meta.json').read_text()) | |
| if meta.get('bundle_export'): | |
| raise ValueError('Do not re-bundle an already bundled checkpoint') | |
| destination.parent.mkdir(parents=True, exist_ok=True) | |
| with tempfile.TemporaryDirectory(prefix='.terra-bundle-', dir=destination.parent) as tmp: | |
| staging = Path(tmp) | |
| for name in HF_FILES: | |
| # Copy without conversion/re-saving: exact BF16 bytes remain intact. | |
| shutil.copyfile(source / name, staging / name) | |
| if (source / 'verification.json').exists(): | |
| shutil.copyfile(source / 'verification.json', staging / 'verification.json') | |
| state = torch.load(source / 'training_state.pt', map_location='cpu', weights_only=True) | |
| if state['step'] != meta['step']: | |
| raise ValueError('Checkpoint step mismatch') | |
| state['optimizer']['state'] = {} | |
| torch.save(state, staging / 'training_state.pt') | |
| meta['bundle_export'] = { | |
| 'source_model_sha256': sha256(source / 'model.safetensors'), | |
| 'source_training_state_sha256': sha256(source / 'training_state.pt'), | |
| 'weight_bytes_unchanged': True, 'additional_lossy_conversion': False, | |
| 'optimizer_statistics_included': False, 'optimizer_restart_required': True, | |
| 'bit_exact_resume': False, | |
| 'note': 'Weights unchanged; optimizer statistics omitted. RNG, groups and step preserved.'} | |
| (staging / 'training_meta.json').write_text(json.dumps(meta, ensure_ascii=False, indent=2) + '\n') | |
| training = staging / 'training' | |
| training.mkdir() | |
| for directory in ('terra', 'scripts', 'configs', 'data', 'tests', 'docs'): | |
| shutil.copytree(repo / directory, training / directory, | |
| ignore=shutil.ignore_patterns('__pycache__', '*.pyc', '*.log')) | |
| for name in ('requirements.txt', 'CHAT_LOG.md', 'CURRENT_MODEL.json'): | |
| shutil.copyfile(repo / name, training / name) | |
| shutil.copyfile(repo / 'docs/BUNDLE_START_HERE.md', staging / 'START_HERE.md') | |
| return pack(staging, destination) | |
| def main(): | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument('source') | |
| parser.add_argument('destination') | |
| args = parser.parse_args() | |
| print(json.dumps({'zip': args.destination, 'sha256': make_bundle(args.source, args.destination)}, indent=2), flush=True) | |
| if __name__ == '__main__': | |
| main() | |