Ir para o conteúdo

Lição 14 · Memory, Speed and Quantization

Llama Speed and Context Settings

Understand time to first token and tokens per second, then improve local Llama speed with GPU offload, context limits, prompt reuse and fair benchmarks.

"Slow" can mean two different things. You may wait a long time before the first word, or the words may arrive slowly. Each has different causes, so measure them separately before you change any settings.

Two phases of a reply

  1. Prompt processing (prefill). The model reads all your input tokens. Long documents make this slow.
  2. Generation (decode). The model produces one token at a time.

The key measurements:

  • Time to first token (TTFT): the wait before output begins. On a cold start it includes model loading.
  • Tokens per second: the speed during generation.
  • Total latency: what the user waits for a complete reply.
Python Estimate total latency

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The long document mostly hurts the first token. The long answer mostly hurts total time. Knowing which one you face tells you what to fix.

What controls speed

Lever Usual effect
Smaller model Faster in both phases
Quantization Less data to move, often faster
All layers on GPU Large gain compared with partial CPU use
Shorter prompt Faster first token
Lower output limit Shorter total time
Fewer parallel requests More speed for each request

Local generation often depends on memory bandwidth, because each new token requires reading much of the weights. That helps explain why a smaller or more quantized model can feel much faster.

GPU offload

In llama.cpp, -ngl sets the number of GPU layers. In Ollama, run ollama ps and look at the PROCESSOR column. Its documentation suggests aiming for 100% GPU for best performance. If you see a split between CPU and GPU, try a smaller model, a smaller quantization or a shorter context.

Context size

Ask for the context that you need, not the maximum. Ollama's context-length documentation (December 2026) says that the default depends on your GPU memory: 4k tokens below 24 GiB, 32k from 24 to 48 GiB, and 256k at 48 GiB or more. Some other pages and older versions state other defaults, so always check with ollama ps. You can set the value with the app's context slider, the OLLAMA_CONTEXT_LENGTH environment variable, a Modelfile num_ctx or an API option. A model cannot use more than its own maximum.

The same documentation advises a large context (at least 64,000 tokens) for agents and coding tools. That costs memory, so plan hardware accordingly, and check that your model supports it well.

Reuse the unchanged start

Many runtimes can reuse cached state for an identical prompt beginning. Put stable content first (system instructions, examples, a shared document) and changing content last (the question, the time). A timestamp at the very top changes the start of every request and defeats reuse. Behavior differs between runtimes, so measure.

Python How much of two prompts is shared at the start?

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Benchmark fairly

  • Warm the model once, then measure. Report cold start separately.
  • Use the same prompts, output length, settings and machine.
  • Repeat several times and report the median and the worst.
  • Record model file, runtime version, context, quantization and GPU layers.
  • Check quality too. A fast wrong answer is no gain.

Change one setting at a time. If you change the model, the quantization and the context together, you cannot tell which change helped.

Heat, power and long runs

Laptops may slow down when hot or on battery. A two-second benchmark can look much better than a ten-minute session. Test the way you plan to use the system.

A diagnosis table

What you notice Likely area First thing to try
Long wait only on the first request Model loading Keep the model loaded, or warm it up
Long wait before every long answer Prompt processing Shorten input, reuse a stable beginning
Words appear very slowly Generation Check GPU placement, try a smaller file
Speed drops after minutes Heat or power limits Improve cooling, use mains power
Everything slows when others use it Shared capacity Limit parallel requests or add hardware

Use the table as a starting point, and let measurements decide.

Reducing work, not only speeding it up

The cheapest token is the one you never process. Remove repeated text from prompts. Retrieve only the passages you need. Summarize or drop old turns, and test whether this hurts answers. Ask for short replies when short replies are enough, and set an output limit as a safety net. These changes help on any hardware, and they also lower cost if you pay a provider.

Speed tricks to know about

Serving tools may offer extra techniques, such as batching several requests together or speculative decoding, in which a small helper model proposes tokens that the main model checks. The gain depends on the models, hardware and text. Turn such features on only after you have a baseline, then measure again.

Report results clearly

When you share a benchmark, give enough detail that someone else could repeat it. A number such as "30 tokens per second" means little without the hardware, file and prompt length.

Know when to stop tuning. Set a target first, such as a maximum wait for the first word and a maximum time for a typical reply. When you meet the target with acceptable quality, stop. Further tuning often trades away quality or stability for speed that nobody notices.

A simple routine. Measure, change one setting, measure again and write both numbers down. Undo any change that does not help.

Check your understanding

Quick check · 3 questions

of 3 answered You scored / 3 · perfect!

  1. 1. A very long prompt mostly increases which delay?

    The input must be processed before the first output token.
  2. 2. In ollama ps, what does '100% GPU' suggest?

    A full GPU placement avoids slower CPU work.
  3. 3. How should you order a prompt to help prefix reuse?

    Cached state can only be reused for an identical beginning.

Summary

  • Measure first-token time and generation speed separately.
  • GPU placement, model size, quantization and context drive speed.
  • Keep stable prompt parts first to allow reuse where supported.
  • Benchmark with fixed settings and check quality.

Next: Retrieval with Llama

Seu progresso fica salvo neste navegador. Não precisa de conta.