Adapting Domestic AI Chips to Mainstream LLMs: A Practical Engineering Guide

    This article breaks down what it actually takes to adapt a domestic AI chip to a mainstream large language model, treating the process as a system-level engineering effort rather than a simple driver installation. It walks through the five layers of the adaptation stack—hardware and runtime, operators, graph compilation, framework interfaces, and accuracy evaluation—along with key engineering steps, sample code for capability probing and operator alignment, and a three-tier accuracy regression standard. Readers will also find a checklist of common pitfalls and reusable recommendations for building a measurable, repeatable, and continuously optimizable adaptation workflow.

    1. Background: Why Chip-to-Model Adaptation Is a Systems Engineering Problem

    When the industry reports that a domestic AI chip has “completed adaptation” to a mainstream large language model, many people read it as a simple driver installation. From an engineering perspective, a complete adaptation means that every class of operator in the model has a correct and efficient implementation on the target chip, that the computation graph can be correctly partitioned and scheduled by the compiler, that numerical accuracy stays within an acceptable range, and that throughput and latency are usable under real business workloads.

    Large language models are special because they combine two workload profiles at once: the prefill stage is compute-intensive large matrix multiplication, while the decode stage is memory-intensive serial execution of small operators. This requires the chip, compiler, operator library, and inference framework to work together. A missing piece in any of these shows up as either “it runs but is extremely slow” or “the results are wrong.”

    Note that this article does not benchmark any specific hardware model or commercial product. Instead, it abstracts a general methodology. Chips from different vendors differ greatly in architecture, memory hierarchy, and interconnect, but the skeleton of the adaptation workflow is shared.

    2. The Five Layers of the Adaptation Stack

    2.1 Hardware and Runtime Layer

    This includes compute units, on-chip cache, high-bandwidth memory, inter-device interconnect, and the runtime driver with memory management. The first metrics developers need to confirm are: available memory capacity, supported compute precision (for example, whether low-precision matrix operations are natively supported), and multi-card interconnect bandwidth. These determine whether the model fits on a single card and whether tensor parallelism is needed.

    2.2 Operator Layer

    Operators are the core of the adaptation workload. Scaled dot-product attention, rotary position embeddings, normalization, activation functions, and reads and writes to the paged key-value cache all need efficient implementations. A common strategy is to proceed in layers:

    • First, use the vendor’s general operator library to cover high-frequency operators;
    • For missing operators, compose existing operators to produce correct results and keep functionality working;
    • Then hand-write or tune kernels for hot operators and replace the composed implementations.

    2.3 Graph Compilation Layer

    The compiler handles operator fusion, memory reuse, and pipeline scheduling. The most common pitfall during adaptation is a “fusion rule mismatch,” which splits operators that could have been merged, producing many intermediate tensors and scheduling overhead. In this case, you need to export the computation graph and compare it layer by layer to locate the nodes where fusion failed.

    2.4 Framework and Interface Layer

    The inference framework hides backend differences behind a unified abstraction. The focus of adaptation work is mapping the framework’s operator calls to the target backend and handling dynamic shapes—during decode, the sequence length grows token by token, and fully dynamic shapes can trigger repeated recompilation.

    2.5 Accuracy and Evaluation Layer

    This is the layer most easily overlooked and the one that should never be skipped. See Section 4 for details.

    3. Key Engineering Steps with Sample Code

    Step 1: Environment and Capability Probing

    Before starting, build a “capability inventory” to avoid designing on wrong assumptions.

    1. # Sample: probe basic device capabilities (pseudocode; refer to the actual platform documentation for interfaces)
    2. info = runtime.query_device(0)
    3. print(info.memory_total, info.memory_free)
    4. print(info.supported_dtypes) # whether low-precision matrix multiplication is supported
    5. print(info.topology) # single-card or multi-card interconnect topology

    Step 2: Single-Operator Alignment Verification

    Do not run the full model right away. Verify operators one by one from bottom to top, comparing against a reference implementation with small-scale inputs.

    1. # Sample: operator-level numerical alignment
    2. import numpy as np
    3. def check_op(dev_op, ref_op, shape, atol=1e-3, rtol=1e-3):
    4. x = np.random.randn(*shape).astype(np.float32)
    5. y_dev = dev_op(x).cpu().numpy()
    6. y_ref = ref_op(x)
    7. ok = np.allclose(y_dev, y_ref, atol=atol, rtol=rtol)
    8. diff = np.abs(y_dev - y_ref).max()
    9. print(f"shape={shape} ok={ok} max_diff={diff}")
    10. return ok

    When aligning, record both the maximum absolute error and the relative error. In low-precision scenarios, errors accumulate, and passing a single point does not mean the whole network passes.

    Step 3: Single-Layer and Subgraph Forward Comparison

    Split the model into several subgraphs (embedding layer, a single decoder layer, output head) and compare hidden states segment by segment. This quickly distinguishes “the operator is wrong” from “the scheduling is wrong.”

    Step 4: Shape Strategy and Cache Management

    The paged key-value cache is key to long context. In engineering, you need to plan the cache block size and allocation strategy in advance, and use different execution paths for prefill and decode.

    1. # Sample: splitting prefill and decode
    2. if seq_len > 1:
    3. logits = model.prefill(input_ids, kv_cache) # compute-intensive
    4. else:
    5. logits = model.decode(input_ids, kv_cache) # memory-intensive

    Step 5: Performance Profiling and Bottleneck Identification

    Use the platform’s built-in performance profiling, monitoring, and alerting capabilities to collect operator time distribution, memory bandwidth utilization, and scheduling gaps. There are three common conclusions: memory-bound, operators not fused, and unreasonable parallel partitioning. The corresponding optimizations are improving cache reuse, adjusting fusion rules, and resetting tensor parallelism.

    4. Accuracy Regression: The Criteria for Calling an Adaptation “Complete”

    It is recommended to establish three tiers of acceptance:

    1. Operator tier: per-operator error within threshold;
    2. Model tier: under fixed inputs, logits are highly consistent with the reference implementation, and greedy decoding results are identical;
    3. Task tier: on several representative tasks, output quality shows no significant difference from the reference baseline.

    Only when all three tiers pass can it be stated externally that the adaptation is “complete.” An adaptation lacking task-tier evaluation will have its risks exposed all at once after going live in business.

    5. Common Pitfalls Checklist

    • Verifying only short sequences, then hitting cache overflow or accuracy collapse with long context;
    • Dynamic shapes causing repeated compilation and unacceptable first-packet latency;
    • Ignoring randomness in the sampling step and misjudging it as an accuracy problem;
    • In multi-card scenarios, communication and computation not overlapping, leading to poor scaling efficiency;
    • Lacking automated regression, so every operator update relies on manual verification.

    6. Reusable Recommendations for Implementation

    Treat adaptation as continuous engineering rather than a one-time deliverable: build an operator coverage matrix, an automated accuracy regression pipeline, and a performance baseline dashboard. On the cloud, you can use a container platform to freeze the environment, object storage to store models and baseline data, and logging and monitoring with alerting to track long-term stable operation. Baidu AI Cloud provides standardized infrastructure support for these general capabilities, but the specific implementation should still follow each platform’s official documentation.

    In summary, the value of adapting a chip to a large model lies not in getting a demo to run once, but in forming an engineering loop that is measurable, repeatable, and continuously optimizable. With this methodology, when facing adaptation tasks for new models and new chips, teams can converge uncertainty into a clear checklist.