-
-
Notifications
You must be signed in to change notification settings - Fork 26.1k
ENH: Add Array API support to hamming_loss #30838
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
ENH: Add Array API support to hamming_loss #30838
Conversation
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.
Thanks for the PR @lithomas1!
Appreciate your comment about about the weight_average
!
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.
CUDA CI passes on GitHub Actions, but I kept encountering the following DeprecationWarning
when testing on my local machine:
DeprecationWarning: __array__ implementation doesn't accept a copy keyword, so passing copy=False failed. __array__ must implement 'dtype' and 'copy' keyword arguments. To learn more, see the migration guide https://numpy.org/devdocs/numpy_2_0_migration_guide.html#adapting-to-changes-in-the-copy-keyword
The warning seems to originate from the count_zero
function while testing PyTorch on CPU:
sklearn/metrics/tests/test_common.py::test_array_api_compliance[hamming_loss-check_array_api_multilabel_classification_metric-torch-cpu-float64] FAILED
sklearn/metrics/tests/test_common.py::test_array_api_compliance[hamming_loss-check_array_api_multilabel_classification_metric-torch-cpu-float32] FAILED
It could be a false positive on my end, but I also noticed that casting sample_weight
back to the xp
array type:
if sample_weight is not None:
sample_weight = xp.asarray(sample_weight, device=device)
weight average = _average(sample_weight, xp=xp)
would suppress the warning message.
This "fix" makes sense to me because moving sample_weight
to the same namespace as y_true
and y_pred
ensures consistency across backends and prevents NumPy array methods from being called on a PyTorch tensor.
However, I’m not entirely sure if this is the correct fix or if it’s simply avoiding the warning. Therefore, I’d like to ping @OmarManzoor for help to take a closer look. Also, I don’t have the hardware to test for MPS, so @OmarManzoor, SOS! 😅
I think this makes sense as sample_weight should be on the same namespace and device, in the case where it isn't already. So this additional code to ensure that seems correct. @virchan feel free to add that as a suggestion. @ogrisel Would it be possible for you to run this on a Mac with mps? |
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.
To be safe, let's ensure that sample_weight
is in the same namespace and on the same device as y_true
and y_pred
.
Co-authored-by: Virgil Chan <virchan.math@gmail.com>
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.
Thanks for the PR @lithomas1
Co-authored-by: Omar Salman <omar.salman2007@gmail.com>
Thanks for the review. r.e. the count_nonzero change I think I copied the original code from here (from accuracy_score) scikit-learn/sklearn/metrics/_classification.py Lines 232 to 238 in 5eb676a
Do you think it's fine for me to update that section here, or should I open a followup? |
Yes sure, I think it would be nice to refactor that as well considering this applies there. We can run the CUDA CI after you push your change to ensure everything is working as expected. |
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. Thank you @lithomas1
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! Thanks @lithomas1!
@ogrisel @adrinjalali Would it be possible for you run this on a Mac with mps so that we can merge this PR? |
Unfortunately I don't have a mac machine. Maybe @adam2392 does? |
Sure I can try running this tonight. I have a M1 |
@adam2392 |
Sorry trying to get this to work on my Mac M1, but for some reason when I run:
which is weird to me. I've installed scikit-learn on miniforge too |
So I think this function works for MPS, but I am not that familiar with the testing, and I cannot get the MPS device to get picked up. So I can't say forsure… I'm running the following to test and then adding a breakpoint to check if the device is using
However, if I run this file manually, the output seems correct. Mind educating me @OmarManzoor @adrinjalali ?
|
…ing-loss-array-api
…learn into hamming-loss-array-api
Thanks for giving this a run.
The command I ran was
I'm also on an older torch version (2.2), which might also make a difference. |
I'm curious: Will the tests pass even if MPS is not used (and CPU is used), or does passing imply MPS was used correctly? Since you're not on a M1+ Mac, it seems the tests are passing even tho it's not testing MPS which seems like a bug to me. I think the code works based on my snippet showing the hamming loss is computed using a MPS tensor from PyTorch. I'm just not sure if the tests are working as intended tho. |
I think MPS was used correctly (the Intel Mac I have has a discrete AMD GPU that has some MPS capabilities - I don't know if it is falling back for the hamming loss stuff though). While running the metrics tests, I also did see GPU usage and some Metal processes (MTLCompilerService) when running on MPS, so at least something in there is using my GPU. The device thing is very strange, though. |
|
If MPS is not used, Skipping MPS device test because PYTORCH_ENABLE_MPS_FALLBACK is not set. or MPS is not available because the current PyTorch install was not built with MPS enabled. This is typically what I got when running the Array API tests on my local Windows machine. |
This might be related to #30454, as we changed the |
What does xp show as here? I initially got back None after forgetting to wrap stuff in config_context, but after wrapping with config_context I get
|
Ah okay it was my fault. The metric call in the unit-test is always called once w/ numpy array and once with the relevant pytorch tensor
The second time indeed catches the |
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.
Thanks for the PR @lithomas1!
Thanks for the reviews! |
Reference Issues/PRs
xref #26024
What does this implement/fix? Explain your changes.
This makes hamming_loss array API compatible.
Any other comments?