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