Repository navigation
feat(scheduler): add runtime setMaxNumSeqs(_:) for adaptive concurrency caps - #27
Merged
Merged
Conversation
…cy caps Adds a runtime-mutable concurrency cap to Scheduler so downstream wrappers can adjust the engine's admission window without rebuilding the engine. Threaded through EngineCore (engine-queue hop) and BatchedEngine. Motivation: downstream provider-swift (Darkbloom) ships an AdaptiveBatchCapPolicy that observes per-batch-size TPS and shrinks or grows the cap dynamically. Previously this could only gate at the admission layer; now the engine's own maxNumSeqs follows suit. Design: - SchedulerConfig.maxNumSeqs stays let (no breaking config-mutation contract). A new private runtimeMaxNumSeqs mirrors the initial value and is what admitWaiting() reads. - setMaxNumSeqs(_:) clamps value < 1 to 1 to prevent permanent admission deadlock from an accidental zero. Init does NOT clamp (preserves the pre-existing 'config.maxNumSeqs = 0 blocks all admission' sentinel). - Thread-safety contract: must be called from the engine queue (typically via EngineCore.setMaxNumSeqs) or before EngineCore.start(). Documented on the method. - Visible via getStats()['max_num_seqs'] for monitoring. EngineCore.setMaxNumSeqs hops onto engineQueue.async before forwarding. BatchedEngine.setMaxNumSeqs is a thin forward to core. Tests (SchedulerConfigTests, +78 LOC, 4 XCTest cases): - setMaxNumSeqs clamps values < 1 to 1 - new value observable via getStats()['max_num_seqs'] - lowered cap gates admission (3 requests with cap=2 -> 2 running, 1 waiting) - raising cap unblocks the waiting request No changes to existing public API. No new dependencies.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a runtime-mutable concurrency cap to `Scheduler` so downstream wrappers can adjust the engine's admission window without rebuilding the engine. Threaded through `EngineCore` (engine-queue hop) and `BatchedEngine`.
Motivation
Downstream provider-swift (Darkbloom) ships an `AdaptiveBatchCapPolicy` that observes per-batch-size TPS and shrinks or grows the cap dynamically based on observed throughput regressions. Previously this could only gate at the admission layer (refusing to call `addRequest` past the cap), but the engine itself had no way to know — `SchedulerConfig.maxNumSeqs` was immutable after construction. This PR closes that gap.
Design
API additions
```swift
// MLXLMCommon/ContinuousBatching/Scheduler.swift
public func setMaxNumSeqs(_ value: Int)
// getStats()["max_num_seqs"]: Int — already exists in the stats dict shape
// MLXLMCommon/ContinuousBatching/EngineCore.swift
public func setMaxNumSeqs(_ value: Int)
// MLXLMCommon/ContinuousBatching/BatchedEngine.swift
public func setMaxNumSeqs(_ value: Int)
```
No existing public API changes. No new dependencies.
Tests
`Tests/MLXLMTests/SchedulerConfigTests.swift` (4 XCTest cases, +78 LOC):
```
$ swift test --filter SchedulerConfigTests
Test Suite 'SchedulerConfigTests' passed
Executed 4 tests, with 0 failures (0 unexpected) in 0.10 seconds
```
Full suite (`swift test`) — 327/327 pass (169 XCTest + 158 Swift Testing).
Downstream use
Used by the `darkbloom` Apple Silicon provider to thread its adaptive-batching policy through to the engine. See the companion PR on Layr-Labs/d-inference.