|
| 1 | +from collections.abc import Callable |
| 2 | +from pathlib import Path |
| 3 | +from tempfile import NamedTemporaryFile, TemporaryFile |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +from numba.core.caching import CacheImpl, _CacheLocator |
| 7 | + |
| 8 | +from pytensor import config |
| 9 | + |
| 10 | + |
| 11 | +NUMBA_PYTENSOR_CACHE_ENABLED = True |
| 12 | +COMPILED_SRC_FUNCTIONS = {} |
| 13 | + |
| 14 | + |
| 15 | +def compile_and_cache_numba_function_src( |
| 16 | + src: str, |
| 17 | + function_name: str, |
| 18 | + global_env: dict[Any, Any] | None = None, |
| 19 | + local_env: dict[Any, Any] | None = None, |
| 20 | + key: str | None = None, |
| 21 | +) -> Callable: |
| 22 | + if key is not None: |
| 23 | + numba_path = config.base_compiledir / "numba" |
| 24 | + numba_path.mkdir(exist_ok=True) |
| 25 | + filename = numba_path / key |
| 26 | + with filename.open("wb") as f: |
| 27 | + f.write(src.encode()) |
| 28 | + else: |
| 29 | + with NamedTemporaryFile(delete=False) as f: |
| 30 | + filename = f.name |
| 31 | + f.write(src.encode()) |
| 32 | + |
| 33 | + if global_env is None: |
| 34 | + global_env = {} |
| 35 | + |
| 36 | + if local_env is None: |
| 37 | + local_env = {} |
| 38 | + |
| 39 | + mod_code = compile(src, filename, mode="exec") |
| 40 | + exec(mod_code, global_env, local_env) |
| 41 | + |
| 42 | + res = local_env[function_name] |
| 43 | + res.__source__ = src # type: ignore |
| 44 | + |
| 45 | + if key is not None: |
| 46 | + COMPILED_SRC_FUNCTIONS[res] = key |
| 47 | + return res |
| 48 | + |
| 49 | + |
| 50 | +class NumbaPyTensorCacheLocator(_CacheLocator): |
| 51 | + def __init__(self, py_func, py_file, hash): |
| 52 | + # print(f"New locator {py_func=}, {py_file=}, {hash=}") |
| 53 | + self._py_func = py_func |
| 54 | + self._py_file = py_file |
| 55 | + self._hash = hash |
| 56 | + # src_hash = hash(pytensor_loader._module_sources[self._py_file]) |
| 57 | + # self._hash = hash((src_hash, py_file, pytensor.__version__)) |
| 58 | + |
| 59 | + def ensure_cache_path(self): |
| 60 | + # print("ensure_cache_path called") |
| 61 | + path = self.get_cache_path() |
| 62 | + path.mkdir(exist_ok=True) |
| 63 | + # Ensure the directory is writable by trying to write a temporary file |
| 64 | + TemporaryFile(dir=path).close() |
| 65 | + |
| 66 | + def get_cache_path(self): |
| 67 | + """ |
| 68 | + Return the directory the function is cached in. |
| 69 | + """ |
| 70 | + # print("get_cache_path called") |
| 71 | + return self._py_file |
| 72 | + |
| 73 | + def get_source_stamp(self): |
| 74 | + """ |
| 75 | + Get a timestamp representing the source code's freshness. |
| 76 | + Can return any picklable Python object. |
| 77 | + """ |
| 78 | + return 0 |
| 79 | + # print("get_source_stamp called") |
| 80 | + return self._hash |
| 81 | + |
| 82 | + def get_disambiguator(self): |
| 83 | + """ |
| 84 | + Get a string disambiguator for this locator's function. |
| 85 | + It should allow disambiguating different but similarly-named functions. |
| 86 | + """ |
| 87 | + # print("get_disambiguator called") |
| 88 | + return self._hash |
| 89 | + |
| 90 | + @classmethod |
| 91 | + def from_function(cls, py_func, py_file): |
| 92 | + """ |
| 93 | + Create a locator instance for the given function located in the given file. |
| 94 | + """ |
| 95 | + # py_file = Path(py_file).parent |
| 96 | + # if py_file == (config.base_compiledir / "numba"): |
| 97 | + if NUMBA_PYTENSOR_CACHE_ENABLED and py_func in COMPILED_SRC_FUNCTIONS: |
| 98 | + # print(f"Applies to {py_file}") |
| 99 | + return cls(py_func, Path(py_file).parent, COMPILED_SRC_FUNCTIONS[py_func]) |
| 100 | + |
| 101 | + |
| 102 | +CacheImpl._locator_classes.insert(0, NumbaPyTensorCacheLocator) |
0 commit comments