Repository navigation
FIX metadata routing shouldn't deep copy estimator - #33827
Conversation
jeremiedbb
left a comment
There was a problem hiding this comment.
LGTM. To be sure, owner being the estimator itself is not actually used currently but will be with #31535, that's correct ?
|
I meant currently we don't leverage the fact that owner is the estimator itself; #31535 will. |
| # `owner` is a reference to the estimator and is only used by | ||
| # `_routing_repr` for display; deep copying it would drag the full | ||
| # estimator state (and fail for non-picklable attributes) for no benefit. | ||
| new = self.__class__.__new__(self.__class__) | ||
| memo[id(self)] = new |
There was a problem hiding this comment.
At a glance, overriding __deepcopy__ feels like a bad code smell.
If the underlying goal is to have a nicer repr for routing, can the owner's repr be passed directly to MetadataRouter?
MetadataRouter(owner_repr=...)There was a problem hiding this comment.
I did think about this pattern and changing it to passing the repr, but in *SearchCV kind of places, we tend to get the routing objects, then change parameters, and then there might be an issue (which admittedly shouldn't happen), and then the error message would be very misleading.
There was a problem hiding this comment.
Thinking about this, I agree __deepcopy__ manipulation smells, so I've replaced it with a _clone now. This should be nicer.
| new_object = klass(**new_object_params) | ||
| try: | ||
| new_object._metadata_request = copy.deepcopy(estimator._metadata_request) | ||
| new_object._metadata_request = estimator._metadata_request._clone() |
There was a problem hiding this comment.
should we then call clone(estimator._metadata_request) and have it implement __sklearn_clone__ ? it looks like that what's _clone is reproducing here
| # `owner` is a reference to the estimator and is only used by | ||
| # `_routing_repr` for display; deep-copying it would drag the full | ||
| # estimator state (and fail for non-picklable attributes) for no benefit. | ||
| new = self.__class__.__new__(self.__class__) | ||
| new.owner = self.owner | ||
| new.method = self.method | ||
| new._requests = deepcopy(self._requests) | ||
| return new |
There was a problem hiding this comment.
Can this be the following to avoid self.__class__.__new__?
return MethodMetadataRequest(
owner=self.owner,
method=self.method
requests=deepcopy(self._requests)
)Same for the other implementations.
There was a problem hiding this comment.
call sites in this file use __sklearn_clone__ instead of calling clone on the object, since base.py imports this file, and if we want to use clone in those call sites, we'd need to do a local import of the function.
This came up here: skorch-dev/skorch#1139 (comment)
At some point we changed
ownerfrom being a string to the class itself, but forgot that this does a deep copy of the estimator many times. This PR fixes the issue.cc @BenjaminBossan
AI usage disclosure
I used AI assistance for: