Skip to content

Conversation

@carlosgmartin
Copy link
Contributor

Adds a tip to tree.reduce and tree.reduce_associative about how to exclude leaves from the reduction.

Context: #30156.

@jakevdp jakevdp self-assigned this Jul 14, 2025
@carlosgmartin
Copy link
Contributor Author

@jakevdp How does this look? If you prefer a different format, let me know.

@jakevdp
Copy link
Collaborator

jakevdp commented Aug 5, 2025

Thanks - sorry I lost track of this.

The tip is a bit abstract, and I'm not entirely sure in what contexts it might be useful. Can you say more? Maybe including the tip in Examples rather than in Notes would be more helpful?

@carlosgmartin
Copy link
Contributor Author

For me, personally, this came up in the context of this PR: I wanted to exclude zero-size leafs when computing the min/max of a whole pytree.

This pattern can be helpful whenever users want to exclude certain leafs from a reduction, for any reason.

To wit, I was originally going to request a filter argument to tree.reduce, but then realized it can be accomplished by simply converting (via tree.map) leafs to something that isn't considered a leaf by tree.reduce (and thus gets excluded from the reduction), e.g. None.

If you think another wording would be clearer, I can reword it.

@google-ml-butler google-ml-butler bot added kokoro:force-run pull ready Ready for copybara import and testing labels Aug 8, 2025
@copybara-service copybara-service bot merged commit 38318c4 into jax-ml:main Aug 8, 2025
18 of 25 checks passed
@carlosgmartin carlosgmartin deleted the tree_reduce_exclude_leaves_tip branch August 8, 2025 18:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kokoro:force-run pull ready Ready for copybara import and testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants