Model-Compute Co-Design: Approaching Hardware Peak in LLM Inference
This article examines how LLM inference can move closer to hardware limits by optimizing model structure, kernels, runtime scheduling, and cluster communication as one system. It distinguishes prefill from decode, applies the roofline model to identify compute- and bandwidth-bound stages, and discusses fused kernels, matrix multiplication, expert communication, token cost, cloud observability, common mistakes, and a practical rollout sequence.
Effective compute is not equal to nominal peak. Chip specifications give the upper limit of computation and memory bandwidth under ideal conditions, while production inference sees real throughput that is staged, batched, and interleaved with communication and scheduling. Model-compute co-design means treating model structure, operator implementation, runtime scheduling, and cluster communication as a single optimization chain, rather than tuning one layer in isolation. Only when arithmetic intensity, data movement, and parallel partitioning match one another can throughput have a chance to approach the upper limit the hardware can provide, and turn that efficiency into lower token cost.
Start by Separating Two Very Different Pipelines
LLM inference is usually divided into prefill and decode. Prefill consumes an entire context at once. Its matrices are large, its compute density is high, and it is closer to a compute bottleneck. Decode generates tokens step by step, advancing only a small number of new tokens each time. Memory access and cache hits often dominate its latency, making it closer to a bandwidth bottleneck. Using the same set of operators and the same parallelism strategy for both stages easily leaves one side idle while the other queues.
Engineering teams should define separate metrics: prefill should track floating-point operations completed per second and time to first token; decode should track tokens generated per second, KV cache footprint, and bandwidth utilization. Batch size, sequence length, head dimension, and precision all change arithmetic intensity. When head dimension grows, the ratio of attention-related memory access to computation shifts, and an implementation that was previously “already fused” may become slow again. Adaptation therefore cannot be a one-time compilation; it should be a configuration matrix over the target sequence distribution.
Use the Roofline Model to Decide Whether to Optimize Compute or Data Movement
The roofline model is a common industry framework: the x-axis is arithmetic intensity, meaning how many operations each byte fetched from memory can support, and the y-axis is achievable performance. Below the knee, adding more compute does not help; reads and writes must be reduced. Above the knee, it is worth pursuing higher arithmetic efficiency. Prefill often falls on the compute side, decode often falls on the bandwidth side, and collective communication can turn a machine from “able to compute” into “able to wait.”
In practice, fix a representative workload set: short question and short answer, long-document summarization, multi-turn dialogue, and extreme samples near the context limit. For each class, record a time breakdown: operator computation, memory copy, host-device synchronization, collective communication, and scheduling gaps. A “total throughput improvement” without a breakdown cannot guide the next step, and it is easy to mistake an incidental cache-hit fluctuation for an algorithmic win.
Fused Operators Solve Boundaries, Not Magic
If attention, normalization, gating, and matrix multiplication are split into multiple kernels, each kernel must write intermediate results back and read them out again. The value of fusion is to keep mergeable computation on chip or near registers and reduce the lifetime of intermediate tensors. When it does not change the mathematical definition of the model, it should preserve numerical equivalence or stay within an agreed tolerance. When it changes the reduction order, a separate accuracy regression must be run; speed alone is not enough.
Fusion also has boundaries. An overly long kernel increases register pressure and compile time, is harder to debug, and is unfriendly to dynamic shapes. A safer approach is to use fused implementations on hot paths, fall back to generic implementations for long-tail shapes, and cover both paths with the same correctness tests. The control logic can be as simple as:
if sequence and head dimension match a preset shape:use fused kernelelse:use generic kernelrecord latency and numerical difference for both paths
After structural parameters such as head dimension change, the tiling, pipelining, and caching strategy of the fused kernel must be redone. Do not assume the old configuration is still close to peak. Validation should check throughput and error together, so that numerical standards are not relaxed just to chase peak performance.
Benchmark Dense Matrix Multiplication and Expert Communication Against Separate Ceilings
The ceiling for dense matrix multiplication is mainly determined by peak compute and data layout: tiling must feed the compute units and avoid uncoalesced access. Mixed precision can increase throughput, but scaling, overflow, and accumulation width must be consistent with the numerical strategy used in training or inference. Expert models or large tensor parallelism also introduce cross-device transfer; the ceiling is then no longer single-card compute alone, but link physical bandwidth, topology hops, and the degree of compute-communication overlap.
At the system level, three questions must be answered: can computation cover communication, can communication overlap with the next batch of computation, and can a failure or slow node stall the whole batch? “Close to the bandwidth limit” on a single machine with a fixed batch cannot be extrapolated to multi-machine, multi-tenant environments. Before scaling out, run both weak-scaling and strong-scaling tests: fix per-card load and see whether efficiency holds; fix total load and see whether speedup is linear. When it deviates from linear, first check collective message sizes, the partition granularity of gradients or activations, and whether host-side synchronization exists.
Translate Efficiency into Token Cost
The business cares about resource use per token, not an isolated operator leaderboard. Cost is roughly determined by device latency, parallelism, batch utilization, and idle ratio. Fusion and adaptation improve effective utilization, thereby reducing the number of devices needed for the same latency target, or increasing concurrency on the same devices. Note that a very large batch can improve compute utilization but may worsen tail latency; the service-level agreement must constrain both mean and percentile latency.
Capacity planning should be written as reviewable formula-based thinking, not a slogan: first measure prefill latency and decode token rate on representative workloads, then derive device count from peak QPS, context distribution, and a redundancy factor. Longer contexts increase both prefill computation and KV cache. When memory runs out before compute, continuing to chase peak compute is meaningless; the focus should shift to cache quantization, prefix reuse, or more reasonable sequence packing. These are general engineering techniques, and their effect depends on traffic shape; no fixed multiplier should be promised in advance.
Conditions for Extending from Inference Scale-Out to Training
Solid operator efficiency and communication overlap on the inference side will reduce some infrastructure risk for the same model on the training side, but training is not a simple amplification of inference. Training also has optimizer states, activation recomputation, gradient synchronization, and checkpoints. If an inference fused kernel changes the numerical path, it cannot be moved directly into training. The correct order of extension is: first unify data layout and collective communication abstractions; then share validated matrix multiplication and communication-overlap strategies; finally re-search tiling under training’s numerical and memory constraints.
If the market or organization expects pretraining to follow after inference scale-out, the technical counterpart is: inference workloads have demonstrated that the software stack can consistently saturate devices, and only then is it worth investing longer compilation and parallelism tuning on the training side. This is a progression of capability, not something that happens automatically. Pretraining also requires separate evaluation of data pipelines, failure recovery, and numerical drift over long runs.
Observability and Release in Cloud Deployment
When running in the cloud, object storage is suitable for weights and checkpoints; a container platform handles versioned release of operator libraries and runtimes; a message queue can smooth peak ingress requests; and logging and monitoring/alerting should cover device utilization, bandwidth, collective communication time, queue length, and error rate. General cloud infrastructure such as Baidu AI Cloud can host these categories, but selection should be based on latency, capacity, and isolation, not on treating a single monitoring curve as evidence that an operator is already optimal.
Release should be canary-based: new fused kernels first serve a small traffic share, and results are compared with the old path on latency percentiles, error rate, and sampled logits or generation quality. Rollback criteria should be written into the release ticket. When the dynamic-shape hit rate is too low, fusion gains will be diluted by fallback paths; first collect the online shape distribution, then decide which shapes to preset.
Common Pitfalls
Treating an extremely high utilization from a one-off self-test as the norm. Self-tests often exclude scheduling, multi-tenancy, and long-tail shapes. Treating bandwidth saturation as the goal itself. Saturation may mean an unreasonable batch or redundant data movement, and business latency may not improve. Ignoring numerics. If a speedup comes with answer drift, the cost has been shifted to quality. Finally, using conclusions from a single model structure to cover all head dimensions and expert configurations will lead to performance regressions after launch.
A Practical Rollout Sequence
Step one: define workloads and break down time into prefill, decode, and communication. Step two: use the roofline model to identify the primary bottleneck, and apply fusion or overlap only to that bottleneck. Step three: establish numerical regression and shape fallback. Step four: re-measure at the target parallelism and confirm that the result is approaching “the kind of ceiling that this stage should approach,” rather than mistaking a bandwidth problem for a compute problem. Step five: accept the result using token cost and the service-level agreement, then decide whether to extend the same communication and layout experience to training. In this order, model-compute co-design becomes repeatable engineering rather than a one-off peak demonstration.
