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

feat(treespec): add tree broadcast functions broadcast_common, tree_broadcast_common, tree_broadcast_map, and tree_broadcast_map_with_path #87

Merged
merged 11 commits into from
Oct 12, 2023

Conversation

XuehaiPan
Copy link
Member

@XuehaiPan XuehaiPan commented Oct 1, 2023

Description

Describe your changes in detail.

Add new methods and functions:

  • PyTreeSpec.broadcast_to_common_suffix()
  • tree_broadcast_common()
  • broadcast_common()
  • tree_broadcast_map()
  • tree_broadcast_map_with_path()
>>> import optree
>>> spec1 = optree.tree_structure({'a': 1, 'b': (2, 3), 'c': [4, 5]})
>>> spec1
PyTreeSpec({'a': *, 'b': (*, *), 'c': [*, *]})
>>> spec2 = optree.tree_structure({'a': (1, 2), 'b': 3, 'c': [4, 5]})
>>> spec2
PyTreeSpec({'a': (*, *), 'b': *, 'c': [*, *]})
>>> spec3 = spec1.broadcast_to_common_suffix(spec2)
>>> spec3
PyTreeSpec({'a': (*, *), 'b': (*, *), 'c': [*, *]})
>>> spec1.is_prefix(spec3)
True
>>> spec2.is_prefix(spec3)
True
>>> import optree
>>> spec1 = optree.tree_structure({'a': 1, 'b': (2, 3), 'c': [4, None]})
>>> spec1
PyTreeSpec({'a': *, 'b': (*, *), 'c': [*, None]})
>>> spec2 = optree.tree_structure({'a': (1, 2), 'b': 3, 'c': [None, 5]})
>>> spec2
PyTreeSpec({'a': (*, *), 'b': *, 'c': [None, *]})
>>> spec3 = spec1.broadcast_to_common_suffix(spec2)
>>> spec3
PyTreeSpec({'a': (*, *), 'b': (*, *), 'c': [None, None]})
>>> spec1.is_prefix(spec3)
True
>>> spec2.is_prefix(spec3)
True
>>> import optree
>>> optree.tree_broadcast_common([1, (2, 3), 4], [5, 6, (7, 8)])
([1, (2, 3), (4, 4)], [5, (6, 6), (7, 8)])
>>> optree.tree_broadcast_map(lambda x, y: x * y, [5, 6, (3, 4)], [{'a': 7, 'b': 9}, [1, 2], 8])
[{'a': 35, 'b': 45}, [6, 12], (24, 32)]
>>> optree.tree_broadcast_map_with_path(lambda p, x, y: (p, x * y), [5, 6, (3, 4)], [{'a': 7, 'b': 9}, [1, 2], 8])
[{'a': ((0, 'a'), 35), 'b': ((0, 'b'), 45)},
 [((1, 0), 6), ((1, 1), 12)],
 (((2, 0), 24), ((2, 1), 32))]

Motivation and Context

Why is this change required? What problem does it solve?
If it fixes an open issue, please link to the issue here.
You can use the syntax close #15213 if this solves the issue #15213

  • I have raised an issue to propose this change (required for new features and bug fixes)

Types of changes

What types of changes does your code introduce? Put an x in all the boxes that apply:

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds core functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation (update in the documentation)

Checklist

Go over all the following points, and put an x in all the boxes that apply.
If you are unsure about any of these, don't hesitate to ask. We are here to help!

  • I have read the CONTRIBUTION guide. (required)
  • My change requires a change to the documentation.
  • I have updated the tests accordingly. (required for a bug fix or a new feature)
  • I have updated the documentation accordingly.
  • I have reformatted the code using make format. (required)
  • I have checked the code using make lint. (required)
  • I have ensured make test pass. (required)

@XuehaiPan XuehaiPan added enhancement New feature or request cxx Something related to the CXX source code py Something related to the Python source code labels Oct 1, 2023
@XuehaiPan XuehaiPan self-assigned this Oct 1, 2023
@codecov
Copy link

codecov bot commented Oct 1, 2023

Codecov Report

All modified lines are covered by tests ✅

Comparison is base (e5ed81c) 100.00% compared to head (7823670) 100.00%.

Additional details and impacted files
@@            Coverage Diff            @@
##              main       #87   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            4         4           
  Lines          428       468   +40     
=========================================
+ Hits           428       468   +40     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
optree/__init__.py 100.00% <ø> (ø)
optree/ops.py 100.00% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@XuehaiPan XuehaiPan force-pushed the broadcast branch 17 times, most recently from 9483b94 to b297016 Compare October 1, 2023 19:19
@XuehaiPan XuehaiPan force-pushed the broadcast branch 6 times, most recently from 438ef1b to 4180be3 Compare October 6, 2023 17:57
@XuehaiPan XuehaiPan force-pushed the broadcast branch 6 times, most recently from 23a82f1 to 762cca3 Compare October 7, 2023 17:42
@XuehaiPan XuehaiPan marked this pull request as ready for review October 7, 2023 17:44
@XuehaiPan XuehaiPan changed the title feat(treespec): add tree broadcast methods feat(treespec): add tree broadcast functions broadcast_common, tree_broadcast_common, tree_broadcast_map, and tree_broadcast_map_with_path Oct 12, 2023
@XuehaiPan XuehaiPan added this to the 0.10.0 milestone Oct 12, 2023
@XuehaiPan XuehaiPan merged commit 1582f22 into metaopt:main Oct 12, 2023
43 checks passed
@XuehaiPan XuehaiPan deleted the broadcast branch October 12, 2023 12:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cxx Something related to the CXX source code enhancement New feature or request py Something related to the Python source code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants