liveBook · Manning

Generate

As explained in Build a Large Language Model (From Scratch)

A function used to produce model outputs from input prompts by running generation over a fine-tuned language model.

The generate function runs generation over a fine-tuned language model, extending an encoded prompt with newly generated token IDs. Its returned text combines the original input and model response, so response extraction removes the prompt before comparison or storage. section 7.7

A typical generation flow:

  • Format the instruction, tokenize it, move token IDs to the selected device, and call generate with the model. section 7.7
  • Bound generation with max_new_tokens, context_size, and eos_id to control length, context capacity, and termination. section 7.7
  • Decode the returned token IDs, remove the original input and response marker, then strip whitespace. section 7.7

Generating test responses

For each held-out test entry, call generate with up to 256 new tokens, decode the result, extract the model response, and store it as model_response for later analysis. section 7.7

Listing 7.9 Generating test set responses
from tqdm import tqdm

for i, entry in tqdm(enumerate(test_data), total=len(test_data)):
    input_text = format_input(entry)

    token_ids = generate(
        model=model,
        idx=text_to_token_ids(input_text, tokenizer).to(device),
        max_new_tokens=256,
        context_size=BASE_CONFIG["context_length"],
        eos_id=50256
    )
    generated_text = token_ids_to_text(token_ids, tokenizer)

    response_text = (
        generated_text[len(input_text):]
        .replace("### Response:", "")
        .strip()
    )
    test_data[i]["model_response"] = response_text

with open("instruction-data-with-response.json", "w") as file:
    json.dump(test_data, file, indent=4)         #1

The complete loop generates responses across the test dataset and saves the augmented records to instruction-data-with-response.json. section 7.7

How Build a Large Language Model (From Scratch) covers it

How deep it goes
Shows call arguments, decoding, prompt slicing, and response storage, but does not detail the generation algorithm internally.
Examples
Uses instruction prompts, decoded model responses, a three-entry comparison, and a full test-set JSON export.
What it assumes
Assumes a fine-tuned model, tokenizer, formatted inputs, token-conversion helpers, and a configured context length.
What you can do after
Apply it via generate calls to produce, decode, compare, and save instruction-following model responses.
How it connects
Connects instruction fine-tuning, tokenization, language-model decoding, held-out test sets, and model evaluation.

Also explained in

Compare how 2 books explain generate →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Large Language Model (From Scratch). The section links point to the parts of the book it draws on.