Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions lib/matplotlib/cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -2344,6 +2344,30 @@ def _picklable_class_constructor(mixin_class, fmt, attr_name, base_class):
return cls.__new__(cls)


def _is_torch_array(x):
"""Check if 'x' is a PyTorch Tensor."""
try:
Comment thread
patel-zeel marked this conversation as resolved.
Resolved
# we're intentionally not attempting to import torch. If somebody
# has created a torch array, torch should already be in sys.modules
return isinstance(x, sys.modules['torch'].Tensor)
except Exception: # TypeError, KeyError, AttributeError, maybe others?
Comment thread
tacaswell marked this conversation as resolved.
Resolved
# we're attempting to access attributes on imported modules which
# may have arbitrary user code, so we deliberately catch all exceptions
return False


def _is_jax_array(x):
"""Check if 'x' is a JAX Array."""
try:
# we're intentionally not attempting to import jax. If somebody
# has created a jax array, jax should already be in sys.modules
return isinstance(x, sys.modules['jax'].Array)
except Exception: # TypeError, KeyError, AttributeError, maybe others?
# we're attempting to access attributes on imported modules which
# may have arbitrary user code, so we deliberately catch all exceptions
return False


def _unpack_to_numpy(x):
"""Internal helper to extract data from e.g. pandas and xarray objects."""
if isinstance(x, np.ndarray):
Expand All @@ -2358,6 +2382,12 @@ def _unpack_to_numpy(x):
# 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):
xtmp = x.__array__()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason not to do xtmp = np.asarray(x) here?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these are equivalent for the handled cases. While pandas claims you shouldn't call __array__ directly, I haven't found any official recommendation for it in numpy (which defines the __array__ API).

For me, either works. __array__ is more explicit, which can be a good thing, but might be too low level. OTOH I don't think np.asarray() will change the implementation so that the asarray abstraction would be safer.

@patel-zeel patel-zeel Jan 26, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jakevdp @timhoffm What's your opinion about using x.numpy(force=True) or x.detach().cpu().numpy() for PyTorch? As a user, I'd find this change useful for day-to-day coding since it saves me from manually writing it for every array I want to plot with matplotlib.

if _is_torch_array(x):
    xtmp = x.numpy(force=True)  # or x.detach().cpu().numpy()
if _is_jax_array(x):
    xtmp = x.__array__()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable to use the x.numpy method provided by torch. Though I have to say, I don't know whether __array__ would do something different.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@timhoffm To add more context, in JAX, .__array__() method converts a JAX array to a NumPy array irrespective of whether the JAX array is on CPU, GPU, TPU and probably other hardware accelerators. OTOH in PyTorch, they don't do it by default due to uncertainty about performance impact (full discussion is in this issue). So, when we do x.numpy(force=True), it forcefully converts it to NumPy irrespective of the device of the array (handles few others cases as well, e.g. if array has a computation graph for backpropagation then x.detach() needs to be called first). I am not sure if this should be handled in a separate issue or we can use x.numpy(force=True) in this PR itself but I'm sure that PyTorch users would love this change to avoid writing x.detach().cpu().numpy() every time they plot a PyTorch array.

cc @jakevdp.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My hesitation about this is that memory overflows will get shunted to Matpolotlib, and then we will get the bug reports, whereas if people did x.*.numpy() in their own code, they would see what the problem is. Jax arrays can be far larger than memory allows, and Matplotlib blindly unpacking them for naive users seems like a bad idea.

@rgommers rgommers Jan 29, 2024 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though I have to say, I don't know whether __array__ would do something different.

__array__ is the last thing tried by NumPy when a user would call np.asarray, after the buffer protocol and __array_interface__ (as documented for example here). So they're not equivalent, and calling np.asarray is idiomatic.


# In case __array__() method does not return a numpy array in future
if isinstance(xtmp, np.ndarray):
return xtmp
return x


Expand Down
44 changes: 44 additions & 0 deletions lib/matplotlib/tests/test_cbook.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import sys
import itertools
import pickle

Expand All @@ -16,6 +17,7 @@
from matplotlib import _api, cbook
import matplotlib.colors as mcolors
from matplotlib.cbook import delete_masked_points, strip_math
from types import ModuleType


class Test_delete_masked_points:
Expand Down Expand Up @@ -938,3 +940,45 @@ def test_auto_format_str(fmt, value, result):
"""Apply *value* to the format string *fmt*."""
assert cbook._auto_format_str(fmt, value) == result
assert cbook._auto_format_str(fmt, np.float64(value)) == result


def test_unpack_to_numpy_from_torch():
"""Test that torch tensors are converted to numpy arrays.
We don't want to create a dependency on torch in the test suite, so we mock it.
"""
class Tensor:
def __init__(self, data):
self.data = data
def __array__(self):
return self.data
torch = ModuleType('torch')
torch.Tensor = Tensor
sys.modules['torch'] = torch

data = np.arange(10)
torch_tensor = torch.Tensor(data)

result = cbook._unpack_to_numpy(torch_tensor)
assert result is torch_tensor.__array__()


def test_unpack_to_numpy_from_jax():
"""Test that jax arrays are converted to numpy arrays.
We don't want to create a dependency on jax in the test suite, so we mock it.
"""
class Array:
def __init__(self, data):
self.data = data
def __array__(self):
return self.data

jax = ModuleType('jax')
jax.Array = Array

sys.modules['jax'] = jax

data = np.arange(10)
jax_array = jax.Array(data)

result = cbook._unpack_to_numpy(jax_array)
assert result is jax_array.__array__()