liveBook · Manning

Token generation loop

As explained in Deep Learning with PyTorch, Second Edition

Autoregressive generation that produces one token at a time and exposes each token as soon as it is created.

A token generation loop produces a response incrementally instead of waiting for the complete sequence. Each pass asks the model for one new token, decodes it, and places it in the requesting client’s result queue. section 17.1.4

A batched implementation repeats these steps:

  • Generate with max_new_tokens=1 using the batched tokens and attention mask. section 17.1.4
  • Extract and decode the newest token for each active request. section 17.1.4
  • Place each decoded token in its request queue under a results lock. section 17.1.4
  • Stop when the maximum token count is reached or no requests remain. section 17.1.4

Streaming an answer

A FastAPI /generate endpoint assigns a request ID, queues the prompt, and returns a StreamingResponse. An asynchronous stream_results generator yields tokens as they become available. section 17.1.4

for _ in range(MAX_TOKENS):
    if not active_requests:
        break  # All requests are done

    with torch.no_grad():
        outputs = model.generate(
            batch_tokens,
            attention_mask=batch_attention_mask,
            max_new_tokens=1,
            pad_token_id=tokenizer.pad_token_id,
        )
    generated_token_ids = [output[-1:] for output in outputs]

    for idx in list(active_requests):
        request_id, _ = batch[idx]
        new_token_id = generated_token_ids[idx]
        new_token = tokenizer.decode(new_token_id)

    ...

    with results_lock:
        if request_id in results:
            results[request_id].put(new_token)

Inspect the loop that generates one token, decodes it, and queues it for the matching request. section 17.1.4

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Traces generation, decoding, queueing, synchronization, and termination, but does not detail model internals.
Examples
Demonstrates batched prompts, decoded token queues, a FastAPI endpoint, curl requests, and concurrent capital-city queries.
What it assumes
Assumes a loaded model, tokenizer, tokenized prompts, attention masks, and basic request-queue handling.
What you can do after
Operate/configure a FastAPI text-generation endpoint that returns model output incrementally.
How it connects
Connects request batching, attention masks, tokenizers, background workers, result queues, and streaming responses.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with PyTorch, Second Edition. The section links point to the parts of the book it draws on.