|
| 1 | +from pytensor.link.basic import JITLinker |
| 2 | +from pytensor.link.utils import unique_name_generator |
| 3 | + |
| 4 | + |
| 5 | +class MLXLinker(JITLinker): |
| 6 | + """A `Linker` that JIT-compiles NumPy-based operations using Apple's MLX.""" |
| 7 | + |
| 8 | + def __init__(self, *args, **kwargs): |
| 9 | + super().__init__(*args, **kwargs) |
| 10 | + self.gen_functors = [] |
| 11 | + |
| 12 | + def fgraph_convert( |
| 13 | + self, |
| 14 | + fgraph, |
| 15 | + order, |
| 16 | + input_storage, |
| 17 | + output_storage, |
| 18 | + storage_map, |
| 19 | + **kwargs, |
| 20 | + ): |
| 21 | + """Convert a PyTensor FunctionGraph to an MLX-compatible function. |
| 22 | +
|
| 23 | + Parameters |
| 24 | + ---------- |
| 25 | + fgraph : FunctionGraph |
| 26 | + The function graph to convert |
| 27 | + order : list |
| 28 | + The order in which to compute the nodes |
| 29 | + input_storage : list |
| 30 | + Storage for the input variables |
| 31 | + output_storage : list |
| 32 | + Storage for the output variables |
| 33 | + storage_map : dict |
| 34 | + Map from variables to their storage |
| 35 | +
|
| 36 | + Returns |
| 37 | + ------- |
| 38 | + callable |
| 39 | + An MLX-compatible function |
| 40 | + """ |
| 41 | + from pytensor.link.mlx.dispatch import mlx_funcify |
| 42 | + |
| 43 | + # We want to have globally unique names |
| 44 | + # across the entire pytensor graph, not |
| 45 | + # just the subgraph |
| 46 | + generator = unique_name_generator(["mlx_linker"]) |
| 47 | + |
| 48 | + # Ensure that torch is aware of the generated |
| 49 | + # code so we can compile without graph breaks |
| 50 | + def conversion_func_register(*args, **kwargs): |
| 51 | + functor = mlx_funcify(*args, **kwargs) |
| 52 | + name = kwargs["unique_name"](functor) |
| 53 | + self.gen_functors.append((f"_{name}", functor)) |
| 54 | + return functor |
| 55 | + |
| 56 | + built_kwargs = { |
| 57 | + "unique_name": generator, |
| 58 | + "conversion_func": conversion_func_register, |
| 59 | + **kwargs, |
| 60 | + } |
| 61 | + return mlx_funcify( |
| 62 | + fgraph, |
| 63 | + input_storage=input_storage, |
| 64 | + storage_map=storage_map, |
| 65 | + **built_kwargs, |
| 66 | + ) |
| 67 | + |
| 68 | + def jit_compile(self, fn): |
| 69 | + """JIT compile an MLX function. |
| 70 | +
|
| 71 | + Parameters |
| 72 | + ---------- |
| 73 | + fn : callable |
| 74 | + The function to compile |
| 75 | +
|
| 76 | + Returns |
| 77 | + ------- |
| 78 | + callable |
| 79 | + The compiled function |
| 80 | + """ |
| 81 | + import mlx.core as mx |
| 82 | + |
| 83 | + return mx.compile(fn) |
| 84 | + |
| 85 | + def create_thunk_inputs(self, storage_map): |
| 86 | + """Create inputs for the MLX thunk. |
| 87 | +
|
| 88 | + Parameters |
| 89 | + ---------- |
| 90 | + storage_map : dict |
| 91 | + Map from variables to their storage |
| 92 | +
|
| 93 | + Returns |
| 94 | + ------- |
| 95 | + list |
| 96 | + The inputs for the thunk |
| 97 | + """ |
| 98 | + from numpy.random import Generator, RandomState |
| 99 | + |
| 100 | + from pytensor.link.mlx.dispatch import mlx_typify |
| 101 | + |
| 102 | + thunk_inputs = [] |
| 103 | + for n in self.fgraph.inputs: |
| 104 | + sinput = storage_map[n] |
| 105 | + # Handle random number generators specially |
| 106 | + if isinstance(sinput[0], RandomState | Generator): |
| 107 | + new_value = mlx_typify( |
| 108 | + sinput[0], dtype=getattr(sinput[0], "dtype", None) |
| 109 | + ) |
| 110 | + sinput[0] = new_value |
| 111 | + thunk_inputs.append(sinput) |
| 112 | + |
| 113 | + return thunk_inputs |
0 commit comments