|$ curl https://forge-ai.dev/api/markdown?path=docs/ai/open-source
$cat docs/ai-engineering-—-open-source-models.md
updated Recently·45 min read·published

AI Engineering — Open Source Models

AI EngineeringModelsIntermediate to Advanced
Introduction

Open source language models have reached parity with proprietary models on many benchmarks. Leading models like Llama 3, Mistral, Qwen, DeepSeek, and Gemma offer competitive performance with the benefits of local deployment, data privacy, custom fine-tuning, and no per-token costs.

The open source ecosystem includes model hubs (Hugging Face), inference engines (vLLM, Ollama, TGI, llama.cpp), quantization techniques (GGUF, AWQ, GPTQ), and tooling for fine-tuning, evaluation, and deployment. This ecosystem enables self-hosted AI that rivals cloud API quality.

Leading Open Models

The open source LLM landscape evolves rapidly. Here are the current leading model families and their key characteristics.

ModelDeveloperParametersContextLicense
Llama 3.1 / 3.2Meta8B, 70B, 405B128KLlama 3 Community
Mistral / MixtralMistral AI7B, 8x7B (MoE), 12B, 123B32K-128KApache 2.0 / MIT
Qwen 2.5Alibaba0.5B, 1.5B, 7B, 14B, 32B, 72B (MoE)32K-128KApache 2.0
DeepSeek V2 / V3DeepSeek (幻方)16B (MoE), 67B, 236B (MoE)128KMIT
Gemma 2Google2B, 9B, 27B8KGemma License
CodeLlama / Llama-CodeMeta7B, 13B, 34B, 70B16K-100KLlama 3 Community

info

For most applications, a 7B-8B parameter model quantized to 4-bit offers the best quality-to-performance ratio. The 70B+ models are 5-10x slower and require multi-GPU setups. Start small, benchmark your use case, then scale up if needed.
Hugging Face Hub

Hugging Face is the primary hub for open source models, datasets, and spaces. The transformers library provides a unified API for loading and running thousands of models. Use pipeline for quick inference or direct model loading for more control.

huggingface-transformers.py
Python
1# Load any model with Hugging Face Transformers
2# pip install transformers torch accelerate
3
4from transformers import pipeline
5
6# Quick pipeline API
7generator = pipeline(
8 "text-generation",
9 model="meta-llama/Llama-3.2-8B-Instruct",
10 device="mps", # or "cuda", "cpu"
11 torch_dtype="auto"
12)
13
14result = generator(
15 "What is machine learning?",
16 max_new_tokens=256,
17 do_sample=True,
18 temperature=0.7
19)
20
21print(result[0]["generated_text"])
22
23# Direct model loading for more control
24from transformers import AutoModelForCausalLM, AutoTokenizer
25
26model_name = "mistralai/Mistral-7B-Instruct-v0.3"
27
28tokenizer = AutoTokenizer.from_pretrained(model_name)
29model = AutoModelForCausalLM.from_pretrained(
30 model_name,
31 device_map="auto",
32 load_in_4bit=True, # 4-bit quantization
33 bnb_4bit_compute_dtype="float16"
34)
35
36messages = [
37 {"role": "user", "content": "Explain quantum computing in 3 sentences."}
38]
39
40inputs = tokenizer.apply_chat_template(
41 messages,
42 add_generation_prompt=True,
43 return_tensors="pt"
44).to(model.device)
45
46outputs = model.generate(
47 inputs,
48 max_new_tokens=256,
49 temperature=0.7
50)
51
52response = tokenizer.decode(outputs[0], skip_special_tokens=True)
53print(response)
Self-Hosting Inference Engines

Several inference engines are optimized for running open models efficiently. Choose based on your hardware, throughput needs, and feature requirements.

vLLM — High-Throughput Production

vLLM uses PagedAttention for efficient memory management, achieving 10-20x higher throughput than naive implementations. Supports continuous batching, tensor parallelism, and an OpenAI-compatible API.

vllm.py
Python
1# pip install vllm
2
3# Start an OpenAI-compatible API server
4# python -m vllm.entrypoints.openai.api_server \
5# --model meta-llama/Llama-3.2-8B-Instruct \
6# --port 8000 \
7# --tensor-parallel-size 2 \
8# --max-model-len 8192
9
10# Client code (identical to OpenAI SDK)
11from openai import OpenAI
12
13client = OpenAI(
14 base_url="http://localhost:8000/v1",
15 api_key="not-needed" # vLLM doesn't require auth
16)
17
18response = client.chat.completions.create(
19 model="meta-llama/Llama-3.2-8B-Instruct",
20 messages=[
21 {"role": "user", "content": "Hello!"}
22 ]
23)
24
25print(response.choices[0].message.content)
26
27# Programmatic usage
28from vllm import LLM, SamplingParams
29
30llm = LLM(
31 model="mistralai/Mistral-7B-Instruct-v0.3",
32 tensor_parallel_size=1,
33 max_model_len=8192
34)
35
36params = SamplingParams(
37 temperature=0.7,
38 max_tokens=512,
39 top_p=0.95
40)
41
42outputs = llm.generate(
43 ["Explain AI in one paragraph."],
44 params
45)
46
47for output in outputs:
48 print(output.outputs[0].text)

