Skip to content

cb2-7 (chain 7/8): scheduler-aware loadModelContainer overloads (batching front door) - #30

Open
ronaldmannak wants to merge 1 commit into
claude/cb2-6-schedulerfrom
claude/cb2-7-loader-frontdoor
Open

ronaldmannak wants to merge 1 commit into
claude/cb2-6-schedulerfrom
claude/cb2-7-loader-frontdoor

Conversation

@ronaldmannak

@ronaldmannak ronaldmannak commented Jul 4, 2026 •

Copy link
Copy Markdown
Owner

Chain 7/8 — stacked on #29 (cb2-6-scheduler). Optional convenience layer; the batching mechanism is complete without it.

What

One new self-contained file (ModelFactory+Scheduler.swift) adding overloads of loadModelContainer / loadContainer that install an InferenceScheduler (and optional cross-request prompt cache) at load time:

let container = try await loadModelContainer(
    configuration: .init(id: "mlx-community/Qwen3-4B-4bit"),
    scheduler: InferenceScheduler(),
    promptCache: LRUPromptCache(maxBytes: 2 << 30)
)

Why

Without this, the loaders always produce a scheduler-free container (_wrap → ModelContainer(context:)), and since ModelContainer.scheduler is immutable by design (that immutability keeps the zero-overhead single-stream default airtight), the only way to opt into continuous batching is to bypass the loaders and construct ModelContainer(context:scheduler:promptCache:) by hand. These overloads make the opt-in discoverable at the API users actually call.

Design constraints

  • scheduler has no default value, so these overloads never compete with the plain loaders in overload resolution — every existing call site keeps resolving to the original functions and keeps today's scheduler-free container.
  • Pure composition: each overload loads a ModelContext through the existing (trampoline-dispatched) loadModel paths and wraps it with the existing scheduler-aware ModelContainer initializer. No existing declaration changes; the ModelFactory protocol surface is untouched.
  • A scheduler passed for a VLM load is inert (generate routes VLM contexts to the single-stream path; generateBatched rejects them), so misuse is harmless.

Why a separate PR

This is an API-surface taste call — whether batching should be visible in the top-level loader signatures — and is split out so it can be accepted or declined independently of the mechanism in PRs 1–6. Declining this PR removes nothing from the batching feature itself.

Update 2026-07-13: rebased onto the audit-hardened chain; content unchanged. Note the upstream-coordination flag in reviews/continuous-batching-audit-2026-07-13.md: ml-explore/mlx-swift-lm PR ml-explore#335 deprecates ModelContainer in favor of a Sendable ModelContext, so this front door (and the container API in 6/8) should be re-confirmed against that direction before upstreaming.

🤖 Generated with Claude Code

https://claude.ai/code/session_011xThp8Po34ruijNS8TaShn

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request introduces scheduler-aware model loading overloads in ModelFactory+Scheduler.swift to enable continuous batching using InferenceScheduler. The feedback points out an incomplete code example in the documentation header that is missing the required 'from' and 'using' parameters, which would cause compilation errors if copied directly.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +11 to +17
// ```swift
// let container = try await loadModelContainer(
// configuration: .init(id: "mlx-community/Qwen3-4B-4bit"),
// scheduler: InferenceScheduler(),
// promptCache: LRUPromptCache(maxBytes: 2 << 30)
// )
// ```

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The code example in the comment is missing the required from and using parameters (from: downloader, using: tokenizerLoader). Since loadModelContainer does not have an overload that omits these parameters, copying this example as-is will result in a compilation error. Let's update the example to include them.

// ```swift
// let container = try await loadModelContainer(
//     from: downloader,
//     using: tokenizerLoader,
//     configuration: .init(id: "mlx-community/Qwen3-4B-4bit"),
//     scheduler: InferenceScheduler(),
//     promptCache: LRUPromptCache(maxBytes: 2 << 30)
// )
// ```

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Fixed. The example now includes the required from:/using: arguments with placeholder comments -- MLXLMCommon cannot reference the MLXHuggingFace convenience macros from its docs. Thanks for catching this; the copy-paste version would not have compiled.


Generated by Claude Code

