Buckets:
| """Count model parameters from safetensors headers, without loading any weights. | |
| The count is the sum of the products of all tensor shapes, the same way the | |
| Hugging Face Hub counts parameters (vision encoder included). | |
| """ | |
| import json | |
| import math | |
| from pathlib import Path | |
| from safetensors import safe_open | |
| def weight_files(snap): | |
| """Return (present, missing) weight files of a snapshot folder.""" | |
| snap = Path(snap) | |
| index = snap / "model.safetensors.index.json" | |
| if index.exists(): | |
| names = sorted(set(json.loads(index.read_text())["weight_map"].values())) | |
| files = [snap / n for n in names] | |
| else: | |
| files = sorted(snap.glob("*.safetensors")) | |
| present = [f for f in files if f.exists()] | |
| missing = [f.name for f in files if not f.exists()] | |
| return present, missing | |
| def count_file(path): | |
| total = 0 | |
| dtypes = set() | |
| with safe_open(str(path), framework="numpy") as f: | |
| for name in f.keys(): | |
| sl = f.get_slice(name) | |
| total += math.prod(sl.get_shape()) | |
| dtypes.add(sl.get_dtype()) | |
| return total, dtypes | |
| def count_snapshot(snap): | |
| """Return (params, dtypes, n_files) for a snapshot folder. Fails if a shard is missing.""" | |
| files, missing = weight_files(snap) | |
| if missing: | |
| raise RuntimeError(f"missing shards in {snap}: {missing}") | |
| if not files: | |
| raise RuntimeError(f"no safetensors files in {snap}") | |
| total, dtypes = 0, set() | |
| for f in files: | |
| n, d = count_file(f) | |
| total += n | |
| dtypes |= d | |
| return total, sorted(dtypes), len(files) | |
Xet Storage Details
- Size:
- 1.59 kB
- Xet hash:
- c7e01a150f4b97fd90350c02aa07ee28290795fa5a1c433995f8164f95bc8b82
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.