File size: 1,514 Bytes
f21d6eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# SPDX-License-Identifier: Apache-2.0
# Copyright 2026 Kale
"""float32-only copy of a kale-tts-1 graph for onnxruntime-web's WebGPU build (and so transformers.js in the browser).
The one-file graph keeps the sine source's phase sum (CumSum) in float64. The plain wasm build runs that, but the WebGPU/JSEP build of
onnxruntime-web >= 1.31 ships no float->double Cast ("Could not find an implementation for Cast(13) node '/g/head/Cast'"), and
transformers.js always loads that build. This rewrites the three Casts so the phase path stays float32, and keeps the graph otherwise as is.
usage: python make_web_graph.py <model.onnx> <out.onnx>"""
import sys, onnx
from onnx import TensorProto, numpy_helper
src, dst = sys.argv[1:3]
m = onnx.load(src); g = m.graph; changed = []
for n in g.node:
    if n.op_type == "Cast":
        for a in n.attribute:
            if a.name == "to" and a.i == TensorProto.DOUBLE: a.i = TensorProto.FLOAT; changed.append(n.name)
    if n.op_type in ("Constant", "ConstantOfShape"):   # double constants on the phase path -> float
        for a in n.attribute:
            if a.name == "value" and a.t.data_type == TensorProto.DOUBLE:
                a.t.CopyFrom(numpy_helper.from_array(numpy_helper.to_array(a.t).astype("float32"), a.t.name)); changed.append(n.name)
for vi in list(g.value_info):
    if vi.type.tensor_type.elem_type == TensorProto.DOUBLE: vi.type.tensor_type.elem_type = TensorProto.FLOAT
onnx.checker.check_model(m); onnx.save(m, dst); print("float32 now:", changed)