Skip to content
ml-recall

Topic notes

LLM Fundamentals & Prompting

What a large language model is and how it is used — next-token pretraining, context windows, chat templates, generation settings, zero-/few-shot and chain-of-thought prompting, and evaluation.

3-min read AI-checked

Practise 36 questions Take a test

Key ideas

Formulas

Worked example

A tokenized text has 12 ids (100–111). Cutting training windows of length L=4L = 4:

stride window starts windows next-token targets
4 0, 4 2 8
2 0, 2, 4, 6 4 16
1 0, …, 7 8 32

A window starting at 8 is impossible: its last target would be the 13th token. Smaller strides give more (overlapping) examples from the same text.

In code

import torch

ids = torch.arange(100, 112)                          # 12 token ids of a tokenized text
L, stride = 4, 4                                      # context length, step between windows
starts = range(0, len(ids) - L, stride)               # each window needs one extra token as target
X = torch.stack([ids[i:i + L] for i in starts])
Y = torch.stack([ids[i + 1:i + L + 1] for i in starts])
print(X.tolist())                                     # [[100, 101, 102, 103], [104, 105, 106, 107]]
print(Y.tolist())                                     # [[101, 102, 103, 104], [105, 106, 107, 108]]
print(len(range(0, len(ids) - L, 2)))                 # 4 windows with stride 2

Common pitfalls

References

  1. Hugging Face LLM Course — Ch. 1, Natural Language Processing and Large Language Models (The Rise of Large Language Models)
  2. Hugging Face LLM Course — Ch. 1, Deep dive into Text Generation Inference with LLMs (Context Length, Two-Phase Inference, Sampling Strategies)
  3. Hugging Face LLM Course — Ch. 11, Chat Templates (Base Models vs Instruct Models, Common Template Formats)
  4. Brown et al. (2020), Language Models are Few-Shot Learners (GPT-3) — Sec. 2 Approach (zero-, one-, few-shot); Sec. 4 Measuring and Preventing Memorization Of Benchmarks
  5. Wei et al. (2022), Chain-of-Thought Prompting Elicits Reasoning in Large Language Models — Sec. 2 Chain-of-Thought Prompting, 3.2 Results
  6. Hugging Face LLM Course — Ch. 11, Evaluation (Automatic Benchmarks, Alternative Evaluation Approaches, Custom Evaluation)
  7. Yao et al. (2023), Tree of Thoughts: Deliberate Problem Solving with Large Language Models — Abstract; Sec. 3 Tree of Thoughts (thought generation, state evaluation, search with backtracking)