LLM Inference Acceleration in Practice: Day-0 Adaptation and Performance Optimization for Sparse Attention Architectures

    This article breaks down how a sparse attention architecture was adapted to a heterogeneous compute framework on Day 0 and optimized for long-context inference. It covers operator reuse, sparse attention kernel fusion, mixed-precision quantization, sequence parallelism for prefill, and scheduling/storage optimizations, along with the resulting latency and throughput gains.

    1. Background: Inference Performance Challenges Posed by a New Architecture

    As large language models continue to grow in both parameter scale and application scope, inference efficiency has become a key factor determining user experience and deployment cost. A recently released open-source large model adopts a more complex hybrid attention mechanism that interleaves state-recurrent attention with index-based sparse attention. While this architecture improves long-sequence modeling capability, it also introduces serious challenges for the underlying compute framework:

    • Dynamic load imbalance: When sparse attention computes relevance over historical tokens, later sequence positions need to traverse longer histories, causing significant load variation across compute devices.
    • Memory bandwidth bottlenecks: In long-sequence scenarios, the key-value cache consumes substantial memory, and high-precision index computation such as Q/K projections generates frequent memory reads and writes.
    • High time-to-first-token latency: During the prefill stage, the computational workload grows linearly with sequence length, which can easily cause slow first-token responses.

    To address these issues, a mature inference optimization solution needs to adapt quickly to new architectures while continuously unlocking additional performance. Based on practical optimization work on the CANN heterogeneous compute framework, this article analyzes the technical path to Day-0 support and discusses a full-stack optimization methodology covering operator fusion, deployment strategy, and scheduling.

    2. Architecture Analysis and Optimization Reuse: The Foundation of Day-0 Support

    When a new model architecture is released, the key to fast and high-quality adaptation lies in reusing existing optimization assets. The core innovation of this model is an interleaved attention design: a state-recurrent attention for global dependencies and a sparse attention that introduces an indexing mechanism on top of multi-head latent attention.

    From an engineering perspective, the underlying compute primitives of the new model overlap significantly with existing optimization work. For example:

    1. Reuse of state-recurrent operators: Previously accumulated fused operator kernels such as FlashKDA and MegaKDA can fully cover the state-recurrent portion of the new architecture without additional development.
    2. Cross-layer aggregation optimization: The model uses an attention residual mechanism to aggregate cross-layer representations. An existing two-stage fusion scheme can be directly reused.
    3. Mixture-of-experts and matrix compute acceleration: The feedforward network can reuse large-scale sparse expert parallelism such as MegaMoE and matrix multiplication optimization techniques such as MC2.

    This strategy of modular reuse plus incremental development shortens the adaptation cycle for a new model from weeks to days. The development team can focus on restructuring the upper-level compute graph rather than worrying about whether lower-level operators are supported. In addition, SuperKernel fusion and graph-mode compilation can automatically combine multiple fine-grained operators into coarser kernels, reducing kernel launch overhead. Combined with compilation caching, the graph compilation stage can be skipped during model loading, enabling load-and-infer behavior.

    3. Deep Fusion for Sparse Attention: Reducing Intermediate Read/Write Overhead

    The sparse attention part of the new architecture consists of two independent stages: an index selection stage and a sparse read stage. The index selection stage scores and selects the Top-K most relevant historical positions for the current query. The sparse read stage then fetches the corresponding key and value vectors from the main cache according to the selected indices.

    Fusion strategy 1: Index kernel fusion

    In the previous implementation, index scoring, head weighting, and Top-K selection were three separate kernels, with intermediate results written back to global memory and then read again. After optimization, these three steps are fused into a single kernel. It directly reads the key cache in paged form, avoiding logical-to-physical address translation overhead, performs similarity computation and top-position selection on-chip, and writes back only the final index list. This change reduces intermediate memory traffic by approximately 65% and lowers operator launch overhead by 80%.

    Fusion strategy 2: Multi-stream parallelism for lower latency

    On top of this, an indexer prelude mechanism was introduced. Through three parallel compute streams, query projection, key normalization, query quantization, and head-weight projection can execute concurrently. In the traditional flow, these operations have strict dependencies that leave compute units idle. Through detailed dataflow analysis, independent data can be split across different compute streams, making better use of idle vector and matrix units and reducing input processing time by more than 40%.

    4. Layered Quantization: Finding the Best Trade-off Between Accuracy and Speed

    In sparse attention, the memory access volume for index data and cache data far exceeds the dense compute volume. Aggressive low-bit quantization is therefore critical for improving throughput.

    • Index-side quantization: The MXFP8 format is used, with a group of 32 elements sharing one scaling factor through per-group dynamic quantization. This granularity was chosen based on accuracy and performance trade-off tests. Finer granularity such as per-token can slightly improve accuracy but increases scaling-factor storage and dequantization overhead. Coarser granularity such as per-tensor causes excessive accuracy loss on long-tail distributed data.
    • Cache-side quantization: The key-value cache uses FP8 with per-token-head-tile granularity, meaning 128-element blocks within each token and attention head. This design leverages the locality characteristics of attention computation. Key and value vectors of the same token share the same temporal locality, and block-wise quantization avoids cross-block data dependencies, making vectorized loading and computation easier.

    With these strategies, index data storage overhead is reduced to one quarter of the original, key-value cache read/write bandwidth consumption is reduced by 50%, and accuracy loss remains within an acceptable range on long-sequence benchmarks, with BLEU dropping by less than 0.2.

    5. Sequence Parallelism in Prefill: Breaking the Long-Sequence Latency Bottleneck

    For extremely long sequences, such as context lengths exceeding 50K tokens, the prefill stage requires massive computation. A single device is constrained by both memory capacity and compute latency. Sequence parallelism is needed to break through the single-device compute limit.

    Combined CPxTP deployment strategy

    This strategy splits input along the sequence dimension on top of tensor parallelism, which partitions attention heads. In an 8-device cluster, the sequence can be split into 8 segments, with each device processing only 1/8 of the sequence. This not only reduces per-device activation memory to 1/8 but also proportionally reduces compute time.

    Zigzag load-balancing layout

    In causal index computation for sparse attention, later sequence positions have longer historical contexts and therefore heavier compute loads. Uniform partitioning would leave earlier devices underutilized while causing congestion on later devices. The optimized approach uses a Zigzag layout that pairs the beginning and end of the sequence. For example, segment 1 and segment 8 are assigned to the same device, segment 2 and segment 7 to another. After this interleaved assignment, each device processes one long-history segment and one short-history segment, making the total workload more balanced. After cross-device index computation is completed, an all-to-all communication restores the original sequence order.

    DSL-based custom operator support

    When sequence parallelism is used during prefill, the standard KV compute graph must be restructured. Through the CANN domain-specific language such as CANNBot-DSL, developers can describe custom operator logic using high-level expressions and generate efficient low-level kernels. This approach significantly reduces the implementation cost of the context-parallel version of FlashKDA, with parallel scaling efficiency reaching more than 85% of theoretical peak.

    6. Inference Scheduling and Storage Optimization: The Key to Marginal Performance Gains

    After completing the core compute optimizations, further throughput improvements can come from scheduling and storage details:

    1. Speculative decoding acceleration: The DSpark speculative inference strategy is reused. A small model generates candidate sequences, and the large model verifies them in parallel. Combined with an SSM state replay mechanism, incremental state-recurrent computation is cached on-chip to avoid repeated full recomputation, reducing single-user latency by 2.3x.
    2. Storage layout rearrangement: Weights use the NZ (Non-Zero) layout, which partitions matrices into specific small blocks and reorders them to match the data loading requirements of matrix multiplication units. This adjustment improves matrix unit utilization by approximately 15%.
    3. Multi-stream and event synchronization: Multi-stream mechanisms distribute independent attention head computations across different streams. Event-based synchronization replaces global synchronization barriers, preventing compute units from waiting for transfers to complete and thereby hiding communication latency.

    7. Summary and Performance Gains

    Through the combined optimizations described above, inference performance of this model on Ascend hardware achieved the following key results:

    • Time-to-first-token latency for long-sequence prefill was reduced by 47% compared with the standard implementation.
    • Decoding throughput improved by 3.1x.
    • Maximum supported context length expanded from 32K to 128K, constrained by available memory capacity.

    The core value of Day-0 adaptation is not merely making a model runnable, but converting hardware compute capability into usable performance gains. This optimization approach can be reused for most new models that adopt hybrid attention mechanisms. When facing similar new architectures, developers should prioritize operator reuse, minimize intermediate results, and carefully manage memory bandwidth. They should also make full use of graph compilation and DSL tools provided by the underlying framework to achieve maximum performance improvement with minimum development cost.