You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Unfortunately, we cannot unconditionally call np.asarray() (__array__) on objects as that can break existing units handling. The workaround so far is to whitelist array types that we support. The relevant discussion is in #25887.
Unfortunately, we cannot unconditionally call np.asarray() (__array__) on objects as that can break existing units handling. The workaround so far is to whitelist array types that we support. The relevant discussion is in #25887.
def _unpack_to_numpy(x):
"""Internal helper to extract data from e.g. pandas and xarray objects."""
if isinstance(x, np.ndarray):
# If numpy, return directly
return x
if hasattr(x, 'to_numpy'):
# Assume that any to_numpy() method actually returns a numpy array
return x.to_numpy()
if hasattr(x, 'values'):
xtmp = x.values
# For example a dict has a 'values' attribute, but it is not a property
# so in this case we do not want to return a function
if isinstance(xtmp, np.ndarray):
return xtmp
if _is_torch_array(x) or _is_jax_array(x) or _is_tensorflow_array(x):
# using np.asarray() instead of explicitly array(), as the latter is
# only one of many methods, and it's the last resort, see also
# https://numpy.org/devdocs/user/basics.interoperability.html#using-arbitrary-objects-in-numpy
# therefore, let arrays do better if they can
xtmp = np.asarray(x)
# In case np.asarray method does not return a numpy array in future
if isinstance(xtmp, np.ndarray):
return xtmp
return x
I think the straight way forward is to explicitly add mlx support.
Alternative: Could we detect units and skip their conversion?
That makes sense. I made a PR as the workaround in #25887. It plotted mlx.core.array successfully.
Is there really not a general solution? If we don't recognize the type we run as array and then check the type of the first element?
I think let it crash if not recognized is right. Array-like is not first class civilian in pythonfor now. There is no PEP or RFC on this. We cannot rely on these unstable implementation, especially for Apple. Apple likes get fancy. But some helper would be convenient.
Yes. I also comes up with these questions. How to deal with different devices(apple is unified memory so nothing with this, but cuda or ROCm?), and dtypes.
If np.asarray doesn't return a numpy array of numpy dtypes, then I think we need to leave the conversion to the user?
It would return only if not bfloat16. I used this script for testing numpy compatibility:
import mlx.core as mx
dtypes = [name for name in dir(mx)
if isinstance(getattr(mx, name), mx.Dtype)]
print(dtypes)
import numpy as np
import traceback
for dt in dtypes:
arr = mx.ones([10], dtype=eval('mx.' + dt))
try:
nparr = np.asarray(arr)
except Exception as e:
print(dt, ':', e)
print(traceback.format_exc())
Activity
timhoffm commentedon Feb 13, 2026
Unfortunately, we cannot unconditionally call
np.asarray()(__array__) on objects as that can break existing units handling. The workaround so far is to whitelist array types that we support. The relevant discussion is in #25887.Relevant code:
matplotlib/lib/matplotlib/cbook.py
Lines 2443 to 2467 in c4b9963
I think the straight way forward is to explicitly add mlx support.
Alternative: Could we detect units and skip their conversion?
jklymak commentedon Feb 13, 2026
Is there really not a general solution? If we don't recognize the type we run as array and then check the type of the first element?
lzqlzzq commentedon Feb 13, 2026
That makes sense. I made a PR as the workaround in #25887. It plotted
mlx.core.arraysuccessfully.lzqlzzq commentedon Feb 13, 2026
I think
let it crash if not recognizedis right.Array-likeis not first class civilian inpythonfor now. There is no PEP or RFC on this. We cannot rely on these unstable implementation, especially for Apple. Apple likes get fancy. But some helper would be convenient.QuLogic commentedon Feb 13, 2026
It seems like this is very specifically about the 16-bit float arrays; see the first Note here: https://ml-explore.github.io/mlx/build/html/usage/numpy.html
lzqlzzq commentedon Feb 13, 2026
Yes. I also comes up with these questions. How to deal with different devices(apple is unified memory so nothing with this, but cuda or ROCm?), and dtypes.
jklymak commentedon Feb 13, 2026
If
np.asarraydoesn't return a numpy array of numpy dtypes, then I think we need to leave the conversion to the user?lzqlzzq commentedon Feb 14, 2026
It would return only if not
bfloat16. I used this script for testing numpy compatibility: