-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
apply_to_collection
improvements and add apply_to_collections
#7769
Conversation
Codecov Report
@@ Coverage Diff @@
## master #7769 +/- ##
======================================
- Coverage 92% 92% -0%
======================================
Files 199 199
Lines 12986 13015 +29
======================================
+ Hits 11994 12010 +16
- Misses 992 1005 +13 |
zipped = {k: (data1[k], data2[k]) for k in data1.keys() | data2.keys()} | ||
return elem_type({ | ||
k: apply_to_collections(*v, dtype, function, *args, wrong_dtype=wrong_dtype, **kwargs) | ||
for k, v in zipped.items() | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@awaelchli this implementation is not OrderedDict safe as data1.keys() | data2.keys()
is a set
.
But I don't think we need to implement it in this PR. Could add a warning if the instance is an ordereddict
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just realized the previous implementation also did not take care of OrderedDict.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM !
v = apply_to_collection(v, dtype, function, *args, wrong_dtype=wrong_dtype, **kwargs) | ||
if include_none or v is not None: | ||
out.append((k, v)) | ||
return elem_type(out) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no guarantee that an arbitrary Mapping
will support construction from Iterate[Tuple[K, T]]. This will fail for custom collections.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For example, this will fail when data is CfgNode (from YAQS) (https://github.com/rbgirshick/yacs/blob/master/yacs/config.py#L74).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you saying this was supported before and now this change broke? Could you elaborate?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, we use Lightning in Detectron2Go (based on Detectron). The configuration there is specified by YACS (a CfgNode object).
We use the save_hyperameters
function on this object, which eventually calls the code here. The previous code (which calls elem_type === CfgDict
with a dict
) worked fine. With this new change, an error is raised since elem_type === CfgDict
cannot be constructed from a List of Tuples.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe the modification:
from collections import OrderedDict
...
elem_type(OrderedDict(out))
will fix this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kandluis are you interested in sending a patch with your proposal?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure -- sent out #7851 :)
What does this PR do?
Part of #7631
Before submitting
PR review