I wanted to open a thread here to add support to Python to support creating deferred evaluation systems:
I wrote about this a bit in a blog post a few months ago, under the " Python Control Flow" header. The basic issue as I understand is that Python has no nice ways of overriding control flow. So different literary’s have to take different work around’s here. I’ll try to start to outline them here:
- Functional Control Flow: One way to work around this is to simply ignore Python’s control flow operators and build all your operations as functions. This is the direction taken by NumPy, Pandas, and the original TensorFlow library. The pro is that it’s conceptually rather simple. The con is that it forces users to stick to a subset of Python without control flow.
- Parsing Bytecode: Numba on the other hand takes a different approach. It parses the bytecode of a function you decorate and has it’s own transpiler for a subset of Python.
Rewrite as functional form: The approach taking by the TF 2.0 autograph library, documented in the “The 800 Pound Python in the Machine Learning Room” paper and
snek-LMSrepo, is a bit of a hybrid here. I believe it relies on the AST instead of the bytecode parsing, but instead of building some intermediate IR from it directly, it rewrites the function to replace the control flow with a functional form.
- Rewrite bytecode on the fly: The codetransformer library lets you modify the bytecode before the interpreter executes it. It seems closest to modifying the CPython interpreter itself, without requiring that.