Repository navigation
perf(models): fuse the qkv and gate/up projections into single GEMVs - #206
kunkunblueberry wants to merge 5 commits into
Conversation
At the sizes this engine runs, each projection is a GEMV that streams a weight matrix and computes almost nothing, so it is bandwidth-bound and each extra launch or pass over the same input row is paid in full. q/k/v read the same hidden row, and gate/up likewise, so three and two separate GEMVs became one each. FusedWeight keeps the parameters where they are and resolves the source tensors per call, building the stacked matrix once per (id, data_ptr, dtype, shape). That resolution order matters and cost three attempts: - holding a strong reference to the owner module makes a cycle, and state_dict/_apply then recurse forever; - a weakref cannot be pickled, and the engine ships the model to the worker; - registering the cache as an nn.Parameter adds a state_dict key and breaks checkpoint round-trip. As a plain object holding a plain tensor, none of those apply. Measured operator-level at M=1..8 (Ouro-1.4B shapes): qkv [6144, 2048] and gate_up [11264, 2048] each 1.411x faster than the separate GEMVs. gate_up is bitwise identical to the separate matmuls; qkv has a maximum relative difference of 0.41% against one BF16 ULP of 0.78%, i.e. the stacked GEMM reassociates the reduction but stays inside a single ulp. End to end on the real checkpoint with CUDA graphs on, 15.286 ms/step -> 14.567 ms/step (1.049x), and 1.092x for the operator-level graphs-only A/B. Related work: InferMatrixLab#167 (draft) and InferMatrixLab#146 (closed) both cover elementwise kernels, not projections, so nothing in flight overlaps this change.
Signed-off-by: kunkunblueberry <kunkunblueberry@gmail.com>
|
The PR stacks the projection weights vertically. For Ouro-1.4B, it builds qkv as [6144, 2048] and gate/up as [11264, 2048]. The main design question is how those fused weights are created and owned.
FusedWeight could accept the source tensors directly. The current implementation stores the owning module and resolves its parameters on each call so it can notice when parameter objects have been replaced.
This is mathematically equivalent to the current linear operation:
Splitting Z along its first dimension can produce contiguous [H, N] blocks. If downstream operators require the usual [N, H] layout, however, transposing those blocks produces non-contiguous views again. For example, with two token rows and a projection width of three:
The feature-major layout is a valid option, but RoPE, KV writes, attention and the MLP’s down projection would need to consume it correctly. Its performance should be measured across the full execution path. Different GEMM choices can also produce small BF16 numerical differences. At the reviewed PR head, aabd446, the native silu_and_mul kernel has been updated to accept the original row-strided [N, D] slices. My earlier finding that it would reject those slices applied to the older head; I have not run the updated kernel on a GPU.
vLLM's implementation uses: • QKVParallelLinear for one fused QKV weight. For the unquantized, single-GPU case, the loader allocates the fused parameter and copies each checkpoint weight into its target slice. It therefore does not need a second persistent copy of the original projection weights. For the MLP, vLLM passes the complete [N, 2D] output to SiluAndMul. The CUDA kernel reads the gate and up halves using offsets within each row and writes a [N, D] output. |
Signed-off-by: kunkunblueberry <kunkunblueberry@gmail.com>
Signed-off-by: kunkunblueberry <kunkunblueberry@gmail.com>
Required human contributor answers
Recorded from the contributor''s instruction: the goal of this work is to bring vllm-rlt''s
operator performance to vLLM parity, for the developers who build and run on this engine,
because these operators are the performance-critical components. Figures cited below are the
measurements in this PR.
1. What outcome are you trying to achieve?
Match vLLM''s operator performance in vllm-rlt. Projections are the largest single item in the
decode step (measured at 6.33 ms/step of 13.86 ms on Ouro-1.4B), and at the batch sizes this
engine runs they stream weights without doing much arithmetic, so they are the place where
operator-level parity is decided.
2. What is the highest-leverage variable for that outcome?
Achieved DRAM bandwidth on the weight stream. The reference measurement is that a single
2048x2048GEMV with distinct weights reaches 597 GB/s of a measured 1770 GB/s peak, and thatthis is a property of how few bytes are in flight per launch, not of kernel quality: on the
same call
torch.compilemeasured 3.4x slower.3. How will this work move that variable rather than create busywork?
q/k/vread the same hidden row andgate/uplikewise, so fusing them stacks the weightmatrices into one GEMV each and raises the bytes in flight per launch. Measured cold-L2, per
call: qkv 370 -> 1174 GB/s (3.17x) and gate_up 934 -> 1327 GB/s (1.42x), which is 15.286 ->
14.567 ms/step end to end. No new kernel, no build step, no numerical contract change:
gate_upis bitwise identical andqkvdiffers by at most 0.003%, inside one BF16 ULP.4. Who is the customer or user, and what do they actually need?
Developers who deploy looped-transformer models on vllm-rlt and need per-step latency parity
with vLLM, and contributors benchmarking operator work who need the projection cost not to
dominate their measurements. They need it without changing model code, checkpoints, or the
shipped configuration.
PR necessity and bugfix evidence
A PR is needed: the projections are the largest measured item in the decode step, the change
is confined to one layer module plus the model wiring, and it needs no configuration or
checkpoint migration. This is a performance change, not a bugfix; the evidence is the
cold-L2 operator sweep and the end-to-end step timing in Test Result, reproducible with the
commands in Test Plan.
Purpose
Summary. Fuse the projections that share an input row -
q/k/vinto[6144, 2048]andgate/upinto[11264, 2048]- so each becomes one GEMV. Measured 3.17x (qkv) and 1.42x(gate_up) at the operator level, 1.049x end to end, with
gate_upbitwise identical.Each projection in this engine is a GEMV that streams a weight matrix and computes almost
nothing, so it is bandwidth-bound, and every extra launch or pass over the same input row is
paid in full:
OuroAttention:q/k/v-> one[6144, 2048]matrix, then split the output.OuroMLP:gate/up-> one[11264, 2048]matrix, then split.o_projanddown_projare deliberately not fused: they consume different inputs.FusedWeight(vllm_rlt/layers/fused_projections.py) keeps the parameters where they are andresolves the source tensors per call, caching the stacked matrix by
(id, data_ptr, dtype, shape). That resolution order is the whole difficulty and cost three attempts, each of whichbroke a real contract:
state_dict/_applythen recurse until they fail;
weakrefcannot be pickled, and the engine ships the model to the worker;nn.Parameteradds astate_dictkey and breaks checkpointround-trip.
As a plain object holding a plain tensor, none of those apply.
Test Plan
vllm-rlt Version / Commit:
b29ffb5, branchpr/fused-projections, based onmainatce200f0.GPU validation on 8x A100-SXM4-80GB (
lyx):pytest tests/test_fused_projections.py -q -p no:randomly # 9 passedThe tests cover the three contracts above directly rather than only the arithmetic: checkpoint
round-trip (the cache must not appear in
state_dict), pickling the model, and re-resolvingafter a parameter''s storage changes.
Test Result
Operator-level, cold-L2, distinct weights per call (a single reused matrix is served from the
A100''s 40 MB L2 and overstates the bottleneck), device ms per call:
[6144,2048][11264,2048]gate_upis bitwise identical to the two separate matmuls.qkvhas a maximum relativedifference of 0.003%, well inside one BF16 ULP (0.78%): the stacked GEMM reassociates the
reduction. The tests assert the measured bound rather than bitwise parity for that reason.
Numerics were re-measured for this PR and are better than the first record. The experiment
note quotes 1.411x for both cases and a 0.41%
qkvdeviation; that was measured with a singlereused weight matrix (cache-resident) and a different reduction shape. The figures above are
the cold-L2, per-call numbers the bandwidth claim actually depends on.
End to end on the real checkpoint, CUDA graphs on, 128-token prompt / 64 decode tokens:
15.286 -> 14.567 ms/step (1.049x), consistent with 1.092x on the graphs-only operator A/B (the
difference is the non-graph host work around the replay, which does not shrink).
Where it stops, measured. The fused path now runs at 1174-1332 GB/s against a measured HBM
peak of 1770 GB/s, so roughly 1.3-1.5x remains and it is the small-matrix regime, not kernel
quality: the same sweep at
o_projwidth (8.4 MB) reaches only 597 GB/s.q/k/vmust stayfused for that reason, and per-call
torch.compileon the same call measured 3.4x slower(59 us vs 17.6 us), so a hand-written GEMV is not the answer.
Related work
activation kernels, so nothing in flight overlaps this change.
projections.
Full experiment record:
docs/experiments/V002-projection-fusion.md.