"""Weight identity for original single-file and derivative sharded checkpoints.""" import hashlib,json from pathlib import Path def file_sha256(path): result=hashlib.sha256() with Path(path).open('rb') as stream: while chunk:=stream.read(8*1024*1024):result.update(chunk) return result.hexdigest() def shard_files(path): path=Path(path);index=json.loads((path/'model.safetensors.index.json').read_text());mapping=index['weight_map'] if not mapping or not all(isinstance(k,str) and isinstance(v,str) and Path(v).name==v and v.endswith('.safetensors') for k,v in mapping.items()):raise ValueError('Invalid shard index') return index,sorted(set(mapping.values())) def checkpoint_fingerprint(path): path=Path(path) if (path/'model.safetensors').exists():return file_sha256(path/'model.safetensors') index,files=shard_files(path) record=dict(index_sha256=file_sha256(path/'model.safetensors.index.json'),shards={name:file_sha256(path/name) for name in files}) return hashlib.sha256(json.dumps(record,sort_keys=True).encode()).hexdigest()