# SPDX-License-Identifier: Apache-2.0 # Copyright 2026 Kale """keep chosen layers of an int8 graph in float32 (16 Nov, nano): for every int8 weight (DequantizeLinear) whose consumer node name matches one of the patterns, use the float32 reference graph's value of that weight instead (same name; checked shape-equal). usage: python keep_fp32.py [ ...]""" import re, sys, collections import numpy as np, onnx from onnx import numpy_helper as nh src, ref, dst, pats = sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4:] m = onnx.load(src); g = m.graph; RI = {i.name: i for i in onnx.load(ref).graph.initializer} cons = collections.defaultdict(list) for n in g.node: for x in n.input: cons[x].append(n) def users(x, depth=0): out = [] for c in cons[x]: out.append(c.name) if c.op_type == "Identity" and depth < 3: out += users(c.output[0], depth + 1) return out keep, add, kept = [], [], [] for n in g.node: if n.op_type == "DequantizeLinear" and any(re.search(p, u) for u in users(n.output[0]) for p in pats): w = nh.to_array(RI[n.output[0]]).astype(np.float32); add.append(nh.from_array(w, n.output[0])); kept.append((n.output[0], w.size)); continue keep.append(n) g.ClearField("node"); g.node.extend(keep); g.initializer.extend(add) sys.path.insert(0, __import__("os").path.dirname(__file__)); from toposort import toposort, prune_initializers toposort(g); prune_initializers(g); onnx.checker.check_model(m); onnx.save(m, dst) print("kept in float32:", len(kept), "weights,", sum(s for _, s in kept), "values (+", round(sum(s for _, s in kept) * 3 / 1e6, 2), "MB vs int8)")