Skip to content

measured · · 9 min

One CPU, one answer: what GitHub's two runners found in a bit-exact port

ocrblaze 0.1.0 matched Python bit for bit on the arm64 machine I tested on. The first parity run on GitHub's x86 and arm64 runners failed on both, for four different reasons: numpy's argsort ties on x86, OpenBLAS picking FMA or not per CPU kernel, GCC fusing multiply-adds in the arm64 wheels only, and ONNX Runtime's blocked layout on x86. How each was found, reproduced and proven across 22 forced kernel settings.

This is part 3 of the ocrblaze series. ocrblaze reproduces RapidOCR 3.10, a Python OCR pipeline, in Rust, and the rule is that its answers are equal bytes, not close. Version 0.1.0 met that rule on the machine it was built on: a Linux VM on an arm64 Mac. Then I ran the whole parity suite on GitHub's two hosted runners, one x86 and one arm64. Both went red.

None of the four causes was a bug in the port's logic. Each was a place where the Python reference gives a different answer on a different CPU, because numpy, OpenBLAS, OpenCV or ONNX Runtime picks different code at run time. A port that matches the reference on one machine is matching that machine. Version 0.2.0 picks the same code the reference would pick, on the CPU it is running on.

The setup

The Parity workflow runs parity/run.sh on two runners: ubuntu-24.04 (an Intel Xeon 6975P-C) and ubuntu-24.04-arm (a Neoverse N2). On each, the Python reference runs in its own container with every package pinned by hash: RapidOCR 3.10, ONNX Runtime 1.31.1, OpenCV 4.10, numpy 2.3.5, Pillow 13.0.0, shapely 2.2, pyclipper 1.4. Each part records what a step of the pipeline returns and compares the Rust code with it exactly.

The first run started minutes after 0.1.0 was published. Here are the first four runs, cell by cell:

figure 1 · the first four parity runs

0.2.0: whole detector on x86

x86 runs the whole model by default. Both runners pass every part, and the cpu part forces 22 kernel settings.

commit 5395e71 · run 47484109192

  • argsort (py)

    x86_64exact · 138,648 identical
    arm64exact · 138,648 identical
  • crop size

    x86_64exact · 3,000 / 3,000
    arm64exact · 3,000 / 3,000
  • warp

    x86_64exact · 2,500 / 2,500
    arm64exact · 2,500 / 2,500
  • Clipper offsets

    x86_64exact · 8,000 / 8,000
    arm64exact · 8,000 / 8,000
  • tiled map

    x86_64note · whole model used
    arm64exact · 0 px differ
  • end to end

    x86_64exact · 216 / 216
    arm64exact · 216 / 216
Play through the four runs, or pick one. Red is a difference from the Python reference, green is exact, dashed means the run stopped before that part.

Run 1 stopped at the first failing part, so it showed little. The first fix was not to the code but to the suite: every part now runs even after one fails (3119a9f). Run 2 showed the whole list. On x86: an argsort that differed, 30 crops of the wrong size, 3 warps with different pixels, and 7,851 of 8,000 polygon offsets exact. On arm64: 19 crops of the wrong size, and everything else it reached exact.

1. numpy's argsort leaves ties in a different order on x86

RapidOCR sorts the text crops by width over height with np.argsort before it batches them for the recogniser. Batches are six crops, padded to the widest in the batch, so the order decides which crops share a batch and how much padding each gets. Two crops with the same ratio are a tie, and the order of ties is whatever the sort leaves.

On arm64, numpy 2.3 uses its own introsort, which the port already replayed step for step. On x86, numpy 2.x hands the sort to Intel's x86-simd-sort when the CPU has AVX-512 (8 lanes) or AVX2 (4 lanes). Its sorting networks and compress-stores leave equal values in their own order. The failing case was an argsort of 27 values.

The fix is a lane-by-lane replay of the exact x86-simd-sort commit numpy 2.3.5 pins (6a7a01d), including the libstdc++ std::sort it falls back to when its quicksort stops making progress. The path is chosen at run time from the CPU, the way numpy's dispatch chooses it:

terminal
pub fn simd_sort_lanes() -> usize {
    #[cfg(target_arch = "x86_64")]
    {
        use std::arch::is_x86_feature_detected as has;
        if has!("avx512f") && has!("avx512cd") && has!("avx512bw") && has!("avx512dq") && has!("avx512vl") {
            return 8;
        }
        if has!("avx2") && has!("f16c") {
            return 4;
        }
    }
    0
}

That is src/xss.rs, 450 lines to reproduce the order of equal floats. It also had one bug of its own: a C++ size_t index that wraps below zero at the last store, which overflowed in Rust's debug builds until it used wrapping_add like the original.

2. OpenBLAS decides a crop's width

Each text box is cut out with RapidOCR's get_rotate_crop_image. The crop's width is the longer of two box sides, measured with np.linalg.norm on float32 points and truncated to an integer. When a side is a hair under a whole number, the last bit of the norm decides the crop's size.

