Abstract
Python has become the de-facto language for training deep neural networks, coupling a large suite of scientific computing libraries with efficient libraries for tensor computation such as PyTorch or TensorFlow. However, when models are used for inference they are typically extracted from Python as TensorFlow graphs or TorchScript programs in order to meet performance and packaging constraints. The extraction process can be time consuming, impeding fast prototyping. We show how it is possible to meet these performance and packaging constraints while performing inference in Python. In particular, we present a way of using multiple Python interpreters within a single process to achieve scalable inference and describe a new container format for models that contains both native Python code and data. This approach simplifies the model deployment story by eliminating the model extraction step, and makes it easier to integrate existing performance-enhancing Python libraries. We evaluate our design on a suite of popular PyTorch models on Github, showing how they can be packaged in our inference format, and comparing their performance to TorchScript. For larger models, our packaged Python models perform the same as TorchScript, and for smaller models where there is some Python overhead, our multi-interpreter approach ensures inference is still scalable.
Introduction
In the deep learning ecosystem, a "two-world" problem persists. Researchers favor Python for its flexibility and rich scientific library ecosystem during the training phase. However, production environments often demand high-performance, static formats like TorchScript or ONNX to run within C++ frameworks. This extraction process is a significant bottleneck—it is frequently time-consuming, brittle, and necessitates manual code refactoring to satisfy static typing constraints.
Furthermore, Python’s Global Interpreter Lock (GIL) has traditionally hindered its use in high-throughput inference services, as it prevents a single process from effectively utilizing multiple CPU cores. This paper introduces a system that allows for production-grade inference directly in Python, bypassing the GIL and matching the performance of compiled formats for most models.
Scalable Inference with torch::deploy
The core of the proposed solution is torch::deploy, a C++ API designed to manage multiple independent CPython interpreters within a single process. By utilizing dlopen with private namespaces, each interpreter is granted its own GIL. This architecture enables a C++ application to load-balance inference requests across multiple Python "workers" residing in the same memory space.
This multi-interpreter coordination ensures that while the underlying libtorch library is shared, the Python state remains private. Heavy data, such as large tensor weights, is shared in C++ memory rather than being duplicated across interpreters, maintaining a low memory footprint while achieving high parallelism.
Hermetic Packaging with torch.package
To address deployment consistency, the authors introduced torch.package, a hermetic container format. Unlike standard Python deployments that depend on the local environment's installed libraries, torch.package bundles the model’s specific source code and weights into a single zip-based artifact.
As shown in Figure 2, the system allows developers to explicitly configure which modules to export, which to mock (exclude), and which to treat as external dependencies. This ensures that the code running in production is an exact replica of the code used during training, eliminating "it works on my machine" issues.
Evaluation and Results
The system was evaluated using 15 popular PyTorch models from GitHub, spanning computer vision (e.g., Yolo v3, Mask R-CNN), NLP (BERT), and speech (Tacotron 2). Performance was compared against standard single-interpreter Python and TorchScript.
Key findings include:
- Performance Parity: For large models where tensor computation dominates, Python-based inference performed identically to TorchScript. The "Python tax" or overhead was negligible.
- Linear Scaling: On medium-sized models like MobileNet v3, the multi-interpreter approach scaled linearly with CPU cores, whereas standard Python hit a performance ceiling immediately due to the GIL.
- Small Model Overhead: For very small models (e.g., DLRM), TorchScript remained approximately 2x faster due to Python overhead. However, the multi-interpreter approach still provided the necessary scalability across cores that standard Python lacks.
Conclusion
By recognizing that deep learning is "code-light but data-heavy," this approach demonstrates that the flexibility of Python can be maintained in production without sacrificing performance. While challenges remain—such as handling complex C-extensions and managing the memory overhead of multiple interpreters (approx. 34MB per instance)—this method significantly reduces the "extraction tax" and simplifies the path from research to deployment.
Summary updated