[tmva][sofie] Fix reading of ONNX external weight data - #23251
Merged
guitargeek merged 1 commit intoSep 4, 2026
Conversation
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
Test Results 22 files 22 suites 3d 11h 17m 10s ⏱️ For more details on these failures, see this check. Results for commit d45bce4. |
dpiparo
approved these changes
Sep 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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