LLM Basics — Cheat Sheet

Tags: llm, nlp, transformers, generative-ai, prompt-engineering

Level: basic

TL;DR: Large Language Models (LLMs) are deep learning models based on the Transformer architecture, trained on massive datasets to predict the next token, enabling them to understand and generate human-like text.

Key concepts

Transformer Architecture: Core mechanism introduced in "Attention Is All You Need", relying on self-attention to process entire sequences in parallel.

Tokens: The fundamental units of data processed by an LLM, which can be words, subwords, or characters.

Pre-training: Self-supervised learning phase where the model predicts the next token on a massive corpus, learning syntax, semantics, and facts.

Fine-tuning (SFT): Supervised learning phase adapting the pre-trained model to specific tasks like instruction following or chat.

RLHF (Reinforcement Learning from Human Feedback): Alignment technique using reward models to align model outputs with human preferences.

Context Window: The maximum number of tokens the model can process in a single inference step, combining input and output.

Temperature: Parameter controlling the randomness of predictions; higher values increase creativity, lower values make it deterministic.

Hallucinations: Instances where the model generates plausible but factually incorrect or nonsensical information.

Typical questions and ready answers

Q: How does a Transformer model differ from traditional RNNs?R: Transformers use self-attention to process all tokens simultaneously rather than sequentially. This allows for massive parallelization during training and better handling of long-range dependencies.

Q: Explain the difference between pre-training and fine-tuning.R: Pre-training builds the base knowledge by predicting the next token on vast unlabeled text. Fine-tuning uses smaller, labeled datasets to adapt the model for specific tasks like QA or conversational chat.

Q: What is the role of the attention mechanism?R: It allows the model to weigh the importance of different words in the input sequence relative to a specific word, capturing context and relationships regardless of distance.

Pitfalls and common mistakes

Assuming the models have true reasoning capability rather than sophisticated probabilistic pattern matching.

Failing to account for the context window limit, leading to truncated inputs or forgotten instructions in production.

Setting the temperature too high for tasks requiring precise, factual, or structured answers.

Trusting outputs blindly without verifying facts, exposing applications to severe hallucination risks.

Snippets and commands

# Basic text generation using Hugging Face Transformers pipeline
from transformers import pipeline

generator = pipeline("text-generation", model="gpt2")
response = generator("The fundamental concept behind LLMs is", max_length=50, temperature=0.7)
print(response[0]['generated_text'])

To review

[ ] Attention matrix calculations (Query, Key, Value vectors)

[ ] Differences between encoder-only, decoder-only, and encoder-decoder architectures

[ ] Tokenization strategies (BPE, WordPiece, SentencePiece)

References

"Attention Is All You Need" (Vaswani et al., 2017)

Hugging Face Transformers Documentation

Jarvis Interviews
Jarvis