Open Source LLMs: Llama, Mistral, Phi

Open-source large language models (LLMs) — most notably Meta's Llama, Mistral AI's Mistral, and Microsoft's Phi families — are powerful transformer-based text generation systems whose weights are publicly released, enabling anyone to download, run, fine-tune, and study them. They matter because they democratize access to frontier AI capabilities that were previously locked behind proprietary APIs, allowing researchers, startups, and individuals to build without usage fees or data-privacy concerns. The counterintuitive fact: Microsoft's Phi-2 (2.7B parameters) outperforms many models ten times its size on reasoning benchmarks, proving that carefully curated training data matters far more than raw scale.

The core insight

The "aha moment" is this: a language model is just a very large function that takes a sequence of tokens and predicts the next token — and if you train that function on enough high-quality text with the right architecture, remarkably general intelligence emerges. Open-source LLMs prove you don't need a billion-dollar data center to use this intelligence; you only need one to train it from scratch. Once the weights exist, running inference is cheap enough to do on a laptop. The real competition between Llama, Mistral, and Phi is therefore not about raw size, but about the training recipe: what data you use, how you fine-tune, and what architectural tricks you apply.

How It Works

Open-source LLMs are all built on the transformer architecture introduced in 2017. The fundamental operation is self-attention: every token in a sequence looks at every other token and decides how much to "attend" to it when producing the next prediction. What distinguishes Llama, Mistral, and Phi from each other — and from the original transformer — is a set of specific architectural choices and training innovations layered on top of this foundation.

Tokenization: Turning Text into Numbers

Before any math happens, text is split into tokens — subword chunks — using a learned vocabulary. Llama and Mistral both use SentencePiece with a ~32,000-token vocabulary. Phi uses a tiktoken-based vocabulary with ~50,000 tokens. ```python

# Install: pip install transformers from transformers import AutoTokenizer tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-hf") text = "Open source LLMs are powerful." tokens = tokenizer.encode(text) print(tokens) # e.g. [1, 4673, 2752, 365, 28755, 29879, ...] print(tokenizer.decode(tokens)) # "Open source LLMs are powerful." ```

The Attention Score

The core computation that makes transformers work is the scaled dot-product attention formula:

1
Attention(Q,K,V)=softmax ⁣(QKTdk)V\quad \text{Attention}(Q, K, V) = \text{softmax}\!\left(\frac{QK^T}{\sqrt{d_k}}\right)V

Explanation: Each query vector Q "asks a question," each key vector K "offers an answer," and the dot product measures how well they match; dividing by √dₖ prevents the values from growing so large that softmax saturates.

  • Q (Query): what the current token is "looking for"
  • K (Key): what each token "offers" to be matched against
  • V (Value): the actual information each token carries
  • dₖ: the dimension of the key vectors (e.g. 128 per head in Llama-7B)
  • softmax: converts raw scores into weights that sum to 1

RMS Normalization

Llama replaced the original LayerNorm with RMSNorm, which is faster because it skips computing the mean:

2
RMSNorm(x)=x1n∑i=1nxi2+ϵ⋅γ\quad \text{RMSNorm}(x) = \frac{x}{\sqrt{\frac{1}{n}\sum_{i=1}^{n} x_i^2 + \epsilon}} \cdot \gamma

Explanation: This rescales each activation vector to have unit root-mean-square magnitude, stabilizing training without the extra cost of computing a mean shift.

  • x: the input activation vector
  • n: the number of elements in x
  • ε: a tiny constant (e.g. 1e-6) added for numerical stability
  • γ: a learned per-dimension scale parameter

Rotary Positional Embedding (RoPE)

Both Llama and Mistral use RoPE instead of learned position embeddings, encoding position by rotating query and key vectors:

3
q~m=qm⋅eimθ\quad \tilde{q}_m = q_m \cdot e^{im\theta}

Explanation: Multiplying a query at position m by a complex rotation of angle m·θ encodes its position so that when you take the dot product with a key at position n, only the relative gap (m−n) influences the attention score.

  • q̃ₘ: the rotated query at sequence position m
  • qₘ: the original query vector
  • e^{imθ}: a complex rotation (implemented as paired 2D rotations in practice)
  • θ: a base frequency (default 10,000), extended in later models for longer contexts

