liveBook · Manning

Language model

As explained in Transformers in Action

A large language model is a neural language model trained on large text corpora to predict and generate natural-language sequences.

A large language model (LLM) is a neural language model trained on a large text corpus to predict and generate natural-language sequences. Modern LLMs generally use a transformer architecture: attention weighs relationships among parts of a sequence, helping the model use context when processing or generating text. section 1.4 section 1.0

The architecture depends on the task. Encoder–decoder models use input context for tasks such as translation; decoder-only models are common for text completion and conversational AI. Changes to attention and positional encoding can extend the length and complexity of context a model handles. Model size alone is not a selection rule: specialized domains may expose weaknesses, while computation and memory costs can complicate deployment. section 1.4 section 1.5

Working with an LLM involves choices beyond its architecture:

  • Adapt a pretrained model to a task through fine-tuning; parameter-efficient fine-tuning aims to reduce the resources needed. section 1.4 section 8.0
  • Manage training and evaluation costs with techniques such as quantization, pruning, distillation, and sharding; track experiments and tune hyperparameters with tools such as Ray and Weights & Biases. section 8.0
  • Check pretraining data and generated outputs for bias or toxicity, and plan safeguards for production use. section 10.5

Generate a response after fine-tuning

Format a question with the model’s prompt template, tokenize it, move the inputs to the selected device, call `model.generate`, and decode the output. A labeled fine-tuning dataset—for example, one specifying a company policy—can change how the model responds. The inference call alone does not establish whether it follows that policy reliably. section 5.2.3 section 5.2.3

Listing 5.21 Running inference on the trained model
prompt = model_tokenizer( #1
    [
      alpaca_template.format(
        "What is the iconic symbol of freedom at the US east coast?",   #2
          "",   #3
        "",   #4
      )
    ] * 1, return_tensors="pt").to("cuda") #5

generation_parameters = {
    "max_new_tokens": 256,  #6
    "use_cache": True   #7
}

outputs = model.generate(**prompt, **generation_parameters) #8
decoded_outputs = model_tokenizer.batch_decode(outputs,
skip_special_tokens=True) #9

The listing shows the complete inference path from a formatted question through tokenization, generation, and decoding; `max_new_tokens` bounds the generated response. section 5.2.3

How Transformers in Action covers it

How deep it goes
Explains the transformer foundation and model-variant trade-offs, then gives a concrete inference workflow rather than a from-scratch LLM implementation.
Examples
Uses translation and conversational AI as task contrasts, plus a tokenized question passed through generation and decoding.
What it assumes
Assumes familiarity with basic model inference code; introduces attention’s role and the distinction between major transformer variants.
What you can do after
Apply it via a model tokenizer and `model.generate` to produce a response, while considering adaptation and deployment costs.
How it connects
Connects attention and positional encoding to transformer variants, and fine-tuning, quantization, and safeguards to practical deployment.

Also explained in

Compare how 67 books explain language model →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Transformers in Action. The section links point to the parts of the book it draws on.