Comparing python objects such as list and tuple with mlx.core.array (#866) - #886
Comparing python objects such as list and tuple with mlx.core.array (#866)#886amirhossein-razlighi wants to merge 0 commit into
mlx.core.array (#866)#886Conversation
awni
left a comment
There was a problem hiding this comment.
Thanks for getting this started. Left some comments inline, please address.
| if (auto pv = std::get_if<nb::bool_>(&v); pv) { | ||
| return true; | ||
| } else if (auto pv = std::get_if<nb::int_>(&v); pv) { | ||
| return true; | ||
| } else if (auto pv = std::get_if<nb::float_>(&v); pv) { | ||
| return true; | ||
| } else if (auto pv = std::get_if<std::complex<float>>(&v); pv) { | ||
| return true; | ||
| } else if (auto pv = std::get_if<nb::object>(&v); pv) { | ||
| return nb::hasattr(*pv, "__mlx_array__") || nb::isinstance<array>(*pv); | ||
| } |
There was a problem hiding this comment.
You had a different version earlier which just pulled out an object if it existed. So if it's not an nb::oject then it's ok (int, bool, float, complex) and if it is then check if its an array or has the attribute? Maybe we should do that instead?
There was a problem hiding this comment.
It's the same line that is present here:
else if (auto pv = std::get_if<nb::object>(&v); pv) {
return nb::hasattr(*pv, "__mlx_array__") || nb::isinstance<array>(*pv);
}
The function that we had earlier (isMlxCoreArray) checked this line. I think right now it's better and we may use this utility function further in more operations (where we need to check if the operands are valid and can be converted to mlx.core.array).
There was a problem hiding this comment.
Ok just thinking about it from an efficiency standpoint (if we wanted to use this in a lot of ops we'd need it to be very fast since it's pure overhead). Can you not do?
if (auto pv = std::get_if<nb::object>(&v); pv) {
return nb::isinstance<array>(*pv) || nb::hasattr(*pv, "__mlx_array__");
} else {
return true;
}Also it might be minor but I would swap the hasattr and the isinstance since the former will be much much more common (and also probably a lot less expensive).
There was a problem hiding this comment.
I swapped the hasattr and isinstance. Also, why should we add else { return true; }? Because if it's not each of the options mentioned above (bool/int/float/complex/array) it is not convertible to array and thus, we should simply return False.
There was a problem hiding this comment.
please check the latest commit and let me know about what you think should be changed
There was a problem hiding this comment.
Also, I think we need a better name for this function. Because, instances like list and tuple are actually convertible to mlx.core.array. They are not comparable to arrays. Don't you agree? This name might be a little misleading in further developments
|
Looks really nice, thank you! I left a couple comments. Could you please take a look and then we can run tests and merge? |
c0c8f86 to
c4fd0e5
Compare
Proposed changes
This pull request is related to issue #866 . When trying to compare python list or tuple with an
mlx.core.array, mlx throws exception. Right now, regarding the comments in the related issue, mlx returns False (following the way it is done in PyTorch). An example is as follows:Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes