| 1 |
import functools |
| 2 |
|
| 3 |
from .autoray import ( |
| 4 |
do, |
| 5 |
infer_backend, |
| 6 |
tree_map, |
| 7 |
tree_iter, |
| 8 |
tree_flatten, |
| 9 |
tree_unflatten, |
| 10 |
is_array, |
| 11 |
) |
| 12 |
from . import lazy |
| 13 |
|
| 14 |
|
| 15 |
class CompilePython: |
| 16 |
"""A simple compiler that unravels all autoray calls, optionally sharing |
| 17 |
intermediates and folding constants, converts this to a code object using |
| 18 |
``compile``, then executes this using ``exec``. |
| 19 |
|
| 20 |
Parameters |
| 21 |
---------- |
| 22 |
fn : callable |
| 23 |
Function to compile - should have signature |
| 24 |
``fn(*args, **kwargs) -> array``, with ``args`` and ``kwargs`` any |
| 25 |
nested combination of ``tuple``, ``list`` and ``dict`` objects |
| 26 |
containing arrays (or other constant arguments), and perform array |
| 27 |
operations on these using ``autoray.do``. |
| 28 |
fold_constants : bool, optional |
| 29 |
Whether to fold all constant array operations into the graph, which |
| 30 |
might increase memory usage. |
| 31 |
share_intermediates : bool, optional |
| 32 |
Whether to cache all computational nodes during the trace, so that any |
| 33 |
shared intermediate results can be identified. |
| 34 |
""" |
| 35 |
|
| 36 |
def __init__(self, fn, fold_constants=True, share_intermediates=True): |
| 37 |
self._fn = fn |
| 38 |
self._fold_constants = fold_constants |
| 39 |
self._share_intermediates = share_intermediates |
| 40 |
self._jit_fn = None |
| 41 |
|
| 42 |
def setup(self, args, kwargs): |
| 43 |
"""Convert the example arrays to lazy variables and trace them through |
| 44 |
the function. |
| 45 |
""" |
| 46 |
variables = tree_map(lazy.array, (args, kwargs)) |
| 47 |
|
| 48 |
if self._share_intermediates: |
| 49 |
# with backend_like("autoray.lazy"), lazy.shared_intermediates(): |
| 50 |
with lazy.shared_intermediates(): |
| 51 |
outs = self._fn(*variables[0], **variables[1]) |
| 52 |
else: |
| 53 |
# with backend_like("autoray.lazy"): |
| 54 |
outs = self._fn(*variables[0], **variables[1]) |
| 55 |
|
| 56 |
return lazy.Function( |
| 57 |
variables, outs, fold_constants=self._fold_constants |
| 58 |
) |
| 59 |
|
| 60 |
def __call__(self, *args, array_backend=None, **kwargs): |
| 61 |
"""If necessary, build, then call the compiled function.""" |
| 62 |
if self._jit_fn is None: |
| 63 |
self._jit_fn = self.setup(args, kwargs) |
| 64 |
|
| 65 |
return self._jit_fn(args, kwargs) |
| 66 |
|
| 67 |
|
| 68 |
class CompileJax: |
| 69 |
""" """ |
| 70 |
|
| 71 |
def __init__(self, fn, enable_x64=None, platform_name=None, **kwargs): |
| 72 |
self._fn = fn |
| 73 |
self._enable_x64 = enable_x64 |
| 74 |
self._platform_name = platform_name |
| 75 |
self._jit_fn = None |
| 76 |
self._jit_kwargs = kwargs |
| 77 |
|
| 78 |
def setup(self): |
| 79 |
import jax |
| 80 |
|
| 81 |
if self._enable_x64 is not None: |
| 82 |
import jax |
| 83 |
|
| 84 |
jax.config.update("jax_enable_x64", self._enable_x64) |
| 85 |
|
| 86 |
if self._platform_name is not None: |
| 87 |
import jax |
| 88 |
|
| 89 |
jax.config.update("jax_platform_name", self._platform_name) |
| 90 |
|
| 91 |
self._jit_fn = jax.jit(self._fn, **self._jit_kwargs) |
| 92 |
self._fn = None |
| 93 |
|
| 94 |
def __call__(self, *args, array_backend=None, **kwargs): |
| 95 |
if self._jit_fn is None: |
| 96 |
self.setup() |
| 97 |
out = self._jit_fn(*args, **kwargs) |
| 98 |
if array_backend != "jax": |
| 99 |
out = do("asarray", out, like=array_backend) |
| 100 |
return out |
| 101 |
|
| 102 |
|
| 103 |
class CompileTensorFlow: |
| 104 |
""" """ |
| 105 |
|
| 106 |
def __init__(self, fn, **kwargs): |
| 107 |
self._fn = fn |
| 108 |
kwargs.setdefault("autograph", False) |
| 109 |
self._jit_fn = None |
| 110 |
self._jit_kwargs = kwargs |
| 111 |
|
| 112 |
def setup(self): |
| 113 |
import tensorflow as tf |
| 114 |
|
| 115 |
self._jit_fn = tf.function(**self._jit_kwargs)(self._fn) |
| 116 |
self._fn = None |
| 117 |
|
| 118 |
def __call__(self, *args, array_backend=None, **kwargs): |
| 119 |
if self._jit_fn is None: |
| 120 |
self.setup() |
| 121 |
out = self._jit_fn(*args, **kwargs) |
| 122 |
if array_backend != "tensorflow": |
| 123 |
out = do("asarray", out, like=array_backend) |
| 124 |
return out |
| 125 |
|
| 126 |
|
| 127 |
class CompileTorch: |
| 128 |
""" """ |
| 129 |
|
| 130 |
def __init__(self, fn, **kwargs): |
| 131 |
import torch |
| 132 |
|
| 133 |
self.torch = torch |
| 134 |
|
| 135 |
if not hasattr(fn, "__name__") and isinstance(fn, functools.partial): |
| 136 |
# torch jit.trace requires fn.__name__ and others |
| 137 |
functools.update_wrapper(fn, fn.func) |
| 138 |
|
| 139 |
self._fn = fn |
| 140 |
self._jit_fn = None |
| 141 |
kwargs.setdefault("check_trace", False) |
| 142 |
self._jit_kwargs = kwargs |
| 143 |
|
| 144 |
def setup(self, *args, **kwargs): |
| 145 |
flat_tensors, ref_tree = tree_flatten((args, kwargs), get_ref=True) |
| 146 |
|
| 147 |
def flat_fn(flat_tensors): |
| 148 |
args, kwargs = tree_unflatten(flat_tensors, ref_tree) |
| 149 |
return self._fn(*args, **kwargs) |
| 150 |
|
| 151 |
self._jit_fn = self.torch.jit.trace( |
| 152 |
flat_fn, [flat_tensors], **self._jit_kwargs |
| 153 |
) |
| 154 |
|
| 155 |
def __call__(self, *args, array_backend=None, **kwargs): |
| 156 |
if array_backend != "torch": |
| 157 |
# torch doesn't handle numpy arrays itself |
| 158 |
args = tree_map(self.torch.as_tensor, args, is_array) |
| 159 |
if self._jit_fn is None: |
| 160 |
self.setup(*args, **kwargs) |
| 161 |
out = self._jit_fn(tree_flatten((args, kwargs))) |
| 162 |
if array_backend != "torch": |
| 163 |
out = do("asarray", out, like=array_backend) |
| 164 |
return out |
| 165 |
|
| 166 |
|
| 167 |
class CompileTorch2: |
| 168 |
def __init__(self, fn, **kwargs): |
| 169 |
import torch |
| 170 |
|
| 171 |
self.torch = torch |
| 172 |
|
| 173 |
def f(*args, **kwargs): |
| 174 |
# for some reason torch compile wants a wrapper around |
| 175 |
return fn(*args, **kwargs) |
| 176 |
|
| 177 |
self._jit_fn = torch.compile(f, **kwargs) |
| 178 |
|
| 179 |
def __call__(self, *args, array_backend=None, **kwargs): |
| 180 |
if array_backend != "torch": |
| 181 |
# torch doesn't handle numpy arrays itself |
| 182 |
args = tree_map(self.torch.as_tensor, args, is_array) |
| 183 |
out = self._jit_fn(*args, **kwargs) |
| 184 |
if array_backend != "torch": |
| 185 |
out = do("asarray", out, like=array_backend) |
| 186 |
return out |
| 187 |
|
| 188 |
|
| 189 |
class CompilePytensor: |
| 190 |
def __init__(self, fn, **kwargs): |
| 191 |
self._fn = fn |
| 192 |
self._jit_fn = None |
| 193 |
self._jit_kwargs = kwargs |
| 194 |
self._output_ref_tree = None |
| 195 |
|
| 196 |
def setup(self, args, kwargs): |
| 197 |
import pytensor |
| 198 |
import pytensor.tensor as pt |
| 199 |
|
| 200 |
flat_arrays, ref_tree = tree_flatten( |
| 201 |
(args, kwargs), |
| 202 |
is_leaf=is_array, |
| 203 |
get_ref=True, |
| 204 |
) |
| 205 |
variables = [ |
| 206 |
pt.tensor(dtype=x.dtype, shape=x.shape) for x in flat_arrays |
| 207 |
] |
| 208 |
pt_args, pt_kwargs = tree_unflatten(variables, ref_tree) |
| 209 |
result = self._fn(*pt_args, **pt_kwargs) |
| 210 |
outs, self._output_ref_tree = tree_flatten( |
| 211 |
result, is_leaf=is_array, get_ref=True |
| 212 |
) |
| 213 |
self._jit_fn = pytensor.function(variables, outs, **self._jit_kwargs) |
| 214 |
|
| 215 |
def __call__(self, *args, array_backend=None, **kwargs): |
| 216 |
if self._jit_fn is None: |
| 217 |
self.setup(args, kwargs) |
| 218 |
|
| 219 |
flat_arrays = tree_flatten((args, kwargs)) |
| 220 |
flat_outs = self._jit_fn(*flat_arrays) |
| 221 |
return tree_unflatten(flat_outs, self._output_ref_tree) |
| 222 |
|
| 223 |
|
| 224 |
_backend_lookup = {} |
| 225 |
|
| 226 |
_compiler_lookup = { |
| 227 |
"jax": CompileJax, |
| 228 |
"tensorflow": CompileTensorFlow, |
| 229 |
"torch": CompileTorch, |
| 230 |
"torch:trace": CompileTorch, |
| 231 |
"torch:compile": CompileTorch2, |
| 232 |
"pytensor": CompilePytensor, |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
class AutoCompiled: |
| 237 |
"""Just in time compile a ``autoray.do`` using function. See the main |
| 238 |
wrapper ``autojit``. |
| 239 |
""" |
| 240 |
|
| 241 |
def __init__(self, fn, backend=None, compiler_opts=None): |
| 242 |
self._fn = fn |
| 243 |
self._backend = backend |
| 244 |
self._compiled_fns = {} |
| 245 |
if compiler_opts is None: |
| 246 |
self._compiler_kwargs = {} |
| 247 |
else: |
| 248 |
self._compiler_kwargs = compiler_opts |
| 249 |
|
| 250 |
def __call__(self, *args, backend=None, **kwargs): |
| 251 |
array_backend = infer_backend( |
| 252 |
next(tree_iter((args, kwargs), is_array)) |
| 253 |
) |
| 254 |
|
| 255 |
if backend is None: |
| 256 |
if self._backend is None: |
| 257 |
# no backend specified anywhere, use the array backend |
| 258 |
backend = array_backend |
| 259 |
else: |
| 260 |
# use the backend specified at init |
| 261 |
backend = self._backend |
| 262 |
|
| 263 |
# work out which compiler to use for combo of backend and array backend |
| 264 |
try: |
| 265 |
key = _backend_lookup[backend, array_backend] |
| 266 |
except KeyError: |
| 267 |
if backend in _compiler_lookup: |
| 268 |
key = backend |
| 269 |
else: |
| 270 |
key = f"python-{array_backend}" |
| 271 |
_backend_lookup[backend, array_backend] = key |
| 272 |
|
| 273 |
try: |
| 274 |
fn_compiled = self._compiled_fns[key] |
| 275 |
except KeyError: |
| 276 |
if "python" in key: |
| 277 |
backend = "python" |
| 278 |
backend_compiler = _compiler_lookup.get(backend, CompilePython) |
| 279 |
compiler_kwargs = self._compiler_kwargs.get(backend, {}) |
| 280 |
fn_compiled = backend_compiler(self._fn, **compiler_kwargs) |
| 281 |
self._compiled_fns[key] = fn_compiled |
| 282 |
|
| 283 |
return fn_compiled(*args, array_backend=array_backend, **kwargs) |
| 284 |
|
| 285 |
|
| 286 |
def autojit(fn=None, *, backend=None, compiler_opts=None): |
| 287 |
"""Just-in-time compile an ``autoray`` function, automatically choosing |
| 288 |
the backend based on the input arrays, or via keyword argument. |
| 289 |
|
| 290 |
The backend used to do the compilation can be set in three ways: |
| 291 |
|
| 292 |
1. Automatically based on the arrays the function is called with, |
| 293 |
i.e. ``cfn(*torch_arrays)`` will use ``torch.jit.trace``. |
| 294 |
2. In this wrapper, ``@autojit(backend='jax')``, to provide a |
| 295 |
specific default instead. |
| 296 |
3. When you call the function ``cfn(*arrays, backend='torch')`` to |
| 297 |
override on a per-call basis. |
| 298 |
|
| 299 |
If the arrays supplied are of a different backend type to the compiler, |
| 300 |
then the returned array will also be converted back, i.e. |
| 301 |
``cfn(*numpy_arrays, backend='tensorflow')`` will return a ``numpy`` array. |
| 302 |
|
| 303 |
The ``'python'`` backend simply extracts and unravels all the ``do`` calls |
| 304 |
into a code object using ``compile`` which is then run with ``exec``. |
| 305 |
This makes use of shared intermediates and constant folding, strips |
| 306 |
away any python scaffoliding, and is compatible with any library, but the |
| 307 |
resulting function is not 'low-level' in the same way as the other |
| 308 |
backends. |
| 309 |
|
| 310 |
Parameters |
| 311 |
---------- |
| 312 |
fn : callable |
| 313 |
The autoray function to compile. |
| 314 |
backend : str, optional |
| 315 |
If set, use this as the default backend. The options are: |
| 316 |
|
| 317 |
- ``'python'``: extract and unravel all the ``do`` calls into a |
| 318 |
code object using ``compile`` which is then run with ``exec``. |
| 319 |
- ``'jax'``: use `jax.jit` to compile the function. |
| 320 |
- ``'tensorflow'``: use `tf.function` to compile the function. |
| 321 |
- ``'torch'``: use `torch.jit.trace` to compile the function. |
| 322 |
- ``'pytensor'``: use `pytensor.function` to compile the function. |
| 323 |
|
| 324 |
If not set, the backend will be inferred from the input arrays, or it |
| 325 |
can be set at call time with the ``backend`` keyword argument. |
| 326 |
compiler_opts : dict[dict], optional |
| 327 |
Dict of dicts when you can supply options for each compiler backend |
| 328 |
separately, e.g.: |
| 329 |
``@autojit(compiler_opts={'tensorflow': {'jit_compile': True}})``. |
| 330 |
|
| 331 |
Returns |
| 332 |
------- |
| 333 |
cfn : callable |
| 334 |
The function with auto compilation. |
| 335 |
""" |
| 336 |
kws = dict(backend=backend, compiler_opts=compiler_opts) |
| 337 |
if fn is None: |
| 338 |
return functools.partial(autojit, **kws) |
| 339 |
return functools.wraps(fn)(AutoCompiled(fn, **kws)) |