Skip to content

Add optax.tree_utils.tree_batch_shape. - #1161

Merged
copybara-service[bot] merged 1 commit into
google-deepmind:mainfrom
carlosgmartin:tree_random_like_batch_shape
Feb 3, 2025
Merged

Add optax.tree_utils.tree_batch_shape.#1161
copybara-service[bot] merged 1 commit into
google-deepmind:mainfrom
carlosgmartin:tree_random_like_batch_shape

Conversation

@carlosgmartin

@carlosgmartin carlosgmartin commented Dec 26, 2024

Copy link
Copy Markdown
Contributor

This commit adds a batch_shape argument to optax.tree_utils.tree_random_like.

This is useful when sampling multiple perturbations, such as for evolution strategies.

@carlosgmartin

Copy link
Copy Markdown
Contributor Author

@rdyro How does this look? Happy to make any needed changes.

@rdyro

rdyro commented Jan 27, 2025

Copy link
Copy Markdown
Collaborator

Hey, sorry for the delayed response. Adding batch is definitely useful in practice, but I'm concerned about API uniformity with the other tree_like methods:

tree_full_like
tree_ones_like
tree_random_like
tree_split_key_like
tree_zeros_like

Can we go with a workaround for the time being (I realize it's not as concise 😞 )

a = {"a": jnp.ones(10), "c": jnp.zeros(4), "d": jnp.array(1.0)}
bs = (10,)
batch_tree = jax.tree.map(lambda x: jax.ShapeDtypeStruct(bs + x.shape, x.dtype), a)
optax.tree_utils.tree_random_like(random.key(0), batch_tree)

@carlosgmartin

carlosgmartin commented Jan 27, 2025

Copy link
Copy Markdown
Contributor Author

How about adding a helper function like the following?

def tree_batch(a, shape, dtype=None, sharding=None):
  return jax.tree.map(lambda a: jax.ShapeDtypeStruct(
    shape + a.shape,
    dtype=dtype or a.dtype,
    sharding=sharding or a.sharding,
  ), a)

Then we can use

from jax import random
from optax import tree_utils as otu
otu.tree_random_like(random.key(0), otu.tree_batch(tree, shape))

@rdyro

rdyro commented Jan 27, 2025

Copy link
Copy Markdown
Collaborator

That sounds like a great idea! Would you be willing to contribute this addition?

Can you call it something a little more verbose than tree_batch maybe tree_batch_shape, but I'm open to better ideas?

@carlosgmartin
carlosgmartin force-pushed the tree_random_like_batch_shape branch from 8fa173b to 3fe2b27 Compare January 28, 2025 19:32
@carlosgmartin carlosgmartin changed the title Add batch_shape argument to optax.tree_utils.tree_random_like. Add optax.tree_utils.tree_batch_shape. Jan 28, 2025
@carlosgmartin

Copy link
Copy Markdown
Contributor Author

@rdyro Done. I thought it might be more helpful for other potential uses to keep the actual values instead of returning ShapeDtypeStructs, so I've switched to using broadcast_to instead. (This uses zero-strides, so it doesn't need additional memory.)

@carlosgmartin

Copy link
Copy Markdown
Contributor Author

@rdyro Is the above ok?

@rdyro

rdyro commented Feb 2, 2025

Copy link
Copy Markdown
Collaborator

@rdyro Is the above ok?

Using broadcast_to makes this function more general, nice!

@copybara-service
copybara-service Bot merged commit b51d9a8 into google-deepmind:main Feb 3, 2025
@carlosgmartin
carlosgmartin deleted the tree_random_like_batch_shape branch February 3, 2025 19:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants