Repository navigation
Raise if fit and predict use different array API namespaces or devices - #29313
jeromedockes wants to merge 33 commits into
Conversation
| return _estimator_with_converted_arrays(estimator, make_converter(ref_array)) | ||
|
|
||
|
|
||
| def check_fitted_attribute(estimator, method_name, attr_name, X): |
There was a problem hiding this comment.
during the meeting we said that this check could be added to utils.validation.check_is_fitted, but at some point we also said that array API support shouldn't modify scikit-learn's public API yet (which check_is_fitted is part of)
|
I think we should have a dedicated paragraph in the array API section of the changelog for 1.6 to explain that the error messages are more informative when passing different array types or devices at fit and prediction times. |
ogrisel
left a comment
There was a problem hiding this comment.
Thanks @jeromedockes.
Maybe the documentation could be updated to specify this behavior of array API fitted estimators.
In addition, here a few suggestions for improvement below but otherwise, LGTM.
Co-authored-by: Olivier Grisel <[email protected]>
…edockes/scikit-learn into check-fitted-same-namespace-device
|
Thank you @ogrisel. IIRC we said in the meeting that this PR should add the converter and the check and add it to an example estimator -- ATM, it is done for |
|
As you prefer. |
adrinjalali
left a comment
There was a problem hiding this comment.
I think this is a nice addition. But we do need a way to convert estimators between namespaces / devices as well (#26083)
| from ..utils._array_api import ( | ||
| _asarray_with_order, | ||
| _average, | ||
| check_fitted_attribute, |
There was a problem hiding this comment.
I find the name confusing. Something like check_same_namespace would be more informative maybe?
| :class:`model_selection.HalvingRandomSearchCV` now support Array API | ||
| compatible inputs when their base estimators do. :pr:`27096` by :user:`Tim | ||
| Head <betatim>` and :user:`Olivier Grisel <ogrisel>`. | ||
| - :class:`linear_model.Ridge` now raises a more informative error message when arrays passed at |
There was a problem hiding this comment.
We should also add this to doc/modules/array_api.rst (ideally in this PR?) I think it would be good for us as reviewers, contributors and users if we start writing down our conventions. I think we have a few now and they mostly just exist in our heads. Maybe this could be the starting topic
|
|
||
| Attributes which are not arrays are left unchanged. | ||
| """ | ||
| return _estimator_with_converted_arrays(estimator, make_converter(ref_array)) |
There was a problem hiding this comment.
Could you explain why we need make_converter? Could we have the same thing by having a function like convert_to(reference, X) which is used here a bit like _estimator_with_converted_arrays(estimator, functools.partial(convert_to, ref_array))?
I'm not sure which approach I prefer, so just trying to understand it at the moment.
There was a problem hiding this comment.
Could we have the same thing by having a function like convert_to(reference, X) which is used here a bit like _estimator_with_converted_arrays(estimator, functools.partial(convert_to, ref_array))?
yes, that would work just as well! I had make_converter return a function because I wanted to use the existing _estimator_with_converted_arrays, which expects a function. But we can also define convert_to and use partial as you suggest, I don't really have a preference
There was a problem hiding this comment.
(note both options will not have the same behavior when the dispatch_array_api config changes between the call to make_converter/partial and using the result, but this should probably be avoided in any case)
| return convert | ||
|
|
||
|
|
||
| def convert_attributes(estimator, ref_array): |
There was a problem hiding this comment.
| def convert_attributes(estimator, ref_array): | |
| def convert_attributes(estimator, reference_array): |
or maybe just reference? I somehow prefer to spell out things instead of using abbrvs (abbreviations) because it is easier to read.
| return _estimator_with_converted_arrays(estimator, make_converter(ref_array)) | ||
|
|
||
|
|
||
| def check_fitted_attribute(estimator, method_name, attr_name, X): |
There was a problem hiding this comment.
| def check_fitted_attribute(estimator, method_name, attr_name, X): | |
| def check_fitted_attribute(estimator, method_name, attribute, X): |
Mostly motivated by trying to avoid abbreviations and it seems like attribute_name would be quite long and maybe not needed?
| return _estimator_with_converted_arrays(estimator, make_converter(ref_array)) | ||
|
|
||
|
|
||
| def check_fitted_attribute(estimator, method_name, attr_name, X): |
There was a problem hiding this comment.
Suggestion for a doc string:
Check that estimator's fitted(?) attribute is compatible with X.
Use this to check that an estimator was fitted using the same array namespace and device.
as X.
|
I like it. I left some comments mostly about naming of things. Finding a different name for |
Yes, but also: can you expand on what you mean in this context? The reason I ask is that the error message in this PR will tell you how to convert your estimator. My current thinking/knowledge is that we have the following ideas on conversion:
(this is the kind of "convention" stuff I was referring to as things we should start writing down) |
Ah yes, I missed that. Two points there:
|
|
I don't think it will work on a pipeline, you'd have to convert the particular step in the pipeline. Which isn't great. I wonder if we could easily make it recurse. Right now you could just iterate over all estimators in a pipeline and convert them. In the future where we have a A more user centred name would be good. |
|
I looked at how cuml deals with this and to my surprise it "just works". You can fit with a Numpy array and then predict with a cupy array or vice versa. I think internally it converts everything to a cupy array? Maybe this is more of a discussion item than useful for this PR, but I didn't know where to put it. |
I think one of the discussion points we've had is: at least to start with, we don't want any implicit movement of data, or try to minimize it, between devices, cause it can cause performance regressions in odd ways. Once we have a better understanding of the array API support and how things work, we can discuss if / when it makes sense to make things implicit / automated, maybe? |
that's interesting, but do you think it might depend on the estimator and the operations |
|
Quote from #26083 (comment)
|
|
This PR has stalled a bit but in the meantime we now integrated a dlpack-based, cross-namespace and device conversion function, namely: There are also fixes in the dlpack support that should be soon shipped with PyTorch 2.9.1 and NumPy 2.4.0 (in the mean time, our relies on to and from numpy/cpu based conversion as a fallback). I think this PR should be synced with |
|
I'm attempting to refresh this PR with the latest changes from |
|
Ahrg, I ran |
|
Fixed it. But I think I'll continue work in #33076 |
e5070ff to
6105654
Compare
|
Closing in favor of #33076 |
What does this implement/fix? Explain your changes.
During the bi-weekly array API meeting it was decided that estimators should raise an error if their fitted attributes are not in the same array API namespace, or on the same device, as the inputs to predict() or transform().
Moreover, there should be a way to convert the attributes to the appropriate namespace and device to solve this error.
This pr adds
_array_api.check_fitted_attributeto raise the error if necessary, and_array_api.convert_attributesto move the estimator to the namespace and device of a reference array.