Ollama — Local Development

Ollama provides the simplest local setup. Download, run ollama pull llama3.2, and you have a running model. Ideal for development, prototyping, and personal use.

ollama-setup.sh
Bash
1# Install: brew install ollama
2# Download a model
3ollama pull llama3.2
4ollama pull mistral
5ollama pull qwen2.5:7b
6
7# Run interactive
8ollama run llama3.2
9
10# API (runs on port 11434 by default)
11curl http://localhost:11434/api/generate -d '{
12 "model": "llama3.2",
13 "prompt": "What is machine learning?",
14 "stream": false
15}'
16
17# Python client
18# pip install ollama
19import ollama
20
21response = ollama.chat(
22 model="llama3.2",
23 messages=[
24 {"role": "user", "content": "Hello!"}
25 ]
26)
27print(response["message"]["content"])

TGI & llama.cpp

Hugging Face TGI (Text Generation Inference) is optimized for production with continuous batching and flash attention. llama.cpp is optimized for CPU and Apple Silicon with GGUF quantization.

tgi-llamacpp.sh
Bash
1# TGI — Docker deployment
2# docker run -p 8080:80 \
3# ghcr.io/huggingface/text-generation-inference:latest \
4# --model-id meta-llama/Llama-3.2-8B-Instruct
5
6# llama.cpp — CPU/Apple Silicon optimized
7# git clone https://github.com/ggerganov/llama.cpp
8# cd llama.cpp && make
9
10# Download GGUF model
11# wget https://huggingface.co/TheBloke/Llama-3.2-8B-Instruct-GGUF/resolve/main/llama-3.2-8b-instruct.Q4_K_M.gguf
12
13# Run server
14./llama-server \
15 -m models/llama-3.2-8b-instruct.Q4_K_M.gguf \
16 --port 8080 \
17 -ngl 99 # GPU layers (99 = all on GPU)
Quantization

Quantization reduces model precision (e.g., 16-bit to 4-bit) to decrease memory usage and increase inference speed with minimal quality loss. The most common formats are GGUF (llama.cpp), AWQ (performance-focused), and GPTQ (GPU-optimized).

FormatEnginePrecisionMemory ReductionBest For
GGUFllama.cpp, Ollama2-8 bit4-6x at Q4_K_MCPU, Apple Silicon, edge
AWQvLLM, TGI, AutoAWQ4 bit~3xGPU production
GPTQvLLM, TGI, AutoGPTQ2-8 bit~3-4xGPU, older models
FP8 / FP16All engines8 or 16 bit1-2xMaximum quality
BitsAndBytesTransformers, PEFT4 or 8 bit~4xFine-tuning, flexible loading
quantization-examples.py
Python
1# GGUF with llama.cpp Python bindings
2# pip install llama-cpp-python
3
4from llama_cpp import Llama
5
6# Load a GGUF model
7llm = Llama(
8 model_path="models/llama-3.2-8b-instruct.Q4_K_M.gguf",
9 n_ctx=8192, # Context length
10 n_threads=8, # CPU threads
11 n_gpu_layers=-1, # -1 = all layers on GPU
12 verbose=False
13)
14
15output = llm.create_chat_completion(
16 messages=[
17 {"role": "user", "content": "What is quantization?"}
18 ],
19 max_tokens=256,
20 temperature=0.7
21)
22
23print(output["choices"][0]["message"]["content"])
24
25# AWQ with vLLM
26# No special loading needed — vLLM detects AWQ automatically
27from vllm import LLM
28
29llm = LLM(
30 model="TheBloke/Llama-3.2-8B-Instruct-AWQ",
31 quantization="awq",
32 max_model_len=8192
33)

best practice

Use Q4_K_M GGUF as your default quantization for the best quality-to-size trade-off. For GPU-only deployment, AWQ offers slightly better quality. Q2 and Q3 quantizations degrade quality noticeably — avoid them for instruction-following tasks. Always benchmark your specific use case as quality loss varies by task.
Hardware Requirements

Hardware requirements vary dramatically by model size and quantization. Here are rough guidelines for running models locally.

Model SizeFP16 VRAMINT4 VRAMMinimum GPU
1-3B2-6 GB1-2 GBAny GPU, Apple Silicon, or CPU
7-8B14-16 GB4-6 GBRTX 3060+, M1 Pro+, or CPU (4-bit)
13-14B26-28 GB8-10 GBRTX 3090+, M2 Max+, or dual GPU
32-34B64+ GB16-20 GBRTX 4090, A100, or multi-GPU
70-72B140+ GB35-40 GB2x RTX 3090+, A100, or CPU offloading
236-405B470+ GB120-200 GBMulti-GPU server cluster
🔥

pro tip

Apple Silicon Macs (M1/M2/M3) are excellent for local LLMs. Unified memory allows running 7-8B models at Q4 on 16GB machines and 70B models on 128GB+ machines. Metal acceleration through llama.cpp or MLX provides competitive token generation speeds.
$Blueprint — Engineering Documentation·Section ID: AI-OS-01·Revision: 1.0