@ronaldmannak
ronaldmannak force-pushed the claude/cb2-6-scheduler branch from d828eef to 7486aa0 Compare July 4, 2026 23:31
@ronaldmannak
ronaldmannak force-pushed the claude/cb2-7-loader-frontdoor branch from 9b40345 to 5ff107c Compare July 4, 2026 23:31
@ronaldmannak
ronaldmannak force-pushed the claude/cb2-6-scheduler branch from 7486aa0 to f5c37d3 Compare July 4, 2026 23:35
@ronaldmannak
ronaldmannak force-pushed the claude/cb2-7-loader-frontdoor branch from 5ff107c to d8bfc13 Compare July 4, 2026 23:35
@ronaldmannak
ronaldmannak force-pushed the claude/cb2-6-scheduler branch from f5c37d3 to 8ccb571 Compare July 6, 2026 23:58
@ronaldmannak
ronaldmannak force-pushed the claude/cb2-7-loader-frontdoor branch from d8bfc13 to 0f0daf3 Compare July 6, 2026 23:58
@ronaldmannak
ronaldmannak force-pushed the claude/cb2-6-scheduler branch from 8ccb571 to 06a44bc Compare July 7, 2026 00:13
@ronaldmannak
ronaldmannak force-pushed the claude/cb2-7-loader-frontdoor branch from 0f0daf3 to 55b17a0 Compare July 7, 2026 00:13
@ronaldmannak
ronaldmannak force-pushed the claude/cb2-6-scheduler branch from 06a44bc to a4e9c0c Compare July 7, 2026 00:19
@ronaldmannak
ronaldmannak force-pushed the claude/cb2-7-loader-frontdoor branch from 55b17a0 to 7ab7720 Compare July 7, 2026 00:19
@ronaldmannak
ronaldmannak force-pushed the claude/cb2-6-scheduler branch from a4e9c0c to cf809fc Compare July 7, 2026 06:04
@ronaldmannak
ronaldmannak force-pushed the claude/cb2-7-loader-frontdoor branch from 7ab7720 to 2b60e84 Compare July 7, 2026 06:04
@ronaldmannak
ronaldmannak force-pushed the claude/cb2-6-scheduler branch from cf809fc to e8eb5a7 Compare July 17, 2026 00:55
@ronaldmannak
ronaldmannak force-pushed the claude/cb2-7-loader-frontdoor branch from 2b60e84 to a1fe6db Compare July 17, 2026 00:55
@ronaldmannak ronaldmannak changed the title cb2-7: scheduler-aware loadModelContainer overloads (batching front door) cb2-7 (chain 7/8): scheduler-aware loadModelContainer overloads (batching front door) Jul 17, 2026
@ronaldmannak
ronaldmannak force-pushed the claude/cb2-7-loader-frontdoor branch from a1fe6db to a3197df Compare July 17, 2026 01:35
@ronaldmannak
ronaldmannak force-pushed the claude/cb2-6-scheduler branch from e8eb5a7 to d664757 Compare July 17, 2026 01:35
@ronaldmannak
ronaldmannak force-pushed the claude/cb2-7-loader-frontdoor branch from a3197df to a5db974 Compare July 17, 2026 01:54
@ronaldmannak
ronaldmannak force-pushed the claude/cb2-6-scheduler branch from d664757 to 1c9e228 Compare July 17, 2026 01:54
Convenience front door for continuous batching: optional overloads of
loadModelContainer / loadContainer that install an InferenceScheduler
(and optional prompt cache) at load time, instead of requiring manual
ModelContainer(context:scheduler:promptCache:) construction. Pure
composition in one new file -- no existing declaration changes, and
`scheduler` has no default value so the plain loaders keep resolving
exactly as before (zero-overhead default untouched).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Jxj5hG5kLt2kkr5WDmHU5S
@ronaldmannak
ronaldmannak force-pushed the claude/cb2-7-loader-frontdoor branch from a5db974 to 6fa7071 Compare July 17, 2026 02:04
@ronaldmannak
ronaldmannak force-pushed the claude/cb2-6-scheduler branch from 1c9e228 to e12db4b Compare July 17, 2026 02:04
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.

2 participants