Repository navigation
Improve session creation time further - #34009
Open
Christian Bourjau (cbourjau) wants to merge 2 commits into
Open
Christian Bourjau (cbourjau) wants to merge 2 commits into
Christian Bourjau (cbourjau) wants to merge 2 commits into
Conversation
Signed-off-by: Christian Bourjau <christian.bourjau@quantco.com>
Signed-off-by: Christian Bourjau <christian.bourjau@quantco.com>
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues block the hash-based lookup and constant-deduplication changes.
No open issues 🎉
What changed in this PR
Improves session creation scalability by replacing two remaining linear-scan bottlenecks with hash-based lookups, building on #33407 and #33939.
Changes:
- Precomputes graph input, output, and implicit-input name sets for node mapping.
- Deduplicates constants using keys containing data type, shape, and raw bytes.
- Adds a regression test verifying that equal constants share initializers while differing values or shapes remain separate.
No actionable findings were identified.
| File | Description |
|---|---|
onnxruntime/test/optimizer/graph_transform_test.cc |
Tests constant sharing across equal and differing values and shapes. |
onnxruntime/core/optimizer/constant_sharing.cc |
Replaces constant-value scans with direct hash-map lookups. |
onnxruntime/core/framework/session_state_utils.cc |
Uses name sets to accelerate graph input/output membership checks. |
Review effort: Balanced
Rui Ren (rui-ren)
approved these changes
Nov 9, 2026
This branch has not been deployed
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.
Description
#33407 and #33939 removed several superlinear costs from session creation. With those in place, two more become visible. Both are fixed by replacing linear scans with hash-based lookups, which also shortens the code.
Number of graph inputs/outputs
SaveInputOutputNamesToNodeMappingchecks every node input against the list of graph inputs, and every node output against the list of graph outputs, by linear scan and string comparison. Session creation is therefore O(nodes × graph inputs/outputs). The fix builds the name sets once per call. This applies at every graph optimization level.(
ninputs →nNegnodes →noutputs,ORT_DISABLE_ALL)Number of distinct constants
ConstantSharingfinds the first equal initializer by scanning all distinct values seen so far, so it is O(constants × distinct constants). The fix keys a hash map by data type, shape, and raw bytes. The new code is shorted at the expense of less readable tails on the key name, but those are never read anyhow. Which initializers are shared, and which one is kept, is unchanged.ConstantSharingcost before(
nnodesMul(x, c_i)with distinctc_i,ORT_ENABLE_BASIC; cost = session creation with minus withoutConstantSharing)A new test,
ConstantSharing_ShareOnlyEqualValuesAndShapes, checks that initializers are shared exactly when their shapes and values are equal.Motivation and Context
Hitting these corner cases is admittedly rare. We use ONNX Runtime to JIT-compile NumPy/pandas code. This requires fast session creation and may generate graphs with many inputs/outputs and many distinct literal constants. I can add the benchmark scripts, but I didn't want to add clutter unnecessarily.