1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// RLX — versatile ML compiler + runtime.
// Copyright (C) 2026 Eugene Hauptmann, Nataliya Kosmyna.
// SPDX-License-Identifier: MIT OR Apache-2.0
//! LoRA-aware request scheduling (plan #33).
//!
//! Borrowed from MAX's `serve/scheduler/lora_scheduler_utils.py`.
//! When multiple LoRA adapters are loaded, each request specifies
//! which adapter it wants. Naïvely interleaving requests forces an
//! adapter swap per request — wasted bandwidth + latency. The
//! scheduler here groups consecutive same-adapter requests into
//! one "batch", so a swap happens once per batch boundary instead
//! of once per request.
//!
//! Pure data-layer scheduling — no executor, no compiled graphs.
//! Plug into a future serving loop by:
//! 1. Push incoming requests into [`LoraScheduler::push`].
//! 2. Drain runnable batches with [`LoraScheduler::drain_batch`].
//! 3. For each batch, swap to that adapter once and run all
//! requests in the batch back-to-back.
//!
//! Pairs with #24 (named weights registry) which owns the adapter
//! bytes, and #9 (LoRA kernel) which is the actual compute.
use crate::weight_registry::WeightRegistry;
use std::collections::VecDeque;
/// One serving request with its target LoRA adapter (or `None`
/// for "use the base model"). The opaque `id: u64` is whatever
/// the caller wants — typically a request UUID hash.
#[derive(Debug, Clone)]
pub struct LoraRequest {
pub id: u64,
pub adapter: Option<String>,
/// Caller-provided payload. The scheduler doesn't interpret
/// it; downstream code reads it after `drain_batch`.
pub payload: LoraPayload,
}
/// Generic request payload. The scheduler is generic over
/// payload type via `Box<dyn Any>`; we keep this concrete here
/// for simplicity and the common case (text generation).
#[derive(Debug, Clone)]
pub struct LoraPayload {
pub prompt_tokens: Vec<u32>,
pub max_new_tokens: usize,
}
/// One batch handed to the executor. All requests in a batch
/// target the same adapter (or all None).
#[derive(Debug)]
pub struct LoraBatch {
pub adapter: Option<String>,
pub requests: Vec<LoraRequest>,
}
impl LoraBatch {
pub fn len(&self) -> usize {
self.requests.len()
}
pub fn is_empty(&self) -> bool {
self.requests.is_empty()
}
}
/// FIFO-ish scheduler with same-adapter coalescing.
pub struct LoraScheduler {
/// Pending requests; insertion order preserved.
pending: VecDeque<LoraRequest>,
/// Maximum requests per drained batch.
pub max_batch: usize,
/// Optional reference to a registry — `validate` checks that
/// adapter names are registered before push. Storing as a raw
/// pointer avoids lifetime entanglement; callers ensure the
/// registry outlives the scheduler.
registry: Option<*const WeightRegistry>,
}
// `*const WeightRegistry` is not Send/Sync by default; we mark
// the scheduler Send because the pointer is only used for read
// queries on a registry that's itself Send + Sync once we wrap
// it in Arc<RwLock>. The pointer should never be mutated.
unsafe impl Send for LoraScheduler {}
impl LoraScheduler {
pub fn new(max_batch: usize) -> Self {
Self {
pending: VecDeque::new(),
max_batch,
registry: None,
}
}
/// Bind a registry for adapter-name validation. Caller must
/// ensure `registry` outlives the scheduler.
pub fn bind_registry(&mut self, registry: &WeightRegistry) {
self.registry = Some(registry as *const _);
}
/// Push a request. Returns `Err` only if a registry is bound
/// and the adapter name isn't registered.
pub fn push(&mut self, req: LoraRequest) -> Result<(), UnknownAdapter> {
if let (Some(reg_ptr), Some(adapter)) = (self.registry, &req.adapter) {
// Safety: caller guaranteed the registry outlives us.
let reg = unsafe { &*reg_ptr };
if reg.lora_adapter_handles(adapter).is_empty() {
return Err(UnknownAdapter {
name: adapter.clone(),
});
}
}
self.pending.push_back(req);
Ok(())
}
/// Look at the next batch's adapter without draining.
pub fn peek_adapter(&self) -> Option<Option<String>> {
self.pending.front().map(|r| r.adapter.clone())
}
/// Drain the next runnable batch — up to `max_batch` requests
/// that all share the same `adapter`. Returns `None` if empty.
pub fn drain_batch(&mut self) -> Option<LoraBatch> {
let head = self.pending.pop_front()?;
let target = head.adapter.clone();
let mut requests = vec![head];
while requests.len() < self.max_batch {
match self.pending.front() {
Some(next) if next.adapter == target => {
requests.push(self.pending.pop_front().unwrap());
}
_ => break,
}
}
Some(LoraBatch {
adapter: target,
requests,
})
}
pub fn pending(&self) -> usize {
self.pending.len()
}
pub fn is_empty(&self) -> bool {
self.pending.is_empty()
}
}
#[derive(Debug, Clone)]
pub struct UnknownAdapter {
pub name: String,
}
impl std::fmt::Display for UnknownAdapter {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "adapter `{}` is not registered", self.name)
}
}
impl std::error::Error for UnknownAdapter {}
/// Count adapter swaps that would occur if a sequence of
/// requests were processed in declared order without coalescing.
/// Useful for observability / unit tests.
pub fn naive_swap_count(reqs: &[LoraRequest]) -> usize {
let mut swaps: usize = 0;
let mut last: Option<&Option<String>> = None;
for r in reqs {
if last.map(|l| l != &r.adapter).unwrap_or(true) {
swaps += 1;
}
last = Some(&r.adapter);
}
swaps.saturating_sub(1) // first "swap" is the initial setup, not a swap
}
#[cfg(test)]
mod tests {
use super::*;
use crate::weight_registry::{WeightKind, WeightRegistry};
use rlx_ir::{DType, Shape};
use std::sync::Arc;
fn req(id: u64, adapter: Option<&str>) -> LoraRequest {
LoraRequest {
id,
adapter: adapter.map(|s| s.to_string()),
payload: LoraPayload {
prompt_tokens: vec![],
max_new_tokens: 4,
},
}
}
#[test]
fn coalesces_same_adapter_runs() {
let mut s = LoraScheduler::new(8);
// Mixed-adapter input order:
// code, code, math, math, code, base, base
for r in [
req(1, Some("code")),
req(2, Some("code")),
req(3, Some("math")),
req(4, Some("math")),
req(5, Some("code")),
req(6, None),
req(7, None),
] {
s.push(r).unwrap();
}
let b1 = s.drain_batch().unwrap();
assert_eq!(b1.adapter.as_deref(), Some("code"));
assert_eq!(b1.len(), 2); // 1, 2 — stops at the math at front
let b2 = s.drain_batch().unwrap();
assert_eq!(b2.adapter.as_deref(), Some("math"));
assert_eq!(b2.len(), 2);
let b3 = s.drain_batch().unwrap();
assert_eq!(b3.adapter.as_deref(), Some("code"));
assert_eq!(b3.len(), 1);
let b4 = s.drain_batch().unwrap();
assert!(b4.adapter.is_none());
assert_eq!(b4.len(), 2);
assert!(s.drain_batch().is_none());
}
#[test]
fn respects_max_batch_cap() {
let mut s = LoraScheduler::new(3);
for i in 0..10 {
s.push(req(i, Some("code"))).unwrap();
}
let b = s.drain_batch().unwrap();
assert_eq!(b.len(), 3, "max_batch=3 should split a long run");
assert_eq!(s.pending(), 7);
}
#[test]
fn registry_validation_rejects_unknown_adapter() {
let mut reg = WeightRegistry::new();
reg.register(
"ffn",
Shape::new(&[8, 8], DType::F32),
Arc::from(vec![0u8; 256]),
WeightKind::Base,
);
reg.register(
"ffn.lora.a",
Shape::new(&[8, 4], DType::F32),
Arc::from(vec![0u8; 128]),
WeightKind::LoraAdapter {
adapter: "code".into(),
},
);
let mut s = LoraScheduler::new(4);
s.bind_registry(®);
// Known adapter passes.
assert!(s.push(req(1, Some("code"))).is_ok());
// None (base model) always passes.
assert!(s.push(req(2, None)).is_ok());
// Unknown adapter rejected.
let err = s.push(req(3, Some("nonexistent"))).unwrap_err();
assert_eq!(err.name, "nonexistent");
}
#[test]
fn swap_count_metric() {
let reqs = [
req(1, Some("a")),
req(2, Some("a")),
req(3, Some("b")),
req(4, Some("a")),
];
// Sequence transitions: a, a→a (no swap), a→b (swap), b→a
// (swap) = 2 swaps after initial.
assert_eq!(naive_swap_count(&reqs), 2);
}
}