MLSYS ENGINEERING

10.2. Python and C++ runtime

All the code snippets in the previous section were written in Python. Developers prefer Python for its clean syntax, whereas C++ runs much faster and provides direct control over memory and hardware. To combine both strengths, frameworks pair a Python frontend with a C++ backend.

Python is an interpreted language: instead of being compiled ahead of time, a Python script (.py) is read and executed line by line by the Python interpreter at runtime, as shown in the following figure. This makes interactive development and debugging easy, but the interpreter is too slow for heavy tensor math and cannot directly control GPU hardware.

a = 1 + 2 b = a * 3 c = b - 1 Python Interpreter CPU
Figure 44. Python code execution at runtime.

In contrast, C++ (along with CUDA) is a compiled language. Instead of interpreting code line by line at runtime, a compiler translates the source files ahead of time into a binary shared library (.so), as shown in the following figure. This .so binary contains both compiled CPU machine code and compiled GPU kernels, which execute directly on the CPU and GPU at runtime without an interpreter.

Build time: Source files Compiler Binary (.so) Runtime: Binary (.so) CPU GPU
Figure 45. C++ and CUDA compilation and execution.

In a machine learning framework, the C++ and CUDA backend is pre-compiled into .so shared libraries before the framework is distributed. At runtime, as the Python interpreter steps through user code line by line, it calls into these .so binaries via language bindings instead of performing the tensor operations in Python, as shown in the following figure.

Runtime: y = x + b z = y @ w a = relu(z) Interpreter Binary (.so) CPU GPU
Figure 46. Python interpreter calling into pre-compiled shared libraries at runtime.