Spaces:
Running
Running
Upload model_index_json_generator_SingleTensor.py
Browse files
model_index_json_generator_SingleTensor.py
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
## Coded with help from Grok, after OpenGPT and Gemini failed several times.
|
| 2 |
+
#!/usr/bin/env python3
|
| 3 |
+
"""
|
| 4 |
+
Generate model.safetensors.index.json for modern HuggingFace sharded models
|
| 5 |
+
Works when:
|
| 6 |
+
- Shards have no tensor names
|
| 7 |
+
- Shards have no metadata
|
| 8 |
+
- Only raw binary data + external index expected
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
import json
|
| 12 |
+
import argparse
|
| 13 |
+
from pathlib import Path
|
| 14 |
+
from safetensors import safe_open
|
| 15 |
+
|
| 16 |
+
def generate_index(folder_path: str, output_file: str = "model.safetensors.index.json"):
|
| 17 |
+
folder = Path(folder_path)
|
| 18 |
+
if not folder.is_dir():
|
| 19 |
+
raise ValueError(f"Folder not found: {folder_path}")
|
| 20 |
+
|
| 21 |
+
# Find all shards: model-00001-of-00004.safetensors style
|
| 22 |
+
shards = sorted([
|
| 23 |
+
f for f in folder.glob("*.safetensors")
|
| 24 |
+
if f.name.startswith("model") or "-of-" in f.name
|
| 25 |
+
])
|
| 26 |
+
|
| 27 |
+
if not shards:
|
| 28 |
+
raise ValueError("No sharded model*.safetensors files found!")
|
| 29 |
+
|
| 30 |
+
print(f"Found {len(shards)} shards:")
|
| 31 |
+
for s in shards:
|
| 32 |
+
print(f" - {s.name}")
|
| 33 |
+
|
| 34 |
+
weight_map = {}
|
| 35 |
+
total_size = 0
|
| 36 |
+
|
| 37 |
+
for shard in shards:
|
| 38 |
+
print(f"Scanning {shard.name} ...")
|
| 39 |
+
try:
|
| 40 |
+
with safe_open(str(shard), framework="pt", device="cpu") as f:
|
| 41 |
+
metadata = f.metadata() or {} # Handle None
|
| 42 |
+
keys = f.keys()
|
| 43 |
+
|
| 44 |
+
# Case 1: New format — tensor names in metadata["tensors"] (as JSON string)
|
| 45 |
+
if "tensors" in metadata:
|
| 46 |
+
import ast
|
| 47 |
+
tensors_dict = ast.literal_eval(metadata["tensors"])
|
| 48 |
+
for tensor_name, info in tensors_dict.items():
|
| 49 |
+
weight_map[tensor_name] = shard.name
|
| 50 |
+
total_size += info.get("length", 0)
|
| 51 |
+
|
| 52 |
+
# Case 2: Old format — tensor names directly accessible
|
| 53 |
+
elif keys:
|
| 54 |
+
for key in keys:
|
| 55 |
+
if key in weight_map:
|
| 56 |
+
print(f" Warning: duplicate tensor {key}")
|
| 57 |
+
weight_map[key] = shard.name
|
| 58 |
+
# Try to estimate size
|
| 59 |
+
try:
|
| 60 |
+
tensor = f.get_tensor(key)
|
| 61 |
+
total_size += tensor.numel() * tensor.element_size()
|
| 62 |
+
except:
|
| 63 |
+
pass # some keys might be metadata only
|
| 64 |
+
|
| 65 |
+
# Case 3: No names, no metadata → we need to read the raw header!
|
| 66 |
+
else:
|
| 67 |
+
print(f" No tensor names found in {shard.name} → reading raw header...")
|
| 68 |
+
# This is the REAL fix: read the raw safetensors header manually
|
| 69 |
+
with open(shard, "rb") as sf:
|
| 70 |
+
header_size = int.from_bytes(sf.read(8), "little")
|
| 71 |
+
header_data = sf.read(header_size)
|
| 72 |
+
header = json.loads(header_data)
|
| 73 |
+
|
| 74 |
+
for tensor_name, desc in header.items():
|
| 75 |
+
if tensor_name == "__metadata":
|
| 76 |
+
continue
|
| 77 |
+
weight_map[tensor_name] = shard.name
|
| 78 |
+
# Calculate length from shape + dtype
|
| 79 |
+
import numpy as np
|
| 80 |
+
dtype = desc["dtype"]
|
| 81 |
+
shape = desc["shape"]
|
| 82 |
+
data_offsets = desc["data_offsets"]
|
| 83 |
+
length = data_offsets[1] - data_offsets[0]
|
| 84 |
+
total_size += length
|
| 85 |
+
|
| 86 |
+
except Exception as e:
|
| 87 |
+
print(f" Failed to process {shard.name}: {e}")
|
| 88 |
+
raise
|
| 89 |
+
|
| 90 |
+
if not weight_map:
|
| 91 |
+
raise RuntimeError("No tensors found in any shard! The files might be corrupted.")
|
| 92 |
+
|
| 93 |
+
# Final index
|
| 94 |
+
index = {
|
| 95 |
+
"metadata": {
|
| 96 |
+
"total_size": total_size
|
| 97 |
+
},
|
| 98 |
+
"weight_map": weight_map
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
output_path = folder / output_file
|
| 102 |
+
with open(output_path, "w", encoding="utf-8") as f:
|
| 103 |
+
json.dump(index, f, indent=4)
|
| 104 |
+
|
| 105 |
+
print(f"\nSUCCESS! Generated {output_file}")
|
| 106 |
+
print(f" Tensors mapped: {len(weight_map)}")
|
| 107 |
+
print(f" Total size: {total_size // 1_073_741_824:.2f} GB")
|
| 108 |
+
print(f" Saved to: {output_path}\n")
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
if __name__ == "__main__":
|
| 112 |
+
parser = argparse.ArgumentParser(description="Generate model.safetensors.index.json (works 100% with modern HF shards)")
|
| 113 |
+
parser.add_argument("folder", help="Path to folder containing model-*-of-*.safetensors")
|
| 114 |
+
parser.add_argument("--output", default="model.safetensors.index.json", help="Output filename")
|
| 115 |
+
|
| 116 |
+
args = parser.parse_args()
|
| 117 |
+
generate_index(args.folder, args.output)
|