For developers4 min read
A 1-second LLM grader on a laptop CPU
Gemma 4 grades a short answer in about a second on a 2019 laptop, with no GPU and identical scores. Four changes got it from 138 seconds per answer to 1.
By Colin Barrett-Fox
We wanted an AI grader that works with no internet. Gemma 4 E4B, a 4B-class open model, agrees with our benchmark labels on 91.5% of open-ended answers, close to the best cloud models. The catch was speed. Our first run on a 2019 Intel MacBook Pro took 138 seconds per answer.
After four changes it takes about 1 second, on the same laptop's CPU, with identical scores. None of the changes is exotic, and most apply to any "LLM as classifier" workload.
The setup: grading is classification, not generation
The grader never writes text. The prompt lays out the grading rules, the question, optionally an answer key, and the student's answer, and ends by asking for one letter: A (correct), B (partial) or C (incorrect). We run one forward pass and read the probabilities of those three letter tokens. There is nothing to parse, the probabilities double as a confidence score, and a small calibration step on top fixes any bias toward one letter.
That shape is what makes everything below possible. We run the model through transformers.js 4 with onnxruntime on the CPU, using the q4 ONNX weights.
1. Load only what you use (138 s to about 8 s)
Gemma 4 is multimodal. Its checkpoint includes vision and audio encoders, and the first run loaded all of them. On a 16 GB laptop that meant 12 GB of swap.
Loading it with AutoModelForCausalLM gives the text decoder alone, about 2.7 GB in memory. Same weights, same answers, and the machine stopped thrashing.
2. Compute one position, not all of them
A plain forward pass computes next-token scores for every position in the prompt, against Gemma's 262k-token vocabulary. We only need the last position. Asking for logits at the final position only (num_logits_to_keep: 1) removes most of the output work.
3. Reuse the shared prefix
Every prompt starts with the same grading rules, and every answer to one question shares the question text and answer key. Recomputing those tokens for every student is pure waste.
So we compute the model's state (the attention key/value cache) once for the shared prefix, once more for each question's prefix, and start each student's answer from there. The prefix boundaries come from the longest common run of token ids, not of text, so tokenization can't drift.
On 40 answers checked both ways, the verdicts were identical and the probabilities matched the uncached run to within 0.00003.
4. Put the rules first (and the answer last)
Our original prompt put the student's answer in the middle, with the instructions after it. That ordering caps how much of the prompt can be shared. Moving everything fixed (rules, question, answer key) before the student's answer means each new answer runs only its own tokens: about 27, out of 222 without an answer key and 503 with one.
Timed back to back on the same 20 answers on a cool, plugged-in laptop:
| From scratch | Prefix reused, rules first | Speed-up | |
|---|---|---|---|
| No answer key | 6.1 s | 1.1 s | 5.8× |
| With answer key | 14.1 s | 1.0 s | 14× |
The probabilities were identical on all 40 answers. Reordering did not cost accuracy either: without a key, the paired comparison was 10 answers vs 9 (p = 1.0). And once the rules come first, the answer key costs almost nothing per answer, which matters because the key is what lifts Gemma to 91.5%.
Why not precompute the state on a server?
We considered shipping each question's prepared state from the server instead of computing it on the device. The numbers said no. Gemma's state is about 112 KB per token, so one question's prefix with an answer key is about 40 MB, downloaded by every student for every question. Computing it on the device costs about as much as grading one answer from scratch, once per question: about 6 seconds without an answer key and up to 37 with one on our throttled old laptop.
What didn't work (yet)
WebGPU through onnxruntime-node ran on the laptop's Radeon GPU but returned NaN scores for every answer, about 20 seconds each. The same fp16 weights give normal scores on the CPU, so the fault is the GPU provider on that machine, not the model. Chrome's own WebGPU is a separate implementation we have not tested yet. Until we have, any GPU path needs a start-up self-test and a fallback to the CPU.
Two library quirks cost us time. generate() disposes GPU-backed caches it did not create, so we call the model's forward pass directly. And for Qwen3.5, the multimodal input preparation sets image inputs to null and then tries to encode them; deleting null modality inputs before the forward pass fixes it.
The takeaway
If you use an LLM as a classifier:
- Score with one forward pass and read the label-token probabilities.
- Load only the parts of the model you use.
- Compute logits for the last position only.
- Order the prompt from most shared to least shared, and cache each shared prefix.
Our laptop is a worst case: a 2019 Intel CPU that macOS throttled to as little as 45% speed during these runs. A newer machine will be faster still.
- llm
- transformers-js
- performance
- offline
- ai-grading