Upload 08_reshard.py
Browse files- 08_reshard.py +73 -0
08_reshard.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
08_reshard.py — Teilt die externe Datendatei des Decoders in <=1.9 GB grosse
|
| 4 |
+
Chunks auf (Chromium 2-GB-ArrayBuffer-Limit / Transformers.js Multi-Chunk).
|
| 5 |
+
|
| 6 |
+
Chunk-Namensschema (belegt via onnx-community/Phi-4-mini-instruct-web-q4f16
|
| 7 |
+
und ORT-Web-Fehlermeldung in transformers.js Issue #1460):
|
| 8 |
+
Chunk 0 -> <base>.onnx_data (ohne Suffix)
|
| 9 |
+
Chunk 1 -> <base>.onnx_data_1
|
| 10 |
+
Chunk k -> <base>.onnx_data_k
|
| 11 |
+
config.json:
|
| 12 |
+
"use_external_data_format": {"decoder_model_merged_q4f16.onnx": <n_chunks>}
|
| 13 |
+
|
| 14 |
+
Verifiziert am Ende den Ladevorgang mit onnxruntime (nicht onnx.checker,
|
| 15 |
+
der bei >2 GB an protobuf scheitert).
|
| 16 |
+
|
| 17 |
+
Start: python3.13 08_reshard.py
|
| 18 |
+
"""
|
| 19 |
+
import os, onnx, onnxruntime as ort
|
| 20 |
+
from onnx import TensorProto
|
| 21 |
+
|
| 22 |
+
SRC_DIR = "/root/train/gemma4-bund-final-v3/onnx"
|
| 23 |
+
IN = os.path.join(SRC_DIR, "decoder_model_merged_q4f16.onnx")
|
| 24 |
+
OUT_DIR = "/root/train/gemma4-bund-reshard"
|
| 25 |
+
BASE = "decoder_model_merged_q4f16.onnx_data"
|
| 26 |
+
CAP = 1_900_000_000 # 1.9 GB Sicherheitsgrenze unter 2^31
|
| 27 |
+
|
| 28 |
+
os.makedirs(OUT_DIR, exist_ok=True)
|
| 29 |
+
|
| 30 |
+
def cname(i):
|
| 31 |
+
return BASE if i == 0 else f"{BASE}_{i}"
|
| 32 |
+
|
| 33 |
+
print("=== A) Modell laden (inkl. externer Daten)", flush=True)
|
| 34 |
+
m = onnx.load(IN, load_external_data=True)
|
| 35 |
+
|
| 36 |
+
print("=== B) Tensoren auf Chunks verteilen", flush=True)
|
| 37 |
+
idx, off, n = 0, 0, 0
|
| 38 |
+
f = open(os.path.join(OUT_DIR, cname(0)), "wb")
|
| 39 |
+
for t in m.graph.initializer:
|
| 40 |
+
if not t.raw_data: # kleine inline-Tensoren bleiben im Graph
|
| 41 |
+
continue
|
| 42 |
+
b = t.raw_data
|
| 43 |
+
sz = len(b)
|
| 44 |
+
if off + sz > CAP and off > 0:
|
| 45 |
+
f.close(); idx += 1; off = 0
|
| 46 |
+
f = open(os.path.join(OUT_DIR, cname(idx)), "wb")
|
| 47 |
+
f.write(b)
|
| 48 |
+
t.ClearField("raw_data")
|
| 49 |
+
t.data_location = TensorProto.EXTERNAL
|
| 50 |
+
del t.external_data[:]
|
| 51 |
+
for k, v in (("location", cname(idx)), ("offset", str(off)), ("length", str(sz))):
|
| 52 |
+
e = t.external_data.add(); e.key = k; e.value = v
|
| 53 |
+
off += sz; n += 1
|
| 54 |
+
f.close()
|
| 55 |
+
n_chunks = idx + 1
|
| 56 |
+
print(f" externalisierte Tensoren: {n}, Chunks: {n_chunks}", flush=True)
|
| 57 |
+
|
| 58 |
+
print("=== C) Graph speichern (Pointer, ohne Daten neu zu schreiben)", flush=True)
|
| 59 |
+
out_onnx = os.path.join(OUT_DIR, "decoder_model_merged_q4f16.onnx")
|
| 60 |
+
onnx.save(m, out_onnx, save_as_external_data=False)
|
| 61 |
+
for i in range(n_chunks):
|
| 62 |
+
p = os.path.join(OUT_DIR, cname(i))
|
| 63 |
+
print(f" {cname(i)}: {os.path.getsize(p)/1e9:.2f} GB", flush=True)
|
| 64 |
+
|
| 65 |
+
print("=== D) Verifikation: Laden mit onnxruntime (CPU)", flush=True)
|
| 66 |
+
sess = ort.InferenceSession(out_onnx, ort.SessionOptions(),
|
| 67 |
+
providers=["CPUExecutionProvider"])
|
| 68 |
+
print(f" OK — Session erstellt, Inputs total: {len(sess.get_inputs())}", flush=True)
|
| 69 |
+
|
| 70 |
+
print("\n=== FERTIG. config.json-Eintrag:", flush=True)
|
| 71 |
+
print(f' "use_external_data_format": {{"decoder_model_merged_q4f16.onnx": {n_chunks}}}', flush=True)
|
| 72 |
+
print("Naechste Schritte: embed_tokens_q4f16.* unveraendert dazukopieren, "
|
| 73 |
+
"alles nach HF, dann Bundesrechner-WebGPU-Test.", flush=True)
|