numpy computes that norm with OpenBLAS's sdot, and OpenBLAS picks a kernel for the CPU at run time. Its arm64 kernels without SVE (generic ARMv8, Cortex-A, ThunderX2, Neoverse N1) accumulate x*x + y*y with a fused multiply-add: one rounding. Its SVE kernels (Neoverse N2, V1, V2, A64FX) and every x86 kernel add two rounded products: two roundings.

The Mac's Linux VM runs OpenBLAS's Neoverse N1 kernel, so 0.1.0 used FMA everywhere on arm64. GitHub's arm64 runner is a Neoverse N2, which has SVE: 19 crops off by one there. On x86, 30.

terminal
fn norm32(p: [f32; 2], q: [f32; 2]) -> f32 {
    let (a, b) = (p[0] - q[0], p[1] - q[1]);
    if sdot_fma() { b.mul_add(b, a * a) } else { a * a + b * b }.sqrt()
}

fn sdot_fma() -> bool {
    #[cfg(target_arch = "aarch64")]
    return !std::arch::is_aarch64_feature_detected!("sve");
    #[cfg(not(target_arch = "aarch64"))]
    false
}

This one matters for production. The production host is a Neoverse N1 (Graviton2) arm64 machine, which has no SVE, so its OpenBLAS uses the fused kernel. A port tested only on a newer arm64 CPU would have been wrong there, and the other way round.

3. GCC fused multiply-adds in the arm64 wheels, not the x86 ones

The x86 runner still had warps with different pixels and polygon offsets that were slightly off. Both come from compiled C++ in the reference: OpenCV for the warp, pyclipper for the offsets that grow each detected box.

The aarch64 wheels of both are built by GCC at -O3, whose default -ffp-contract=fast turns a*b + c into a single FMA instruction wherever it can. The port had matched that, because it was proven on arm64. The x86 wheels target a baseline x86-64 CPU without an FMA instruction, so the same source line rounds twice. One warp case had 90 of its 126 bytes different, by up to 20 levels. Clipper offsets: 7,851 of 8,000 exact.

The port now has one trait for every such spot in the source:

terminal
impl GccFma for f32 {
    fn gcc_fma(self, b: f32, c: f32) -> f32 {
        if cfg!(target_arch = "aarch64") {
            self.mul_add(b, c)
        } else {
            self * b + c
        }
    }
}

The aarch64 OpenCV wheel also has one more difference: it is built with Carotene, a hardware acceleration layer that takes over some 8-bit resizes with its own arithmetic. The port has that resize path too, and now uses it only on aarch64.

This one is a compile-time choice, not a run-time one, because it follows how the wheels were built rather than what the CPU can do. An arm64 Python built another way would need its own answer. The README says so under Limits.

4. ONNX Runtime pools in a different order on x86

With those three fixed (9234044), arm64 passed every part, and x86 passed everything up to the detector. The tiled detector, which sums page means by hand in ONNX Runtime's arm64 order, gave a map with 134,166 pixels different by at most 0.000486 on x86, and the end-to-end part had 213 of 216 answers identical (run 47482004454).

ONNX Runtime on x86 pools those layers in its blocked NCHWc layout. The order of additions comes from that layout, and the tiles cannot reproduce it from outside. Part 2 has the detail. The decision in 0.2.0 (5395e71): x86_64 runs the whole detector by default, at about 250 MiB more memory, and arm64 keeps the tiles. With that, both runners passed every part (run 47484109192), 216 of 216 answers on each.

Forcing every kernel the runner can run

Passing on two runners proves two CPUs. The production host is a third. So 0.2.0 added a cpu part: the reference is run once per kernel setting the runner's CPU can be forced into, and ocrblaze is checked against each.

  • OPENBLAS_CORETYPE forces OpenBLAS's kernel: Haswell, SkylakeX, Zen and so on on x86; ARMV8, CORTEXA57, NEOVERSEN1, NEOVERSEN2, A64FX and so on on arm64.
  • NPY_DISABLE_CPU_FEATURES turns off numpy's AVX-512, and then AVX2, so the x86 runner also exercises the AVX2 and introsort argsort paths.

Each setting runs 60,000 norms and 3,000 argsorts. The norm cases are not just random: most are box sides within a few float32 steps of a whole length, where the kernels' roundings show. On the ocrblaze side, the test reads which kernel the reference reported and checks the norms against the fused or the two-rounding formula to match.

