Replace C-code generation and compilation backend #376
Replies: 10 comments 5 replies
-
Totally agree. I suppose we first need some functionality to enable usage of Cython. |
Beta Was this translation helpful? Give feedback.
-
It's not actually that difficult to use Cython-generated code in Aesara right now. For instance, an My impression is that this approach isn't the best because it doesn't use Aesara's C-based thunk machinery. This machinery is assumedly faster than the corresponding pure Python machinery, perhaps due to reduced Python-to-C and C-to-Python overhead—among other things. Aesara graph evaluation primer
For anyone who's not familiar with the idea of a "thunk" in Aesara, this paragraph might help. Simply put, a "thunk" is an argumentless function that calls an Here's a simple example: inputs = [1, 2]
outputs = [None]
class SomeOp(Op):
def perform(self, inputs, outputs):
outputs[0] = inputs[0] + inputs[1]
def a_thunk(inputs=inputs, outputs=outputs):
SomeOp().perform(inputs, outputs)
a_thunk()
# `outputs` should contain `3` Those storage arrays make up the graph's memory model, and they're stored inside a thunk function's closure. When the thunk is evaluated those output arrays are populated with the computed values. A thunk is created for each node/ Continuing from the previous example: other_outputs = [None]
class SomeOtherOp(Op):
def perform(self, inputs, outputs):
outputs[0] = inputs[0]**2
def another_thunk(inputs=outputs, outputs=other_outputs):
SomeOtherOp().perform(inputs, outputs)
# This thunk depends on the output of the previous thunk
another_thunk() This allows Here's what # Using the example thunks above, we can create a function
# that computes the graph for `(a + b)**2`
def compiled_graph_fn(a, b):
inputs[0] = a
inputs[1] = b
for thunk in [a_thunk, another_thunk]:
thunk()
return other_outputs[0] The How compiled C code is usedThere are a few places where the C and Python thunks are clearly distinguished. In the From the Python side (e.g. when graph evaluation is performed using the pure Python From what I can tell, Regarding those thunk pointers, they seem to come from |
Beta Was this translation helpful? Give feedback.
-
For anyone who wants to try this (e.g. @aseyboldt for #327), take a look at how |
Beta Was this translation helpful? Give feedback.
-
https://github.com/pymc-devs/aesara/blob/erfcx_c/aesara/graph/op.py#L552 doesn't work. |
Beta Was this translation helpful? Give feedback.
-
Actually, it looks like it might be as simple as creating an The The first two values (i.e. the |
Beta Was this translation helpful? Give feedback.
-
Just to update this thread, we've been making considerable progress using Numba as a complete replacement for the existing C/Python backend machinery. See the recent Numba-related PRs here. |
Beta Was this translation helpful? Give feedback.
-
@brandonwillard does it mean that very soon we'll just delete all the C related code? If that is the case, I can focus on numba related stuff instead of figuring out mysterious thread safety failures |
Beta Was this translation helpful? Give feedback.
-
Is numba backend as fast as C backend? |
Beta Was this translation helpful? Give feedback.
-
If we are not going to delete C code, I can try to refactor the C code into Cython |
Beta Was this translation helpful? Give feedback.
-
Could we use the same code-generating approach that we're using with the Numba and just make Cython another backend? |
Beta Was this translation helpful? Give feedback.
-
The text-based C-code generation and compilation backend in Aesara is difficult to use, debug, maintain, and extend—relative to mature alternatives like Cython and Numba. We need to fix that ASAP.
Cython is a well established Python-to-C transpiler that provides a much cleaner, automatic means of generating the same kind of Python C API code that's written by hand in Aesara. Here are some possible benefits to replacing our current C implementations with Cython-generated C code:
Op
implementations, resulting in more C-only code (i.e. fewer calls to-and-from Python/C during graph evaluation)Subtensor*
/indexing operations with a little bit of Cython (instead of our very limited C implementations for only certain types of indexing)This general idea has been brought up in numerous different locations, so I'm creating this issue as a means of collecting all the relevant details, ideas, discussions, requirements, etc., into one place.
Related issues:
Function
evaluation code #306Beta Was this translation helpful? Give feedback.
All reactions