loader.tsx
Back to the Grimoire

VolumeBuild an LLM from Scratch

ch1 - Understanding LLMs

MAY MMXXVFolio I of II

What is a LLM?

Its a neural network with the capability of understand the input text and generate a competent answer. LLM AKA Large Language Models are trained with a vast amount of data and have severals neuronal layers that provide the ability of understand text, process and generate a response.

LLMs have 2 big stage of training called pre-training and fine-tuning. The pre-training stage its where de model is training with a large volume of data and after that obtain the ability of predict the next token, at that moment we have a foundation model . For example, given the input the sum of 2 + 2 is the model probably return 2 o something like that. After that, in the fine-tuning stage the foundation model is trained in a such of specific fields like classification, summarization, Personal assistant, etc.

Transformer Architecture

LLMs like GPTs (Generative Pre-trained Transformers) use the Transformer Architecture. The most basic transformer consist in a encoder and a decoder, the encoder takes the input a turn into a numerical representation or vectors that capture the contextual information of the input, this process is also called embedding. Then the decoder module takes this embeddings a generate a output text. Both the encoder and decoder consist of many layers connected by a so-called self-attention mechanism. This mechanism is important because allow to de model "keep in mind" the most relevant part of the input and how interact with other words or token in the sequence.

LLM Development & Fine-Tuning Workflow

Stage 1: Building the LLM

  1. Data Preparation & Sampling

    • Collect and clean a large text corpus
    • Apply sampling strategies (e.g., sentence-level, document-level, sharded)
  2. Attention Mechanism

    • Define queries, keys, and values
    • Implement causal or bidirectional masking as required
  3. LLM Architecture

    • Specify number of layers (n_layers)
    • Set model dimensionality (d_model), number of heads (num_heads)
    • Include normalization layers and dropout

→ Step 4: Pretraining

  • Train this architecture on unlabelled text
  • Output: Foundation Model (pretrained base model)

Stage 2: Foundation Model Training & Validation

  1. Training Loop

    • Forward pass, loss computation (e.g., cross-entropy)
    • Backpropagation and weight updates (Adam, LAMB, etc.)
  2. Model Evaluation

    • Monitor validation metrics (e.g., perplexity)
    • Use early stopping and checkpoints
  3. Load Pretrained Weights

    • Import the foundation model weights for downstream tasks

Stage 3: Fine-Tuning for Specific Applications

  1. Fine-Tuning → Classifier

    • Use a labeled dataset with class annotations
    • Replace or augment the head layer for classification outputs
    • Train in a supervised fashion
  2. Fine-Tuning → Personal Assistant / Chatbot

    • Use an instruction-response dataset
    • Fine-tune the entire model for conversational behavior