Return non-float outputs instead of crashing (int64, int32, float16)

Merged
TO
1d ago
+331-69

linml run crashed with NullReferenceException on any model with an output that isn't float32, after ONNX Runtime had already run it. The model-zoo measured run found it (#81): bertsquad-12 has an int64 output (unique_ids:0) and failed on every provider, including CPU.

Cause

OnnxInferenceSession.Run called AsTensor<float>() on every output. For any other element type it returns null, and the next line dereferences it. A Debug-build run of the new test puts the exception at the tensor.Dimensions line.

Fix

A small OutputTensorReader reads each output by its own element type:

  • float32, float16 (widened), int64 and int32 are returned, and dataType names the tensor's real type.
  • Integer outputs are returned only from -16,777,216 to 16,777,216. A value outside that range fails with an error naming the output and the value.
  • Any other type (double, bool, string, int8/uint8, sequence, map, ...) fails with an error naming the output and the type.

TensorOutput.Data stays IReadOnlyList<float>, and the JSON field names don't change.

Trade-off to check. LinML.Contracts ships on NuGet, so I didn't change the element type of Data. The cost is that integer outputs beyond ±16,777,216 are rejected, including values float32 happens to hold exactly, because JSON can still write them rounded (2^27 is written as 134217730, which a consumer reading doubles takes as a different integer). Every integer inside the range was checked to survive float32 and the JSON text. Returning the full int64 range would be a contract change, worth its own PR.

docs/diagnostics-schema.md describes this behaviour.

Tests

  • New fixture tests/fixtures/typed-outputs.onnx, built by scripts/fetch-test-fixture.sh. The existing model.onnx regenerates byte-identical.
  • 12 new Runtime.Onnx tests and 1 CLI test (run --provider cpu --require-provider --json).
  • Before the fix, the session test and the CLI test both failed with the NullReferenceException.
  • Full suite on WSL2 with an RTX 5070 at the first commit: 515 passed, 0 failed, build with 0 warnings. After the integer-range change, the Runtime.Onnx suite (64) and the CLI output and exit-code tests pass locally; CI runs the rest.

Not in this PR

  • Without --require-provider, an exception from the run is caught and reported only as All providers failed at execution, which hides messages like the new ones (InferencePipeline.cs).
  • The input side has the matching limit and does not report it: input data is parsed as float, so an int64 input of 16777217 is silently read as 16777216.
  • compile --allow-partial on TensorRT caches an artifact ORT can't load, and labels it passthrough.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Model outputs now support float32, float16, int32, and int64 values. Output data is provided as float32 while retaining its original type.
  • Bug Fixes
    • Runs now report an output-specific error when an output type is unsupported or an integer cannot be represented exactly as float32.
  • Documentation
    • Clarified output type and conversion behavior, including how large, exactly representable values may appear in exponent notation.