Clarus reads text out of screenshots using PaddleOCR models, run through
tract, a pure-Rust inference engine. Getting that
working involved one piece of surgery I never explained to myself: the official model files
would not load until I deleted a field called value_info out of them.
I wrote the workaround down and moved on. That is the part worth examining, because a workaround that works is a place you stop looking. The note was not a dead end. It was a bug report I had already half written and never finished.
Starting properly
The first job was to check the problem still existed. I was pinned to tract 0.20.7 and the current release is 0.23.5, so it could easily have been fixed already. I downloaded the official model from PaddlePaddle’s own HuggingFace repository, untouched, and loaded it with the current version.
Failed analyse for node #168 "Conv.0" ConvHir
1: Applying rule outputs[0].shape == ...,(DynamicDimension.1+1)/2
2: Unifying shapes ...,1+(DynamicDimension.1)/2
and ...,(DynamicDimension.1+1)/2
3: Impossible to unify Add([Val(1), Div(Sym(DynamicDimension.1), 2)])
with Div(Add([Sym(DynamicDimension.1), Val(1)]), 2).
Two expressions for the output width of one convolution. tract had computed
1 + W/2. The file declared (W+1)/2. Those differ for every even
W.
The surprising half
The obvious reading is that the file is wrong, because a library thousands of people use is more trustworthy than one exported model. So I checked instead of assuming.
The convolution carries its own attributes in the file: kernel 3×3, stride 2, padding
1. The ONNX specification gives the output size as
floor((W + pad + pad - kernel) / stride) + 1, which for those values is
floor((W-1)/2) + 1. At a width of 320 that is 160.
The file said 160. tract said 161. The model was right.
Four theories, all wrong
This is the part worth keeping, because every one of these was plausible and each had to be killed by an experiment rather than by thinking harder about it.
- “tract’s convolution shape rule is broken.” I read it.
It builds
(W + pad + pad - kernel) / stride + 1, which is exactly the spec. So I built that same expression by hand using tract’s own symbolic maths and evaluated it at seven widths. Correct every time. Dead. - “The simplifier rewrites it wrongly.” Plausible, because
(W-1)/2 + 1turning intoW/2 + 1is exactly the kind of rewrite that is valid for real numbers and invalid for integers. But tract simplifies it to(W+1)/2, which is right. Dead. - “The parser silently drops input it cannot handle.” I fed it
"W + 1 THIS IS COMPLETE GARBAGE"expecting it to quietly returnW+1. It refused, correctly. Dead. - “It is a parity bug, so odd widths will work.” A neat theory with an obvious test. It failed at every width, odd and even. Dead.
Being wrong four times is not the failure here. Each attempt eliminated something and narrowed where the fault could be, which is what left only one place still standing.
The answer
The tool that exports these models writes the width as a text expression:
floor(DynamicDimension.1/2 - 1/2) + 1
The 1/2 is one half. The floor() wrapped around it exists
precisely because the arithmetic inside is fractional. tract has to parse that string, and it
evaluates 1/2 using integer division.
parse_tdim("1/2") -> 0
parse_tdim("W/2 - 1/2") -> W/2
parse_tdim("floor(W/2 - 1/2) + 1") -> 1 + W/2
The - 1/2 becomes - 0 and vanishes. No error, no warning. The
expression simply stops being the one written in the file, and the shape it produces is off by
one for every even width.
That also explains why deleting value_info fixes it. That field is where the
exporter records those expressions. Removing it repairs nothing: it deletes the correct
information, leaving tract’s own answer, which was right all along, with nothing left to
contradict it.
What I sent
A report has to be small enough that a maintainer can act on it in a few minutes, so a 21 MB model was the wrong attachment. I built a synthetic file holding a single convolution and one declared shape, which reproduces the identical error in 763 bytes. Then a version with no model at all: four lines calling the parser directly.
One more check before sending, because claiming a library computes wrong shapes is a serious accusation. I ran the model at seven widths and compared tract’s actual output against the specification. Correct at every one. The engine is fine. The only wrong number is the one it reads out of the file, which is a much smaller claim and a much more accurate one.
It is issue #2724.
A workaround is an unanswered question you have agreed to stop asking.
Mine sat in a notes file for months, phrased as a solution. It took an afternoon to turn it into an explanation, and most of that was being wrong in an organised way.