Class QwenMtp
Fuses the previous backbone hidden state with the embedding of the last
sampled token (pre_fc_norm_* + fc), runs one or more full-attention
transformer blocks, applies norm, then scores with the shared
lm_head. Multi-step drafts chain this module autoregressively
(NEXTN-style).
-
Field Summary
Fields inherited from class LayerBlock
device, dtype, module -
Constructor Summary
ConstructorsConstructorDescriptionQwenMtp(QwenModelArgs args, TensorShardSpec shard, TensorParallelGroup tpGroup) Constructs MTP modules on CPU. -
Method Summary
Modifier and TypeMethodDescriptionHistory-aware MTP step: fusesmconsecutive positions[startPos, startPos+m)of one request, writes their K/V into the (persistent, request-bound) MTP KV pool, and attends over the request's entire MTP history[0, startPos+m)— unlikedraftStep(Tensor, Tensor, int, int), whose per-round pool only ever contains the current round's own draft positions (so the head never sees the prompt).absorbBatch(Tensor tokens, Tensor hidden, int[] startPositions, int[] lastRealRow) Batched, history-aware MTP step for a cohort of concurrent requests, each at its own absolute position.voidbeginRound(int draftWindow) Clears MTP KV for a new single-request speculative round (bind capacity for the draft window).voidbeginRound(int batchSize, int draftWindow) Clears MTP KV for a new speculative round sized forbatchSizeconcurrent requests, each reservingdraftWindow + 1slots.voidbindShared(EmbeddingLayer embeddings, LinearLayer lmHead, PartialRotaryEncoding.CosSin rope) Binds shared backbone embeddings, LM head, and RoPE tables.voidDrops the chained draft hidden (history-aware path keeps the KV pool bound).One MTP draft step: fuse embedding(token) with previous hidden, run layers, return float logits[B, V]and updatelastDraftHiddenwith the pre-lm-head hidden for the next draft step.One MTP draft step (single logical request; broadcastspositionto every row of a batch — only correct when every row is truly at the same absolute position).voidendRound()Releases MTP draft KV after a speculative round.Forward propagation (or forward pass) through the layer.Returns the MTP KV layout for a short draft window.Returns the MTP KV pool.Returns the last draft hidden from the most recentdraftStep(Tensor, Tensor, int, int).intReturns the number of MTP transformer layers.voidsetKvCachePool(KvCachePool pool, boolean closePrevious) Installs an MTP-local KV pool and wires full-attention layers.Moves the layer block to a device.to(Device device, ScalarType dtype) Moves the layer block to a device.Methods inherited from class LayerBlock
add, add, device, dtype, eval, isTraining, load, loadStateDict, module, save, setRequiresGrad, toString, train
-
Constructor Details
-
QwenMtp
Constructs MTP modules on CPU. Callto(Device)after weight load; thenbindShared(EmbeddingLayer, LinearLayer, PartialRotaryEncoding.CosSin)andsetKvCachePool(KvCachePool, boolean).- Parameters:
args- backbone args (QwenModelArgs.mtpNumHiddenLayers()> 0).shard- TP shard, ornull.tpGroup- TP group, ornull.
-
-
Method Details
-
setKvCachePool
Installs an MTP-local KV pool and wires full-attention layers.- Parameters:
pool- MTP KV pool.closePrevious- whether to close a previous pool.
-
kvCacheLayout
Returns the MTP KV layout for a short draft window.- Returns:
- MTP KV layout for a short draft window.
-
kvCachePool
Returns the MTP KV pool.- Returns:
- MTP KV pool, or
nullbefore install.
-
numLayers
public int numLayers()Returns the number of MTP transformer layers.- Returns:
- number of MTP transformer layers.
-
draftStep
One MTP draft step (single logical request; broadcastspositionto every row of a batch — only correct when every row is truly at the same absolute position). PreferdraftStep(Tensor, Tensor, int[], int)for the request-pooled batched path.- Parameters:
tokenIds- token ids[B](last sampled / previous draft).prevHidden- previous hidden[B, D](backbone or prior MTP).position- absolute RoPE / cache write position for this step.draftStep- zero-based index within the current draft window (MTP KV).- Returns:
- logits
[B, V]in float32 (caller owns).
-
draftStep
One MTP draft step: fuse embedding(token) with previous hidden, run layers, return float logits[B, V]and updatelastDraftHiddenwith the pre-lm-head hidden for the next draft step.- Parameters:
tokenIds- token ids[B](last sampled / previous draft).prevHidden- previous hidden[B, D](backbone or prior MTP).positions- absolute RoPE / cache write position per batch row — concurrent requests are generally at different absolute positions, unlike the relative in-rounddraftStepindex below.draftStep- zero-based index within the current draft window (MTP KV); uniform across the batch — every row in a round is always at the same relative draft-step.- Returns:
- logits
[B, V]in float32 (caller owns).
-
absorb
History-aware MTP step: fusesmconsecutive positions[startPos, startPos+m)of one request, writes their K/V into the (persistent, request-bound) MTP KV pool, and attends over the request's entire MTP history[0, startPos+m)— unlikedraftStep(Tensor, Tensor, int, int), whose per-round pool only ever contains the current round's own draft positions (so the head never sees the prompt).Position
i's input is(token t_{i+1}, backbone hidden h_i)and is stored at RoPE/KV positioni, matching vLLM's Qwen3.5 MTP proposer. The caller must have activated the MTP pool for the request. Always refresheslastDraftHidden()with the last row's post-norm hidden[1, 1, D](the next chained draft step's input).- Parameters:
tokens- next-token ids[1, m](int64, on this device).hidden- backbone/MTP hidden[1, m, D].startPos- absolute position of the first row.wantLogits- whether to score the last row with the shared LM head.- Returns:
- float logits
[1, 1, V]for the last row, ornull.
-
lastDraftHidden
Returns the last draft hidden from the most recentdraftStep(Tensor, Tensor, int, int).- Returns:
- last draft hidden
[B, 1, D]or[B, D], ornull.
-
beginRound
public void beginRound(int draftWindow) Clears MTP KV for a new single-request speculative round (bind capacity for the draft window). PreferbeginRound(int, int)for the request-pooled batched path.- Parameters:
draftWindow- number of draft steps to reserve.
-
beginRound
public void beginRound(int batchSize, int draftWindow) Clears MTP KV for a new speculative round sized forbatchSizeconcurrent requests, each reservingdraftWindow + 1slots.- Parameters:
batchSize- number of concurrent requests in this round's cohort.draftWindow- number of draft steps to reserve per request.
-
absorbBatch
Batched, history-aware MTP step for a cohort of concurrent requests, each at its own absolute position. Writes K/V for every column of every row into the persistent request-bound pool and attends over each row's whole history, likeabsorb(Tensor, Tensor, int, boolean).Rows are padded to a common width
S: rowihaslastRealRow[i]+1real columns (positionsstartPositions[i]..) followed by dummy columns. Dummy columns come after the real ones, so causal attention keeps real columns unaffected; their K/V lands in the row's speculative region (past its committed history) and is overwritten before it can be read. The logits and chained hidden are taken at each row's last real column.- Parameters:
tokens- next-token ids[B, S](int64, on this device).hidden- backbone/MTP hidden[B, S, D].startPositions- absolute KV/RoPE position of column 0, per row.lastRealRow- index of each row's last real column, in[0, S).- Returns:
- float logits
[B, 1, V]for each row's last real column. The chained hidden[B, 1, D]is available fromlastDraftHidden().
-
clearDraftHidden
public void clearDraftHidden()Drops the chained draft hidden (history-aware path keeps the KV pool bound). -
endRound
public void endRound()Releases MTP draft KV after a speculative round. -
to
-
to
Description copied from interface:LayerMoves the layer block to a device.- Specified by:
toin interfaceLayer- Overrides:
toin classLayerBlock- Parameters:
device- the compute device.dtype- the data type.- Returns:
- this layer.
-
forward