Skip to content

Suspend instead of blocking cooperative threads while weights load - #579

Merged
davidkoski merged 2 commits into
ml-explore:mainfrom
aleroot:async_load_weights
Sep 1, 2026
Merged

davidkoski merged 2 commits into
ml-explore:mainfrom
aleroot:async_load_weights

Conversation

@aleroot

@aleroot aleroot commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

Proposed changes

Follow-up to a review comment on #575: the loadWeights API is synchronous and blocks its thread on file I/O and DispatchQueue.concurrentPerform, but every model factory calls it from an async context. That parks a Swift concurrency cooperative thread for the whole load, which is an anti-pattern and a potential source of thread starvation, since the cooperative pool only has one thread per core and expects them to never block.

This PR adds an async overload of loadWeights that hops to a global GCD queue and suspends the caller instead of blocking it. The four async model factories (LLM, VLM, embedders, MTP drafter) now use it. The synchronous overload is unchanged and remains for synchronous callers such as model conversion.

No performance change.

Note: downstream code that calls loadWeights from an async context will now resolve to the async overload and needs to add await.

Checklist

Put an x in the boxes that apply.

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

The Load API is synchronous and blocks its thread on file I/O and
DispatchQueue.concurrentPerform, but every model factory calls it from
an async context, parking a Swift concurrency cooperative thread for
the whole load. Add an async loadWeights overload that hops to a global
queue and suspends the caller, and use it from the factories. The
synchronous overload remains for synchronous callers such as convert.
The new async loadWeights overload made three DocC symbol links
ambiguous, failing the docs verification in CI. Point each link at the
intended overload: the async one (-8b333) from porting.md, which
describes the inference path, and the synchronous one (-6eqw7) from the
model-conversion docs and the async overload's own doc comment.
@aleroot

aleroot commented Aug 27, 2026

Copy link
Copy Markdown
Contributor Author

@thechriswebb just to understand how this labelling system is working, when you mark something as needs-changes does it mean I need to make some changes to meet the gate ? Thanks.

@thechriswebb

Copy link
Copy Markdown
Collaborator

Hey @aleroot! Yes, that's the goal. The two contributor-facing labels will be needs-changes and needs-lint whereas the others are more maintainer-facing labels that help us keep track of PR state. It's something I'm working on now, so would love to hear any feedback you have.

@aleroot

aleroot commented Aug 27, 2026

Copy link
Copy Markdown
Contributor Author

Hey @aleroot! Yes, that's the goal. The two contributor-facing labels will be needs-changes and needs-lint whereas the others are more maintainer-facing labels that help us keep track of PR state. It's something I'm working on now, so would love to hear any feedback you have.

Sounds good, they give a bit of color which is always good 😀.

@davidkoski davidkoski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks great, thank you!

@davidkoski davidkoski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Change looks good. Once mlx-swift is released with ml-explore/mlx-swift#462 we can do followup work to use that instead.

@davidkoski
davidkoski merged commit 1c1b257 into ml-explore:main Sep 1, 2026
3 checks passed
thechriswebb added a commit that referenced this pull request Sep 3, 2026
- Adds `await` to the two `loadWeights` calls in `Gemma4AssistantDraftModelIntegrationTests.swift`, both inside `async throws` test functions.
- Adds `await` to the `loadWeights` call in `loadTargetAndDrafter` in `MTPIteratorEndToEndDiagnosticTests.swift`.
- Adds `await` to the two `loadWeights` calls in `loadRung4Drafter` in `MTPRung4TokenParityTests.swift`.

#579 added an async `loadWeights` overload next to the existing synchronous one. Swift prefers the async overload inside an async function, so a plain `try loadWeights(...)` in an async function now needs `await`. These five calls sit inside async functions but never awaited. None compiled. The package's CI never builds IntegrationTesting. The only job that builds it, `integration_build_xcode27`, is disabled with `if: false`, so #579 merged despite breaking this project.
djandrewgibbons pushed a commit to djandrewgibbons/mlx-swift-lm that referenced this pull request Sep 26, 2026
…l-explore#579)

* Suspend instead of blocking cooperative threads while weights load

The Load API is synchronous and blocks its thread on file I/O and
DispatchQueue.concurrentPerform, but every model factory calls it from
an async context, parking a Swift concurrency cooperative thread for
the whole load. Add an async loadWeights overload that hops to a global
queue and suspends the caller, and use it from the factories. The
synchronous overload remains for synchronous callers such as convert.

* Disambiguate loadWeights doc links now that an async overload exists

The new async loadWeights overload made three DocC symbol links
ambiguous, failing the docs verification in CI. Point each link at the
intended overload: the async one (-8b333) from porting.md, which
describes the inference path, and the synchronous one (-6eqw7) from the
model-conversion docs and the async overload's own doc comment.
djandrewgibbons pushed a commit to djandrewgibbons/mlx-swift-lm that referenced this pull request Sep 26, 2026
…e#605)

- Adds `await` to the two `loadWeights` calls in `Gemma4AssistantDraftModelIntegrationTests.swift`, both inside `async throws` test functions.
- Adds `await` to the `loadWeights` call in `loadTargetAndDrafter` in `MTPIteratorEndToEndDiagnosticTests.swift`.
- Adds `await` to the two `loadWeights` calls in `loadRung4Drafter` in `MTPRung4TokenParityTests.swift`.

ml-explore#579 added an async `loadWeights` overload next to the existing synchronous one. Swift prefers the async overload inside an async function, so a plain `try loadWeights(...)` in an async function now needs `await`. These five calls sit inside async functions but never awaited. None compiled. The package's CI never builds IntegrationTesting. The only job that builds it, `integration_build_xcode27`, is disabled with `if: false`, so ml-explore#579 merged despite breaking this project.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants