keras icon indicating copy to clipboard operation
keras copied to clipboard

Selecting Metal (MPS) as the GPU in MacOS (torch backend)

Open dkgaraujo opened this issue 2 years ago • 11 comments

First off, congratulations on keras-core: keras is awesome, keras-core is awesomer! Using a Mac, I was trying to manually set a keras-core more with torch backend to benefit from the Metal GPU acceleration, which works on both Apple silicon and AMD GPUs. I noted this was not possible due to:

As can be seen here, in MacOS systems using the GPU can be as easy as setting the device to "mps". So I would like to ask if there would be an opportunity for a PR that implements some version of the snippet below that wouldn't fail on non-Apple machines:

# keras_core/backend/torch/core.py
if torch.cuda.is_available():
    DEFAULT_DEVICE = "cuda"
elif:
    if torch.backends.mps.is_available():
    DEFAULT_DEVICE = "mps"
else:
    DEFAULT_DEVICE = "cpu"

The goal is to have Keras-core seamlessly choose MPS as the default GPU in PyTorch backends, just as it already does for tensorflow implementations using the Metal pluggable device.

dkgaraujo avatar Jul 19 '23 21:07 dkgaraujo