File size: 3,340 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
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
# SPDX-License-Identifier: Apache-2.0
# Copyright 2026 Kale
"""float32 one-file web graph from the release graph (16 Nov): the release graph stores weights int8 (DequantizeLinear on initializers)
and fp16 (Cast on initializers); activations are already float32. This folds both into float32 initializers (the same weight values the
int8 graph computes with) and, as release-draft/runtimes/onnxruntime-web/make_web_graph.py does, keeps the sine source's phase path in
float32 (onnxruntime-web's WebGPU build has no float->double Cast). Inputs, outputs and names unchanged.
usage: python make_fp32_graph.py <model.onnx> <model.fp32.onnx>"""
import sys
import numpy as np, onnx
from onnx import TensorProto, numpy_helper as nh
src, dst = sys.argv[1:3]
m = onnx.load(src); g = m.graph
init = {i.name: i for i in g.initializer}
consumers = {}
for n in g.node:
    for x in n.input: consumers[x] = consumers.get(x, 0) + 1
keep, new_init, folded = [], {}, {"DequantizeLinear": 0, "Cast": 0}
for n in g.node:
    val = lambda x: new_init[x] if x in new_init else nh.to_array(init[x])  # noqa: E731  (scales are fp16 -> Cast -> float, folded first)
    if n.op_type == "DequantizeLinear" and all(x in init or x in new_init for x in n.input if x):
        q = val(n.input[0]).astype(np.float32); s = val(n.input[1]).astype(np.float32)
        z = val(n.input[2]).astype(np.float32) if len(n.input) > 2 and n.input[2] else np.zeros_like(s)
        ax = next((a.i for a in n.attribute if a.name == "axis"), 1)
        if s.ndim == 1 and s.size > 1:
            shp = [1] * q.ndim; shp[ax] = -1; s = s.reshape(shp); z = z.reshape(shp)
        new_init[n.output[0]] = (q - z) * s; folded["DequantizeLinear"] += 1; continue
    if n.op_type == "Cast" and n.input[0] in init and init[n.input[0]].data_type == TensorProto.FLOAT16:
        to = next(a.i for a in n.attribute if a.name == "to")
        if to == TensorProto.FLOAT:
            new_init[n.output[0]] = nh.to_array(init[n.input[0]]).astype(np.float32); folded["Cast"] += 1; continue
    keep.append(n)
used = {x for n in keep for x in n.input} | {o.name for o in g.output}
inits = [i for i in g.initializer if i.name in used] + [nh.from_array(v.astype(np.float32), k) for k, v in new_init.items() if k in used]
del g.node[:]; g.node.extend(keep); del g.initializer[:]; g.initializer.extend(inits)
dbl = []
for n in g.node:   # phase path float64 -> float32 (WebGPU build)
    if n.op_type == "Cast":
        for a in n.attribute:
            if a.name == "to" and a.i == TensorProto.DOUBLE: a.i = TensorProto.FLOAT; dbl.append(n.name)
    if n.op_type in ("Constant", "ConstantOfShape"):
        for a in n.attribute:
            if a.name == "value" and a.t.data_type == TensorProto.DOUBLE:
                a.t.CopyFrom(nh.from_array(nh.to_array(a.t).astype("float32"), a.t.name)); dbl.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
for i in g.initializer:
    assert i.data_type != TensorProto.DOUBLE
sys.path.insert(0, str(__import__('pathlib').Path(__file__).resolve().parent)); from toposort import toposort; toposort(g)
onnx.checker.check_model(m); onnx.save(m, dst)
print("folded", folded, "double->float", dbl, "initializer dtypes", sorted({i.data_type for i in g.initializer}))