Overview of Qwen3.5 0.8B From Scratch implementation
mainlinear_attention and full_attention layers. The implementation is designed to be readable while leveraging specific linear-attention building blocks.repository·main·Indexed 13 days ago
https://github.com/rasbt/llms-from-scratchOfficial implementation code for the book 'Build a Large Language Model (From Scratch)'. This repository provides a step-by-step guide to developing, pretraining, and finetuning a GPT-like Large Language Model using PyTorch, including implementations of BPE tokenizers, attention mechanisms, and multi-GPU training via DDP and torchrun.
linear_attention and full_attention layers. The implementation is designed to be readable while leveraging specific linear-attention building blocks.Tiny Aya is a 3.35B parameter multi-lingual decoder-style transformer model by Cohere. It is designed for local experimentation and research, though it is restricted to non-commercial use due to its licensing terms.
There are several specialized versions of the model optimized for different linguistic regions:
tiny-aya-base: The base model.tiny-aya-global: Provides the best balance across all languages and regions (default in notebooks).tiny-aya-fire: Optimized for South Asian languages (e.g., Hindi, Bengali, Tamil, Telugu).tiny-aya-water: Optimized for Asia Pacific (e.g., Chinese, Japanese, Korean, Vietnamese) and European languages (e.g., English, French, German, Spanish).tiny-aya-earth: Optimized for West Asian (e.g., Arabic, Turkish, Hebrew) and African languages (e.g., Swahili, Yoruba, Zulu).01_main-chapter-code directory. This chapter serves as the foundation for understanding how GPT architectures are constructed and used to generate text.The code for Chapter 5 is organized into main chapter components and optional standalone scripts.
Main Chapter Code:
ch05.ipynb: The primary Jupyter Notebook containing all code as presented in the chapter.previous_chapters.py: A required utility module containing the MultiHeadAttention module and GPTModel class. This must be imported into ch05.ipynb to facilitate GPT model pretraining.gpt_download.py: Contains utility functions for downloading pretrained GPT model weights.exercise-solutions.ipynb: Contains the solutions to the exercises provided in the chapter.Optional Standalone Scripts:
gpt_train.py: A standalone Python script summarizing the GPT model training implementation found in the notebook.gpt_generate.py: A standalone Python script for loading and using pretrained model weights from OpenAI.A KV (Key-Value) cache is a mechanism used during LLM inference to store intermediate key (K) and value (V) computations from previous tokens.
Why use it? During text generation, LLMs produce one token at a time. Without a cache, the model must recompute the K and V vectors for all previous tokens in every single generation step, leading to quadratic $O(n^2)$ complexity. With a KV cache, you only compute K and V for the newly generated token and append it to the existing cache, reducing per-step complexity to linear $O(n)$.
Trade-offs:
Sliding Window Attention (SWA) is a local attention mechanism used as an alternative to global Multi-Head Attention (MHA). Instead of each token attending to every other token in a sequence, each token only attends to a fixed-size local window around its position.
This approach significantly reduces the size of the KV cache, leading to substantial memory and compute savings. Modern models like Google's Gemma 3 use a hybrid approach, combining SWA layers with full attention layers (e.g., a 5:1 ratio) to balance efficiency with the ability to model global context.
Gated DeltaNet (Gated Delta Network) is a linear-attention layer used as an alternative to standard softmax attention. It is designed to scale linearly with context length rather than quadratically. It combines the gated decay mechanism of Mamba2 with a delta rule, where the delta rule computes the difference between new and predicted values to update a hidden memory state.
Key characteristics:
The CloudFormation template includes a lifecycle configuration script that automates the installation of a specialized Python environment. This ensures all deep learning dependencies are correctly configured with CUDA support for GPU acceleration.
2.15.0 (with CUDA support)2.1.0 (with CUDA support)The Qwen3Model class is designed to be a drop-in replacement for the GPTModel class used in earlier chapters of this project. This allows you to use it for:
Mixture of Experts (MoE) is a technique to increase a model's capacity without proportionally increasing inference compute. It replaces standard dense feed-forward (FFN) modules with multiple 'expert' layers (also FFNs).
Key concepts:
top_k out of num_experts) per token. This makes the module 'sparse'.The KVCache class manages the Key-Value cache across transformer layers to improve compute efficiency during autoregressive generation.
get(layer_idx): Retrieves the cache for a specific layer.update(layer_idx, value): Updates the cache for a specific layer.reset(): Clears all cached values.In the Qwen3Model.forward pass, if a cache object is provided, the model uses it to append new keys and values instead of recomputing the entire sequence history.
class KVCache:
def __init__(self, n_layers):
self.cache = [None] * n_layers
def get(self, layer_idx):
return self.cache[layer_idx]
def update(self, layer_idx, value):
self.cache[layer_idx] = value
def get_all(self):
return self.cache
def reset(self):
for i in range(len(self.cache)):
self.cache[i] = None