Download storage.py from xcx0902/QOJDoudizhuModel: direct link, hf CLI and curl.
- Browser
- Download file 2.09 kB
-
https://huggingface.co/xcx0902/QOJDoudizhuModel/resolve/main/storage.py
- Command line
-
hf download hf://xcx0902/QOJDoudizhuModel/storage.py
-
curl -L -o storage.py https://huggingface.co/xcx0902/QOJDoudizhuModel/resolve/main/storage.py
2.09 kB
| """Atomic portable checkpoints and input provenance.""" | |
| import hashlib | |
| import json | |
| from pathlib import Path | |
| import torch | |
| def digest(path): | |
| return hashlib.sha256(Path(path).read_bytes()).hexdigest() | |
| def json_digest(value): | |
| return hashlib.sha256(json.dumps(value, sort_keys=True).encode()).hexdigest() | |
| def teacher_hashes(teacher): | |
| return {role: digest(path) for role, path in teacher.paths.items()} | |
| def cpu_tree(value): | |
| if torch.is_tensor(value): | |
| return value.detach().cpu().clone() | |
| if isinstance(value, dict): | |
| return {key: cpu_tree(item) for key, item in value.items()} | |
| if isinstance(value, list): | |
| return [cpu_tree(item) for item in value] | |
| if isinstance(value, tuple): | |
| return tuple(cpu_tree(item) for item in value) | |
| return value | |
| def atomic_save(value, path): | |
| path = Path(path) | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| temporary = path.with_suffix(path.suffix + '.tmp') | |
| torch.save(cpu_tree(value), temporary) | |
| temporary.replace(path) | |
| def export(policy, path, metadata=None): | |
| atomic_save({'format': 2, 'model_config': policy.model.config.dict(), | |
| 'teacher_model_type': policy.teacher.model_type, | |
| 'model': policy.model.state_dict(), | |
| 'teacher_sha256': teacher_hashes(policy.teacher), | |
| 'metadata': metadata or {}}, path) | |
| def source_hashes(): | |
| root = Path(__file__).resolve().parents[1] | |
| paths = list((root / 'QOJDoudizhuModel').glob('*.py')) | |
| paths += [root / 'qojbot' / name for name in ( | |
| 'rules.py', 'game.py', 'adapter.py', 'douzero.py', 'history.py', 'model_benchmark.py', | |
| 'history_training.py', 'curriculum.py', 'policy.py')] | |
| paths += [root / 'Douzero_Resnet/douzero/env/env.py', | |
| root / 'Douzero_Resnet/douzero/dmc/models.py', | |
| root / 'douzero-resnet-2.0/Douzero_Resnet/douzero/dmc/models_res.py', | |
| root / 'douzero-resnet-2.0/Douzero_Resnet/douzero/env/env_res.py'] | |
| return {str(path.relative_to(root)): digest(path) for path in sorted(paths)} | |