-
Notifications
You must be signed in to change notification settings - Fork 181
feat(mlx): pt.random support with mlx backend #1979
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
Open
williambdean
wants to merge
8
commits into
pymc-devs:v3
Choose a base branch
from
williambdean:mlx-random
base: v3
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.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5679f45
add currently supported random module
williambdean 7a0abc3
add test suite
williambdean a5f9e3f
handle rng input
williambdean d8807ca
address review: shared rng dance in linker, xor-fold 128-bit pcg64 seed
williambdean 480010a
fix import after core.py renamed to tensor_basic.py
williambdean b1dd5e7
address review: bernoulli shape boilerplate, mvnormal decomposition m…
williambdean 2131f4e
fix ruff isort: declare pytensor and tests as known-first-party
williambdean 5d450de
fix import: pytensor.compile.function removed in v3 refactor
williambdean 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,211 @@ | ||
| from functools import singledispatch | ||
|
|
||
| import mlx.core as mx | ||
| from numpy.random import Generator | ||
|
|
||
| import pytensor.tensor.random.basic as ptr | ||
| from pytensor.link.mlx.dispatch.basic import mlx_funcify, mlx_typify | ||
| from pytensor.link.mlx.dispatch.tensor_basic import ( | ||
| convert_dtype_to_mlx, | ||
| mlx_to_list_shape, | ||
| ) | ||
|
|
||
|
|
||
| def numpy_generator_to_mlx_key(rng: Generator) -> mx.array: | ||
| """Convert a NumPy Generator to an MLX random key. | ||
|
|
||
| MLX uses a functional RNG model where each random call takes an explicit | ||
| key rather than mutating shared state. The PCG64 state is 128 bits, which | ||
| MLX cannot accept directly. We fold both 64-bit halves together via XOR | ||
| to use all 128 bits of entropy in a single 64-bit seed. | ||
| """ | ||
| state_128 = int(rng.bit_generator.state["state"]["state"]) | ||
| upper = (state_128 >> 64) & 0xFFFFFFFFFFFFFFFF | ||
| lower = state_128 & 0xFFFFFFFFFFFFFFFF | ||
| return mx.random.key(upper ^ lower) | ||
|
|
||
|
|
||
| @mlx_typify.register(Generator) | ||
| def mlx_typify_Generator(rng, **kwargs): | ||
| return numpy_generator_to_mlx_key(rng) | ||
|
|
||
|
|
||
| @mlx_funcify.register(ptr.RandomVariable) | ||
| def mlx_funcify_RandomVariable(op, node, **kwargs): | ||
| rv = node.outputs[1] | ||
| out_dtype = rv.type.dtype | ||
|
|
||
| sample_fn_inner = mlx_sample_fn(op, node) | ||
|
|
||
| def sample_fn(rng, size, *parameters): | ||
| new_keys = mx.random.split(rng, num=2) | ||
| new_rng = new_keys[0] | ||
| sampling_key = new_keys[1] | ||
| sample = sample_fn_inner(sampling_key, size, out_dtype, *parameters) | ||
| return (new_rng, sample) | ||
|
|
||
| return sample_fn | ||
|
|
||
|
|
||
| @singledispatch | ||
| def mlx_sample_fn(op, node): | ||
| raise NotImplementedError( | ||
| f"No MLX implementation for the given distribution: {op.name}" | ||
| ) | ||
|
|
||
|
|
||
| @mlx_sample_fn.register(ptr.NormalRV) | ||
| def mlx_sample_fn_normal(op, node): | ||
| def sample_fn(rng_key, size, dtype, mu, sigma): | ||
| mlx_dtype = convert_dtype_to_mlx(dtype) | ||
| mu = mx.array(mu, dtype=mlx_dtype) | ||
| sigma = mx.array(sigma, dtype=mlx_dtype) | ||
| if size is None: | ||
| shape = mx.broadcast_arrays(mu, sigma)[0].shape | ||
| else: | ||
| shape = mlx_to_list_shape(size) | ||
| s = mx.random.normal(shape=shape, dtype=mlx_dtype, key=rng_key) | ||
| return mu + sigma * s | ||
|
|
||
| return sample_fn | ||
|
|
||
|
|
||
| @mlx_sample_fn.register(ptr.UniformRV) | ||
| def mlx_sample_fn_uniform(op, node): | ||
| def sample_fn(rng_key, size, dtype, low, high): | ||
| mlx_dtype = convert_dtype_to_mlx(dtype) | ||
| low = mx.array(low, dtype=mlx_dtype) | ||
| high = mx.array(high, dtype=mlx_dtype) | ||
| if size is None: | ||
| shape = mx.broadcast_arrays(low, high)[0].shape | ||
| else: | ||
| shape = mlx_to_list_shape(size) | ||
| return mx.random.uniform( | ||
| low=low, high=high, shape=shape, dtype=mlx_dtype, key=rng_key | ||
| ) | ||
|
|
||
| return sample_fn | ||
|
|
||
|
|
||
| @mlx_sample_fn.register(ptr.BernoulliRV) | ||
| def mlx_sample_fn_bernoulli(op, node): | ||
| def sample_fn(rng_key, size, dtype, p): | ||
| p = mx.array(p) | ||
| shape = mlx_to_list_shape(size) if size is not None else None | ||
| return mx.random.bernoulli(p=p, shape=shape, key=rng_key) | ||
|
|
||
| return sample_fn | ||
|
|
||
|
|
||
| @mlx_sample_fn.register(ptr.CategoricalRV) | ||
| def mlx_sample_fn_categorical(op, node): | ||
| def sample_fn(rng_key, size, dtype, p): | ||
| logits = mx.log(mx.array(p)) | ||
| shape = mlx_to_list_shape(size) if size is not None else None | ||
| return mx.random.categorical(logits=logits, axis=-1, shape=shape, key=rng_key) | ||
|
|
||
| return sample_fn | ||
|
|
||
|
|
||
| @mlx_sample_fn.register(ptr.MvNormalRV) | ||
| def mlx_sample_fn_mvnormal(op, node): | ||
| method = op.method | ||
|
|
||
| def sample_fn(rng_key, size, dtype, mean, cov): | ||
| mlx_dtype = convert_dtype_to_mlx(dtype) | ||
| mean = mx.array(mean, dtype=mlx_dtype) | ||
| cov = mx.array(cov, dtype=mlx_dtype) | ||
| n = cov.shape[-1] | ||
| batch_shape = mlx_to_list_shape(size) if size is not None else [] | ||
| if batch_shape: | ||
| # size is given: PyTensor pads param dims with leading 1s via ExpandDims; | ||
| # reshape to 2D/1D to strip those before decomposition. | ||
| cov_2d = cov.reshape(n, n) | ||
| mean_1d = mean.reshape(n) | ||
| if method == "cholesky": | ||
| A = mx.linalg.cholesky(cov_2d, stream=mx.cpu) | ||
| elif method == "svd": | ||
| U, s, _ = mx.linalg.svd(cov_2d, stream=mx.cpu) | ||
| A = U * mx.sqrt(s)[None, :] | ||
| else: # eigh | ||
| w, vecs = mx.linalg.eigh(cov_2d, stream=mx.cpu) | ||
| A = vecs * mx.sqrt(w)[None, :] | ||
| z = mx.random.normal(shape=[*batch_shape, n], dtype=mlx_dtype, key=rng_key) | ||
| return (z @ A.T) + mean_1d | ||
| else: | ||
| # size is None: param shape is the true batch (or scalar). | ||
| if method == "cholesky": | ||
| A = mx.linalg.cholesky(cov, stream=mx.cpu) | ||
| elif method == "svd": | ||
| U, s, _ = mx.linalg.svd(cov, stream=mx.cpu) | ||
| A = U * mx.sqrt(s)[..., None, :] | ||
| else: # eigh | ||
| w, vecs = mx.linalg.eigh(cov, stream=mx.cpu) | ||
| A = vecs * mx.sqrt(w)[..., None, :] | ||
| z = mx.random.normal(shape=mean.shape, dtype=mlx_dtype, key=rng_key) | ||
| return (z[..., None, :] @ A.swapaxes(-1, -2))[..., 0, :] + mean | ||
|
|
||
| return sample_fn | ||
|
|
||
|
|
||
| @mlx_sample_fn.register(ptr.LaplaceRV) | ||
| def mlx_sample_fn_laplace(op, node): | ||
| def sample_fn(rng_key, size, dtype, loc, scale): | ||
| mlx_dtype = convert_dtype_to_mlx(dtype) | ||
| loc = mx.array(loc, dtype=mlx_dtype) | ||
| scale = mx.array(scale, dtype=mlx_dtype) | ||
| if size is None: | ||
| shape = mx.broadcast_arrays(loc, scale)[0].shape | ||
| else: | ||
| shape = mlx_to_list_shape(size) | ||
| s = mx.random.laplace(shape=shape, dtype=mlx_dtype, key=rng_key) | ||
| return loc + scale * s | ||
|
|
||
| return sample_fn | ||
|
|
||
|
|
||
| @mlx_sample_fn.register(ptr.GumbelRV) | ||
| def mlx_sample_fn_gumbel(op, node): | ||
| def sample_fn(rng_key, size, dtype, loc, scale): | ||
| mlx_dtype = convert_dtype_to_mlx(dtype) | ||
| loc = mx.array(loc, dtype=mlx_dtype) | ||
| scale = mx.array(scale, dtype=mlx_dtype) | ||
| if size is None: | ||
| shape = mx.broadcast_arrays(loc, scale)[0].shape | ||
| else: | ||
| shape = mlx_to_list_shape(size) | ||
| s = mx.random.gumbel(shape=shape, dtype=mlx_dtype, key=rng_key) | ||
| return loc + scale * s | ||
|
|
||
| return sample_fn | ||
|
|
||
|
|
||
| @mlx_sample_fn.register(ptr.PermutationRV) | ||
| def mlx_sample_fn_permutation(op, node): | ||
| batch_ndim = op.batch_ndim(node) | ||
| if batch_ndim: | ||
| raise NotImplementedError( | ||
| "MLX random.permutation does not support batch dimensions." | ||
| ) | ||
|
|
||
| def sample_fn(rng_key, size, dtype, x): | ||
| return mx.random.permutation(x, key=rng_key) | ||
|
|
||
| return sample_fn | ||
|
|
||
|
|
||
| @mlx_sample_fn.register(ptr.IntegersRV) | ||
| def mlx_sample_fn_integers(op, node): | ||
| def sample_fn(rng_key, size, dtype, low, high): | ||
| mlx_dtype = convert_dtype_to_mlx(dtype) | ||
| low = mx.array(low, dtype=mlx_dtype) | ||
| high = mx.array(high, dtype=mlx_dtype) | ||
| if size is None: | ||
| shape = mx.broadcast_arrays(low, high)[0].shape | ||
| else: | ||
| shape = mlx_to_list_shape(size) | ||
| return mx.random.randint( | ||
| low=low, high=high, shape=shape, dtype=mlx_dtype, key=rng_key | ||
| ) | ||
|
|
||
| return sample_fn | ||
This file contains hidden or 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 hidden or 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,3 +1,6 @@ | ||
| import warnings | ||
|
|
||
| from pytensor.compile.sharedvalue import SharedVariable, shared | ||
| from pytensor.link.basic import JITLinker | ||
|
|
||
|
|
||
|
|
@@ -18,7 +21,7 @@ def __init__(self, use_compile=True, *args, **kwargs): | |
| self.gen_functors = [] | ||
| self.use_compile = use_compile | ||
|
|
||
| def fgraph_convert(self, fgraph, **kwargs): | ||
| def fgraph_convert(self, fgraph, input_storage, storage_map, **kwargs): | ||
| """Convert a PyTensor FunctionGraph to an MLX-compatible function. | ||
|
|
||
| Parameters | ||
|
|
@@ -32,9 +35,63 @@ def fgraph_convert(self, fgraph, **kwargs): | |
| An MLX-compatible function | ||
| """ | ||
| from pytensor.link.mlx.dispatch import mlx_funcify | ||
| from pytensor.tensor.random.type import RandomType | ||
|
|
||
| shared_rng_inputs = [ | ||
| inp | ||
| for inp in fgraph.inputs | ||
| if (isinstance(inp, SharedVariable) and isinstance(inp.type, RandomType)) | ||
| ] | ||
|
|
||
| # Replace any shared RNG inputs so that their values can be updated in place | ||
| # without affecting the original RNG container. This is necessary because | ||
| # MLX does not accept Generators as inputs, and they will have to | ||
| # be typified | ||
| if shared_rng_inputs: | ||
| warnings.warn( | ||
| f"The RandomType SharedVariables {shared_rng_inputs} will not be used " | ||
| f"in the compiled MLX graph. Instead a copy will be used.", | ||
| UserWarning, | ||
| ) | ||
| new_shared_rng_inputs = [ | ||
| shared(inp.get_value(borrow=False)) for inp in shared_rng_inputs | ||
| ] | ||
|
|
||
| fgraph.replace_all( | ||
| zip(shared_rng_inputs, new_shared_rng_inputs, strict=True), | ||
| import_missing=True, | ||
| reason="MLXLinker.fgraph_convert", | ||
| ) | ||
|
|
||
| for old_inp, new_inp in zip( | ||
| shared_rng_inputs, new_shared_rng_inputs, strict=True | ||
| ): | ||
| new_inp_storage = [new_inp.get_value(borrow=True)] | ||
| storage_map[new_inp] = new_inp_storage | ||
| old_inp_storage = storage_map.pop(old_inp) | ||
| # Find index of old_inp_storage in input_storage | ||
| for input_storage_idx, input_storage_item in enumerate(input_storage): | ||
| # We have to establish equality based on identity because input_storage may contain numpy arrays | ||
| if input_storage_item is old_inp_storage: | ||
| break | ||
| else: # no break | ||
| raise ValueError() | ||
| input_storage[input_storage_idx] = new_inp_storage | ||
| # We need to change the order of the inputs of the FunctionGraph | ||
| # so that the new input is in the same position as to old one, | ||
| # to align with the storage_map. We hope this is safe! | ||
| old_inp_fgraph_index = fgraph.inputs.index(old_inp) | ||
| fgraph.remove_input( | ||
| old_inp_fgraph_index, | ||
| reason="MLXLinker.fgraph_convert", | ||
| ) | ||
| fgraph.inputs.remove(new_inp) | ||
| fgraph.inputs.insert(old_inp_fgraph_index, new_inp) | ||
|
|
||
| return mlx_funcify( | ||
| fgraph, | ||
| input_storage=input_storage, | ||
| storage_map=storage_map, | ||
| **kwargs, | ||
| ) | ||
|
|
||
|
|
@@ -70,9 +127,16 @@ def create_thunk_inputs(self, storage_map): | |
| list | ||
| The inputs for the thunk | ||
| """ | ||
| from numpy.random import Generator | ||
|
|
||
| from pytensor.link.mlx.dispatch import mlx_typify | ||
|
|
||
| thunk_inputs = [] | ||
| for n in self.fgraph.inputs: | ||
| sinput = storage_map[n] | ||
| if isinstance(sinput[0], Generator): | ||
|
Member
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. you need to do the same dance jax linker does with shared Generator variables |
||
| # Convert Generator into MLX PRNG key | ||
| sinput[0] = mlx_typify(sinput[0]) | ||
| thunk_inputs.append(sinput) | ||
|
|
||
| return thunk_inputs | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
MvNormal supports different decomposition strategies, you may want to implement like numba dispatch/op.perform which is more low level if mx.random.multivariate_normal doesn't support them. Or if it's unfeasible issue a warning that it isn't respected and will fallback to svd (if it wasn't svd to begin with)