Skip to content

Fix segfault in MatMulNBits prepacking when weights/scales are parent-graph initializers in subgraph - #31141

Open
tianleiwu with Copilot wants to merge 5 commits into
mainfrom
copilot/fix-segfault-matmulnbits
Open

Fix segfault in MatMulNBits prepacking when weights/scales are parent-graph initializers in subgraph#31141
tianleiwu with Copilot wants to merge 5 commits into
mainfrom
copilot/fix-segfault-matmulnbits

Conversation

Copilot AI commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Description

When a MatMulNBits node lives inside a subgraph (e.g. If branch) and its quantized weights B and scales are initializers of the enclosing graph, TryGetConstantInput fails to resolve those tensors during kernel construction and PrePack. On ARM64 with accuracy_level=4 (KleidiAI path), this causes a nullptr dereference → segfault at session initialization.

Root cause: KernelRegistryManager::CreateKernel builds OpKernelInfo using session_state.GetConstantInitializedTensors(), which only contains the current subgraph's constants. Parent-scope initializers are absent, so TryGetConstantInput(scales) silently returns null, and SQ4BitGemmPackQuantBDataAndBlkSum asserts QuantBScaleBegin != nullptr.

Changes:

  • session_state.h — adds GetConstantInitializedTensorsForKernelCreation() method + two private fields (outer_scope_augmented_constant_tensors_, outer_scope_augmented_map_built_).

  • session_state.cc — in FinalizeSessionStateImpl, between CleanInitializedTensorsFromGraph() and CreateKernels(), builds an augmented constant-tensor map for subgraph session states by iterating parent_node->ImplicitInputDefs() and re-indexing each parent-scope constant into the current subgraph's OrtValueNameIdxMap. The parent's own augmented map is consulted (already built, since parent finalizes before child), so arbitrary nesting depth is handled correctly. This map is intentionally not used by PrepackConstantInitializedTensors to avoid double-prepacking outer-scope tensors.

  • kernel_registry_manager.cc — one-line change: GetConstantInitializedTensors()GetConstantInitializedTensorsForKernelCreation() in CreateKernel.

  • matmul_4bits_test.cc — regression test MatMulNBits.SubgraphParentScopeInitializers that constructs the exact topology from the bug report (If node, MatMulNBits in both branches, B/scales as parent-graph initializers, accuracy_level=4) and asserts session initialization and inference succeed.

Motivation and Context

On ARM64, ORT segfaults during session initialization when loading a model with a MatMulNBits node inside a subgraph whose quantized weights and scales are initializers of the parent graph with accuracy_level=4. Reported against v1.28.0 on macOS/ARM64 and reproducible with any model following this pattern (e.g. decoder models with quantized weights shared across If branches).

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

… initializers in subgraph

Co-authored-by: tianleiwu <30328909+tianleiwu@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix segfault in MatMulNBits prepacking with parent graph initializers Fix segfault in MatMulNBits prepacking when weights/scales are parent-graph initializers in subgraph Jul 29, 2026
Copilot AI requested a review from tianleiwu July 29, 2026 16:42
@hariharans29
hariharans29 requested a review from Copilot July 29, 2026 18:28

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a session-initialization crash when a MatMulNBits node lives inside a control-flow subgraph (e.g., If branch) but its quantized weights/scales are initializers in an enclosing (parent) graph scope. The core change ensures kernel construction (and prepacking) can resolve those parent-scope constant initializers when building OpKernelInfo.

Changes:

  • Add SessionState::GetConstantInitializedTensorsForKernelCreation() and subgraph-only bookkeeping to expose a kernel-creation constant map augmented with parent-scope initializers.
  • Build the augmented constant map for subgraph SessionState instances during FinalizeSessionStateImpl before kernel creation.
  • Use the augmented map in KernelRegistryManager::CreateKernel, plus add a regression test for the reported If-subgraph topology.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
onnxruntime/test/contrib_ops/matmul_4bits_test.cc Adds regression test covering MatMulNBits inside If with parent-scope Bq/Bs initializers and accuracy_level=4.
onnxruntime/core/framework/session_state.h Declares new kernel-creation constant map accessor and stores subgraph augmented-map state.
onnxruntime/core/framework/session_state.cc Implements the accessor and builds the subgraph augmented constant map prior to CreateKernels().
onnxruntime/core/framework/kernel_registry_manager.cc Switches kernel creation to use the new kernel-creation constant tensor map.

Comment thread onnxruntime/core/framework/session_state.cc
Comment thread onnxruntime/test/contrib_ops/matmul_4bits_test.cc
…s post-finalization

After all subgraphs are finalized, erase the parent-scope OrtValue copies from
outer_scope_augmented_constant_tensors_ (tracked in outer_scope_parent_only_indices_).
This allows the parent's constant_initialized_tensors_ to free underlying tensor buffers
once all prepack use counts reach zero, avoiding the memory regression the reviewer
identified where copied OrtValues would hold extra shared_ptr references preventing
memory release after PrepackConstantInitializedTensors.

The copies are retained until after the subgraph finalization loop so that nested
(grandchild) session states can still read them via GetConstantInitializedTensorsForKernelCreation()
when building their own augmented maps.
@tianleiwu
tianleiwu marked this pull request as ready for review July 30, 2026 05:42

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can commit the suggested changes from lintrunner.

Comment thread onnxruntime/core/framework/session_state.cc Outdated
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Segfault in MatMulNBits prepacking when weights and scales are initializers of parent graph

4 participants