mlverse / torch

R Interface to Torch
https://torch.mlverse.org
Other
483 stars 66 forks source link

Mac M2 Max Tensor Matrix Algebra on GPU #1099

Closed LopezRicardo1 closed 9 months ago

LopezRicardo1 commented 10 months ago

Is there a way to perform matrix algebra operations using Mac M2 Max Gpu system using Torch R?

dfalbel commented 9 months ago

That should just work if you move tensors to the MPS device.

Eg:

x <- torch_randn(10, 10)$to(device="mps")
y <- torch_randn(10, 10)$to(device="mps")

torch_mm(x, y)

If both tensors are on the MPS device, then the operation will happen on the GPU if there's a kernel for it.

LopezRicardo1 commented 9 months ago

I see, thank you!

Though, using this setting the operation will be done in the GPU, right? How can I check if there's a kernel for GPU?

I'm still in a learning curve on this!

dfalbel commented 9 months ago

The easiest way is to run and see if it works :) At this point, most operations already support running on mac GPU's. If you have large enough tensors you will quicky see your GPU usage going up in the system monitor app:

library(torch)
x <- torch_randn(10000, 10000)$to(device="mps")

for (i in 1:1000) {
  y <- torch_mm(x, x)  
}
y[10]
LopezRicardo1 commented 9 months ago

Yes, GPU 90% on system monitor!

Thanks!