Skip to content

fix: add guard to aoti package loader - #205

Merged
mattwittwer merged 2 commits into
mainfrom
mwittwer/pytorch_aoti_loader_guard
Aug 20, 2026
Merged

mattwittwer merged 2 commits into
mainfrom
mwittwer/pytorch_aoti_loader_guard

Conversation

@mattwittwer

@mattwittwer mattwittwer commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

What does the PR do?

Serializes torch::inductor::AOTIModelPackageLoader construction in the pt2 path behind a process-wide mutex.

Checklist

  • PR title reflects the change and is of format <commit_type>: <Title>
  • Changes are described in the pull request.
  • Related issues are referenced.
  • Populated github labels field
  • Added test plan and verified test passes.
  • Verified that the PR passes existing CI.
  • Verified copyright is correct on all changed files.
  • Added succinct git squash message before merging ref.
  • All template sections are filled out.
  • Optional: Additional screenshots for behavior/output changes with before/after.

Commit Type:

Check the conventional commit type
box here and add the label to the github PR.

  • build
  • ci
  • docs
  • feat
  • fix
  • perf
  • refactor
  • revert
  • style
  • test

Related PRs:

Where should the reviewer start?

Test plan:

  • CI Pipeline ID:

62435323

Caveats:

Background

Related Issues: (use one of the action keywords Closes / Fixes / Resolves / Relates to)

  • closes GitHub issue: #xxx

@greptile-apps

greptile-apps Bot commented Aug 13, 2026 •

Copy link
Copy Markdown

Greptile Summary

The PR serializes AOTI package-loader construction to prevent concurrent access to libtorch’s shared JSON parser state.

  • Adds a process-wide mutex for package-loader construction.
  • Limits the critical section to TorchModelLoader construction for both CPU and GPU models.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
src/pt2/model_state.cc Adds a narrowly scoped constructor lock that addresses concurrent metadata parsing without serializing subsequent call-spec processing.

Reviews (2): Last reviewed commit: "narrow mutex scope" | Re-trigger Greptile

Comment thread src/pt2/model_state.cc Outdated
@mattwittwer mattwittwer self-assigned this Aug 13, 2026
@mattwittwer mattwittwer changed the title draft: add guard around aoti package loader fix: add guard around aoti package loader Aug 17, 2026
@mattwittwer mattwittwer changed the title fix: add guard around aoti package loader fix: add guard to aoti package loader Aug 17, 2026
@mattwittwer
mattwittwer requested review from whoisj and yinggeh August 17, 2026 16:31
@whoisj

whoisj commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

Can only a single model be loaded safely at a time?

@mattwittwer

Copy link
Copy Markdown
Contributor Author

Can only a single model be loaded safely at a time?

@whoisj - Good clarification, model loading overall is still parallel. The mutex covers only the construction of torch::inductor::AOTIModelPackageLoader. Non-AOTI models are unaffected, and for AOTI models everything after construction (call-spec parsing, instance setup) runs outside the lock.

The issue is in libtorch here: torch/csrc/inductor/aoti_package/model_package_loader.cpp this parses each package's metadata into a function-local static nlohmann::json and returns a reference to it, so two loaders constructing concurrently corrupt each other's view.

@mattwittwer
mattwittwer merged commit 62e07f1 into main Aug 20, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants