Why LLM Inference Engines Split Between C++ and Python

    This article explains why large language model inference engines are commonly built with a mix of C++ and Python. It breaks inference cost into three layers—numerical operators, request scheduling and batching, and service-level protocol handling—and maps three engineering paths to them: a zero-dependency, all-platform path, a high-throughput serving path, and a structured acceleration path. For each path, it covers the constraints, the trade-offs, and where each language fits, then closes with a decision checklist based on deployment shape, concurrency, memory budget, hardware openness, iteration frequency, and team skills. The core conclusion: constraints decide the path, and the path decides the language ratio.

    1. The Core Issue: Language Choice Is a Map of Where Performance Comes From

    Choosing a language for an inference engine looks like a syntax preference, but it is really an answer to a more fundamental question: where will performance be squeezed out? The time cost of LLM inference can be split into three parts: numerical computation in low-level matrix multiplication and attention operators, request scheduling and batch orchestration in the upper layer, and out-of-process service handling and protocol processing. The relative weight of these three parts depends on the hardware, the concurrency scale, and the model shape. The language is simply the engineering means that fixes how each of these costs is paid.

    A common mistake is to equate “compiled language” with “faster.” When single-card compute is limited and operators cannot be heavily parallelized, cutting runtime dependencies and memory movement pays off far more than instruction-level optimization. When a cluster has ample compute and the bottleneck moves to request queuing and batch assembly, scheduling strategy outweighs language-level differences. So the first principle of language selection is: locate the bottleneck first, then decide which layer goes to a compiled language and which goes to a scripting language.

    2. Three Typical Engineering Paths, Abstracted

    Setting specific implementations aside, the industry can be grouped into three paths.

    The first is the zero-dependency, all-platform path. The core goal is to run models on general-purpose devices that have no dedicated accelerator, so the implementation language must compile directly to native code on each platform, and the runtime must not pull in a large deep learning framework or driver stack. Such implementations usually ship a lightweight tensor library of their own, keeping operators, memory allocation, and quantization/dequantization inside their own code, with the scripting language handling only format conversion and testing.

    The second is the high-throughput serving path. The core goal is to max out throughput and concurrency across a multi-card cluster, which requires a full set of scheduling mechanisms: continuous batching, paged key-value cache, tensor parallelism and pipeline parallelism, and prefix reuse. These mechanisms evolve very fast, and implementing them in a scripting language keeps the cost of changing scheduling policy as low as possible, while the compiled language is kept only for the lowest-level attention and normalization operators.

    The third is the structured acceleration path. It sits between the two: the main logic stays in a scripting language, but the parts of the decode phase with highly predictable shapes are pushed down into compiled kernels, while reusing external high-performance operator libraries. What it buys is a controlled increase in complexity.

    3. Constraints and Costs of the Zero-Dependency Path

    The motivation for this path is very concrete: environment setup is itself the biggest source of user churn. An image containing a deep learning framework, acceleration libraries, and driver tooling can easily run to several gigabytes, and on some consumer devices the general-purpose acceleration backend does not cover all operators, while dedicated acceleration ecosystems are simply not open. In that situation the only workable option is to prune the dependency tree to the shortest possible form.

    The costs are equally clear. Without a mature framework’s automatic differentiation and graph optimization, every graph structure, memory reuse scheme, and quantization scheme has to be built by hand; every new hardware backend needs separate adaptation, and the code volume grows linearly with the number of platforms. A minimal inference loop looks roughly like this:

    1. // Pseudocode: typical structure of the zero-dependency path
    2. graph_t g = build_graph(model, tokens); // build the graph
    3. ctx_t c = alloc_ctx(&g, mem_limit); // preallocate and reuse memory
    4. for (int layer = 0; layer < n_layer; ++layer) {
    5. matmul(&g, layer); // in-house operator
    6. attention(&g, layer, kv_cache); // hand-written attention
    7. }
    8. int next = sample(&g, temperature, top_p); // sampling

    Its applicability is clear: single-machine or edge deployment, very low concurrency, medium model size, and sensitivity to memory footprint. Once it enters high-concurrency online serving, the lack of a mature scheduler becomes a hard limitation.

    4. Constraints and Costs of the High-Throughput Serving Path

    The key metric for serving is not single-request latency but the number of tokens completed per unit of time and the utilization of GPU memory. That requires three kinds of mechanisms: continuous batching, which packs requests of different lengths into the same batch; memory management that slices the key-value cache into fixed-size blocks allocated on demand; and caching and reuse of computation results for repeated prefixes. These mechanisms are essentially policy problems, and their parameters and trigger conditions need frequent tuning.

    Carrying the scheduling layer in a scripting language has clear benefits: the policy experimentation cycle drops from hours to minutes, and it integrates easily with general cloud capabilities such as container platforms, monitoring and alerting, and logging services. The cost is higher interpreter overhead and memory usage, which has to be recovered by compiled operators at the bottom. When concurrency is not large, the scheduling gains cannot amortize that overhead, and the approach feels too heavy.

    5. Trade-offs of the Structured Acceleration Path

    This path recognizes a fact: in the prefill phase, shapes vary with the input and are hard to fix; in the decode phase, each step processes only one token, so shapes are regular and highly repetitive, which makes them the best fit for dedicated kernels. Pushing that part down yields stable gains without giving up the flexibility of the scripting layer. The risk is that the kernels become more tightly coupled with the scheduling logic, maintenance costs rise when the model structure changes, and the approach becomes more sensitive to the versions of external operator libraries.

    6. A Selection Checklist

    When facing new hardware or a new model structure, work through the following in order:

    • Deployment shape: single machine, edge, or multi-card cluster;
    • Concurrency scale: do requests arrive in batches, and is queuing required;
    • Memory budget: is paged caching and prefix reuse needed;
    • Hardware openness: does the general-purpose acceleration backend cover operators completely;
    • Iteration frequency: does scheduling policy need day-level adjustment;
    • Team composition: is there capacity to maintain hand-written operators and kernels.

    The conclusion is not complicated: constraints decide the path, and the path decides the language ratio. If zero dependency and all-platform coverage are the goal, make the compiled language the main body; if cluster throughput and policy iteration speed are the goal, make the scripting language the main body and push operators down; in between, selectively push down the most regular segment of computation. Baidu AI Cloud follows the same logic in practice: locate the bottleneck first and implement in layers, rather than taking a prior stance at the language level.