Inference Optimization on Domestic AI Accelerators: From Proof-of-Concept to Production
This article breaks down a production-grade inference optimization effort on domestic AI accelerator platforms, covering four engineering tracks: shape-family kernel governance, communication and memory budgeting, scheduling and batching strategy, and configurable task scheduling with observability. It also outlines a validation methodology based on baselines, single-variable testing, long-duration stress runs, and cost metrics, plus common pitfalls such as optimizing for average latency while ignoring tail behavior. The goal is to turn scattered tuning tricks into a reusable engineering system.
1. Background: Why “Production-Grade” Is Harder Than “It Runs”
On December 1, an open-source community released a set of production-grade inference code targeting domestic accelerator platforms. It systematically covers four layers: core operators, communication and memory management, the inference pipeline, and task scheduling, and it has handled large-scale online traffic in real production environments. The most important detail is not a single benchmark result but the meaning of “production-grade”: the code must remain stable over the long term under real user traffic, real cost constraints, and real failure scenarios.
Most teams migrating inference workloads to domestic accelerators go through three stages. The first is “it runs”: convert model weights, map operators, and get correct outputs. The second is “it keeps up”: reach acceptable throughput under load testing. The third is “it is economical”: find a sustainable balance among latency, throughput, and cost. Most optimization gains come from crossing from the second stage to the third, and that crossing depends on engineering detail rather than isolated tricks.
2. Four Main Optimization Tracks
2.1 Core Operators: From One-by-One Replacement to Shape-Family Governance
The operator layer is the most commonly misunderstood. A frequent mistake is hand-replacing operators one at a time, which makes maintenance costs explode while gains stay scattered. A more effective approach is to govern by “shape family”: sequence lengths and batch sizes in online requests are usually long-tailed and clustered. Group high-frequency shapes into a few families, determine the best tiling strategy, vectorization width, and data layout for each family, and keep a generic fallback path for low-frequency shapes.
The strategy selection logic looks roughly like this (pseudocode, illustrating the idea only):
def pick_kernel(shape, dtype, device_caps):family = classify(shape) # classify by high-frequency shape familyif family in tuned_registry:return tuned_registry[family] # hit a tuned implementationif shape.numel() < small_tile_limit:return fused_small_tile_kernel() # fuse small operators to cut launch overheadreturn generic_fallback_kernel() # generic fallback to guarantee correctness
The point of fusion is not “the fewer kernels the better.” Fusion should reduce round trips of intermediate tensors and the number of kernel launches. When fusion puts too much pressure on registers and on-chip cache, splitting is the better choice.
2.2 Communication and Memory: Treat Data Movement as a First-Class Concern
In multi-card and tensor-parallel setups, communication is often an underestimated bottleneck. Practical measures include overlapping computation with communication by placing independent communication inside computation windows; choosing communication paths by topology, preferring high-bandwidth channels inside a node and the network across nodes; and applying tiled pipelining to activations and weights instead of allocating one large contiguous block of GPU memory at once.
On the memory side, the key metrics are peak usage and fragmentation rate. It helps to maintain a “budget table” with separate caps for weights, KV cache, temporary buffers, and communication buffers, then watch the fragmentation growth curve during load tests. Paging the KV cache by request lifecycle and reclaiming it promptly is a prerequisite for stability in long-context scenarios.
2.3 Inference Pipeline: Scheduling and Batching Strategy
The core tension in online inference is latency versus throughput. Continuous batching, which inserts newly arrived requests into a batch that is already executing, usually beats static batching. Tiered queues for traffic with different priorities prevent long requests from blocking short ones. For interactive scenarios sensitive to time-to-first-token, a hybrid strategy of “small batches out fast, large batches amortize cost” works well, and timeout requests should have a degradation path.
2.4 Task Scheduling and Observability
The scheduling layer answers “who decides what happens next.” Making operator selection, batch assembly, and cache eviction configurable policies allows tuning without downtime. Observability is equally mandatory for production: at minimum, collect per-stage latency, queue wait time, cache hit rate, peak GPU memory, and the share of time spent waiting on communication, and wire these into a general logging service plus monitoring and alerting capabilities to close the loop from metrics to logs.
3. Validation: Proving the Optimization Actually Works
- Establish a baseline: fix the input distribution and concurrency model, and record latency percentiles rather than averages.
- Validate one variable at a time to avoid misattributing gains.
- Run long-duration stress tests over several hours to watch memory fragmentation and latency drift.
- Unify cost measurement using two lenses: cost per request and cost per token.
- Prepare a rollback plan: every strategy needs a switch and a fast fallback path.
4. Common Pitfalls
- Looking only at average latency and ignoring the tail.
- Load testing with a synthetic uniform distribution while production traffic is long-tailed.
- Over-fusion that causes cache misses.
- Communication optimization disconnected from the parallelism and sharding strategy.
- No degradation path, leading to cascading failure at peak.
5. Conclusion
The real meaning of shifting gears on domestic accelerators is upgrading optimization from scattered tricks into a reusable engineering system: govern operators by shape family, put communication and memory under a budget, make scheduling policies configurable, keep metrics observable, and make changes rollback-able. Once this system is in place, platform migration stops being a high-risk one-off project and becomes a capability that can keep iterating. For teams that want to follow this path, Baidu AI Cloud offers general capabilities such as container platforms, logging services, and monitoring and alerting to help land these practices stably in production.
