Free self-study · No account needed · Progress stays in this browser. Back it up before clearing site data or changing devices.

← Week 21: Local AI Models

Week 21 / Lesson 1

Choosing a model that fits the machine

Estimate memory and compare compatible local models for a workload.

Section 1.1 1 of 7

Overview

Focus

Estimate memory and compare compatible local models for a workload.

Your route through the lesson

  • 1.2 Weights, numerical precision, and quantization
  • 1.3 Main memory, accelerator memory, and context growth
  • 1.4 Model quality, licensing, provenance, and compatibility
  • 1.5 Put it into practice
  • 1.6 Guided comparison
  • 1.7 Weights fitting on disk but not in runtime memory

All required teaching is included here. Review selections are saved. Choose a correct answer for every question to continue; retries are unlimited. Study time and personal notes are optional.

Section 1.2 2 of 7

Weights, numerical precision, and quantization

Focus

Estimate ideal weight storage and distinguish quantization from shrinking the architecture.

Model weights are the learned numeric parameters used during prediction. Precision describes how those numbers are represented. A thirty-two-bit floating-point value occupies four bytes, while a nominal four-bit value occupies half a byte before metadata. Quantization represents values with fewer possible levels, reducing storage while introducing approximation.

For five hundred million parameters at four bits each, ideal storage is 500,000,000×4÷8 = 250,000,000 bytes. This calculation counts weight bits only. Real formats also store scales, block metadata, and sometimes higher-precision tensors. Quantizing a model does not reduce its layer count or automatically reduce every runtime buffer.

Consider a weight near 0.37 represented by a coarse grid allowing 0.25 and 0.50. Choosing the nearest level changes the calculation. Many small changes can affect outputs, so compare quality on the actual task rather than treating lower precision as free compression. The term Q4_K_M is a runtime format label, not a promise that every stored value uses exactly four bits. Record the exact artifact identity and quantization format in a model comparison.

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. A 200-million-parameter model uses ideal 8-bit weights. What is ideal storage?
2. Does four-bit quantization remove model layers?
3. A weight-only estimate excludes scales and buffers. How should it be labeled?

Section 1.3 3 of 7

Main memory, accelerator memory, and context growth

Focus

Account for main memory, accelerator memory, runtime workspace, and context-dependent state.

Main memory stores programs and data accessible to the processor. Accelerator memory, often called video random-access memory, holds data used by a graphics processor. A runtime may keep all weights on the processor, offload layers to an accelerator, or use a mixture. A model fitting on disk does not establish that its execution fits either memory pool.

Runtime demand includes weights, temporary workspace, tokenizer and runtime overhead, and context state. During autoregressive generation, each new token attends to earlier tokens. Cached attention keys and values avoid recomputing part of that history, but their storage grows with context length and active requests. A longer context or more concurrent sequences can exhaust memory after a short single request succeeded.

Start with a small model and a deliberately bounded context. Leave headroom for the operating system and other applications. Measure process peak working set and accelerator peak usage using the operating-system and hardware monitoring tools available on the machine. A runtime residency report is useful but may omit allocations outside its accounting. Record measurement method and interval; a snapshot after generation is not a peak measurement.

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. Weights fit disk but execution fails with long prompts. Which omitted term can explain it?
2. A model works for one sequence but fails at four concurrent sequences. What changed?
3. Which observation establishes peak memory most directly?

Section 1.4 4 of 7

Model quality, licensing, provenance, and compatibility

Focus

Choose a compatible local artifact using task evidence, provenance, license, tokenizer, and template.

A tokenizer converts text into the token identifiers expected by a model. A chat template wraps messages with role markers and control tokens. Weights, tokenizer, and template form a compatible input system. A correct model with the wrong template may treat instructions as plain text or continue the wrong role.

Inspect the model card, origin, redistribution terms, quantization, and runtime support before installation. Licensing permission and technical compatibility are separate questions. Test a small representative task set, including required structured output and refusal behavior. A larger parameter count does not establish better performance on your workload.

The small Qwen2.5 0.5-billion artifact is a hardware-sized starting candidate, not a quality endorsement. Its page exposes model details, license, and template. The 1.5-billion artifact provides a larger comparison when memory permits. These optional downloads need disk space and network access; subsequent local inference needs no hosted credentials. Preserve actual artifact identifiers because mutable tags may resolve to different content later. A short successful greeting proves basic compatibility, not useful domain competence.

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. Weights load but messages use another model’s role markers. What should be inspected?
2. A candidate has more parameters. What establishes suitability for a ticket task?
3. A greeting succeeds with a new model. What has been demonstrated?

