Architecting Operator Libraries for Heterogeneous AI Chips: Design Principles and Migration Practices
Large language model inference frameworks are typically built around general-purpose GPU ecosystems, relying heavily on built-in operator interfaces. Migrating these workloads to heterogeneous or domestically developed accelerators requires a well-designed operator library that fills coverage gaps, adapts compute paths, and optimizes performance. This article examines a layered operator library architecture, a multi-language kernel development ecosystem, and practical adaptation strategies for new model structures such as sparse attention, along with a reusable migration methodology for building high-performance inference stacks on new hardware.
Architecting Operator Libraries for Heterogeneous AI Chips: Design Principles and Migration Practices
Why Operator Libraries Are Central to Heterogeneous Migration
LLM inference frameworks usually run on general-purpose GPU ecosystems, with model implementations depending heavily on framework-provided operator interfaces. When the compute platform switches to domestic or other heterogeneous accelerator chips, three typical problems arise:
- Incomplete operator coverage: New-architecture models (such as sparse attention, linear attention, and Mixture-of-Experts) introduce computational primitives that do not exist in traditional operator libraries. Running them directly triggers frequent fallbacks to CPU or inefficient implementations.
- Divergent compute paths: The Prefill and Decode stages have fundamentally different computational characteristics. Prefill is compute-intensive batched matrix multiplication, while Decode is memory-bound, small-batch incremental generation—each requires tailored optimization.
- Compiler toolchain maturity: Underlying compilers need time to support new hardware features, and some critical operators must rely on specialized compilers or hand-written kernels to reach usable performance.
An operator library targeting heterogeneous hardware therefore carries three responsibilities—operator gap-filling, path adaptation, and performance optimization—and represents the first critical gate for a successful inference stack migration.
Layered Architecture of an Operator Library
Mature operator libraries typically adopt a multi-layer structure to balance coverage, performance, and maintainability:
1. Foundational Operator Layer
This layer covers high-frequency basic operators such as elementwise operations, reductions, matrix multiplication, Softmax, LayerNorm/RMSNorm, and RoPE rotary position embeddings. The goal here is completeness—any gap prevents the model from running end to end. Implementations typically combine hand-written kernels with compiler-generated code: regularly shaped operators are left to the compiler’s autogeneration, while irregularly shaped or performance-critical operators receive hand-written, highly tuned kernels.
2. Fused Operator Layer
Inference is extremely sensitive to memory access, and operator fusion is the core technique for reducing memory bandwidth pressure. Typical fusions include:
- Norm + linear layers: Merging normalization with input preparation for the subsequent matrix multiplication, eliminating one global memory read/write round trip.
- Attention pre/post-processing fusion: Fusing position encoding, attention score computation, Softmax, and the PV multiplication into a single kernel.
- Sampling and logits post-processing fusion: Completing temperature scaling, Top-K/Top-P filtering, and sampling in one step.
3. Domain-Specific Operator Layer
This layer provides specialized operators for LLM-specific structures, such as PagedAttention KV cache reads and writes, index-selection operators for sparse attention, and expert routing/aggregation operators for MoE. It directly determines whether new-architecture models can run efficiently and represents the largest share of migration work.
Coordinating a Multi-Language Operator Ecosystem
A single operator implementation approach cannot cover all scenarios. In practice, teams build a multi-level operator development ecosystem:
Hand-written kernels: Used for performance-critical core operators. They manipulate hardware instructions or low-level programming models directly to extract maximum performance, but at high development cost.
Triton-like tile-level programming languages: These take blocks (tiles) as the unit of programming and hide thread scheduling details. Developers only describe computation logic and tiling strategies while the compiler handles memory movement and parallel mapping. They are ideal for rapidly implementing and iterating on attention-variant operators, making them the tool of choice when migrating new models.
# Illustrative pseudocode for tile-level programming: sparse attention score computation@tile_kerneldef sparse_attn_score(q, k, block_indices):acc = zeros([BLOCK_M, BLOCK_D], dtype=fp32)for idx in range(block_indices.count):kv_block = load_kv_cache(block_indices[idx])score = dot(q, transpose(kv_block)) * scaleacc += online_softmax_update(acc, score)return acc
Domain-specific compilers: For particular computation patterns (such as tensor program compilation), they provide auto-tuning by searching tiling strategies, pipeline schedules, and other parameters—generating code approaching hand-written performance when hand-written kernels are unavailable.
These three approaches complement each other in engineering practice: first validate correctness quickly with high-level languages, then progressively push hotspot operators down to lower-level optimizations.
Migration and Adaptation Practices for New Model Architectures
Using recently popular new model structures as examples, here is how key compute paths are adapted.
1. Adapting Sparse Attention (DSA)
Sparse attention dynamically selects a small number of relevant KV blocks for each query token, reducing attention computation complexity from linear to sub-linear in sequence length. Its adaptation involves three parts:
- Index-selection operator: Quickly selects the Top-K relevant blocks based on similarity between query vectors and KV cache block centers. This operator involves large-scale Top-K sorting and typically requires a dedicated blocked Top-K kernel.
- KV cache layout adjustments: Sparse attention requires the KV cache to be organized in blocks with random block access, so the Paged KV cache’s page table structure and metadata layout must be redesigned to serve both dense and sparse paths.
- Dual-path Prefill and Decode implementation: Prefill handles long sequences and requires blocked computation with mixed dense/sparse access within blocks. Decode processes a single query at a time, with the bottleneck in random KV block read latency, mitigated through memory access coalescing and software pipelining.
2. Coordinated Adaptation of Other New Structures
The complete execution chain also needs to cover:
- Linear attention / state-space structures: These involve recursive state updates (such as KDA-style gated state management). The Decode path must maintain fixed-size state tensors, and operators must support in-place updates to avoid frequent memory allocation.
- Cross-layer residual connections: Breaking the layer-by-layer serial residual paradigm requires the execution engine to support cross-layer tensor references and lifecycle management, preventing premature release of activations that are still referenced.
- Vision encoder attention: The vision branches of multimodal models often use different attention implementations, requiring multiple attention kernels maintained in a unified operator library, with correct handling of image-text token concatenation during Prefill.
A General Methodology for Migration Engineering
Drawing on the practices above, a reusable migration workflow emerges:
- Baseline analysis: Run the model’s forward pass, profile operator call frequency and coverage, and identify missing operators and expected hotspots.
- Correctness first: Implement functional versions of all missing operators in high-level languages, ensure end-to-end execution, and verify numerical alignment (layer-by-layer comparison of intermediate results with reasonable tolerances is recommended).
- Hotspot optimization: Use profiling to locate top operators and iterate in the order of “fusion > blocked optimization > hand-written lowering,” prioritizing memory-bound bottlenecks in the Decode path.
- End-to-end validation: Run regression tests under the full inference service across long-sequence, concurrency, and multimodal scenarios, verifying the coordinated correctness of KV cache management, the scheduler, and operators.
- Continuous evolution: Graduate new operators into the operator library’s standard interface layer so subsequent model iterations can reuse them, lowering marginal migration costs.
Conclusion
Migrating LLM inference to heterogeneous compute platforms is, at its core, a project of rebuilding the operator ecosystem. A well-designed operator library should organize foundational, fused, and domain-specific operators in a layered architecture, coordinate three implementation paths—hand-written kernels, tile-level programming languages, and domain-specific compilers—and perform deep, compute-path-level adaptation for new model structures such as sparse attention and state management. With this methodology, developers can systematically build high-performance inference stacks when facing new hardware and new model architectures, rather than falling into fragmented, patch-style development.
