Put requests that use different LoRA adapters into the same diffusion batch.
Status: design only, nothing benchmarked yet. See HANDOFF.md for the full spec, BENCH_CHECKLIST.md for the benchmark plan, and TASKS.md for the remaining work by complexity level.
A LoRA adapter is a small add-on to a model's weights (W + B·A). Diffusion servers handle it badly when requests use different adapters:
- vLLM-Omni puts the adapter id in the batch key. Two requests with different adapters never share a batch.
- SGLang-Diffusion keeps one LoRA active per model. Changing adapters means unmerging the old one and loading the new one.
So with many adapters in play, batches stay small and the GPU sits underused. In the one public production trace we know of (Alibaba, 24 days), 16.8% of requests use a LoRA, there are 874 distinct adapters, and the median adapter is used 5 times. Adapter-keyed batching is a bad fit for that.
Keep the base weights untouched and shared. For each linear layer, run the base matmul on the whole batch, then add each row's own LoRA correction on top. Different rows can use different adapters, or none.
Two pieces make that work:
- A segmented LoRA kernel. One call applies a different
(A, B)pair to each contiguous group of rows. - A GPU adapter pool. A fixed number of slots on the GPU hold adapters in stacked buffers. Adapters not in a slot wait in pinned CPU memory and are copied in the background.
The kernel idea already exists for LLMs (Punica, S-LoRA, vLLM's Triton LoRA ops). What is missing for diffusion is the rest: DiT layer shapes, classifier-free guidance (CFG) doubling the batch, torch.compile compatibility, the many LoRA file formats, and a scheduler that doesn't split batches by adapter.
The top half shows today's behavior: requests are grouped by adapter, so each group becomes its own small batch. The bottom half shows the proposal: one batch holds requests with any mix of adapters. The panel at the side lists the evidence and the pass/fail bars for the kill test.
Request path:
client -> scheduler -> adapter pool -> DiT denoise loop -> VAE -> image
^
LoRA file (Hub or disk) -> format normalizer
The scheduler admits a request into the running batch once its adapter is on the GPU. The batch key is model, resolution, step count and CFG on/off. It does not include the adapter.
The scheduler also keeps text encoding adapter-free: prompt embeddings go through one shared cache keyed by (encoder_id, prompt, max_len), so requests with the same prompt and different LoRAs encode it once. See the next diagram.
For one linear layer in one denoise step:
- The batch's tokens are flattened to
Xwith shape[T, d_in]. Each request owns a contiguous range of rows (a segment). - The base layer computes
Y = X·Wᵀfor everything. This is the normal fast matmul. - Shrink: for each segment,
Z = X · Aᵀusing that segment's adapter.Zis small (rank 16 to 64 columns). - Expand-add:
Y += scale · Z · Bᵀ, written in place. Segments with no adapter (slot −1) skip both steps. - The adapter's weights come from the pool: per layer, one stacked buffer for all
As and one for allBs, indexed by slot.
Details that matter:
alpha/ris folded intoBonce when the adapter is loaded. The runtimescaleis only the user's strength. Mixing these up is a common bug, so there is a test that pins it.- Fused QKV layers are handled when the adapter is loaded: the three
As are stacked and the threeBs are placed block-diagonally. The kernel never needs to know about fusion. - The op is registered as a
torch.librarycustom op, sotorch.compileshould not add graph breaks. - A slot is never evicted while a request is using it. Unused slots are evicted least-recently-used.
Goal: a LoRA request should take about as long as a base-model request.
- Timeline. When a request arrives, its adapter starts copying to the GPU on a side stream while the text encoder runs (prompt embeddings come from the shared cache when the prompt was seen before). If the copy finishes first, the adapter load adds no latency. VAE decode and image encoding run on separate streams so finished requests leave the batch immediately.
- Latency breakdown. Every request logs: queue wait, adapter wait, text encode, denoise steps, VAE decode, image encode, response transfer. Adapter wait is only the part not hidden behind text encoding. It is split by where the adapter was (GPU, CPU, disk, or had to be downloaded).
- Adapter payload. Each adapter is converted once to a single packed file: bf16, native rank (not padded), QKV already composed,
alpha/ralready folded in. Loading is one file read, one host-to-GPU copy, and one scatter kernel that writes into the slot. Optional lossy compression (FP8 storage, SVD rank truncation) is off by default and only enabled if image quality holds.
Mind Lab's LLM design rests on one fact: the adapter never touches the part of the model whose output can be cached and shared. The same holds in a DiT, because LoRA only targets the transformer's linears and the text encoder and VAE carry no adapter. The diagram maps each Mind Lab idea to its loramux counterpart and marks what is new (the shared prompt-embedding cache), what already existed (segmented kernels, adapter pool), and what is not ported (prefill/decode pools, MoE locality). Details in docs/mindlab-port.md.
- Kill test (cap 25 USD of GPU). Check correctness, kernel overhead, and whether a simulator on a realistic trace shows a real throughput or tail-latency gain. Result: GO, GREY or NO-GO, decided against bars written down before any run.
- Library. Kernels, pool, normalizer, diffusers integration, tests. Then the latency work.
- vLLM-Omni branch, behind a flag, only if the kill test is GO.
The bars are in HANDOFF.md section 5. They do not change after seeing data.
loramux/ package: context, metadata, layers, pool, blob, formats/, ops/
bench/ microbenchmarks, step benchmarks, trace + simulator, serve loop
tests/ pytest; GPU tests are marked `gpu`
docs/ diagrams (png, svg, editable excalidraw)
Do not run the test suite on a laptop. Run it on Modal (CPU for non-GPU tests, one H100 for -m gpu):
modal run modal_app.py::cpu_tests
Single GPU only. No LoKr, LoHa or DoRA (rejected, or SVD-approximated behind a flag). No ControlNet or IP-Adapter. One resolution per batch. Step caching (Cache-DiT, TeaCache) is off in mixed batches.



