AlgoViz

Run an open-weight model on your computer

medium 7 min

A model download supplies learned parameters and configuration.

Video lesson · Chapter 1 of 4

An offline tutor on a workbench

0:00 / 4:06

Theory

Read or listen first. About 2 min.

What it is

A model download supplies learned parameters and configuration. It still needs a tokenizer to turn input into IDs, a chat template to serialize roles and turns, and an inference runtime with kernels for the model architecture and weight format. The runtime allocates buffers, processes the prompt, chooses a next token and repeats until a stop condition is met. After the complete artifact is local, this example performs inference on your own machine.

Why it exists

Start with an instruction-tuned checkpoint small enough to leave room for the runtime and context cache. Identify the exact revision, license and file digest. GGUF is a container for model metadata and tensors; it can hold several different numeric representations. A safetensors checkpoint uses a different packaging convention and normally ships alongside configuration and tokenizer files. Choose a compatible runtime before choosing a download purely by size.

From a downloaded checkpoint to a local chat reply

Pinned GGUF → llama.cpp runtime (verified weights). Chat formatter → llama.cpp runtime (prompt tokens). llama.cpp runtime → Decoder (next-token scores). Decoder → llama.cpp runtime (generated token)
Pinned GGUF → llama.cpp runtime (verified weights). Chat formatter → llama.cpp runtime (prompt tokens). llama.cpp runtime → Decoder (next-token scores). Decoder → llama.cpp runtime (generated token)

Inside this chapter

Chapter sources
Open weights and open source
Availability of model weights, availability of training data information, availability of training code and the license are distinct questions. Read the particular checkpoint’s model card and license before deciding whether it can be redistributed or modified. The OSI definition describes additional freedoms and materials for open-source AI.

People use open loosely. A checkpoint can have downloadable weights under a licence that restricts commercial use, or a permissive licence but no information about its training data. The Open Source Initiative's definition for AI asks for more than weights: enough information and code to study and rebuild the system. For a deployment decision, read the licence for the exact artefact you ship and record each component's licence separately.

Example. The pinned Qwen GGUF used in this lab lists Apache-2.0. Record that artifact license separately from llama.cpp’s runtime license and the provenance of any adapter.

Separate questions, separate answers

QuestionWhere to lookIn this lab
Are the weights downloadable?model repositoryyes, a pinned GGUF
What licence covers the weights?model card and licence fileApache-2.0
What licence covers the runtime?runtime repositoryllama.cpp's own licence
Is training data described?card or technical reportcheck the card
Is training code released?project repositoriescheck separately
Where did any adapter come from?adapter cardrecord its own licence
Downloadable weights answer only the first row.
Revision, tokenizer and template
The chat template encodes boundaries that the instruction-tuned model saw during training. Mismatched special tokens, extra role delimiters or the wrong end token can change behavior even when weight loading succeeds. Keep the template and tokenizer matched to the model revision.

Instruction-tuned models learned to respond inside a specific wrapper of special tokens marking roles and turn ends. The runtime must reproduce that wrapper exactly, and the tokenizer must map those markers to the special IDs the model saw in training. If a marker is wrong or the end-of-turn token is not treated as a stop, the model may echo role labels, keep writing the user's next turn, or never stop. Inspect the rendered prompt before touching sampling settings.

Example. First send structured messages to the chat endpoint. If the assistant echoes role labels, inspect the rendered template before changing temperature or retraining.

Another example. A local coding assistant keeps generating User: lines after its answer. The runtime was using a generic template; switching to the checkpoint's bundled template fixes it without any other change.

Text
messages:  [{"role": "user", "content": "What is 2 + 3?"}]rendered for a ChatML-style template:<|im_start|>userWhat is 2 + 3?<|im_end|><|im_start|>assistant
Illustrative rendering. The checkpoint's own template, shipped with its tokenizer, is the authority.
CPU, Metal and GPU execution
Kernels perform matrix operations on supported hardware. llama.cpp can run on CPUs and offload supported work to accelerators. Apple Silicon shares physical memory across CPU and GPU. MLX-LM is another Apple-focused runtime. Device choice, unsupported kernels and partial offload affect speed and peak memory.

A runtime has kernels, small optimised programs for matrix maths, for each device type it supports. llama.cpp can run fully on CPU or place some layers on an accelerator and the rest on CPU. On Apple Silicon the CPU and GPU share one pool of memory, so there is no separate VRAM to fill. Partial offload lets a large model run on modest hardware, but the CPU layers and the transfers between devices often set the decoding speed.

Example. A model can fit partly in VRAM and partly in system RAM yet decode slowly because layers or activations cross a slower boundary.

Partial offload and the slow boundary

Fitting is not the same as running fast: every token crosses the boundary.
Prefill, decoding and a smoke test
First validate artifact loading and server health. Then send a short deterministic request and inspect model identity, returned text, finish reason and usage. This tests the end-to-end wiring. A task-specific held-out set is still needed before trusting the model for a product.

