Interview
Python, OCaml, and Machine Learning with Laurent Mazare
Overview
- Jane Street utilizes a dual-language strategy: OCaml for production-critical trading systems and Python for data analysis, machine learning, and research.
- Laurent Mazarin, a speaker at the event, spends approximately 50% of his time on each language, noting they serve fundamentally different development modes.
- OCaml is characterized by strong static typing, enabling robust, long-lived systems where code written years ago often remains functional.
- Python is preferred for interactive, exploratory research due to its dynamic typing, which eliminates the "negotiation" step required to satisfy a static type system during rapid iteration.
Development Modes and Tooling
- Python Workflow: Developers use interactive notebooks (Jupyter) to create a quick feedback loop: edit code, evaluate, visualize results (plotting), and debug.
- This environment integrates tightly with a vast ecosystem of ML libraries (PyTorch, TensorFlow) and visualization tools (matplotlib).
- The workflow is stateful; notebooks can fall out of sync, as executing cells out of order or re-running specific cells does not guarantee a consistent computational graph state.
- Alternative tools like Netflix's "Polynode" or the Julia-based "Pluto" attempt to solve state consistency via explicit dependency tracking, but adoption remains low.
- OCaml Workflow: Development focuses on building resilient systems through rigorous engineering practices rather than visual iteration.
- Jane Street employs "expect tests," a technique where code and its expected output are unified; the build system flags changes in output, facilitating exploratory programming without a graphical component.
- The static type system enforces exhaustiveness in case analysis, automatically catching bugs related to missing scenarios before the code is run.
- The trade-off is higher initial setup time; developers must adapt their logic to fit the type system, which is unnecessary for simple, one-off research scripts.
Interoperability and Integration
- Jane Street uses an open-source library called PyML to facilitate bidirectional communication between OCaml and Python.
- OCaml code can be compiled into shared libraries and loaded into the Python runtime, allowing Python notebooks to access Jane Street's internal data services and compute functions.
- Conversely, OCaml can call Python functions to leverage specialized libraries like matplotlib for plotting.
- This architecture allows core computational logic to be written once in OCaml and exposed to Python, preventing code duplication and ensuring shared logic adheres to strict type safety.
- Garbage Collection Challenge: A known issue arises from the interaction between OCaml's mark-and-sweep garbage collector and Python's reference-counting collector.
- Cycles formed between OCaml objects and Python values can be invisible to both collectors, leading to memory leaks (e.g., tens of megabytes wasted).
- Similar challenges exist in Jane Street's "Ecamml" project, which interfaces OCaml with Emacs Lisp.
Machine Learning and Differentiable Programming
- Swift and Automatic Differentiation: At DeepMind, Mazarin explored Swift as a replacement for Python in machine learning to leverage symbolic differentiation (automatic differentiation).
- Modern ML relies on gradient descent; compilers that compute derivatives symbolically (rather than numerically) enable optimization of functions with millions of parameters.
- Swift was selected because its compiled nature, functional aspects (protocols), and lack of heavy global state make it more suitable for automatic differentiation than Python's dynamic runtime.
- Hardware Compilation: Swift projects aim to allow the compiler to automatically partition code for execution on CPUs, GPUs, or TPUs, handling data transfers and gradient computation across hardware boundaries.
- Resource Management:
- Python's reference counting and Swift's similar model allow for immediate release of large GPU memory buffers (tensors), a critical advantage over garbage-collected languages during long training runs.
- Jane Street is investigating algebraic effects in OCaml to implement fine-grained resource management (e.g., for file handles or GPU memory) without the strict ownership constraints of Rust.
Comparative Ecosystem Analysis
- OCaml Strengths:
- Superior for manipulating symbolic values, syntax extensions, and combinatorial data structures where exhaustive pattern matching prevents logical errors.
- Libraries generally have higher quality but lower quantity compared to Python; developers often bind to Python or other ecosystems (Rust) to access missing tools.
- Python Strengths:
- The de facto standard for ML due to a "snowball effect" of tutorials, libraries, and community support.
- Dynamic typing allows rapid experimentation on arbitrary data without rewriting code for type compatibility (e.g., switching between integers and floats).
- Python Weaknesses:
- Reliance on runtime exceptions rather than compile-time guarantees; bugs often manifest as subtle numerical errors (e.g., incorrect float values) rather than crashes.
- High-level libraries like Pandas and NumPy have obscure corner cases that can lead to silent failures, similar to Excel's dynamic type inference issues.
Future Directions and Decisions
- Jane Street is actively improving Python tooling and testing culture to match the rigor applied to OCaml, though practices currently lag behind.
- The organization plans to maintain Python for model development and research while using OCaml for the resilient infrastructure required to deploy and serve these models in trading systems.
- Future work includes integrating precise resource tracking into OCaml (likely via algebraic effects) to match the efficiency of Rust and Swift in managing scarce hardware resources.