Accelerating Open-Source LLM Inference on Ascend NPUs: A Practical Engineering Guide

    This article breaks down the practical work of running large language model inference on domestic accelerators such as Ascend NPUs using open-source inference code. It covers the five layers of the inference stack, operator adaptation, graph compilation and memory management, quantization and parallelism, and the service-layer requirements for handling production traffic, along with a deployment checklist for teams moving from a working prototype to a reliable online service.

    1. Background: Why Open-Source Inference Code plus Domestic Compute Is Gaining Momentum

    Open-source inference code targeting domestic accelerators has been updated continuously, and some model versions have already shifted part of their live traffic onto this type of compute. For developers and enterprise users, this marks a shift for heterogeneous compute: from “it runs” to “it can carry production traffic.” The real difficulty is not whether a single card can complete one forward pass, but whether throughput, latency, stability, cost, and the operations stack can meet online requirements at the same time.

    Note that this article does not evaluate or recommend any specific vendor or product. Instead, it abstracts the situation into a general engineering problem: how to build a large-model inference service that can keep evolving on a non-mainstream accelerator architecture.

    2. Technical Foundation: Key Layers of the Inference Path

    Inference on heterogeneous compute can usually be split into five layers:

    1. Model layer: weight format, tensor layout, vocabulary and tokenizer consistency;
    2. Operator layer: implementation and coverage of core operators such as attention, normalization, activation, and matrix multiplication;
    3. Graph layer: graph capture, constant folding, operator fusion, memory reuse;
    4. Parallelism layer: sharding strategies for tensor parallelism, pipeline parallelism, and expert parallelism;
    5. Service layer: dynamic batching, cache reuse, rate limiting and degradation, metrics and tracing.

    A weakness in any layer becomes a throughput bottleneck. Missing operators, for example, force heavy fallback to generic implementations and cause a sharp performance drop; insufficient graph optimization leaves memory bandwidth consumed by repeated data movement.

    3. Operator Adaptation: From “Runnable” to “High Performance”

    Operator adaptation is the first step and the largest share of the work. In practice, it is best to proceed in this order:

    • Coverage audit: inventory the set of operators appearing in the model, and flag high-frequency and time-consuming ones;
    • Accuracy alignment: use a mainstream implementation as the baseline, compare output error layer by layer, and locate numerically unstable points;
    • Fusion: combine adjacent normalization, activation, and residual additions into single operators to reduce intermediate tensors;
    • Layout optimization: adjust tensor arrangement according to the hardware’s memory access characteristics to increase the proportion of contiguous reads.

    Illustrative adaptation check logic:

    1. def check_op_coverage(model, op_registry):
    2. missing = []
    3. for node in model.graph.nodes:
    4. if node.op_type not in op_registry:
    5. missing.append(node.op_type)
    6. return sorted(set(missing))
    7. # Fill in high-frequency missing operators first, then handle the long tail
    8. missing_ops = check_op_coverage(model, registry)

    4. Graph Compilation and Memory Management: Spending Bandwidth Where It Matters

    The core tension during inference is memory bandwidth versus capacity. The value of graph compilation lies in merging many small operator calls into a few large ones, reducing scheduling overhead and intermediate results spilling to memory. Memory management focuses on three things: weights staying resident, paged KV cache, and reuse of temporary buffers.

    Common practices include:

    • Managing the KV cache with paging, allocating dynamically by sequence length to reduce fragmentation;
    • Pre-allocating weights from a unified memory pool to avoid repeated allocation and release at runtime;
    • Completing constant folding and shape specialization at compile time to reduce runtime branching.

    5. Quantization and Parallelism: Balancing Accuracy and Throughput

    Quantization can significantly reduce memory footprint and bandwidth pressure, but sensitive layers need attention. In practice, attention and normalization usually keep higher precision, matrix multiplication uses low-bit weights, and accuracy regression is evaluated with a small set of calibration samples.

    For parallel sharding, prefer tensor parallelism within a single multi-card node, then add pipeline parallelism across nodes; the sharding granularity must match the operator implementations, so that frequent cross-card communication does not cancel out the compute gains. One actionable rule: if communication time consistently exceeds compute time, adjust the sharding dimension or merge communications.

    6. Service Layer and Stability: Prerequisites for Carrying Live Traffic

    Live traffic is not controllable, so the service layer must provide:

    • Dynamic batching: aggregate requests under latency constraints to raise throughput per unit time;
    • Priority and rate limiting: protect critical requests and prevent long-tail requests from dragging down the whole;
    • Cache reuse: reuse computation results for requests with identical prefixes;
    • Observability: coverage of latency percentiles, queue length, memory watermark, and per-operator time distribution.

    If the enterprise side also needs model fine-tuning, data labeling, or training compute, a closed loop can be built with general capabilities such as the container platform, object storage, log service, and monitoring and alerting offered by Baidu AI Cloud, while the inference service itself stays decoupled from the specific infrastructure to make migration and replacement easier.

    7. Deployment Checklist

    1. Establish operator coverage and accuracy baselines, and turn them into regression cases;
    2. Do graph-level fusion first, then operator-level hand-written optimization;
    3. Account for memory in three parts — weights, cache, and temporary buffers — and reserve headroom;
    4. Cover burst traffic and long-sequence scenarios in load tests;
    5. Roll out gradually, and decide scaling by percentile latency rather than average latency.

    8. Conclusion

    Combining open-source inference code with domestic compute is essentially engineering “runnable” into “operable.” For developers, the real competitive edge is systematic understanding of the five layers — operators, graph, memory, parallelism, and service — rather than a single hardware metric. Following this path, teams can gradually take on real live traffic at a controlled cost while keeping enough flexibility for future model iterations.