Repository navigation
Suspend instead of blocking cooperative threads while weights load - #579
Conversation
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.
|
@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. |
|
Hey @aleroot! Yes, that's the goal. The two contributor-facing labels will be |
Sounds good, they give a bit of color which is always good 😀. |
davidkoski
left a comment
There was a problem hiding this comment.
Change looks good. Once mlx-swift is released with ml-explore/mlx-swift#462 we can do followup work to use that instead.
- 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.
…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.
…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.
Proposed changes
Follow-up to a review comment on #575: the
loadWeightsAPI is synchronous and blocks its thread on file I/O andDispatchQueue.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
asyncoverload ofloadWeightsthat 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
loadWeightsfrom an async context will now resolve to the async overload and needs to addawait.Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes