Serve many LoRA adapters in one batch
Why it matters Serving hundreds of fine-tuned customers from one GPU depends on batching requests for different LoRA adapters together, as multi-LoRA servers do.
Multi-adapter servers keep one base model and many small LoRA adapters, and mix requests for different adapters in the same batch. Each row adds its own low-rank update on top of the shared base matmul.
Write multi_lora_forward(X, W, A_list, B_list, adapter_ids, scale). X is (N, d_in), W is (d_in, d_out). A_list[i] is (d_in, r_i) and B_list[i] is (r_i, d_out); ranks may differ between adapters. adapter_ids is a length-N list where -1 means the row uses the base model only. Return Y (N, d_out) with Y[n] = X[n] W + scale * (X[n] A_i) B_i for i = adapter_ids[n]. Compute the base matmul once for the whole batch and the adapter part once per adapter group.
def multi_lora_forward(X, W, A_list, B_list, adapter_ids, scale)Example
X [[1, 2], [3, 4]], W identity, one adapter A [[1], [0]], B [[0, 1]], ids [0, -1], scale 2 gives [[1, 4], [3, 4]].
Understand the idea
Picture one big shared kitchen (the base model) and many small spice boxes (LoRA adapters), one per customer. Every dish goes through the same kitchen, and then gets a pinch from its own spice box. Doing all the kitchen work together is efficient; the spice step is cheap because each box is small. Worked example: row [1, 2] with W = identity gives [1, 2]. Its adapter A = [[1], [0]], B = [[0, 1]] gives (X A) = [1], then [0, 1], times scale 2 is [0, 2], so the row becomes [1, 4]. Rule: y = x W + scale (x A_i) B_i, with no update for base-only rows. Multiplying x A first keeps the work at rank r instead of building the full A B matrix. This is how one GPU serves many fine-tuned variants at once.
Shapes and data flow
X (N, d_in) @ W (d_in, d_out) -> (N, d_out); X[rows] (n_i, d_in) @ A_i (d_in, r_i) @ B_i (r_i, d_out) -> (n_i, d_out)