-
-
Notifications
You must be signed in to change notification settings - Fork 153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move nested static numba-jit functions #1438
Draft
Smit-create
wants to merge
9
commits into
aesara-devs:main
Choose a base branch
from
Smit-create:i-1419-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2e10a82
Prototype of cache numba-jit functions
Smit-create d99dda8
Fix mypy issues
Smit-create 1c29a6a
Install dill
Smit-create febc082
try other way
Smit-create d8b19d1
simplify and just use python dict
Smit-create 2038a5f
Use an option to disable numba cache
Smit-create 2b6bfc7
Fixes assertion error
Smit-create 34739a9
Fix a test
Smit-create 58ccd1b
Apply suggestions
Smit-create File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import hashlib | ||
import operator | ||
import pickle | ||
import warnings | ||
from contextlib import contextmanager | ||
from functools import singledispatch | ||
from textwrap import dedent | ||
from typing import TYPE_CHECKING, Callable, Optional, Union, cast | ||
from typing import TYPE_CHECKING, Callable, Dict, Optional, Union, cast | ||
|
||
import numba | ||
import numba.np.unsafe.ndarray as numba_ndarray | ||
|
@@ -350,7 +352,63 @@ def numba_const_convert(data, dtype=None, **kwargs): | |
|
||
def numba_funcify(obj, node=None, storage_map=None, **kwargs) -> Callable: | ||
"""Convert `obj` to a Numba-JITable object.""" | ||
return _numba_funcify(obj, node=node, storage_map=storage_map, **kwargs) | ||
numba_py_fn = None | ||
if config.DISABLE_NUMBA_PYTHON_IR_CACHING: | ||
numba_py_fn = _numba_funcify(obj, node=node, storage_map=storage_map, **kwargs) | ||
else: | ||
node_key = make_node_key(node) | ||
|
||
if node_key: | ||
numba_py_fn = check_cache(node_key) | ||
if node_key is None or numba_py_fn is None: | ||
# We could only ever return the function source in our dispatch | ||
# implementations. That way, we can compile directly to the on-disk | ||
# modules only once. | ||
numba_py_fn = _numba_funcify( | ||
obj, node=node, storage_map=storage_map, **kwargs | ||
) | ||
|
||
# This will determine on-disk module name to be generated for | ||
# `numba_py_src` and return the corresponding Python function | ||
# object using steps similar to | ||
# `aesara.link.utils.compile_function_src`. | ||
if node_key: | ||
numba_py_fn = add_to_cache(node_key, numba_py_fn) | ||
|
||
# TODO: Presently numba_py_fn is already jitted. | ||
# numba_fn = numba_njit(numba_py_fn) | ||
return cast(Callable, numba_py_fn) | ||
|
||
|
||
numba_db: Dict[str, Callable] = {} | ||
|
||
|
||
def make_node_key(node): | ||
"""Create a cache key for `node`. | ||
TODO: Currently this works only with Apply Node | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
""" | ||
if not isinstance(node, Apply): | ||
return None | ||
# TODO: Add a stronger hashing mechanism | ||
key = str(node) | ||
# key = (node.op,) | ||
# key = tuple(inp.type for inp in node.inputs) | ||
# key += tuple(inp.type for inp in node.outputs) | ||
|
||
hash_key = hashlib.sha256(pickle.dumps(key)).hexdigest() | ||
|
||
return hash_key | ||
|
||
|
||
def check_cache(node_key): | ||
"""Check disk-backed cache.""" | ||
return numba_db.get(node_key, None) | ||
|
||
|
||
def add_to_cache(node_key, numba_py_fn): | ||
"""Add the numba generated function to the cache.""" | ||
numba_db[node_key] = numba_py_fn | ||
return numba_py_fn | ||
|
||
|
||
@singledispatch | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI: I think the tests are failing because we haven't finished refactoring the rest of the code so that it's aware of
numba_funcify
now returning un-njit
ed Python functions. For example,test_config_options_cached
is expectingnumba_mul_fn
to be a NumbaCPUDispatcher
object and not a plain Python function object.