Sliding Window Attention (Mistral's Key Innovation)

Mistral 7B introduced Grouped Query Attention (GQA) and Sliding Window Attention (SWA) to handle long contexts efficiently. With a window of size W, each token only attends to the W most recent tokens: ```python

# Conceptual illustration of sliding window attention import torch def sliding_window_mask(seq_len: int, window: int) -> torch.Tensor: """Returns a boolean mask: True where attention is allowed.""" mask = torch.zeros(seq_len, seq_len, dtype=torch.bool) for i in range(seq_len): start = max(0, i - window + 1) mask[i, start:i+1] = True return mask mask = sliding_window_mask(seq_len=8, window=3) print(mask.int())

# Each row: token can only see itself + 2 previous tokens ```

Running a Model: The Minimal Example

```python from transformers import AutoModelForCausalLM, AutoTokenizer import torch model_name = "microsoft/phi-2" # ~2.7B params, fits on 8GB GPU tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True) model = AutoModelForCausalLM.from_pretrained( model_name, torch_dtype=torch.float16, # half precision = half the memory device_map="auto", # auto-place layers on GPU/CPU trust_remote_code=True, ) prompt = "Explain transformers in one sentence:" inputs = tokenizer(prompt, return_tensors="pt").to(model.device) with torch.no_grad(): output = model.generate( **inputs, max_new_tokens=60, temperature=0.7, # randomness: 0=greedy, 1=sampling do_sample=True, ) print(tokenizer.decode(output[0], skip_special_tokens=True)) ```

Instruction Tuning and RLHF

A base model only predicts next tokens — it doesn't "follow instructions." To make a chat model, two extra steps are applied: 1. Supervised Fine-Tuning (SFT): continue training on (instruction, ideal-response) pairs 2. Reinforcement Learning from Human Feedback (RLHF) or Direct Preference Optimization (DPO): optimize so the model prefers responses humans rated higher ```python

# Simplified SFT data format (the actual format used by Llama-2-chat) sft_example = { "instruction": "Summarize the following article in 3 bullet points.", "input": "...article text...", "output": "• Point 1\n• Point 2\n• Point 3", }

# Libraries: trl (TransformersRL), axolotl, LLaMA-Factory all automate this ```

researcher · Ashish Vaswani | b. 1983 | Invented the Transformer Architecture

Vaswani led the Google Brain team that published "Attention Is All You Need" (2017), the paper that eliminated recurrent networks entirely and introduced the pure-attention transformer that all modern LLMs — Llama, Mistral, and Phi included — are built upon. His specific contribution was showing that multi-head self-attention alone, without any convolution or recurrence, could achieve state-of-the-art machine translation while being dramatically faster to train in parallel. Surprisingly, Vaswani left Google shortly after the paper's publication and later co-founded Adept AI before moving to independent research.

researcher · Guillaume Lample | b. 1990 | Co-Creator of Llama

Lample is a French researcher who, while at Meta AI (FAIR), co-led the development of the original Llama model family alongside Hugo Touvron, establishing that a carefully trained smaller model could match GPT-3-scale performance. His earlier work on cross-lingual language models and unsupervised machine translation laid the groundwork for the multilingual pretraining recipes used in Llama 2 and beyond. Notably, Lample left Meta in 2023 to co-found Mistral AI — meaning the same researcher is directly responsible for two of the three model families covered in this article.

Figure
Open-Source LLM Architecture Comparison
Open-Source LLM Architecture Comparison LLAMA 3 (8B) MISTRAL 7B PHI-3 (3.8B) Params Layers Attn Heads Context Positional Norm Attn Type License Train Tokens 8B 32 32 (GQA: 8 KV) 8 192 tokens RoPE RMSNorm GQA Llama 3 Custom 15T+ 7.3B 32 32 (GQA: 8 KV) 32 768 tokens RoPE RMSNorm GQA + SWA Apache 2.0 ~1T (est.) 3.8B 32 32 128 000 tokens RoPE LayerNorm MHA MIT ~3.3T (quality) shared innovation model-unique feature
Figure
Token Prediction Data Flow
How an LLM Generates One Token Input Text "The cat sat" Tokenizer [464, 3797, 7057] Embeddings 3 × 4096 floats 32 × Transformer Block Attn + FFN + Norm Logits → Softmax P over 32k tokens Sample: "on" append → repeat → autoregressive loop

Intuition

The Mental Model

Imagine the model as an incredibly well-read librarian who has absorbed the patterns in trillions of words. When you hand them a partial sentence, they don't look up the "correct" continuation in a database — they use their internalized sense of language patterns to guess what word most plausibly comes next. This is all a language model does, one token at a time, in an autoregressive loop. The transformer's self-attention mechanism is like the librarian's ability to hold the entire sentence in mind simultaneously and consider how every word relates to every other word before making a prediction — unlike older recurrent networks that had to read left-to-right and often "forgot" early context. The difference between Llama, Mistral, and Phi is mostly in the quality and composition of the librarian's reading list: Phi-3 was trained on heavily filtered, textbook-quality synthetic data, which is why a small librarian with focused education outperforms a much larger one who read more but read widely.

Historical Development

Origins · 1 minhow this was discovered
History
2017
Vaswani et al. publish "Attention Is All You Need" (Google Brain) — the transformer architecture is born, replacing RNNs with pure self-attention and enabling massively parallel training.
2020
OpenAI releases GPT-3 (175B parameters) via API only — demonstrating emergent few-shot capabilities but keeping weights entirely proprietary, motivating the open-source movement.
2022
Meta AI releases OPT (Open Pre-trained Transformer) — an early attempt at open-weight large models (up to 175B), but training instabilities and limited quality disappoint the community.
2023
(Feb): Meta releases Llama 1 (7B–65B) — a landmark moment; within days the weights are leaked publicly, sparking an explosion of community fine-tuning projects including Alpaca, Vicuna, and WizardLM.
2023
(Jul): Meta releases Llama 2 with an official license permitting commercial use under 700M daily active users — the first time a competitive LLM was properly licensed for business use, with Llama-2-chat trained via RLHF.
2023
(Sep): Mistral AI (founded by ex-Google DeepMind and Meta researchers) releases Mistral 7B under Apache 2.0 — outperforming Llama-2-13B despite being nearly half the size, thanks to sliding window attention and grouped query attention.
2023
(Dec): Microsoft Research releases Phi-2 (2.7B) — trained almost entirely on synthetic "textbook-quality" data generated by GPT-4, demonstrating that data quality can substitute for parameter count and beating many 7B models on benchmarks.
2024
(Apr): Meta releases Llama 3 (8B and 70B) trained on 15T+ tokens — competitive with GPT-3.5 on most benchmarks and achieving strong multilingual performance; Llama 3.1 405B follows in July.
2024
(Apr): Microsoft releases Phi-3-mini (3.8B) with 128K context window and MIT license — runs entirely on a smartphone, achieving GPT-3.5-level performance on many tasks with a model small enough to fit in RAM.

Why It Matters

Real-World Impact
Local Private Assistants (Llama 3 8B via Ollama)Privacy-sensitive organizations like hospitals and law firms run Llama 3 locally using tools like Ollama or LM Studio — patient records and legal documents never leave the building, something impossible with cloud-only APIs like GPT-4.
Edge Deployment (Phi-3-mini on Android)Microsoft demonstrated Phi-3-mini running entirely on a Qualcomm Snapdragon phone with no internet connection, enabling AI features in apps that must function offline — translation, document summarization, and code completion all on-device.
Fine-Tuning for Domain Specialization (Llama 3 + LoRA)Companies use parameter-efficient fine-tuning (LoRA/QLoRA) on Llama 3 to create specialized models for customer support, medical coding, or legal drafting in hours on a single GPU — cost measured in dollars rather than millions.
Open Research Benchmarking (Mistral 7B)Academic labs that previously could not afford GPT-4 API costs now run controlled ablation studies on Mistral 7B, significantly accelerating publishable research in NLP and advancing understanding of scaling laws.
Mixture-of-Experts Scaling (Mixtral 8×7B)Mistral's follow-up model uses a sparse mixture-of-experts architecture where only 2 of 8 expert FFN layers activate per token — achieving 70B-equivalent quality at 13B active-parameter inference cost, deployed in production at many European AI startups.
Retrieval-Augmented Generation (RAG) PipelinesLlama and Mistral models are the most commonly embedded as the "generator" in RAG systems (using LangChain or LlamaIndex) that answer questions grounded in private document databases — combining