Skip to content

[tmva][sofie] Fix reading of ONNX external weight data - #23251

Merged
guitargeek merged 1 commit into
root-project:masterfrom
guitargeek:sofie-onnx-external-data
Sep 4, 2026
Merged

[tmva][sofie] Fix reading of ONNX external weight data#23251
guitargeek merged 1 commit into
root-project:masterfrom
guitargeek:sofie-onnx-external-data

Conversation

@guitargeek

@guitargeek guitargeek commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

The external-data reading in the ONNX parser had two problems:

  • The data file name was resolved once per parser instance: it defaulted to .onnx.data on the first Parse call and was never reset, and the ifstream stayed open across calls. Reusing one parser instance for several models with external data silently read every subsequent model's weights at the stored offsets of the first model's data file, producing garbage weights (or short reads leaving malloc'ed buffers uninitialized).

  • The "location" key of a tensor's external_data, which per the ONNX spec names the data file relative to the model directory, was ignored entirely, so files whose data location does not follow the .onnx.data convention could not be read at all.

Resolve the data file per tensor: an explicitly set file (SetExternalDataFile) takes precedence, then the tensor's stored location relative to the model directory, then the conventional .onnx.data. Track which file is open, and reset the whole external-data state after each Parse call.

This surfaced with models exported by the torch.export-based ONNX exporter of PyTorch 2.x, which stores initializers externally.

Add a unit test covering the resolution of the external data file (stored location relative to the model directory, per-Parse-call default name, SetExternalDataFile precedence) and the reset of the state between Parse calls. The test hand-writes the minimal ONNX protobuf wire format, so it does not depend on the onnx Python package.

🤖 Done with the help of AI

The external-data reading in the ONNX parser had two problems:

- The data file name was resolved once per parser instance: it defaulted
  to <model>.onnx.data on the first Parse call and was never reset, and
  the ifstream stayed open across calls.  Reusing one parser instance
  for several models with external data silently read every subsequent
  model's weights at the stored offsets of the *first* model's data
  file, producing garbage weights (or short reads leaving malloc'ed
  buffers uninitialized).

- The "location" key of a tensor's external_data, which per the ONNX
  spec names the data file relative to the model directory, was ignored
  entirely, so files whose data location does not follow the
  <model>.onnx.data convention could not be read at all.

Resolve the data file per tensor: an explicitly set file
(SetExternalDataFile) takes precedence, then the tensor's stored
location relative to the model directory, then the conventional
<model>.onnx.data.  Track which file is open, and reset the whole
external-data state after each Parse call.

This surfaced with models exported by the torch.export-based ONNX
exporter of PyTorch 2.x, which stores initializers externally.

Add a unit test covering the resolution of the external data file
(stored location relative to the model directory, per-Parse-call default
name, SetExternalDataFile precedence) and the reset of the state between
Parse calls.  The test hand-writes the minimal ONNX protobuf wire
format, so it does not depend on the onnx Python package.

🤖 Done with the help of AI
@github-actions

github-actions Bot commented Sep 4, 2026

Copy link
Copy Markdown

Test Results

    22 files      22 suites   3d 11h 17m 10s ⏱️
 3 870 tests  3 868 ✅ 0 💤 2 ❌
75 967 runs  75 959 ✅ 6 💤 2 ❌

For more details on these failures, see this check.

Results for commit d45bce4.

@guitargeek
guitargeek merged commit 1c92f5e into root-project:master Sep 4, 2026
31 of 38 checks passed
@guitargeek
guitargeek deleted the sofie-onnx-external-data branch September 4, 2026 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants