Skip to content
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

Adding tree_util.stack_leaves() and tree_util.unstack_leaves() #20934

Open
ayaka14732 opened this issue Apr 25, 2024 · 5 comments
Open

Adding tree_util.stack_leaves() and tree_util.unstack_leaves() #20934

ayaka14732 opened this issue Apr 25, 2024 · 5 comments
Labels
enhancement New feature or request

Comments

@ayaka14732
Copy link
Member

  • stack_leaves: Stack the leaves of one or more PyTrees along a new axis.
  • unstack_leaves: Unstack the leaves of a PyTree.

References:

@ayaka14732 ayaka14732 added the enhancement New feature or request label Apr 25, 2024
@ayaka14732 ayaka14732 changed the title Adding tree_utils.stack_leaves() and tree_utils.unstack_leaves() Adding tree_util.stack_leaves() and tree_util.unstack_leaves() Apr 25, 2024
@jakevdp
Copy link
Collaborator

jakevdp commented Apr 25, 2024

To be clear, are these the semantics you have in mind?

def stack_leaves(pytrees, axis):
  return jax.tree.map(lambda *xs: jnp.stack(xs, axis), pytrees)

@ayaka14732
Copy link
Member Author

To be clear, are these the semantics you have in mind?

Yes

@jakevdp
Copy link
Collaborator

jakevdp commented Apr 25, 2024

For something like this, I'd probably lean toward recommending users implement what they need via existing API composability, rather than providing a new API for something that can already be pretty succinctly expressed. What do you think?

@ASEM000
Copy link

ASEM000 commented Apr 26, 2024

Maybe adding tree util cookbook would be useful? @jakevdp

@jakevdp
Copy link
Collaborator

jakevdp commented Apr 26, 2024

A pytree cookbook would be an interesting idea! This idea also came up in #20594. @ayaka14732, is that something you'd be interested in thinking about?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants