WhoBuilt

Low Level Technicals of LLMs

Jul 31, 2024· 2 h 52 min· 57K views·Watch on YouTube

Speaker

Daniel Han of Unsloth and Un0.ai leads a workshop at the AI Engineer’s World Fair on language model internals, implementation bugs, and fine-tuning, with audience questions throughout.

Why model implementations need careful review

Han describes finding differences and bugs across implementations of Gemma, Grok, and NVIDIA NeMo, including exact versus approximate GELU. He explains that checking model code against papers and reference implementations can uncover issues, but deciding which implementation is correct often requires human judgment. Tokenizers bring separate challenges, including inconsistent handling across model variants.

How next-token prediction and attention work

Han presents the Transformer as a stack of attention, normalization, and MLP operations, repeated across layers. He explains token IDs and embeddings, then shows how training aligns each token with the next token as its target. A causal attention mask prevents future tokens from influencing predictions and lets the model process the sequence in parallel.

Transformer components and numerical choices

Han walks through attention as softmax of query-key scores multiplied by values, and describes RoPE as a way to encode token positions. He discusses layer norms and residual connections as choices that can make training more stable, and compares MLP designs including SwiGLU and squared ReLU. He also highlights precision and casting decisions, noting that implementation details can affect numerical stability and model behavior.

Finding and fixing architecture bugs

Using Gemma and Phi-3 as examples, Han shows how to compare layer outputs across implementations and inspect model papers and configuration files. He describes fixes involving RoPE precision, GELU variants, sliding-window size, padding tokens, and fused Q, K, and V weights. For Gemma, the goal was to match the original implementation, even when another change appeared to reduce measured error.

Fine-tuning workflows and model export

Han introduces Unsloth’s fine-tuning optimizations, reporting up to two times faster training and lower memory use without accuracy loss. He covers long-context fine-tuning with gradient checkpointing and system RAM offloading, then demonstrates dataset formatting, chat templates, and exporting models to GGUF for Ollama. He emphasizes testing small experiments before scaling training and checking templates because formatting errors can harm model responses.

Takeaways

  • Compare model implementations with a trusted reference and inspect intermediate layer outputs to locate discrepancies.
  • Use causal masking to train on next-token targets across a sequence without constructing every prefix separately.
  • Treat tokenizer behavior, precision, and configuration values as part of model correctness, not just implementation details.
  • Test training choices on smaller runs before committing to expensive large-scale training.
  • Define and validate chat templates and dataset formatting before fine-tuning or exporting a model.

Mentioned in this talk

Search inside this talk

What was claimed 6

  • feature
    Unsloth: Fine-tuning language models up to two times faster with 80% less memory usage, with no accuracy degradation, as described in the talk
    “generally makes fine-tuning language models ... up to two times faster, and we have 80% less memory usage now”
    YouTube
    Unverified1 source· Talk
  • launched
    With his brother
    “Unsloth—we launched this last December. My brother and I launched it.”
    YouTube
    Unverified1 source· Talk
  • uses
    “We use Triton kernels from OpenAI.”
    YouTube
    Unverified1 source· Talk
  • feature
    Unsloth: Long-context fine-tuning methodology increases context size by four times with 1–2% execution slowdown
    “you can increase your context size by four times ... execution is only 1–2% slower”
    YouTube
    Unverified1 source· Talk
  • feature
    Unsloth: Chunked cross-entropy to reduce memory use and support large vocabulary sizes
    “you can use chunked cross-entropy, and this is very helpful for large models”
    YouTube
    Unverified1 source· Talk
  • feature
    Unsloth: Exports fine-tuned models to GGUF and automatically creates a model file using the chat template
    Exports fine-tuned models to GGUF and automatically creates a model file using the chat template
    “You must merge it for Ollama support and GGUF ... we also automatically create a model file using your chat template.”
    YouTube
    Unverified1 source· Talk

Related talks