PP-OCRv6 rec_medium / det_small: denormal weights make CPU inference 4–11× slower (zeroing them gives identical output)
TL;DR: PP-OCRv6_rec_medium.onnx and PP-OCRv6_det_small.onnx (v3.9.2) contain ~200k float32 denormal weights each. Zeroing them makes CPU inference 11× faster (rec) and 4× faster (det), with identical output. Suggest flushing them to zero before publishing.
Counts (float32 initializers, 0 < |w| < 1.18e-38):
| model | denormal weights | tensors affected |
|---|---|---|
| PP-OCRv6_rec_medium | 197,046 (1.03%) | 51 |
| PP-OCRv6_det_small | 255,958 (10.43%) | 7 |
| PP-OCRv6_rec_small | 241 | 2 |
| PP-OCRv6_rec_tiny | 0 | 0 |
The published Paddle inference.pdiparams (sha256 1b01c79a… / 5043d4cc…) has exactly the same counts, so the conversion does not introduce them. They are concentrated in wide 1×1 convs, e.g. det_small conv2d_165.w_0 [384,768,1,1] is 62.8% denormal, and rec_medium conv2d_107.w_0 [512,1024,1,1] is 7.4%.
Impact (onnxruntime 1.30.0, CPUExecutionProvider, 1 thread, Intel i7-8550U):
| model | input | original | denormals zeroed | max |Δ| of output |
|---|---|---|---|---|
| rec_medium | 1×3×48×800 | 3,713 ms | 329 ms | 0 |
| det_small | 3 pages, longest side 960 px | 1,139 ms/page | 301 ms/page | 0 |
In the onnxruntime profile, Conv takes 98.6% of the rec_medium time (69% in rec_small).
onnxruntime recommends exporting models without denormals instead of relying on the runtime flag (session options config keys).
Suggested fix: flush the denormals in the conversion step:
import numpy as np, onnx
from onnx import numpy_helper
m = onnx.load("PP-OCRv6_rec_medium.onnx")
tiny = np.finfo(np.float32).tiny
for init in m.graph.initializer:
if init.data_type == onnx.TensorProto.FLOAT:
a = numpy_helper.to_array(init).copy()
n = np.count_nonzero((a != 0) & (np.abs(a) < tiny))
if n:
a[np.abs(a) < tiny] = 0
init.CopyFrom(numpy_helper.from_array(a, init.name))
onnx.save(m, "PP-OCRv6_rec_medium-ftz.onnx")Source: RapidAI/RapidOCR