kale-tts-1-tiny / recipe /export /make_onefile.py
NERDDISCO's picture
kale-tts-1
f21d6eb
Raw History Blame Contribute Delete
7.11 kB
# SPDX-License-Identifier: Apache-2.0
# Copyright 2026 Kale
"""release one-file graphs (16 Nov) in today's release layout from the per-voice / voice-id training exports, so every runtime
keeps working unchanged:
nano: r2 one-file with `sid` -> inputs input_ids, style [1,256] (unused; kept for Kokoro-shaped callers, added as 0 * sum(style)), speed,
voice_prosody [1,32] (= rt.pstyle[sid]), voice_prosody_col [1,32,1] (the same values as a column), voice_sound [1,16] (= ra.style[sid]);
node names get the "/g" prefix the release graph had (WebGPU forceCpuNodeNames lists / transformers.js snippet).
tiny: the per-voice k-<v> graphs differ only in kdec.style [1,16] -> one graph with input voice_sound [1,16].
Voice files: voices/<id>.bin = vp-<id>.f32 ([510,256] style table, row = tokens - 1), voices/<id>.extra.bin = the voice inputs in
voices.json order. voices.json = the current release one (ids, names, order, accent), voice_inputs rewritten.
usage: python make_onefile.py <nano|tiny> <bundle dir> <current release model dir (tokenizer, config)> <out dir> [--voices voices.json]
[--nano file] [--tiny "**/k-{v}.int8dec.onnx"] [--packs "**/vp-{v}.f32"] (defaults = the training exports' layout)"""
import argparse, glob, json, shutil, sys
from pathlib import Path
import numpy as np, onnx
from onnx import helper as H, TensorProto as TP, numpy_helper as nh
ap = argparse.ArgumentParser()
ap.add_argument("size", choices=["nano", "tiny"]); ap.add_argument("bundle"); ap.add_argument("cur"); ap.add_argument("out")
ap.add_argument("--voices", help="voices.json (ids, names, order, accent); default: <cur>/voices.json")
ap.add_argument("--nano", help="nano one-file export with a sid input; default: **/r2.int8dec.onnx in the bundle (export.json next to it)")
ap.add_argument("--tiny", default="**/k-{v}.int8dec.onnx", help="per-voice tiny export pattern under the bundle ({v} = voice id)")
ap.add_argument("--packs", default="**/vp-{v}.f32", help="per-voice style table [510,256] float32 pattern under the bundle")
ap.add_argument("--tiny-base", help="tiny: convert THIS graph (e.g. the Ettin text side build, one voice baked in) instead of the first per-voice export;"
" the per-voice decoder rows still come from --tiny")
ap.add_argument("--row", default="kdec.style", help="tiny: name of the base graph's [1,16] decoder voice row (Ettin build: sty)")
a = ap.parse_args()
size, B, cur, out = a.size, Path(a.bundle), Path(a.cur), Path(a.out)
def find(pat, v=None):
hits = sorted(glob.glob(str(B / pat.format(v=v)), recursive=True))
if not hits: raise SystemExit(f"not found in {B}: {pat.format(v=v)}")
return Path(hits[0])
out.mkdir(parents=True); (out / "voices").mkdir()
for f in ("tokenizer.json", "tokenizer_config.json", "config.json"):
if (cur / f).exists(): shutil.copy(cur / f, out / f)
vj = json.loads(Path(a.voices or cur / "voices.json").read_text()); vj.pop("_note", None); ids = [v["id"] for v in vj["voices"]]
def arr(g, name):
i = {x.name: x for x in g.initializer}
return nh.to_array(i[name]).astype(np.float32) if name in i else nh.to_array(i[name + "_16"]).astype(np.float32)
if size == "nano":
nf = Path(a.nano) if a.nano else find("**/r2.int8dec.onnx")
order = json.loads((nf.parent / "export.json").read_text())["voices"]; assert set(ids) <= set(order), (ids, order)
m = onnx.load(nf); g = m.graph
ps, rs = arr(g, "rt.pstyle"), arr(g, "ra.style") # [8,32], [8,16] in export order
gp = next(n for n in g.node if n.op_type == "Gather" and n.input[1] == "sid" and n.input[0] == "rt.pstyle")
gs = next(n for n in g.node if n.op_type == "Gather" and n.input[1] == "sid" and n.input[0] == "ra.style")
un = [n for n in g.node if gp.output[0] in n.input and n.op_type == "Unsqueeze"]
ren = {gp.output[0]: "voice_prosody", gs.output[0]: "voice_sound"}
drop = {gp.name, gs.name}
for u in un: ren[u.output[0]] = "voice_prosody_col"; drop.add(u.name)
keep = [n for n in g.node if n.name not in drop]
for n in keep: n.input[:] = [ren.get(x, x) for x in n.input]
g.ClearField("node"); g.node.extend(keep)
ins = [i for i in g.input if i.name != "sid"]; g.ClearField("input"); g.input.extend(ins)
g.input.insert(1, H.make_tensor_value_info("style", TP.FLOAT, [1, 256]))
g.input.extend([H.make_tensor_value_info("voice_prosody", TP.FLOAT, [1, 32]), H.make_tensor_value_info("voice_prosody_col", TP.FLOAT, [1, 32, 1]),
H.make_tensor_value_info("voice_sound", TP.FLOAT, [1, 16])])
for n in g.node: # waveform = head + 0 * sum(style), as the release graph
n.output[:] = ["waveform_head" if o == "waveform" else o for o in n.output]
g.initializer.append(nh.from_array(np.zeros(1, np.float32), "onefile/zero"))
g.node.extend([H.make_node("ReduceSum", ["style"], ["onefile/ssum"], keepdims=0, name="onefile/ReduceSum"),
H.make_node("Mul", ["onefile/ssum", "onefile/zero"], ["onefile/s0"], name="onefile/Mul"),
H.make_node("Add", ["waveform_head", "onefile/s0"], ["waveform"], name="onefile/Add")])
vr = {o: "/g" + o for n in g.node for o in n.output if o.startswith("/") and not o.startswith("/g/")} # node AND value names, as the release graph
for n in g.node:
if n.name.startswith("/") and not n.name.startswith("/g/"): n.name = "/g" + n.name
n.input[:] = [vr.get(x, x) for x in n.input]; n.output[:] = [vr.get(x, x) for x in n.output]
for vi in g.value_info: vi.name = vr.get(vi.name, vi.name)
layout = [{"name": "voice_prosody", "dims": [1, 32]}, {"name": "voice_prosody_col", "dims": [1, 32, 1]}, {"name": "voice_sound", "dims": [1, 16]}]
extra = {v: np.concatenate([ps[order.index(v)], ps[order.index(v)], rs[order.index(v)]]) for v in ids}
else:
m = onnx.load(a.tiny_base or find(a.tiny, ids[0])); g = m.graph
extra = {v: arr(onnx.load(find(a.tiny, v)).graph, "kdec.style").ravel() for v in ids}
inits = [i for i in g.initializer if i.name not in (a.row, a.row + "_16")]; g.ClearField("initializer"); g.initializer.extend(inits)
for n in g.node:
n.input[:] = ["voice_sound" if x == a.row else x for x in n.input]
g.input.append(H.make_tensor_value_info("voice_sound", TP.FLOAT, [1, 16]))
layout = [{"name": "voice_sound", "dims": [1, 16]}]
sys.path.insert(0, str(Path(__file__).resolve().parent)); from toposort import toposort, prune_initializers
toposort(g); prune_initializers(g); onnx.checker.check_model(m); onnx.save(m, out / "model.onnx")
for v in ids:
vp = np.fromfile(find(a.packs, v), np.float32); assert vp.size == 510 * 256, (v, vp.size)
vp.tofile(out / f"voices/{v}.bin"); extra[v].astype(np.float32).tofile(out / f"voices/{v}.extra.bin")
vj["voice_inputs"] = layout
for v in vj["voices"]: v.pop("pitch_floor_hz", None)
(out / "voices.json").write_text(json.dumps(vj, indent=1, ensure_ascii=False))
print(size, "->", out / "model.onnx", "inputs", [i.name for i in g.input], "extra", {v: extra[v].size for v in ids[:1]})