Llama 3
lastmod 2024-08-16created 2024-04-2213 edits
Local Demo
Steps:
- Download the Meta-Llama-3-8B-Instruct.Q8_0.gguf file from the Hugging Face repository
-
git clonehttps://github.com/teddylee777/langserve_ollama -
cd ./langserve_ollama/ollama-modelfile/Llama3-8B-Instruct -
ollama create llama3-instruct-8b -f Modelfile -
ollama list -
ollama run llama3-instruct-8b
LLaMA 3 RAG
Ollama is required.
Implementing Llama3 from scratch
llama3 implementation one matrix multiplication at a time
- Implements Llama3 from scratch, one tensor and matrix multiplication at a time.
- Loads tensors directly from the Llama3 model file provided by Meta
Summary of the from-scratch implementation of the LLaMA-3 model
- Tokenizer setup
- Set up the tokenizer using the Tiktoken library
- Define special tokens and add them to the tokenizer
- Load the model file (consolidated.00.pth) using PyTorch
- Read the model configuration from the params.json file
- Includes information such as the number of dimensions (dim), number of layers (n_layers), and number of heads (n_heads)
- Convert the prompt text into a token sequence using the tokenizer
- Convert each token into its corresponding embedding
- Normalize the embeddings using RMS normalization
- Load the query (wq), key (wk), value (wv), and output (wo) matrices from the model
- Compute query, key, and value vectors for each token
- Add positional information using RoPE (Rotary Positional Embedding)
- Compute attention scores by taking the dot product of queries and keys
- Mask attention scores for future tokens
- Apply the Softmax function to compute the attention distribution
- Multiply the attention distribution by the value vectors to compute the attention output
- Perform the attention computation for all attention heads
- Concatenate the results of each head to produce the final attention output
- Implement a feed-forward network using the SwiGLU (Swish Gated Linear Unit) activation function
- Add the attention output and the feed-forward network output to produce the final embedding
- Repeat the attention and feed-forward network computations for all transformer layers
- Apply RMS normalization to the final embedding
- Multiply the final embedding by the output matrix to compute the logits
- Predict the token with the highest logit value as the next token
- Decode and output the predicted token
GN⁺'s opinion
- This article is very useful for understanding the internal structure and behavior of the Llama3 model. In particular, the process of implementing it from scratch makes it clear how each component of the model interacts.
- It may be somewhat complex for junior software engineers. However, it is explained well step by step, so you can understand it by following along slowly.
- You can learn how advanced concepts such as RoPE (rotary positional embedding) are introduced to improve model performance. This can be useful when implementing or improving other NLP models.
- This article provides a deep understanding of the internal structure and behavior of deep learning models. This will be a great help when optimizing or debugging models.
See also
- LLM
- llama
- llama 2
- ollama
- LangChain
- RAG
- LLaVA
- nano-llama31 - Llama 3.1 version in the style of nanoGPT
Favorite site
- Introducing Meta Llama 3: The most capable openly available LLM to date
- Meta releases Llama 3. Shocking performance.. GPT-4-class AI running on an "iPhone"? - YouTube
- Llama 3 finally released! | Performance comparison, how to download, speed test - YouTube
- Llama 3 model implemented in pure NumPy | GeekNews
Pages that link here