Sub topic 1
Open-weight models and what vLLM loads
This chapter covers what an open-weight model is, which files make up one in the Hugging Face format, and what vLLM reads from them when it starts. Knowing this explains most startup errors and most memory numbers you will see later in the tutorial.
Prerequisites
This chapter assumes you already have the following:
- Basic Python and command-line use.
- A general idea of what a large language model is, and how a prompt becomes a completion.
- A basic idea of GPUs and GPU memory.
- Basic familiarity with HTTP APIs and JSON.
You do not need to have installed vLLM yet. The later chapters on offline inference and vllm serve use the files described here.
What "open-weight" means
An open-weight model is a model whose trained parameters (the weights) are published for download. You can run it on your own hardware, and you decide how it is deployed.
Open-weight is not the same as open source. The weights are available, but the training data and training code often are not. The license also varies by model. Some licenses are permissive, such as Apache 2.0. Others restrict commercial use, limit the size of the user base, or require that you accept terms before download. Check the license before you build a product on a model.
Serving an open-weight model differs from calling a hosted API in three ways:
- You own the capacity. Throughput and latency depend on your GPUs and your settings.
- You own the version. The weights do not change unless you change them.
- You own the operations: memory, scaling, monitoring, and upgrades.
vLLM is the engine that loads these weights and runs generation efficiently. The rest of the tutorial is about running it well.
The Hugging Face model format
Most open-weight models are published on the Hugging Face Hub. Each model is a repository, identified as organization/name, for example Qwen/Qwen2.5-1.5B-Instruct. A repository is a Git repository holding the files the model needs.
A typical repository contains these files:
| File | Purpose |
|---|---|
| `config.json` | The architecture and its dimensions: number of layers, hidden size, attention heads, vocabulary size, maximum context length |
| `model.safetensors` or `model-0000X-of-0000Y.safetensors` plus `model.safetensors.index.json` | The weights, in one file or in shards |
| `tokenizer.json`, `tokenizer_config.json` | The tokenizer, which converts text to token IDs and back |
| `generation_config.json` | Default generation settings, such as temperature and end-of-sequence tokens |
| `tokenizer_config.json` (chat template field) | The chat template for instruction-tuned models |
Safetensors
Safetensors is the standard weight file format. It stores tensors with a small JSON header and raw data. Loading does not run arbitrary code, and the file can be memory-mapped, which makes loading fast. Older PyTorch .bin files use Python pickle, which can execute code when loaded. Prefer safetensors when a repository offers both.
Large models are split into shards, typically a few gigabytes each. The index file maps each tensor name to its shard so a loader can read what it needs.
Base and instruction-tuned models
Many families publish two variants. A base model is trained only to continue text. An instruction-tuned model (often named with -Instruct or -chat) is further trained to follow instructions and hold conversations. For a chat service, you usually want the instruction-tuned variant. The chapter on chat templates returns to why this matters.
Reading the weight footprint
The weights occupy memory according to the parameter count and the numeric precision (dtype):
| Precision | Bytes per parameter | Example: 1.5B parameters |
|---|---|---|
| float32 | 4 | about 6 GB |
| float16 / bfloat16 | 2 | about 3 GB |
| 8-bit | 1 | about 1.5 GB |
| 4-bit | 0.5 | about 0.75 GB |
Most current models are published in bfloat16, so a quick estimate is the parameter count in billions, times two, in gigabytes. A 32B model needs about 64 GB for weights alone. That figure does not fit on a single 40 GB or 48 GB GPU, which is the motivation for quantization and tensor parallelism later on.
What vLLM does when it loads a model
When you pass a model name to vLLM, it goes through several steps.
- Resolve the model. The argument is either a Hub ID or a path to a local directory. For a Hub ID, vLLM downloads into the Hugging Face cache (by default
~/.cache/huggingface) and reuses it on later runs. - Read
config.json. This determines the architecture class, such as a Llama-style or Qwen-style decoder. vLLM must have an implementation of that architecture. If it does not, startup fails with an error that the architecture is not supported. - Load the tokenizer. By default it comes from the same repository.
- Load the weights. Tensors are read from the safetensors files and placed on the GPU in the chosen dtype.
- Profile memory. vLLM runs a test forward pass to measure peak activation memory.
- Allocate the KV cache. The memory that remains within your budget becomes cache space for running requests. The next chapters explain this in detail.
The consequence for planning: weights are a fixed cost, and whatever is left over from your memory budget sets how many concurrent requests you can hold.
Settings that affect loading
These arguments are available both in the LLM class and in vllm serve.
| Setting | Effect |
|---|---|
| `model` | Hub ID or local directory |
| `dtype` | Precision for weights and compute. `auto` follows the model's config |
| `max_model_len` | Maximum context length. Defaults to the value in `config.json` |
| `revision` | A specific branch, tag, or commit of the repository |
| `trust_remote_code` | Allows custom Python code from the repository to run |
| `tokenizer` | Use a tokenizer from a different repository or path |
Pinning revision to a commit hash is good practice in production, because a repository owner can update a model after you tested it.
Gated models and authentication
Some repositories are gated. You must accept a license on the Hub, and downloads then require an access token. Set the token in the environment before starting vLLM:
export HF_TOKEN="hf_your_token_here"
vllm serve meta-llama/Llama-3.1-8B-Instruct --max-model-len 4096Without a valid token, the download fails with an authorization error (HTTP 401 or 403). Never put a token in source code or commit it to a repository.
Inspecting a model before you serve it
You can estimate the memory needs of a model before you start vLLM by reading its config. The following script downloads only config.json and prints the key dimensions and a weight-size estimate. It needs the huggingface_hub package, which vLLM installs.
import json
from huggingface_hub import hf_hub_download
repo_id = "Qwen/Qwen2.5-1.5B-Instruct"
path = hf_hub_download(repo_id=repo_id, filename="config.json")
with open(path) as f:
config = json.load(f)
print("architectures:", config["architectures"])
print("layers:", config["num_hidden_layers"])
print("hidden size:", config["hidden_size"])
print("attention heads:", config["num_attention_heads"])
print("kv heads:", config.get("num_key_value_heads"))
print("max positions:", config["max_position_embeddings"])
print("dtype:", config.get("torch_dtype"))The num_key_value_heads value is important for the next chapters. It controls how large the KV cache is per token. Models that use grouped-query attention have fewer KV heads than attention heads, which makes their cache smaller.
Common errors at load time
| Symptom | Likely cause | Fix |
|---|---|---|
| Out-of-memory error during load | Weights do not fit in the GPU | Use a smaller or quantized model, or split across GPUs |
| Error about KV cache not fitting `max_model_len` | The model's default context is longer than the leftover memory can hold | Lower `max_model_len` |
| "Architecture not supported" | The architecture in `config.json` has no vLLM implementation | Check the supported-models list, or upgrade vLLM |
| 401 or 403 on download | Gated model without a token | Accept the license and set `HF_TOKEN` |
| Repeated slow downloads | Cache not persisted, such as in a container | Mount the cache directory |
| Garbled chat output | A base model used with chat messages, or a missing chat template | Use an instruct model |
Summary
An open-weight model is a set of downloadable files: a config, weights in safetensors, a tokenizer, and often a chat template. vLLM resolves them, builds the architecture from config.json, loads the weights onto the GPU, and gives the remaining memory budget to the KV cache. The weight size, roughly parameters times bytes per parameter, is the first number to check when planning a deployment.
Exercise
A model has 14 billion parameters and is published in bfloat16. Roughly how much GPU memory do the weights alone need, and can they fit on a single 24 GB GPU?
Show answer
bfloat16 uses 2 bytes per parameter, so 14 billion parameters need about 28 GB. That is more than 24 GB, so the weights alone do not fit, before any space for the KV cache. You would need a larger GPU, a 4-bit quantized version (about 7 GB), or multiple GPUs.
Exercise
Your server downloads a gated model and fails with a 401 error. Name the two steps needed to fix it.
Show answer
Accept the model's license on the Hugging Face Hub with your account, then provide an access token to vLLM by setting the HF_TOKEN environment variable before starting it.
Exercise
Why is it better to pin revision to a commit hash in production than to rely on the default branch?
Show answer
The default branch can change when the repository owner updates the files. Pinning a commit keeps the weights, config, and tokenizer identical to what you tested, so behavior does not change unexpectedly.