Reducing LLM Inference Costs: Combining Expert Pruning with Speculative Decoding

    This article explains how to cut LLM inference costs through three complementary approaches: structured pruning of expert modules in Mixture-of-Experts models, speculative decoding with a draft model, and right-sizing hardware specifications. It covers the components of inference cost, step-by-step pruning procedures with quality safeguards, the mechanics and trade-offs of speculative decoding, a hardware selection framework, and an end-to-end engineering workflow that combines all three methods with baseline measurement, quality regression, acceptance-rate testing, and per-token cost calculation.

    1. Background: Why Inference Costs Remain High

    Once a large model finishes training, the ongoing expense comes almost entirely from inference. Every user request requires GPU memory to hold the model weights, and generation proceeds token by token, keeping compute resources occupied for extended periods. The larger the parameter count, the more memory the weights consume, the fewer concurrent requests a single deployment can serve, and the higher the cost per token.

    Inference cost typically consists of four components:

    • Memory capacity: Weights and runtime caches together occupy GPU memory, determining how many GPUs are needed to host the model.
    • Compute throughput: Determines how many tokens can be generated per second.
    • Memory bandwidth: During autoregressive generation, every token requires reading large amounts of weights, so bandwidth is often the bottleneck.
    • Context length: Longer inputs mean larger caches and reduced concurrency.

    Therefore, reducing inference cost comes down to three things: shrinking the number of parameters that must be stored and moved, increasing the effective output of each GPU, and choosing hardware that is just sufficient for the model.

    2. Path One: Structured Pruning and Expert Module Trimming

    Mixture-of-Experts (MoE) models split the feed-forward layers into multiple expert sub-networks, activating only a few of them for each input token. Such models can have a very large total parameter count while using only a fraction of it per forward pass. The problem is that all expert weights must remain resident in GPU memory; otherwise, the router cannot invoke them on demand.

    The idea behind expert trimming is to measure each expert’s activation frequency and contribution on validation data, remove experts that contribute little to the target tasks, and then recalibrate the router.

    Key points during implementation:

    1. Build an expert importance score. Common metrics include activation frequency, the change in output norm, and the loss increase after removing the expert.
    2. Evaluate across multiple task categories. Relying on a single benchmark can hide problems. Evaluation should cover knowledge QA, mathematical reasoning, code generation, and long-text understanding to avoid significant degradation on less common tasks.
    3. Trim gradually and record the quality curve. Remove a small number of modules at each step, observe score changes, identify the inflection point where quality starts to drop noticeably, and treat the ratio just before that point as the candidate upper bound.
    4. Recalibrate routing after trimming. Removing experts breaks the original load balance, so a small amount of fine-tuning or statistical calibration is needed to restore reasonable usage proportions across the remaining experts.

    The risks are equally clear. A removed expert may serve low-frequency but critical scenarios, such as a specific language, a specialized domain, or long-tail knowledge. Offline metrics cannot cover every case, so before going live, the new version should be compared against the old one using shadow traffic.

    One publicly reported case showed that after trimming part of the modules from a very large MoE model, the model retained the vast majority of its original score on a difficult scientific reasoning benchmark. The premise of that case was an acceptable quality loss; the goal was to free up memory.

    3. Path Two: Speculative Decoding with a Draft Model

    The bottleneck of autoregressive generation is its strictly serial, token-by-token execution. Speculative decoding works as follows: a small, fast draft model first proposes several subsequent tokens at once, and the large model then verifies these proposals in parallel. The verified portion is accepted directly; at the first position of disagreement, a new token is sampled from the large model’s probability distribution, and the next round begins.

    The benefit of this approach depends on two factors: how closely the draft model’s distribution matches the main model’s, and how fast the draft model itself is. The higher the agreement, the higher the acceptance rate and the better the effective speedup.

    Below is framework-agnostic pseudocode illustrating the flow:

    1. def speculative_decode(target, draft, prompt, k=4, max_len=512):
    2. tokens = list(prompt)
    3. while len(tokens) < max_len and not is_end(tokens):
    4. # The draft model proposes k subsequent tokens at once
    5. proposals = draft.propose(tokens, num_tokens=k)
    6. # The target model computes the probability distribution
    7. # at each position in parallel
    8. target_probs = target.score(tokens, proposals)
    9. # Verify tokens one by one according to the acceptance rule;
    10. # stop accepting at the first rejected position
    11. accepted = verify_and_resample(proposals, target_probs)
    12. tokens.extend(accepted)
    13. return tokens

    Speculative decoding theoretically preserves the output distribution unchanged, provided the acceptance and resampling rules are implemented correctly. In engineering practice, the draft model’s memory footprint must also be counted in the total budget. If both models share the same memory, the draft model may squeeze cache space, reducing concurrency and offsetting part of the speedup.

    4. Path Three: Hardware Specification Selection

    Cost reduction ultimately lands on hardware. For the same model, if memory usage drops enough to fit into a lower-tier GPU, or the required GPU count shrinks, the overall cost structure changes accordingly.

    Selection can follow this sequence:

    • First, define acceptable time-to-first-token latency and overall throughput targets.
    • Calculate the combined memory requirement of weights, caches, and the draft model, leaving headroom for peak concurrency and long requests.
    • Compare the per-token cost of different GPU models and GPU-count combinations. The unit price of a single card is only one input; the final decision should be based on cost per token.
    • Account for intra-node interconnect bandwidth. With multi-GPU parallelism, communication overhead may offset the gains from using fewer GPUs, so fewer GPUs do not necessarily mean lower total cost.

    5. An Engineering Workflow for Combining These Methods

    Pruning, speculative decoding, and hardware selection are not mutually exclusive. In practice, they are usually carried out in the following order.

    Step 1: Establish a baseline. Record the original model’s memory footprint, throughput, latency, and scores across task categories. Without a baseline, subsequent gains cannot be quantified.

    Step 2: Generate candidate variants. Sort experts by importance and produce several versions at different trimming ratios, keeping complete configuration records for each version.

    Step 3: Run quality regression. Compare each version against evaluation sets covering general QA, code, mathematics, long text, and safety refusals. Set a quality floor in advance—for example, core task scores must not fall below an agreed proportion of the baseline—and eliminate any version that fails to meet it.

    Step 4: Configure acceleration. Choose a draft model, measure acceptance rates at different proposal lengths, and pick the combination with the highest throughput. Note that acceptance rates vary considerably across business scenarios, so measurements should use samples of real traffic.

    Step 5: Stress test and convert to cost. Run concurrency stress tests on the target hardware, compute the cost per token, and compare it with the current setup.

    Cost conversion can use a function like the following:

    ```python
    def cost_per_million_tokens(gpu_hourly_price, gpu_count, throughput_tps):

    1. # throughput_tps is the overall effective throughput,