Datasets:
Download code/tools/hash_graph.py from VEHwang/cocogni4d: direct link, hf CLI and curl.
- Browser
- Download file 2.04 kB
-
https://huggingface.co/datasets/VEHwang/cocogni4d/resolve/main/code/tools/hash_graph.py
- Command line
-
hf download hf://datasets/VEHwang/cocogni4d/code/tools/hash_graph.py
-
curl -L -o hash_graph.py https://huggingface.co/datasets/VEHwang/cocogni4d/resolve/main/code/tools/hash_graph.py
2.04 kB
| #!/usr/bin/env python3 | |
| """Read-only: sha256 of every file that must change (local paths / host names), then which files embed those digests.""" | |
| import hashlib, json, collections as C | |
| from concurrent.futures import ThreadPoolExecutor | |
| from pathlib import Path | |
| H = Path('${LOCAL}/simulation-workspace/runs/hf-release-20261120') | |
| res = json.load(open(H / 'work/scan.json')) | |
| def sha(p): | |
| h = hashlib.sha256() | |
| with open(p, 'rb') as f: | |
| for b in iter(lambda: f.read(1 << 22), b''): h.update(b) | |
| return h.hexdigest() | |
| seed = [x for x in res if x['n_abs'] or x['hosts']] | |
| with ThreadPoolExecutor(16) as ex: | |
| digs = list(ex.map(lambda x: sha(Path(x['root']) / x['rel']), seed)) | |
| d2f = {d: x['rel'] for d, x in zip(digs, seed)} | |
| changed = {x['rel'] for x in seed}; byrel = {x['rel']: x for x in res} | |
| # closure: a file embedding the digest of a changed file must change too | |
| frontier = set(d2f); rounds = 0; edges = C.defaultdict(set) | |
| while frontier: | |
| rounds += 1; new = set() | |
| for x in res: | |
| for h in x['hashes']: | |
| if h in frontier: | |
| edges[x['rel']].add(d2f[h]) | |
| if x['rel'] not in changed: new.add(x['rel']) | |
| frontier = set() | |
| if new: | |
| with ThreadPoolExecutor(16) as ex: | |
| ds = list(ex.map(lambda r: sha(Path(byrel[r]['root']) / r), sorted(new))) | |
| for r, d in zip(sorted(new), ds): d2f[d] = r; frontier.add(d) | |
| changed |= new | |
| print('round', rounds, 'new', len(new)) | |
| bin_ = [r for r in changed if not byrel[r]['text']] | |
| print('changed total', len(changed), 'binary among them', len(bin_), C.Counter(Path(r).suffix for r in bin_)) | |
| print('GB', round(sum(byrel[r]['size'] for r in changed) / 1e9, 2), 'roots', C.Counter('shm' if byrel[r]['root'].startswith('/dev') else 'disk' for r in changed)) | |
| print('files with hash edges', len(edges), 'edges', sum(map(len, edges.values()))) | |
| json.dump(dict(changed=sorted(changed), digest_of={v: k for k, v in d2f.items()}, edges={k: sorted(v) for k, v in edges.items()}), open(H / 'work/graph.json', 'w')) | |