Prefill processes the whole prompt in one parallel pass and fills the KV cache; decoding then produces one token per step. They stress hardware differently: prefill is compute-heavy and scales with prompt length, decoding is limited by memory bandwidth and scales with output length. A smoke test checks that every link works end to end. It says nothing about whether a 0.5B-class model can extract invoice fields.

Example. The lab uses a 0.5B-class instruction model and a 2,048-position context. Receiving a short arithmetic answer confirms the local path, not invoice-extraction accuracy.

A smoke test that proves wiring, not quality

  1. 1

    Verify the artefact

    Compare the file's SHA-256 digest with the pinned value.

  2. 2

    Start and wait for health

    Launch the server on loopback and poll its health endpoint until ready.

  3. 3

    Send a deterministic request

    One short arithmetic question at temperature 0 with a token cap.

  4. 4

    Inspect the response

    Model alias, returned text, finish reason and token usage.

  5. 5

    Then evaluate the task

    Only a held-out set for your real task says whether to trust it.

Stop conditions and finish reason
Generation ends when the model emits an end-of-sequence or end-of-turn token, when the output matches a configured stop string, or when it hits the maximum token count. OpenAI-compatible servers report which happened in a finish reason field, typically stop or length.

Stop handling is where template mistakes become visible: if the runtime does not know the model's end-of-turn token, the model's natural stop is ignored and generation runs on until the cap. Log finish reasons in production; a rising share of length means truncated answers your users are seeing.

Example. A reply with finish reason length was cut off by the token cap. Treat it as incomplete rather than as a finished answer.

After each generated token

Always read the finish reason before trusting the text.

How it works

Step through the idea, or press Play.

Step 1 of 4

Pinned GGUF. Verify 491,400,032 bytes

Understand it

Worked example

From a downloaded checkpoint to a local chat reply

  1. 1

    Verify 491,400,032 bytes

    Download the Q4_K_M file at the pinned revision, compare its SHA-256 digest and retain the license alongside the artifact.

  2. 2

    Load the model and reserve context

    Start llama-server bound to loopback with the local file, explicit alias and a 2,048-token context. Check readiness before sending a request.

  3. 3

    Serialize one user message

    The chat endpoint applies the loaded model’s template, tokenizes the input and sends the IDs to prefill.

  4. 4

    Return a local completion

    Decode a bounded response and inspect its finish reason. Stop the server when finished to release the model and KV memory.

Follow the handoffs

Start with an instruction-tuned checkpoint small enough to leave room for the runtime and context cache. Identify the exact revision, license and file digest. GGUF is a container for model metadata and tensors; it can hold several different numeric representations. A safetensors checkpoint uses a different packaging convention and normally ships alongside configuration and tokenizer files. Choose a compatible runtime before choosing a download purely by size.

Lifecycle of the local server

Requests are sent only in Ready; stopping the server releases memory.

Hands-on lab

Run the checkpoint locally

Shell
brew install llama.cpp
On macOS with Homebrew, install the runtime. On other systems, use a compatible llama.cpp release from the chapter sources. The remaining commands use a POSIX shell.
Shell
curl -fL --retry 2 -o 'qwen2.5-0.5b-instruct-q4_k_m.gguf' 'https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct-GGUF/resolve/9217f5db79a29953eb74d5343926648285ec7e67/qwen2.5-0.5b-instruct-q4_k_m.gguf'
Download the 491,400,032-byte file from the pinned model revision into a working directory with enough free space.
Shell
printf '%s  %s\n' '74a4da8c9fdbcd15bd1f6d01d621410d31c6fc00986f5eb687824e7b93d7a9db' 'qwen2.5-0.5b-instruct-q4_k_m.gguf' | shasum -a 256 -c -
Continue when the file reports OK. On Linux, use sha256sum -c - in place of shasum -a 256 -c -. The checksum covers the entire GGUF.
Shell
llama-server -m 'qwen2.5-0.5b-instruct-q4_k_m.gguf' --alias local-tutor -c 2048 --host 127.0.0.1 --port 18447
Leave this terminal open. The server reads the checkpoint, reserves space for context and listens on your computer’s loopback interface.
Shell
curl --fail 'http://127.0.0.1:18447/health'curl --fail 'http://127.0.0.1:18447/v1/models'curl --fail 'http://127.0.0.1:18447/v1/chat/completions' \  -H 'Content-Type: application/json' \  -d '{"model":"local-tutor","messages":[{"role":"user","content":"What is two plus two?"}],"temperature":0,"max_tokens":24}'
Use a second terminal after the model is ready. In the tested run, the assistant replied “Two plus two is four.” Check the returned model alias, message and finish reason. Press Ctrl-C in the server terminal when finished.

Summary

The whole topic in one minute.

A serialization mismatch can change the meaning of an otherwise valid prompt. Check the boundary before comparing model quality.

Start with an instruction-tuned checkpoint small enough to leave room for the runtime and context cache. Identify the exact revision, license and file digest. GGUF is a container for model metadata and tensors; it can hold several different numeric representations. A safetensors checkpoint uses a different packaging convention and normally ships alongside configuration and tokenizer files. Choose a compatible runtime before choosing a download purely by size.

Sources