← Back to Blog
Architecture

Mix-Mode Execution: Unified Kernels from Python and C++

Python for prototyping, C++ for performance — but what if you didn't have to choose? Mix-mode execution compiles Python and C++ into a single optimized kernel.

Dmitri Goloubentsev
Dmitri Goloubentsev
· 2 min read
Python C++ mix-mode kernel JIT NumPy performance

The language divide in quantitative finance is real. Quants prototype in Python. Production runs in C++. The translation between the two is expensive, error-prone, and never-ending.

The problem

A typical workflow: a quant writes a new model in Python with NumPy. It works. Risk engineering rewrites it in C++ for production. The C++ version has a bug that takes two weeks to find. Meanwhile, the quant has updated the Python version three times. The C++ version is always behind.

Mix-mode execution

What if the Python code and the C++ code could coexist on the same computation tape?

Mix-mode execution records operations from both languages into a single computational graph. The graph is then JIT-compiled to native machine code, producing the same compiled kernel regardless of which language originated the computation.

This means:

  • Python code runs at C++ speed. Not “close to C++ speed,” but identical, because both compile to the same machine code.
  • C++ libraries callable from Python. A quant can call into an existing C++ pricing library from Python, and the entire computation (Python + C++) lands on one tape.
  • NumPy ufuncs supported. Standard NumPy operations are captured on the tape and compiled.
  • One kernel, full adjoint Greeks. The backward pass differentiates through both the Python and C++ portions seamlessly.

How it works

  1. Record: Execute the Python + C++ computation once. Operations from both languages are captured on the tape.
  2. Compile: The tape is JIT-compiled to a native AVX-vectorized kernel.
  3. Replay: The kernel executes millions of times with different inputs. No Python interpreter, no C++ framework. Just compiled machine code.

Use cases

  • Gradual migration: Start with Python, move hot paths to C++ one function at a time. The kernel handles both.
  • Quant-developer collaboration: Quants write model logic in Python. Developers write infrastructure in C++. Both contribute to the same kernel.
  • Legacy integration: Wrap an existing C++ library, call it from Python, get adjoint Greeks through the entire stack.

Implemented using AADC, a commercial adjoint AD compiler (matlogica.com).

Want to see these results on your own portfolio?

Get in Touch

Interested in these opportunities?

Let's arrange a free demo for you and your team.

Book a Demo