Skip to content

add parallelism to prepacking/weight loading - #31691

Merged
Xavier Dupré (xadupre) merged 43 commits into
mainfrom
xadupre/loadpara
Sep 17, 2026
Merged

Xavier Dupré (xadupre) merged 43 commits into
mainfrom
xadupre/loadpara

Conversation

@xadupre

@xadupre Xavier Dupré (xadupre) commented Aug 6, 2026 •

Copy link
Copy Markdown
Member

Description

Prepacking was not fully parallelized during session initialization. Memory-mapped weights were retrieved sequentially before each kernel processed them, so data retrieval did not overlap with the expensive PrePack() work.

This PR:

  • dispatches eligible CPU OpKernel::PrePack() calls across the session intra-op thread pool;
  • synchronizes shared bookkeeping while leaving the expensive per-kernel packing work concurrent;
  • avoids nested parallelism in MatMulNBits;
  • keeps cross-session shared prepacked-weight caching on its existing serialized path;
  • enables outer prepack parallelism in all Android variants, including basic minimal builds.

Performance

Measured with a ModelBuilder-generated Qwen3.6-27B model quantized to INT4 on an Intel Xeon Platinum 8480C.

Before:

-- start: 2026-08-06 11:16:48.388195
---- end: 2026-08-06 11:17:32.072674
loading time: 43.68443649681285
-- start: 2026-08-06 11:17:32.072778
---- end: 2026-08-06 11:18:16.123237
loading time: 44.0504179620184
-- start: 2026-08-06 11:18:16.123356
---- end: 2026-08-06 11:19:00.205266
loading time: 44.08187505789101

After:

-- start: 2026-08-06 14:56:41.478934
---- end: 2026-08-06 14:56:48.648836
loading time: 7.169875857885927
-- start: 2026-08-06 14:56:48.648966
---- end: 2026-08-06 14:56:57.359275
loading time: 8.710286310408264
-- start: 2026-08-06 14:56:57.359358
---- end: 2026-08-06 14:57:05.959221
loading time: 8.599843401927501

Android minimal binary size

With the outer prepack path included, the GitHub Actions Android minimal baseline on the current merge commit measured 1,594,365 bytes. Its threshold is raised from 1,590,272 to 1,595,392 bytes, retaining approximately 1 KiB of headroom. The independent Azure MinimalBaseline threshold remains unchanged at 1,306,224 bytes pending an Azure-specific artifact measurement.

Validation

  • Built onnxruntime_test_all.
  • Ran SessionStateTestSharedInitalizersWithPrePacking.ParallelPrepackConvertsExceptionsToStatus.
  • Ran lintrunner on all changed files.

@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>
@xadupre

Copy link
Copy Markdown
Member Author

Copilot fix lint

@xadupre Xavier Dupré (xadupre) changed the title add parallelism to prepacking add parallelism to prepacking/weight loading Aug 6, 2026
@xadupre

Copy link
Copy Markdown
Member Author

Copilot fix lint

@xadupre

Copy link
Copy Markdown
Member Author

Copilot fix lint again

@xadupre

Copy link
Copy Markdown
Member Author

Copilot resolve the merge conflicts in this pull request

# Conflicts:
#	onnxruntime/core/framework/session_state.cc

Co-authored-by: xadupre <22452781+xadupre@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings August 24, 2026 07:40

Copilot AI commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Copilot resolve the merge conflicts in this pull request

Resolved in 7a47e69 — merged origin/main and fixed the conflict in onnxruntime/core/framework/session_state.cc (a ReplaceWithReferenceIfSaving → DiscardAndReplaceWithReferenceIfSaving rename from main that conflicted with the unrelated parallel-prepacking changes here).

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

Adds opt-in parallel weight prepacking during session initialization to reduce model-loading latency.

Changes:

  • Introduces session.prepack.enable_parallel.
  • Dispatches per-node prepacking through the intra-op thread pool.
  • Adds MatMulNBits coverage for the option.

Reviewed changes

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

File Description
onnxruntime/core/framework/session_state.cc Implements parallel prepacking and synchronization.
include/onnxruntime/core/session/onnxruntime_session_options_config_keys.h Defines and documents the option.
onnxruntime/test/contrib_ops/matmul_4bits_test.cc Adds option-based MatMulNBits tests.

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread onnxruntime/core/framework/session_state.cc Outdated
Comment thread onnxruntime/core/framework/session_state.cc Outdated
Comment thread onnxruntime/core/framework/session_state.cc Outdated
Comment thread onnxruntime/test/contrib_ops/matmul_4bits_test.cc
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

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

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Comment thread onnxruntime/core/framework/session_state.cc Outdated
Comment thread onnxruntime/core/framework/session_state.cc Outdated
Comment thread onnxruntime/contrib_ops/cpu/quantization/matmul_nbits.cc Outdated
Co-authored-by: xadupre <22452781+xadupre@users.noreply.github.com>
This was referenced Oct 13, 2026
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.

5 participants