Software-Hardware Co-Design for Inference Adaptation of Ultra-Large Sparse Models

    This article details the Day-0 inference adaptation of a trillion-parameter sparse model on a custom accelerator, covering sparse attention, low-precision expert weights, operator and compiler coverage, and service-level validation. It provides a practical reference for engineers tackling the software stack challenges introduced by next-generation large model architectures.

    Introduction: Inference Challenges of Trillion-Parameter Sparse Models

    As large model parameter counts continue to grow, inference systems face a challenge that goes beyond simply “whether the model can run.” The real question is how to convert an ultra-large model into a stable, externally accessible inference service under the constraints of limited memory, bandwidth, and compute. The complexity increases significantly when the model reaches trillion-scale parameters and introduces sparse structures, ultra-long contexts, multimodal inputs, and low-precision weights.

    The model discussed in this article is a publicly available large sparse model with approximately three trillion parameters. It supports a context length of over one million tokens and processes both text and image inputs. The model performs well on several public benchmarks—ranking near the top among open-weight models on agent tasks and scoring above average on terminal and long-horizon code benchmarks. While these scores reflect model capability, from an engineering perspective they also indicate that the internal architecture includes several new designs, such as sparse attention, cross-layer state management, mixture-of-experts (MoE) computation, low-precision quantized weights, and multimodal encoding paths.

    Such models are typically released by the community as open weights, and inference frameworks often add support quickly. However, for custom accelerators and proprietary software stacks, the operator ecosystem, compiler pipeline, and memory model differ from mainstream training frameworks. Achieving “basic inference validation on the day the model is released” is not straightforward. This article breaks down a Day-0 adaptation practice and examines its key stages and general methodology.

    What Day-0 Adaptation Really Means

    Day-0 adaptation does not mean completing all optimizations and going live on the same day the model weights are released. Instead, it refers to closing the full chain—from model loading to returning a basic inference result—within the shortest possible time after the weights become public. At this stage, the goal is not maximum throughput, minimum latency, or highest concurrency, but rather validating four things:

    1. Whether the model weights can be correctly read and mapped to accelerator device memory;
    2. Whether the key operator paths in the new architecture can execute correctly;
    3. Whether basic text and image inputs can complete a forward pass;
    4. Whether the output results maintain reasonable consistency with the reference implementation.

    For training tasks, the Day-0 focus might be whether the loss decreases. For inference services, the focus is whether the service can start, requests can return, and results are plausible. All later work—performance tuning, resource compression, and concurrency scaling—builds on this foundation.

    Therefore, the core challenge of Day-0 adaptation is often not “whether compute is sufficient” but “whether the software stack covers the new computation patterns introduced by the model.” When the model architecture includes paths not yet covered by the existing operator library, compiler, or runtime, those gaps must be migrated, patched, or fallback-implemented in a very short time.

    Overall Adaptation Framework: Layered Breakdown from Loading to Serving

    Adapting a large-scale model on a custom accelerator typically requires breaking down the problem layer by layer. The figure below shows an abstract inference adaptation pipeline.

    1. Model weights and configuration
    2. │
    3. ▼
    4. Model loading and tensor mapping
    5. │
    6. ▼
    7. Graph execution plan generation
    8. │
    9. ▼
    10. Operator selection and compiler lowering
    11. │
    12. ▼
    13. Device memory allocation and KV cache planning
    14. │
    15. ▼
    16. Inference service startup
    17. │
    18. ▼
    19. Text/image basic inference validation

    Each step can become a blocking point. For example, if the model weights use a special compressed format, the loading stage may require online dequantization or layout conversion. If the computation graph contains new operators, the corresponding implementation must be found in the operator library or generated automatically by the compiler. If the service startup stage lacks support for ultra-long contexts or dynamic shapes, the scheduler may fail to build an execution plan.

    In this practice, model loading and computation graph generation were handled by the inference engine, while key computation paths were covered by the operator library, compiler, and a domain-specific programming interface in the software stack. The overall strategy can be summarized as: reuse existing operators whenever possible, generate generic implementations through the compiler toolchain, and finally specialize hot paths with targeted optimizations.

    Adapting Sparse Attention: From Indexing to Cache Layout

    A key feature of this model is sparse attention—not all tokens attend to each other. During decoding, only a selected subset of historical tokens participates in attention computation, based on a model-defined strategy. This mechanism reduces memory and compute overhead under ultra-long contexts, but it introduces new challenges for the inference system.

    The first issue is index selection. During the prefill phase, the system must determine which positions enter attention computation according to the model’s sparse policy, and these positions must be maintained dynamically during the decode phase. Since different requests have different sequence lengths and content, index selection must support dynamic shapes and cannot rely on fixed-size static graphs.

    The second issue is KV cache layout. In conventional dense attention, KV caches are typically arranged contiguously by layer, batch, and sequence length. In sparse attention, however, only selected positions need to store KV, and different layers or heads may use different sparse patterns. A uniform layout wastes memory, while fully dynamic allocation adds memory management complexity. A common solution is a “logically contiguous, physically sparse” layout—exposing a unified index space externally while storing only the physically hit positions internally.

    The third issue is the difference between prefill and decode paths. Prefill processes long sequences and is suited to block-based attention computation. Decode adds a single token at a time and depends on efficient single-step index updates and localized computation. Both paths must be adapted and must remain numerically consistent with each other.

    In this practice, the software stack adapted index selection, KV cache layout, and the prefill/decode computation paths, and pushed the sparse attention implementation down into the operator library. Sparse attention also needed to coordinate with the state management module, cross-layer residual structures, and visual attention paths to form a complete executable graph. In other words, sparse attention is not an isolated operator—it is embedded in a complex, multimodal, cross-layer dependency structure.

    Online Handling of Low-Precision Expert Weights

    The mixture-of-experts architecture means that a portion of the model’s parameters consists of multiple expert networks, and during inference each token activates only a small subset of experts. When expert parameters are large, weights may be stored at lower precision than FP16 to reduce memory usage. The model discussed in this article uses a more compact integer-plus-scale-factor representation rather than a standard floating-point format.

    This imposes two requirements on the inference system. First, the loading stage must recognize this weight format and convert it into a numerically computable representation. Second, the computation stage must minimize peak memory usage, especially avoiding the creation of a full high-precision copy for all experts at startup.

    A common approach is an “online conversion + workspace reuse” strategy. Instead of converting all expert weights to high precision offline in advance, each activated expert’s weights are converted into a reusable workspace right before expert computation. After computation, the workspace is released or overwritten by the next expert. This significantly reduces resident memory at the cost of a small online conversion overhead.

    In this practice, low-precision expert weights were converted to a more general 8-bit floating-point format for computation. Workspace memory was allocated per layer and reused across experts, avoiding the need to generate full offline weights for all experts. This approach balanced implementation feasibility with memory pressure during the Day-0 stage. Subsequent optimizations—such as operator fusion, asynchronous conversion, and weight caching—can further reduce the cost.

    Operators and Compiler: Covering New Computation Paths

    New model architectures often introduce computation patterns not yet covered by the existing operator library. There are typically three ways to handle this:

    1. Write key operators by hand using a domain-specific programming interface for precise control and fast validation;
    2. Use the compiler to automatically generate operators, covering non-hot but numerous computation paths;
    3. Combine multiple basic operators to implement a path, first ensuring correctness, then progressively fusing for optimization.

    All three approaches were used in this adaptation. For the sparse attention path—which has clear structure and high performance sensitivity—implementation was handed directly to the operator library. For numerous element-wise operations, shape transformations, and intermediate computations, the compiler generated code automatically. For certain decomposable expert computations, basic operator combinations were used first to validate logic, and later decisions on fusion were based on measured performance.

    An important engineering principle worth emphasizing: during the Day-0 phase, do not rush into aggressive operator fusion and extreme optimization. The model structure and serving pipeline are not yet stable, and over-optimizing too early can make subsequent adjustments difficult. A more reliable pace is: first run the full pipeline with a basic implementation, then identify bottlenecks based on real workloads, and finally specialize hot operators.

    On the debugging front, printing intermediate results layer by layer, comparing them against the reference implementation, and narrowing down error ranges step by step are effective ways to quickly localize issues. This is especially important when the model simultaneously involves low-precision weights, sparse attention, and cross-layer state—a small deviation in any layer can be amplified by subsequent layers. Layer-by-layer verification is far more efficient than guessing at the end-to-end level.

    Service Validation and the Basic Inference Pipeline

    The final step of Day-0 is integrating the model into an accessible inference service. Service deployment is not simply wrapping a forward function. It involves practical issues such as request ingress, dynamic batching, ultra-long inputs, multimodal inputs, memory management, and task cancellation.

    For a million-token context model, a single request can consume a large portion of device memory. The service must therefore plan KV cache allocation at startup—for example, budgeting by maximum concurrency and maximum length, or using chunked caching with dynamic expansion and shrinking. At the Day-0 stage, at least the following validations should be completed:

    • Short text input can complete prefill and decode;
    • Image input can be encoded and flow into the rest of the network;
    • Multi-turn requests can reuse the cache and update state correctly;
    • Memory usage stays within expected bounds with no abnormal growth.

    After these basic validations pass, stress testing, long-input testing, multimodal mixed testing, and precision comparison testing can be introduced incrementally. At that point, the Day-0 goal is achieved, and the focus shifts to Day-N performance optimization and stability hardening.

    Summary and Takeaways

    The Day-0 adaptation of this trillion-parameter sparse model on a custom accelerator was not about inventing new algorithms. It was about quickly establishing an executable pipeline that covers the key paths of a new architecture. Specifically:

    • Identifying new model features and mapping them to software stack capabilities;
    • Adapting sparse attention indexing, caching, and execution paths;
    • Online conversion of low-precision expert weights with workspace reuse;
    • Combining the operator library, compiler, and custom implementations;
    • Performing basic inference validation in a service-oriented form.

    A broadly applicable conclusion can be drawn from this practice: as model architectures continue to evolve, inference systems must have an extensible software stack and a clear hierarchical adaptation path. Rather than aiming for complete support in one shot, it is more effective to establish a short-cycle process before model release—covering architecture assessment, path mapping, core operator completion, and service validation. Only by making Day-0 a standardized engineering process can subsequent performance tuning and production deployment have a stable starting point.