Shivam Negi

Writing / Field guide

SYSTEMS JOURNAL/FIELD NOTE 02/Field guide

How to Shrink a Transformer for CPU Serving Without Losing Your Mind (or Accuracy)

A field manual for distilling, pruning, and quantizing language models for CPU-only, latency-critical serving, and why most of the tricks fail before one works.

13 min read9 systems diagrams

  • small language models
  • distillation
  • pruning
  • inference
SYSTEMS DISCUSSED · NOT AN ENDORSEMENTONNX

A prototype can look excellent on a multi-billion-parameter model, then encounter a deployment constraint such as CPU-only serving, limited memory, or a strict p50 latency objective. That is when model compression becomes an engineering problem rather than a leaderboard exercise.

This guide is the playbook I wish I had. It is opinionated, and it includes the failures, because in compression the failures are the map.

Know where the milliseconds go

Do not compress blind. Profile first. For a decoder at small batch sizes, latency is dominated by memory bandwidth (moving weights), not arithmetic. For an encoder classifier, the cost is set by the layer count, the hidden size, and the sequence length.

You can see where the arithmetic goes without a profiler. For one BERT-base layer (hidden size d = 768, feed-forward size 3072) at sequence length n = 128, count the multiply-accumulates per token:

Multiply-accumulates per token in one BERT-base layer (d = 768, n = 128), in millionsQ, K, V, output projections (4dsquared)2.36M (32%)Attention scores + weighting(2nd)0.20M (3%)Feed-forward block (2 x d x 3072)4.72M (65%)
Figure 1 Multiply-accumulates per token in one BERT-base layer (d = 768, n = 128), in millions (swipe to see the full diagram)

Two lessons fall out of the arithmetic. First, at this length the feed-forward block, not attention, dominates compute. The famous quadratic term only takes over at long sequences. Second, every term scales with the number of layers, so removing whole layers removes all three at once. That is why layer pruning tends to beat micro-optimizations.

The compression ladder

Climb it in order. Each rung is cheaper to try than the next.

The compression ladder, cheapest experiment firstRight-size the taskDo you need generation at all? A discriminative head beats decoding forclassification.Shorten the inputTruncate, dedupe, cap sequence length. Quadratic attention rewardsbrevity.Prune layersDrop whole transformer blocks, then fine-tune to recover.DistillTrain a small student on a large teacher's soft labels.Quantizeint8 or int4 weights. Big win when it works, fragile when it doesn't.Compile and fuseONNX, graph fusion, kernel tuning. Last mile, not first.
Figure 2 The compression ladder, cheapest experiment first (swipe to see the full diagram)
Insight The biggest win often is not compression at all. It is changing the task formulation. If you generate a JSON label token by token, you pay for a decoder to do a classifier's job. One forward pass plus a classification head replaces the whole decode loop.

Why generation is the wrong tool for classification

Generative vs. discriminative formulation of the same intent taskGenerate the labelN forward passes, one per output tokenOutput must be parsed, and can be malformedLatency grows with label lengthFlexible: handles open-ended outputsClassify in one passOne forward pass (prefill), softmax overlabelsNo parse failures, by constructionLatency independent of label lengthRequires a fixed label set
Figure 3 Generative vs. discriminative formulation of the same intent task (swipe to see the full diagram)

If your label set is fixed, choose the right column. You eliminate parse failures entirely and remove every decode step after the prompt is processed. With a long prompt the prefill still dominates, so measure the gain on your own inputs rather than assuming a multiple.

Layer pruning that actually works