figure 2 · one CPU, one answer, per kernel
Parity per forced kernel: 22 settings, each 60,000 of 60,000 norms and 3,000 of 3,000 argsorts identical to the Python reference.
runnerforced settingkernel chosensdotnormsargsort pathargsorts
x86_64defaultSkylakeXtwo roundings60,000/60,000x86-simd-sort, AVX-5123,000/3,000
x86_64CooperlakeSkylakeXtwo roundings60,000/60,000x86-simd-sort, AVX-5123,000/3,000
x86_64HaswellHaswelltwo roundings60,000/60,000x86-simd-sort, AVX-5123,000/3,000
x86_64NehalemNehalemtwo roundings60,000/60,000x86-simd-sort, AVX-5123,000/3,000
x86_64PrescottKatmaitwo roundings60,000/60,000x86-simd-sort, AVX-5123,000/3,000
x86_64SandybridgeSandybridgetwo roundings60,000/60,000x86-simd-sort, AVX-5123,000/3,000
x86_64SapphireRapidsSkylakeXtwo roundings60,000/60,000x86-simd-sort, AVX-5123,000/3,000
x86_64SkylakeXSkylakeXtwo roundings60,000/60,000x86-simd-sort, AVX-5123,000/3,000
x86_64ZenHaswelltwo roundings60,000/60,000x86-simd-sort, AVX-5123,000/3,000
x86_64numpy without AVX-512SkylakeXtwo roundings60,000/60,000x86-simd-sort, AVX23,000/3,000
x86_64numpy without AVX-512 or AVX2SkylakeXtwo roundings60,000/60,000numpy introsort3,000/3,000
arm64defaultneoversev2two roundings60,000/60,000numpy introsort3,000/3,000
arm64A64FXa64fxtwo roundings60,000/60,000numpy introsort3,000/3,000
arm64ARMV8armv8fma60,000/60,000numpy introsort3,000/3,000
arm64ARMV8SVEarmv8svetwo roundings60,000/60,000numpy introsort3,000/3,000
arm64CORTEXA57cortexa57fma60,000/60,000numpy introsort3,000/3,000
arm64CORTEXA72cortexa57fma60,000/60,000numpy introsort3,000/3,000
arm64NEOVERSEN1neoversen1fma60,000/60,000numpy introsort3,000/3,000
arm64NEOVERSEN2neoversev2two roundings60,000/60,000numpy introsort3,000/3,000
arm64NEOVERSEV1neoversev1two roundings60,000/60,000numpy introsort3,000/3,000
arm64THUNDERX2T99thunderx2t99fma60,000/60,000numpy introsort3,000/3,000
arm64VORTEXarmv8fma60,000/60,000numpy introsort3,000/3,000

22 settings shown, 6 of them with a fused sdot. Runners: GitHub's ubuntu-24.04 (Intel Xeon 6975P-C) and ubuntu-24.04-arm (Neoverse N2).

Every setting the runners' CPUs could be forced into. The sdot column is what decides a crop's size: one fused multiply-add on arm64 kernels without SVE, two rounded products on SVE kernels and on every x86 kernel. Note the kernel OpenBLAS actually picked: asking for Zen gives Haswell's, asking for NEOVERSEN2 gives neoversev2's. Every cell is exact.

Two things in that table surprised me. First, the kernel you ask for is not always the kernel you get: asking for Zen gives Haswell's, Prescott gives Katmai's, NEOVERSEN2 gives neoversev2's, VORTEX gives the generic armv8 one. The probe records what OpenBLAS reports, not what was asked. Second, the forced NEOVERSEN1 setting is the closest GitHub gets to the production host: it runs the real N1 kernel, on an N2 CPU.

CI was not green the whole way

The parity workflow was not the only red. On release day the regular CI on the ocrblaze repository ran 15 times, and several runs failed:

  • rustfmt diffs, more than once. A commit titled fmt is in the history.
  • A link error. The unit tests could not link ONNX Runtime statically on the runner (-lstdc++ missing). They now build with load-dynamic (c19dc48).
  • A container that would not start on x86. 0.2.0 made the whole detector the x86 default, but the image still deleted the whole detector after cutting the pieces. The contract check caught it; the image now keeps it (423437b).

The service that uses ocrblaze had two red runs of its own that day: model files saved readable only by root, and a published port left behind by a cancelled run. Both were fixed the same day.

What I'd do differently, and what's still open

  • Run the parity suite on both architectures before publishing. 0.1.0 went to crates.io before the first cross-CPU run. Its README said parity was proven on linux/arm64 and flagged amd64 and the OpenBLAS norm as unproven. The flags were right; the first claim was wider than the proof, which covered one arm64 kernel. 0.2.0 followed the same morning.
  • The real production host is still open. The cpu part forces the Neoverse N1 kernel on an N2. A full parity/run.sh e2e on the Graviton2 host itself is the last check, and it has not been run.
  • AVX2 was proven only by turning AVX-512 off, on an AVX-512 CPU. A machine that really has only AVX2 has not run the suite.
  • Other builds are not covered. The reference is numpy 2.3.5 with its bundled OpenBLAS, OpenCV and pyclipper from their official wheels. Another numpy, or one linked to another BLAS, may order ties or size a crop differently in rare cases.
  • Speed did not change. Every fix here picks a code path; none is a speed-up. The service is as fast as the Python one, not faster.

The other half of "the same answer" is matching Python's own arithmetic, which does not depend on the CPU but is just as easy to get wrong: part 4.

Working through something like this? I help teams ship AI and cloud systems that hold up, and cost what they should.