LLM Inference Engineering Optimization on Domestic AI Compute: Bottlenecks and Reusable Methods

    This article examines common engineering bottlenecks in production-grade LLM inference on domestic AI compute platforms, using an open-source implementation as a reference. It analyzes cross-device synchronization during Mixture-of-Experts prefill, memory copy submission granularity, embedding state storage tiering, and operator fusion with huge pages. It then outlines a reusable optimization methodology and deployment guidance, showing that meeting high service standards depends on memory management efficiency, communication path length, and operator execution efficiency.

    1. Introduction: From “It Runs” to “Production-Grade Service”

    When a set of LLM inference code is open-sourced in a production-ready form on a domestic AI compute platform, what the industry really cares about is not whether it runs, but whether it can meet online service standards on three fronts at once: throughput, latency, and stability. The value of such code lies in exposing the engineering details: which parts already achieve hardware-native optimization, and which still carry extra overhead from cross-device synchronization and interface granularity. Using this kind of implementation as an observation sample, this article abstracts four common problems and provides general optimization methods.

    It is worth emphasizing that the “highest standard” for inference service is a quantifiable target: time to first token, time per output token, concurrent throughput, long-context stability, and fault recovery time. Whether any platform can meet these targets ultimately comes down to three things: memory management efficiency, communication path length, and operator execution efficiency.

    2. Engineering Analysis of Four Typical Bottlenecks

    2.1 Cross-Device Synchronization in Mixture-of-Experts Prefill

    In the prefill stage of a Mixture-of-Experts architecture, expert dispatch needs to route each token to the device hosting its assigned expert. Some implementations perform a device-side synchronization before dispatch. The usual reason is that the memory peak is unpredictable, so synchronizing first allows the buffer size to be computed precisely and avoids allocation failures. The cost is that the pipeline is broken: computation and communication cannot overlap, and the more synchronization points there are, the more noticeable the tail latency jitter becomes.

    A more general approach is to switch to “upper-bound estimation plus fallback”: pre-allocate buffers according to the theoretical upper bound of the routing distribution, and fall back to a synchronization only if actual usage exceeds a threshold; or push the synchronization down into the communication operator, replacing explicit blocking with asynchronous handles and event dependencies.

    1. # Pseudocode: replace explicit sync with upper-bound estimation plus fallback
    2. buf = alloc(upper_bound_of_tokens_per_expert)
    3. if not dispatch_async(buf):
    4. sync_once()
    5. buf = alloc(exact_size)
    6. dispatch_async(buf)

    2.2 Submission Granularity of Bulk Memory Copies

    Another common difference lies in how bulk copies are implemented: one side submits asynchronous copies one by one in a loop, while the other uses a native bulk copy interface to submit multiple tasks at once. Submitting one by one accumulates call overhead and scheduling overhead linearly as the number of blocks grows, directly affecting the latency of paths such as prefix cache reuse and weight reloading.

    There are three levels to the optimization: first, prefer the platform’s bulk submission capability; second, if it is not yet available, merge contiguous address ranges and bucket them by block size before submission; third, use multi-stream concurrency to hide the latency of individual submissions, while ensuring that dependencies between streams are maintained by events rather than global synchronization.

    2.3 Storage Tiering for Vocabulary and Embedding State

    Some implementations force embedding state to stay resident in device memory, and this takes effect even when the user explicitly disables it. Such designs are often motivated by access frequency considerations, but they permanently occupy scarce device memory, compress the space available for the key-value cache, and thereby limit concurrency and context length.

    A reasonable approach is to make the storage location a configurable policy: keep hot data resident on the device, place warm data in host memory, and push cold data down to remote storage, while exposing a capacity query interface so the scheduler can decide the concurrency limit accordingly. After tiering, the same amount of device memory can typically support a noticeably larger concurrency scale.

    2.4 Operator Fusion and Huge Pages

    Fusion is a key means for domestic AI compute to catch up on hardware utilization. Merging originally separate operators such as projection, normalization, residual mixing, and gating scores reduces round trips of intermediate tensors to and from memory; combined with operator templates generated according to platform characteristics, this can achieve near-peak compute utilization without sacrificing generality.

    On the memory side, huge pages are essential: define a separate allocation policy for the key-value cache, use 1 GiB-level huge pages to reduce the number of page table entries and address translation overhead, and provide a huge page headroom query in the platform abstraction layer for capacity planning.

    3. A Reusable Optimization Methodology

    1. Measure before optimizing: use operator-level timelines to locate synchronization points and submission overhead, and avoid changing things based on intuition.
    2. Turn “hard-coded policies” into “configurable policies”: storage tiering, synchronization mode, and copy granularity should all be switchable.
    3. Isolate hardware differences with an abstraction layer: unify the three types of interfaces for memory, communication, and operators, keeping upper-layer logic platform-agnostic.
    4. Build a capacity model: use huge page headroom, cache hit rate, and expert distribution entropy as scheduling inputs.
    5. Canary release and fallback: any fusion or asynchronous change must retain a fallback path.

    4. Deployment Recommendations and Cloud Form

    When hosting this kind of inference service in the cloud, you can combine a container platform for elastic scaling, object storage for model weights, and logging and monitoring services for full-link observability. Baidu AI Cloud typically provides general compute scheduling and observability support in such scenarios; refer to the official documentation for specific specifications.

    Overall, domestic AI compute already has the foundation to provide inference services to a high standard. The remaining gap is concentrated in the “last mile” engineering details of synchronization granularity, copy submission, and memory tiering, and all of these details have clear general solutions.