Section 1.5 5 of 7

Put it into practice

Focus

Install explicitly, run a real small model, and record fit evidence without substituting fixtures.

Save week21.py. The core arithmetic runs with python week21.py --lesson 1 and standard Python only. For real inference, install Ollama using its official setup, choose local execution, and explicitly run ollama pull qwen2.5:0.5b. This downloads a model; inspect disk and free memory first. Start the desktop runtime, or use ollama serve if it is not already serving. Avoid starting a second server on the same port.

Run python week21.py --lesson 1 --real --model qwen2.5:0.5b. The script uses urllib.request, Python's built-in HTTP client, to send JSON to loopback. It records runtime version, installed artifact metadata, template, and an actual generated response. Observe operating-system peak memory during this run and add that measurement to your comparison record.

Budget one model, context limited to 512 tokens, and at most 24 generated tokens for this smoke test. Stop if memory pressure occurs and select a smaller compatible artifact. Without --real, the script explicitly reports that actual fit and inference remain pending. Arithmetic or a fixture cannot complete that execution competency.

Download this week’s Python lab ↓

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. A learner runs without --real. Is actual model inference complete?
2. A local runtime already listens on its port. What setup is appropriate?
3. A real smoke test becomes memory constrained. What is the appropriate next step?

Section 1.6 6 of 7

Guided comparison

Focus

Compare ideal bytes, real artifact size, resident memory, and measured peak honestly.

The arithmetic path prints 250,000,000 ideal bytes for a nominal five-hundred-million-parameter four-bit model. An actual downloaded artifact can be larger because the exact parameter count and representation differ and the file includes metadata. Runtime memory can be larger again because execution needs workspace and context state. These are three different observations, not inconsistent measurements of one quantity.

The real path obtains model metadata and a generated answer from the existing local endpoint. It also prints the runtime's resident-model report. That report is not automatically an operating-system peak. The lab deliberately prints the missing measurement method rather than claiming a value it did not observe.

Create a comparison record with artifact identity, runtime version, template, context bound, output bound, hardware, observed process peak, accelerator peak if used, and the smoke-test result. If a measurement tool is unavailable, mark the field unmeasured. The weekly extension compares a second hardware-appropriate candidate on the same frozen task set. Keep download size, fit, speed, and task quality in separate columns so a convenient small file is not mistaken for an adequate application.

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. Ideal weight bytes are lower than a real model file. Is this necessarily an error?
2. The runtime reports resident bytes after generation. What should the report call them?
3. A memory monitor is unavailable. What is the honest field value?

Section 1.7 7 of 7

Weights fitting on disk but not in runtime memory

Focus

Diagnose a model that fits its file budget but exceeds runtime capacity.

A laptop has enough free disk space for a model artifact and the download succeeds. The first request uses a large context and several concurrent sequences; the process is terminated for memory pressure. Disk success established only storage availability. It did not reserve processor memory or accelerator memory for weights, cache, and workspace.

The corrected experiment starts one small model with one request and a 512-token context, leaving operating-system headroom. Record peak memory while it runs. Increase context or concurrency one variable at a time and stop before sustained swapping or process failure. The point is to measure a usable operating envelope, not to force the largest available tag onto the machine.

If no compatible model fits, retain the arithmetic and contract-check evidence but explicitly leave actual inference uncompleted. A prepared output can teach report interpretation, but it cannot demonstrate that this machine generated tokens. Hardware limits are legitimate engineering findings. A production decision may use a permitted hosted service instead, but that routing choice must respect data policy and must be evaluated separately.

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. A download succeeds and generation runs out of memory. What did download success establish?
2. To find a usable envelope, how should context and concurrency change?
3. No model fits available hardware. What completion claim is honest?

Your personal notes

Optional, ungraded, and saved only in this browser. Export a backup from the Academy homepage to keep them.

References and further reading
  • Ollama Qwen2.5 0.5B artifact: format, license, and chat template ↗

    Optional official runtime artifact page. Reading needs only a browser. Actual use requires explicit Ollama installation, a model download, available disk and memory, and local inference; no hosted credentials are needed. Inspect the current artifact identity rather than treating its file size as peak runtime memory.

Ask your own AI assistant

Copy this prompt into an assistant you already use. Nothing is sent automatically, and your personal notes are not included.

Next lesson: Serving and benchmarking a local model →