Not all layers are equal. For a narrow task, some layers of a fine-tuned model are often redundant, and removing them costs little after a short recovery fine-tune. Here is the procedure (skip_layer and remove_layer are stand-ins for your framework's equivalents):

Iterative layer pruning with recoveryBaseline evalScore eachlayer (probeor ablation)Droplowest-impactlayerFine-tune torecoverRe-eval
Figure 4 Iterative layer pruning with recovery (swipe to see the full diagram)
pythondef prune_once(model, eval_set):
    scores = {}
    for i in range(model.num_layers):
        with model.skip_layer(i):
            scores[i] = evaluate(model, eval_set)
    victim = max(scores, key=scores.get)  # least damage when removed
    model.remove_layer(victim)
    finetune(model, epochs=1)              # recover before the next cut
    return victim, evaluate(model, eval_set)

Stop when one more cut costs more accuracy than your budget allows. The typical shape is a long plateau followed by a cliff. Cut to the edge of the plateau, then back off one step for safety margin.

Typical pruning curve: accuracy vs. fraction of layers removedFraction of layers removed →Task accuracy →Recovered accuracyStop hereCliff
Figure 5 Typical pruning curve: accuracy vs. fraction of layers removed (swipe to see the full diagram)

The graveyard: what failed, and why

Negative results save you weeks. These are common failure modes when compressing modern LLM-family models for CPU serving:

TechniqueTypical failureRoot cause
Naive int8 on large decodersAccuracy collapseDettmers et al. found systematic activation outliers emerging around 6.7B parameters; they wreck a shared quantization range. Small encoders often quantize fine, so test instead of assuming
ONNX export of a decoderExport errors or slow graphsDynamic KV-cache shapes do not map cleanly to static graphs
torch.compile on CPUCrashes or no speedupCodegen paths are less mature on CPU than on GPU
Vendor CPU extensionsNo speedupKernels assume instruction sets (for example AVX-512) your fleet lacks
Warning Always check the hardware you will actually deploy on. A benchmark on a developer laptop with AVX-512 tells you nothing about a fleet without it.

Distillation without tears

When pruning plateaus, distill. Train a small student to match the teacher's full output distribution, not just the argmax.

Knowledge distillationUnlabeled +labeled dataTeacher(large)Soft targets(temperatureT)Student loss =CE + KLSmall student
Figure 6 Knowledge distillation (swipe to see the full diagram)

Soft targets carry "dark knowledge": the teacher says this is 70% billing, 25% refunds, and that similarity structure is exactly what a small model cannot learn from hard labels alone.

Make the benchmark answer the deployment question

A compression result is meaningful only when the measurement resembles the production workload. Freeze a representative, privacy-safe evaluation set and record the input-length distribution, batch policy, concurrency, hardware model, thread count, runtime, tokenizer, and whether startup is included. For an interactive service, measure end-to-end latency at the service boundary as well as model-only latency. Report a distribution, not one favorable run; separate cold start from steady state and record throughput, memory, and failures alongside latency. There is no universal CPU speedup to promise: kernels, instruction sets, runtime versions, sequence lengths, and batching change the result.

A repeatable baseline can be simple: warm the process as production would, run the same ordered prompts through the old and candidate artifacts, synchronize timing where the runtime requires it, and save raw per-request results with artifact hashes. Repeat under both isolated and realistic concurrent load. Randomize or alternate candidate order to reduce bias from thermal changes and background activity. If concurrency queues requests, distinguish queue delay from model execution. Keep the test set fixed for comparison, but rotate a separate holdout to catch tuning to the benchmark.

Compression experiment loopFreeze task+ dataMeasurebaseline ontarget CPUChange onetechniqueMeasurequality andsystemscostsInspectfailuresKeep orrevert
Figure 7 Compression experiment loop (swipe to see the full diagram)

Define a quality budget before starting. For classification, include per-class precision/recall, calibration where decisions use confidence, and performance on high-cost errors; aggregate accuracy can hide a rare-class collapse. For generation, grade task success, factuality, format validity, and refusal behavior using a consistent protocol. Human-review a blinded sample of changed outputs. Decide in advance what regression forces a revert, and preserve the original artifact so the experiment is reversible.

Choose an intervention by bottleneck

The compression ladder is a sequence of hypotheses, not a mandatory checklist. If the service is CPU-bound on long inputs, input truncation or a more suitable encoder may dominate. If memory bandwidth is limiting, quantization may help more than reducing arithmetic. If per-request overhead dominates at tiny batch sizes, exporting or compiling may add complexity without useful benefit. Profile representative requests before choosing a technique, then change one variable at a time so the cause of any improvement or regression remains interpretable.

Observed bottleneckFirst experimentWatch for
Long prefill on long textInput cap, deduplication, chunking policyLost evidence near truncation boundary
High model memory / weight trafficSupported weight quantizationQuality shifts from activation outliers or calibration mismatch
Excessive layer computeStructured layer reduction or a smaller architectureAbrupt task-specific quality cliffs
Decode dominates a fixed-label taskDiscriminative head instead of generationLabel mapping and calibration changes
Startup or graph overheadRuntime warmup, export, operator fusionCold-start and fallback behavior

A smaller file on disk does not necessarily mean a faster service. Dequantization may add work; a compressed operator may fall back to an unoptimized implementation; a runtime may allocate a larger workspace. Measure resident memory and peak memory under concurrency as well as artifact size. If your service batches requests, evaluate at the actual batch policy: a technique that wins for a single request might lose when the runtime already amortizes overhead.

Quantization: treat calibration as data curation

Quantization maps a wide numeric representation to a smaller set of values. Weight-only schemes reduce weight storage, while activation quantization also constrains intermediate values. The latter can be sensitive to outliers and activation ranges. Calibration inputs should resemble deployment inputs in length, language, formatting, and edge cases, but must not leak evaluation labels or private content. Keep the calibration set versioned and record the quantization method and runtime, because “int8” is not a complete experiment description.

A disciplined trial compares the original model, the quantized model, and-if available-a mixed-precision fallback on the same examples. Inspect errors by category and input length. If accuracy degrades, check whether the issue is quantization range, unsupported operations, tokenizer differences, or a changed preprocessing path before abandoning the entire approach. For sensitive decisions, route uncertain cases to a higher-precision model or a non-model fallback, but include that route's latency and cost in the benchmark. Never describe a compression ratio as a quality-preserving speedup unless both storage and measured serving behavior support the claim.

Distillation: specify the student’s job

Distillation works best when teacher outputs are relevant to the student’s actual task. Define a fixed label set or output schema, teacher prompting policy, temperature, loss mixture, and sampling strategy before generating targets. Keep examples where the teacher is wrong or uncertain in a review queue; a student trained on confident-looking errors can faithfully inherit them. Compare hard-label training, soft-target training, and the original student baseline, holding data splits and optimization budgets as constant as practical.

For a narrow classifier, a teacher can generate class probabilities or explanations that a human then validates; the student need not imitate verbose teacher prose. For open-ended generation, evaluate exact task behavior rather than lexical similarity to the teacher. Preserve independent test examples and include adversarial and out-of-domain cases. Distillation transfers behavior, not authority: it can reproduce the teacher’s bias, refusal gaps, and unsupported claims. Track provenance and permission for teacher outputs and source data.

Layer pruning that actually works

The illustrative pruning snippet above is not drop-in framework code: APIs such as skip_layer, remove_layer, evaluate, and finetune depend on the model library. One subtle issue: the selection objective must be explicit. The code assumes evaluate returns a loss where lower is better, so max(scores, key=scores.get) chooses the least harmful removal. If it returns accuracy, that line is wrong; use the smallest accuracy drop instead. Evaluate candidates on a fixed set, but verify each chosen cut on a holdout before repeating. A layer’s effect depends on what was already removed, so do not rank all layers once and blindly remove a batch.

Recovery fine-tuning can overfit a small validation set. Split training, tuning, and final evaluation data before pruning, and reserve the final set for the stopping decision. Keep checkpoints at each pruning stage. If a model serves multiple tasks, evaluate every task and important slice after each cut; a layer that appears redundant for common inputs may encode a rare capability. Pruning is not automatically safer or more interpretable merely because the model is smaller.

Investigate a quality regressionCandidate loses qualityDoes it affect all inputs?Check tokenizer,preprocessing, and labelmappingIs it concentrated in longinputs?Check truncation, positionlimits, and attention pathIs it concentrated in oneclass or subgroup?Audit examples andcalibration inputsDoes output change only onquantized path?Compare ranges, operators,and fallback kernels
Figure 8 Investigate a quality regression (swipe to see the full diagram)

A release checklist for compressed models

Before shipping, verify the artifact can be loaded in the target container without network access to an unpinned dependency. Record model, tokenizer, runtime, compiler, and configuration versions together. Validate schema, empty input, maximum input, malformed Unicode, unsupported locale, and timeout behavior. Check deterministic behavior if downstream code assumes it; quantized kernels can have different numerical behavior, and generation may vary even when the model is unchanged.

Run a canary against the uncompressed or prior release with task-quality and service guardrails. Include memory pressure, queueing, and fallback rates. Put a hard input limit and request deadline at the service boundary. If the compressed model fails to load or exceeds its budget, the system should fail safely to a known path rather than repeatedly retrying an expensive initialization. Retain rollback artifacts until the new version has survived the agreed monitoring window.

Decide when not to compress

Compression has engineering costs that a parameter count does not show. A smaller artifact may require a specialized runtime, a more complex build, extra calibration data, custom operators, or a second model for fallback. That raises maintenance burden and can make security updates harder. Compare the complete cost of serving and maintaining the candidate against simpler options: a smaller off-the-shelf model, caching where requests are safely repeatable, an input policy change, or keeping the existing model with a different hardware allocation. If the target latency and memory budgets already have comfortable headroom, avoid compression work that creates a fragile dependency without solving a real constraint.

Also ask whether the task itself is stable enough to justify a compact fixed model. A fixed-label classifier is a strong candidate; rapidly changing knowledge may be better handled through retrieval than repeated fine-tuning. A model that must handle many languages or open-ended generation may lose important coverage under aggressive compression even if the benchmark’s average score looks acceptable. Define the operating envelope-supported languages, input size, label set, freshness needs, and fallback behavior-so that “small enough” does not quietly mean “works only on the demo.”

A small, reproducible experiment plan

For each candidate, write a one-page record: problem being solved, baseline artifact hash, target hardware and runtime, data split, quality budget, latency and memory measures, transformation applied, and rollback criteria. Save the exact command or configuration and the raw results. Make the output machine-readable where convenient, but keep the interpretation human-readable: which inputs changed, why the change matters, and what remains uncertain. This makes a future runtime update testable instead of turning old screenshots into folklore.

A simple experiment matrix avoids confounding. Rows can be model variants and columns can be original precision, supported quantization, and runtime options; evaluate each against the same fixed data and hardware conditions. Do not combine pruning, distillation, quantization, and input truncation in the first trial. If a combined approach wins, follow with ablations to find which component produced the gain and which created the quality risk. Test reproducibility across a fresh process, not just repeated calls inside a warmed notebook.

What a useful result containsModel behaviorOverall and per-class task qualityCritical-slice and malformed-input outcomesCalibration or output validity where relevantServing behaviorLatency distribution and throughputPeak and resident memory under loadStartup, fallback, and failure behavior
Figure 9 What a useful result contains (swipe to see the full diagram)

Capacity and failure modes under load

A CPU model can perform well in isolation and collapse when several requests compete for cores and memory bandwidth. Run a concurrency test that resembles the service’s expected arrival pattern, including bursts if they occur. Observe queue time, worker saturation, memory pressure, and tail latency. Set bounded queues and deadlines; an unbounded backlog converts overload into a slowly growing outage. If the system uses dynamic batching, measure the waiting time added while it forms a batch and check that low traffic does not wait excessively.

Test graceful behavior when the model process restarts, a worker is unavailable, the artifact is corrupt, or the input exceeds the supported size. Validate that health checks do not report readiness before the artifact is loaded and warmup is complete. Ensure retries are bounded and do not multiply load during an outage. These are deployment properties, not compression benchmarks, but they determine whether a compressed model is safe to operate.

Use a rollback threshold that combines quality and service outcomes. For example, a candidate may be within its quality budget but have a severe memory regression, or may be faster but fail an important class. Those are distinct failure conditions. Keep the candidate behind a deployment flag until both are acceptable, and maintain a tested route back to the previous model. A canary is particularly useful because a runtime bug can appear only under live input shapes not represented in the benchmark.

Key takeaways

  • Profile before compressing. Count where the arithmetic goes; it is often not where you assume.
  • Fix the task formulation first: classify, don't generate, when labels are fixed.
  • Prune whole layers iteratively, with recovery fine-tuning, and stop at the plateau edge.
  • Keep a graveyard of failed techniques with root causes. It is your most valuable doc.
  • Validate on production hardware, not your laptop.

Further reading: Hinton et al., Distilling the Knowledge in a Neural Network (2015). Sanh et al., DistilBERT (2019). Dettmers et al., LLM.int8() (2022), on activation outliers.

Shivam Negi is an AI engineer and Principal Data Scientist who works on evaluating voice agents, small language models, and applied ML. More about me