Adapting Trillion-Parameter Sparse Attention Models on Domestic GPU Software Stacks

    This article examines the engineering methods required to run trillion-parameter models on a domestic AI accelerator software stack on Day-0. It covers the four-layer adaptation architecture, the three distinct computational paths of sparse attention (index selection, KV cache layout, and separate Prefill/Decode handling), an online conversion strategy for 4-bit expert weights that avoids offline copies by reusing an FP8 workspace across layers, and a practical checklist for porting large models to new hardware platforms.

    Trillion-Parameter Model Inference Adaptation: Engineering Paths for Sparse Attention and Low-Bit Experts

    1. Background: When Model Architectures Evolve Faster Than Hardware Adaptation

    Once large models cross the trillion-parameter scale, the bottleneck in inference systems shifts from “is there enough compute” to “does the structure map onto the hardware.” New-generation models commonly introduce three structural changes:

    • Ultra-long context: context windows reach the million-token level, making KV cache capacity and access patterns the primary constraint;
    • Sparse attention: dynamic index selection replaces full attention computation, turning the computation graph from “regular and dense” into “data-dependent”;
    • Low-bit expert weights: in Mixture-of-Experts (MoE) structures, expert weights are stored in 4-bit-level floating-point formats and must be converted numerically at inference time.

    These three changes impose requirements on the hardware-software stack that differ from the adaptation path for traditional dense Transformers. This article uses a Day-0 adaptation practice for a trillion-parameter model as its thread, laying out the specific engineering methods a domestic AI accelerator software stack applies in three directions: sparse attention, KV cache layout, and low-bit expert computation, as a reference for developers working on inference adaptation.

    “Day-0 adaptation” here means completing weight loading, service startup, and a working basic inference path on the day model weights are publicly released. What it tests is not long-term tuning capability but the software stack’s ability to rapidly map a new computing paradigm.

    2. Overall Adaptation Architecture: From Community Model Code to a Running Service

    The adaptation work does not implement the model from scratch. Instead, it takes the existing model implementation in an upstream open-source inference framework and migrates the key computation paths to the target hardware. The overall chain can be summarized in four layers:

    1. Model definition (community code)
    2. ↓
    3. Inference framework scheduling layer (request orchestration, batching, sampling)
    4. ↓
    5. Operator layer (attention, MoE, normalization, residual, etc.)
    6. ↓
    7. Compilation and runtime (DSL compiler, kernel generation, memory management)

    In this chain, the operator library handles “equivalent implementations of key computation paths,” the domain-specific DSL and compiler handle “rapid generation of new operators,” and the inference framework handles “service encapsulation.” Missing any one of the three leads to a model that loads but does not run, or runs but delivers unusable performance.

    The adaptation team’s public descriptions mention three supporting components: an operator library for the in-house architecture, compilation backends for Triton and TileLang, and an in-house compiler toolchain. Their roles can be understood as follows:

    • Operator library: provides hand-optimized implementations for high-frequency paths such as attention, matrix multiplication, and normalization, guaranteeing a performance floor;
    • DSL compilation backend: compiles computations described in a Python-level DSL into kernels executable on the target hardware, solving the problem of “no ready-made operator for a new structure”;
    • Compiler toolchain: handles cross-kernel fusion, memory layout, and instruction generation, and is a prerequisite for landing complex computation graphs.

    A common principle in engineering practice: use hand-written operators for hot paths to guarantee performance, and use the DSL for long-tail and new structures to guarantee coverage. In the Day-0 phase, functional correctness comes first, and performance optimization is left for later iterations.

    3. Adapting Sparse Attention: Three Computation Paths Must Be Handled Separately

    The core idea of sparse attention (referred to as DSA in the source material) is: instead of computing attention between every query token and all historical tokens, first perform index selection and compute attention only over the small set of selected key-value pairs. This change looks like “computing less,” but its impact on the inference stack is global.

    3.1 Index Selection: Splitting a Data-Dependent Branch Out of the Dense Computation Graph

    The index selection stage selects the top-k positions based on the similarity between query vectors and historical keys. Its characteristics are:

    • Relatively little computation, but it depends on the full historical key representations and cannot be merged with the main attention computation;
    • Its output is a variable-length index set that determines the tiling granularity of subsequent computation;
    • It is latency-sensitive because it sits at the front of the critical path.

    During adaptation, this branch must be implemented as a separate operator, and its output must be directly consumable by the subsequent attention operator, avoiding round trips of intermediate results between host and device.

    3.2 KV Cache Layout: Memory Organization Under Sparse Access

    The KV cache for dense attention is usually laid out contiguously, ordered by layer and by head, with regular access patterns. Sparse attention performs random access by index set; if the contiguous layout is retained, a large number of uncoalesced memory accesses appear and bandwidth utilization drops sharply.

    Two common approaches in practice:

    • Tiling + index mapping: organize the KV cache in fixed-size blocks, with indices pointing to blocks rather than individual tokens, converging random access into block-level access;
    • Layered paging: allocate independent page tables for different layers and heads, combined with prefetching strategies to hide memory access latency.

    There is no “standard answer” here; it requires co-design with the hardware’s cache hierarchy and access granularity. The “KV cache layout adaptation” mentioned in the adaptation work is essentially reorganizing memory to match sparse access patterns.

    3.3 Separate Adaptation for Prefill and Decode

    The characteristics of the two phases differ greatly:

    Phase Computational characteristics Adaptation focus
    Prefill Long sequences, high parallelism, compute-intensive Large-block parallelism of index selection and main attention, operator fusion
    Decode Single-token increments, memory-intensive Cache read latency, incremental maintenance of the index set

    Adapting the two paths separately rather than reusing one set of kernels is a key decision for landing this class of models. In the Prefill phase, index selection and attention computation can be pipelined in parallel; in the Decode phase, per-step synchronization overhead should be minimized and index updates made incremental.

    3.4 Linking With Other Structures

    Sparse attention does not exist in isolation. The model also contains a state management module (for maintaining recurrent state across positions), cross-layer residual connections, and attention computation in the vision branch. These structures must share the same cache and state management mechanism with sparse attention to form a complete execution chain. If adaptation only connects attention itself, correctness deviations often appear at the state synchronization step—the pitfall most easily hit in actual debugging.

    4. Low-Bit Expert Weights: An Online Conversion Strategy Without Offline Copies

    Expert weights in the MoE structure are stored in 4-bit-level floating-point formats (such as block floating-point formats like MXFP4), while compute units typically execute at 8-bit floating point or higher precision. The conventional approach is to convert weights offline to the target precision and generate a complete copy, but for trillion-parameter models this brings two problems:

    1. Doubled storage: converting 4-bit to 8-bit directly doubles weight volume, putting pressure on both memory and load time;
    2. Version coupling: offline copies and original weights must be maintained in sync, raising iteration costs.

    The adaptation solution chooses online conversion + cross-layer reuse of a workspace:

    • At inference time, expert weights are converted from 4-bit to 8-bit on demand, with the conversion result placed in a pre-allocated FP8 workspace;
    • This workspace is reused across layers, avoiding a separate buffer allocation for each layer;
    • Since MoE activates only part of the experts per layer, the actual workspace footprint is far smaller than the full weight set.

    The cost of this strategy is the introduced conversion overhead; the benefit is significantly lower peak memory and weight preparation time. For the Day-0 scenario, this is a reasonable trade-off: get it running first, then talk about throughput.

    The following illustrative pseudocode expresses the logic of “on-demand conversion + workspace reuse”:

    1. # Pseudocode: online conversion of expert weights with workspace reuse
    2. fp8_workspace = alloc_fp8_buffer(max_expert_size)
    3. def moe_forward(x, layer):
    4. # 1. Routing: select the experts activated in this layer
    5. expert_ids, gates = route(x, layer.router)
    6. # 2. Process experts one by one; convert weights on demand into the reused workspace
    7. outputs = []
    8. for eid, gate in zip(expert_ids, gates):
    9. w_mxfp4 = layer.experts[eid].weight # original low-bit weights
    10. convert_to_fp8(w_mxfp4, fp8_workspace) # online conversion, written into the reused region
    11. y = matmul(x, fp8_workspace) # execute expert computation in FP8
    12. outputs.append(gate * y)
    13. # 3. Combine expert outputs
    14. return combine(outputs)

    It is important to note that the workspace size should be estimated from the “maximum experts activated in a single layer,” not the sum of all experts; and conversion must be pipelined with computation, otherwise it degrades into a serial bottleneck.

    5. Engineering Checklist

    Based on the analysis above, adapting a trillion-parameter model to a new hardware platform can be summarized in the following checklist:

    1. Align the computation graph before discussing performance: verify semantic equivalence subgraph by subgraph, especially the boundary conditions of three structure types—sparse indices, recurrent state, and residual accumulation.
    2. Split kernels by phase: adapt Prefill and Decode separately; do not try to cover both load characteristics with one kernel.
    3. Memory layout first: the tiling and paging design of the KV cache should be settled before operator implementation, otherwise rework costs later are extremely high.
    4. Prefer online quantization strategies: for low-bit weights, prioritize online conversion + workspace reuse to avoid the storage and maintenance burden of offline copies.
    5. Reuse rather than rewrite: take over the upstream framework’s model definition and scheduling logic and replace only the computation paths, greatly reducing correctness risk.
    6. Preserve observability: instrument index selection, cache hits, and expert routing to make it easier to locate precision and performance issues.

    6. Conclusion

    The architectural evolution of trillion-parameter models is turning inference adaptation from “operator replacement” into “computing paradigm mapping.” Sparse attention changes memory access patterns, low-bit experts change numerical paths, and ultra-long context changes cache organization—stacked together, these require the software stack to possess both hand-optimization capability and the ability to rapidly generate new operators. For developers, understanding the engineering constraints behind these structural changes is more valuable than memorizing a specific implementation. Day-0 adaptation is only the starting point; the real test lies in iterating “it runs” into “it runs efficiently and reliably.”