tract icon indicating copy to clipboard operation
tract copied to clipboard

ghost_1_256.onnx: silent numeric divergence vs onnxruntime on real inputs (zeros match)

Open Josepavese opened this issue 2 weeks ago • 4 comments

Summary

tract-onnx 0.23.10 loads and runs the GHOST face-swap generator (ghost_1_256.onnx, from the facefusion/models-3.0.0 HF repo, Apache-2.0) without any error, but the numeric results silently diverge from onnxruntime on non-trivial inputs. With an all-zeros input, all nine declared outputs match onnxruntime to ~6 decimals; with a real image on target, the encoder outputs already diverge (first exposed output /encoder/conv7/2/LeakyRelu: mean|x| 0.0086 in ORT vs 0.0675 in tract) and the final Tanh image is garbage.

Environment

  • tract-onnx 0.23.10 (also tract-core/tract-data 0.23.10)
  • Rust 1.98.0, aarch64 (Raspberry Pi 5, Debian 13)
  • onnxruntime 1.23.x used as the reference
  • model: https://huggingface.co/facefusion/models-3.0.0/resolve/main/ghost_1_256.onnx (inputs target [1,3,256,256], source [1,512], outputs 0..8 where 0 is /generator/Tanh)

Reproduction

  1. Run the model with both engines, source = zeros:
    • zeros target + zeros source: tract == ORT on all outputs (e.g. out0 mean|x| 0.059483 vs 0.059484)
    • real target (a face crop in [-1,1]) + zeros source: ORT out0 mean|x| 0.105211, tract 0.599763 (saturated Tanh)
    • zeros target + real source: tract == ORT (0.0778 both) So the divergence is in the target (encoder) path only.
  2. I rewrote all 8 InstanceNormalization nodes into primitive ops (ReduceMean/Sub/Mul/Div/Reshape, verified identical in ORT: 0.105211) and tract still diverges with the same numbers, so InstanceNormalization is not the cause.
  3. The graph declares intermediate outputs, which makes the divergence visible at the encoder tail. Ops in the path: Conv, LeakyRelu, BatchNormalization, Reshape, Gemm, Expand, Sub, Mul, Relu, Sigmoid.

What I expected

Either a load-time rejection or numerics matching onnxruntime within float tolerance; a silent wrong result is the dangerous case.

Question

Any hint on which op to bisect next (or a known-broken op in this set on 0.23)? Happy to run further experiments; my plan was to tap every node output, but on this 2 GiB device the shape-inference pass OOMs.

Thank you for tract — apart from this it has been a pleasure to embed.

Josepavese avatar Nov 20 '26 02:11 Josepavese

Hello ! Thanks for your interest.

Surprising, because the Operators you're listing here are not exotic in any way, I would expect them to be bug free by now. Good reflex on re-writing the InstanceNorm.

Tapping all output is a brute force, but maybe we can guess a bit. So my questions:

  • is this a f16 or f32 model ? (if it is f16, we need to look at gemms, conv, normalisation and everything that accumulates for overflows)
  • did you reproduce on other platforms ? (then we need to look for a broken kernel)

kali avatar Nov 20 '26 09:11 kali

Thanks for the quick reply!

  • f32: the model is all F32 (491 MB, no f16 anywhere).
  • platforms: reproduced only on aarch64 (Raspberry Pi 5, Debian 13, Rust 1.98.0) so far — my x86_64 box currently has a broken rustup cache; I can rerun there once restored, but given the f32/zero-inputs results below I would first suspect a codegen path rather than a platform kernel.

New data point that should help: I bisected with cut models (feed both engines the same intermediate activations as graph inputs, compare the final output):

  • Cutting right after /encoder/conv7/2/LeakyRelu (all boundary tensors fed from an onnxruntime dump of the full model): tract and onnxruntime agree to 6 decimals on the final Tanh output (0.109760 vs 0.109759). So the whole decoder/generator side (InstanceNorm, Resize, ConvTranspose, AAD blocks, Tanh) runs correctly in tract when given identical inputs.
  • The divergence is therefore strictly inside the encoder prefix (conv1..conv7 chain, ~77 nodes): Conv, LeakyRelu, InstanceNormalization, Gemm, Expand, Sub, Mul, Relu, Sigmoid, Reshape.
  • Notable detail: with an all-zeros target the encoder outputs match onnxruntime exactly; the divergence needs real (non-trivial) pixel data. With zeros target + real source, outputs also match — so it is not the style path either.

I can share the cut model + boundary tensors (about 400 MB) or a minimal script pair that reproduces the conv7-cut comparison, whichever is more useful to you.

Josepavese avatar Nov 20 '26 10:11 Josepavese

I tried to reproduce on x86_64 and on a Raspberry Pi 5 (Debian, aarch64), with both 0.23.10 and current main, using the tract CLI. I fed a non-trivial target in [-1,1] and a random source, and compared against onnxruntime: all 9 outputs agree within ~6e-5 max abs error, and the encoder outputs match too.

So I suspect the input tensor your Rust code builds rather than the engine. An all-zeros target would match whatever its layout or scale, and the encoder is the first thing to see it. Could you check that the tensor really is NCHW [1,3,256,256] (not HWC data reshaped), in [-1,1], with the same channel order as your ORT path?

The quickest way to separate the two: save the exact target/source you give onnxruntime, plus its outputs, into one io.npz (keys = input/output names), then run tract ghost_1_256.onnx --input-facts-from-bundle io.npz run --input-from-bundle io.npz --assert-output-bundle io.npz (or --save-outputs-npz out.npz to diff it yourself). If the CLI matches, the problem is in the embedding code; if it doesn't, please attach that npz and I'll dig in.

kali avatar Nov 20 '26 12:11 kali

I built the exact io.npz bundle from the Rust path and it resolves this: tract is not the cause.

The old Larva generator normalised an interleaved HWC crop and then handed that flat buffer to a tensor declared as [1,3,256,256] (planar CHW) without a transpose. Zero target hides the mistake; real pixels do not. I extracted the exact flat buffer the old path fed to tract, plus the correctly transposed CHW tensor, and ran both on tract-onnx 0.23.10 and onnxruntime 1.22.0 (CPU).

For the identical flat buffer:

engine out0 mean|x|
tract-onnx 0.23.10 0.753432
onnxruntime 1.22.0 CPU 0.753431

max abs difference 8.75e-5; correct CHW gives 0.5856 on both; zero target gives 0.06046 on both.

Bundle (base64 text because gists accept text files only, decode command included): https://gist.github.com/Josepavese/62da0551f9bb2990594f79fcfe3ac151

The current Larva path transposes HWC→CHW (to_chw_normalised) and uses ONNX Runtime; I had misattributed the HWC/CHW bug to the engine. The report is retracted. Thank you for the bisection hints, and sorry for the noise.

No tract code was modified; the bundle contains only synthetic deterministic inputs (no subject data).

Josepavese avatar Nov 24 '26 17:11 Josepavese