LLM Inference Optimization on Domestic AI Accelerators: A Practical Guide
This guide walks through a production-oriented approach to optimizing large language model inference on domestic (China-built) AI accelerators. It is organized around three main lines of work—custom kernels, communication and memory management, and scheduling—and adds practical advice on benchmarking methodology and a phased rollout plan, so teams can balance latency, throughput, and cost without changing model quality. Code and configuration snippets illustrate the verification and scheduling steps.
1. Why Production-Grade Inference Optimization Has Become a Must
Once large models move into online serving, the complexity of the inference side often exceeds that of the training side. Training is a centralized, long-running workload that can tolerate queuing, while inference must handle massive concurrency, strict latency constraints, and unpredictable input length distributions. When the compute platform switches from general-purpose accelerator cards to domestic accelerator cards, the existing operator libraries, communication libraries, and memory management strategies may not be reusable as-is. Engineering teams must therefore redo a system-level balance of performance and cost without sacrificing model quality.
A recent signal from the industry: a team open-sourced the production-grade code of an inference engine running on domestic accelerator cards, stating that it already serves large-scale online traffic for a lightweight version of a large model. What deserves attention here is not any specific number, but the transferable methodology behind it—the three main lines of operators, communication and memory, and workflow and scheduling apply to almost every domestic compute migration scenario.
2. Three Main Lines of Optimization
2.1 Core Operators: From “It Runs” to “It Runs Fast”
Operators are the first gate to performance. A common problem in the early stage of migration: framework-generated implementations produce correct results, but memory accesses are uncoalesced, thread block configurations are unreasonable, and special shapes take inefficient branches. Optimization usually proceeds in this order:
- First establish operator-level benchmarks to identify the top few operators by time consumption;
- Fuse hot spots such as attention, normalization, and elementwise operations to reduce the round-trip reads and writes of intermediate tensors;
- Rewrite key kernels for the vector width, cache hierarchy, and instruction characteristics of the domestic hardware;
- Use precision alignment tests to hold the line and avoid sacrificing numerical stability for performance.
A schematic validation flow can be written as:
for op in hot_ops:ref = framework_reference(op, inputs)got = custom_kernel(op, inputs)assert allclose(ref, got, rtol=1e-3, atol=1e-3)bench(op, warmup=20, iters=100)
2.2 Communication and Memory Management: Spend Bandwidth Where It Matters
The bottleneck in multi-card inference is often not compute, but data movement between cards and between layers. Common practices include: re-partitioning tensor parallelism and pipeline parallelism according to operator characteristics so that communication overlaps with computation; pipelining chunked transfers to hide transfer latency; and on the memory side, introducing paged key-value caching, prefix reuse, and block-level reclamation to significantly reduce memory pressure in long-input scenarios.
Memory management must also account for fragmentation. A long-running service that frequently allocates and frees large blocks of device memory can hit “enough total memory but allocation fails” situations, so a combination of pooling plus tiered caching is needed, along with continuous observation of cache hit rates.
2.3 Inference Workflow and Task Scheduling: The Key to Tail Latency
Time to first token and per-stream output speed are essentially determined by scheduling quality. Actionable techniques include:
- Continuous batching, which batches requests arriving at different times to improve throughput;
- Chunked prefill, which slices very long inputs to avoid a single request monopolizing the device for a long time;
- Priority and admission control, which reserves a fast lane for interactive requests;
- Warm-up and resident replicas, which reduce the long tail caused by cold starts.
Scheduling policies are best managed through configuration to make canary rollout easier:
scheduler:batching: continuouschunked_prefill: truemax_num_seqs: 256kv_cache: pagedpriority_lane: interactive
3. Evaluation Methodology: Making the Data Trustworthy
The place where migration projects most easily go wrong is inconsistent metric definitions. It is recommended to fix the following elements in reports: hardware configuration and interconnect, input and output length distributions, concurrency and arrival model, statistical percentiles (P50/P90/P99), and the cost conversion method (for example, converting off-peak resource unit price into per-card hourly output). Only with a unified methodology does horizontal comparison become meaningful, and only then can peak values under ideal conditions be avoided being treated as an online commitment.
4. Rollout Roadmap and Cloud Support
The recommended pace is: first do operator and precision alignment, then tune single-card throughput, then get multi-card communication and cache reuse working, and finally integrate scheduling and canary release. Cloud environments usually also require supporting capabilities, for example using a container platform to host elastic replicas, using object storage to distribute model weights, and using logging services and a monitoring and alerting system to observe latency percentiles and cache hit rates.
Baidu AI Cloud can provide a combination of general-purpose containerized deployment, storage, and observability capabilities in these scenarios, helping teams turn optimization results into stable online services.
5. Conclusion
Inference optimization on domestic compute is not a single-point trick, but a coordinated engineering effort across operators, memory and communication, scheduling, and the evaluation system. Only by defining the methodology clearly, thoroughly optimizing the hot spots, and refining scheduling can a sustainable balance between low latency and cost efficiency be achieved, allowing optimization experience to be reused across teams.
