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

fix(treespec): make PyTreeSpec.is_prefix to be consistent with PyTreeSpec.flatten_up_to #94

Merged
merged 5 commits into from
Oct 11, 2023

Conversation

XuehaiPan
Copy link
Member

Description

Describe your changes in detail.

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 bug Something isn't working cxx Something related to the CXX source code labels Oct 7, 2023
@XuehaiPan XuehaiPan self-assigned this Oct 7, 2023
@codecov
Copy link

codecov bot commented Oct 7, 2023

Codecov Report

All modified lines are covered by tests ✅

Comparison is base (2ff91d7) 100.00% compared to head (7310a7a) 100.00%.

Additional details and impacted files
@@            Coverage Diff            @@
##              main       #94   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            4         4           
  Lines          427       428    +1     
=========================================
+ Hits           427       428    +1     
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/ops.py 100.00% <100.00%> (ø)

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

@XuehaiPan XuehaiPan marked this pull request as draft October 7, 2023 06:41
@XuehaiPan XuehaiPan force-pushed the flatten-up-to branch 4 times, most recently from 4c6bb14 to a918781 Compare October 7, 2023 17:34
@XuehaiPan XuehaiPan marked this pull request as ready for review October 7, 2023 17:44
@XuehaiPan XuehaiPan force-pushed the flatten-up-to branch 2 times, most recently from ea093c6 to 6b3533d Compare October 8, 2023 10:37
@@ -88,7 +88,7 @@ bool PyTreeSpec::operator==(const PyTreeSpec& other) const {
// NOLINTNEXTLINE[readability-qualified-auto]
for (auto a = m_traversal.begin(); a != m_traversal.end(); ++a, ++b) {
if (a->kind != b->kind || a->arity != b->arity ||
(a->node_data.ptr() == nullptr) != (b->node_data.ptr() == nullptr) ||
static_cast<bool>(a->node_data) != static_cast<bool>(b->node_data) ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why change this? what's the original type of a->node_data, is it a pointer?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a->node_data is a py::object. pybind11 suggests to use operator bool() to check whether the py::object instance is set. It is equivalent to this->ptr() != nullptr.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good job!

if (a->kind != b->kind || a->arity != b->arity ||
(a->node_data.ptr() == nullptr) != (b->node_data.ptr() == nullptr) ||
if (a->arity != b->arity ||
static_cast<bool>(a->node_data) != static_cast<bool>(b->node_data) ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

@@ -288,8 +382,8 @@ py::list PyTreeSpec::Entries() const {
return py::list{root.node_entries};
}
switch (root.kind) {
case PyTreeKind::None:
case PyTreeKind::Leaf: {
case PyTreeKind::Leaf:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reorder the switch cases to match the order of the enum definitions.


case PyTreeKind::DefaultDict:
case PyTreeKind::Deque:
case PyTreeKind::Custom:
case PyTreeKind::Custom: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm

@XuehaiPan XuehaiPan requested a review from JieRen98 October 11, 2023 07:14
@XuehaiPan XuehaiPan merged commit e5ed81c into metaopt:main Oct 11, 2023
41 checks passed
@XuehaiPan XuehaiPan deleted the flatten-up-to branch October 11, 2023 13:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working cxx Something related to the CXX source code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants