Model forward pass

This part processes the input through the model's layers to produce prediction scores.

Columns are parts of the code, grouped by process. Read top to bottom. Solid arrow: hands work over. Dashed: the reply. Purple: crosses between processes. Hook: work inside one part. Dashed boxes repeat. Hover a column name or a step for its full text.

MAIN PROCESSSingle prefill passToken generation loopBeam search loopAssisted decoding loopExtend input sequen…Request model scoresCom…Request next token sc…Com…Initialize beam sea…Score beam candidatesCom…Setup assisted deco…Verify candidate toke…Com…
Generation
Model forward
1Prefill prompt and select first token — The loop starts by processing the full prompt input in a single prefill pass. It then copies the resulting prediction scores to the main device and applies any configured processors. The system selects the first token using either multinomial sampling with softmax or a simple argmax for greedy selection.
2Compute logits from model output — The forward pass receives the prepared inputs and calls the underlying model to process them. It takes the final hidden state and passes it through a slicing operation and a linear head to produce vocabulary logits. This step returns the prediction scores along with the updated cache and any auxiliary outputs.
3Select next token in loop — This step runs repeatedly for each new token after the initial prefill. It prepares a decoding request, often for just one token if a cache exists. It then samples the next token from the logits and appends it to the growing sequence.
4Run beam search initialization — If beam search is active, this step initializes multiple sequence buffers to track candidate paths. It sets up the necessary data structures for ancestry and scores. The system then enters a loop that continues as long as unfinished sequences remain.
5Process beam search forward pass — The forward pass evaluates the current candidates for all active beams. It computes the logits for the next potential tokens across these different paths. The resulting scores feed back into the beam search algorithm for ranking and selection.
6Verify assisted decoding candidates — For assisted decoding, this step obtains candidate tokens from an assistant model. It adjusts the input positions and masks to align with these candidates. The system then requests the necessary logits to verify the speculative tokens.
7Evaluate speculative candidates — The forward pass processes the candidate tokens to produce their prediction scores. It computes the logits needed to validate or reject the speculative choices. This output allows the generation loop to decide which candidates to accept.
STEP · hover for full text