llm_logits / scripts /verify_index_tensors.py
Your Name
fixed safetensors.py; moved scripts into scripts/
f413f92
#!/usr/bin/env python3
import json, safetensors, os, sys, tqdm
index_filename, = sys.argv[1:]
with open(index_filename) as fh:
index = json.load(fh)
weight_map = index['weight_map']
filenames = set(weight_map.values())
total_size = sum([os.stat(fn).st_size for fn in filenames])
PASS = True
if total_size != index['metadata']['total_size']:
PASS = False
print(f"Wrong total_size in index. stored={index['metadata']['total_size']} correct={total_size}")
for filename in tqdm.tqdm(filenames, desc=index_filename, unit='f'):
f = safetensors.safe_open(filename, framework='pt')
index_keys = set([k for k, fn in weight_map.items() if fn == filename])
file_keys = set(f.keys())
if file_keys != index_keys:
PASS = False
for in_file in (file_keys - index_keys):
print()
print('{in_file} present in {filename} but not {index_filename}')
print()
for in_index in (index_keys - file_keys):
print()
print('{in_file} present in {index_filename} but not {index_filename}')
print()
if PASS:
print('index content matches safetensors content.')
sys.exit(0)
else:
print('index content MISMSATCHES safetensors content.')
sys.exit(-1)