Skip to content

feat(scheduler): add runtime setMaxNumSeqs(_:) for adaptive concurrency caps - #27

Merged
Gajesh2007 merged 1 commit into
mainfrom
feat/scheduler-set-max-num-seqs
May 24, 2026
Merged

Gajesh2007 merged 1 commit into
mainfrom
feat/scheduler-set-max-num-seqs

Conversation

@Gajesh2007

Copy link
Copy Markdown
Member

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

  • `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, preserving the pre-existing "`config.maxNumSeqs = 0` blocks all admission" sentinel behavior.
  • Thread-safety contract: must be called from the engine queue (typically via `EngineCore.setMaxNumSeqs`) or before `EngineCore.start()`. Documented on the method.
  • Current value visible via `getStats()["max_num_seqs"]` for monitoring.
  • `EngineCore.setMaxNumSeqs` hops onto `engineQueue.async` before forwarding.
  • `BatchedEngine.setMaxNumSeqs` is a thin forward to `core`.

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):

Test Asserts
`testSetMaxNumSeqsClampsBelowOne` `setMaxNumSeqs(-3)` clamps to 1, visible via `getStats()`
`testSetMaxNumSeqsUpdatesValue` default (4) and updated (2) both observable via `getStats()`
`testRuntimeCapGatesAdmission` 3 requests with cap lowered to 2 ⇒ 2 running, 1 waiting
`testRuntimeCapRaisingAllowsAdmission` raising cap unblocks the waiting request on next step

```
$ 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.

…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.
@Gajesh2007
Gajesh2007 merged commit 096e792 into main May 24, 2026
3 checks passed
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.

1 participant