
“用Python训练,用C++服务”,就谷歌的大量机器学习工作而言,这句话几乎可以当作它的座右铭。Python便于数据科学家使用,但把它的解释器留在服务链路中会增加开销,并带来部署上的麻烦。由业界支持的OpenXLA项目提供工具,可把机器学习计算编译成面向不同处理器的原生代码。这套方案中最新开源的部分是XSE,即XLA Serving Engine(XLA服务引擎)的缩写。
"Train in Python, serve in C++" might as well be Google's motto when it comes to much of its ML work. Python is easy for data scientists to wield, but keeping its interpreter in the serving pipeline can add overhead and deployment headaches. The industry-backed OpenXLA Project provides tools for compiling machine learning computations into native code for different processors. The latest component of this setup to be open sourced is XSE, short for the XLA Serving Engine.
一次性推理XSE并不太适合大规模LLM推理(那更适合用vLLM或SGLang),而是更适合规模更小、任务指向更明确的模型,而谷歌有大量这类模型。谷歌产品管理总监内森·比奇周二在领英上发文写道:“眼下LLM服务最受关注,但相当一部分生产推理仍然是排序、推荐、嵌入和视觉模型。这些请求往往很短,每秒查询数非常高,延迟预算也很紧。”
One-shot inference XSE is not so much suited for large-scale LLM inference (which would be better served by vLLM or SGLang), but rather for smaller, more sharply task-oriented models, of which there are plenty at Google. "LLM serving gets most of the attention right now, but a significant amount of production inference is still ranking, recommendation, embedding, and vision models.
在谷歌,XSE已经在YouTube Shorts、谷歌广告和搜索等服务中每秒处理超过600万次查询。这些模型不是用来悠闲聊天的。它们对结果排序、推荐视频,并即时做出其他决策,用户往往察觉不到。让Python解释器逐一处理每个请求会成为瓶颈,甚至可能让谷歌自身难以为继。在谷歌针对短请求的测试中,XSE的吞吐量达到其最佳优化Python服务器的两到五倍,而内存占用仅为后者的四分之一。
These tend to be short requests with very high [queries-per-second] and tight latency budgets," Nathan Beach, Google director of product management, wrote in a LinkedIn missive Tuesday. At Google, XSE already handles more than six million queries per second across services including YouTube Shorts, Google Ads, and Search. These models aren't there for a leisurely chat. They rank results, recommend videos, and make other decisions on the fly, often without users noticing. A Python interpreter mulling each request would be a bottleneck, if not the end of Google itself.
这里没有Python在这套方案中,机器学习专家用Python编写模型代码,并借助谷歌的JAX完成计算。谷歌还计划与TorchTPU集成,让PyTorch用户能够导出模型,以便在TPU上进行C++服务。
In Google's tests on short requests, XSE delivered two to five times the throughput of its best optimized Python server, using a quarter of the memory. No Python here In this setup, the ML boffins write their model code in Python, using Google's JAX to help with the sums.
一个导出工具将模型的计算打包成StableHLO,这是一种可移植的中间表示,同时将训练好的权重打包进“XLA SavedModel”。C++服务运行时已准备好进行预认知,它使用XLA将这些计算编译成面向目标芯片的原生代码。这里看不到Python解释器。XLA SavedModel可以在GPU和CPU上运行——谷歌与Arm和英特尔都建立了合作,共同开发XLA生态系统。
Google also plans integration with TorchTPU so PyTorch users can export models for C++ serving on TPUs. An export tool packages the model's computations in StableHLO, a portable intermediate representation, alongside its trained weights in an "XLA SavedModel." Ready to precognate, the C++ serving runtime uses XLA to compile those computations into native code for the target silicon. No Python interpreter to see here.
但谷歌显然认为,如果你在自己的TPU加速器产品线上运行模型,那会酷得多。Python的迟缓仍然是机器学习社区的一个问题。除了XSE之外,人们还采取了其他方法,其中许多都涉及尽快抛弃Python代码。PyTorch提供了一个名为LibTorch的原生C++运行时。此外,微软及其合作者提供了ONNX Runtime,它支持一种带有Python绑定的开放图格式。甚至Python自己的参考实现也严重依赖C,而Rust如今也开始跻身其中。®
XLA SavedModels can run on GPUs and CPUs – Google partnered with both Arm and Intel to develop the XLA ecosystem. But Google obviously thinks it'd be a lot cooler if you ran your models on its own line of TPU accelerators. Python's sluggishness continues to be an issue for the ML community. Beyond XSE, other approaches have been taken, many of which involve jettisoning the Python code as quickly as possible. PyTorch offers a native C++ runtime called LibTorch. Also, Microsoft and its collaborators offer the ONNX Runtime, which supports an open graph format with Python bindings. Even Python's own reference implementation relies heavily on C, with Rust now getting a foot in the door. ®