How to run Pytorch on Macbook pro (M1) GPU? - pytorch

I tried to train a model using PyTorch on my Macbook pro. It uses the new generation apple M1 CPU. However, PyTorch couldn't recognize my GPUs.
GPU available: False, used: False
TPU available: False, using: 0 TPU cores
IPU available: False, using: 0 IPUs
Does anyone know any solution?
I have updated all the libraries to the latest versions.

PyTorch added support for M1 GPU as of 2022-05-18 in the Nightly version. Read more about it in their blog post.
Simply install nightly:
conda install pytorch -c pytorch-nightly --force-reinstall
Update: It's available in the stable version:
Conda:conda install pytorch torchvision torchaudio -c pytorch
pip: pip3 install torch torchvision torchaudio
To use (source):
mps_device = torch.device("mps")
# Create a Tensor directly on the mps device
x = torch.ones(5, device=mps_device)
# Or
x = torch.ones(5, device="mps")
# Any operation happens on the GPU
y = x * 2
# Move your model to mps just like any other device
model = YourFavoriteNet()
model.to(mps_device)
# Now every call runs on the GPU
pred = model(x)

It looks like PyTorch support for the M1 GPU is in the works, but is not yet complete.
From #soumith on GitHub:
So, here's an update.
We plan to get the M1 GPU supported. #albanD, #ezyang and a few core-devs have been looking into it. I can't confirm/deny the involvement of any other folks right now.
So, what we have so far is that we had a prototype that was just about okay. We took the wrong approach (more graph-matching-ish), and the user-experience wasn't great -- some operations were really fast, some were really slow, there wasn't a smooth experience overall. One had to guess-work which of their workflows would be fast.
So, we're completely re-writing it using a new approach, which I think is a lot closer to your good ole PyTorch, but it is going to take some time. I don't think we're going to hit a public alpha in the next ~4 months.
We will open up development of this backend as soon as we can.
That post:
https://github.com/pytorch/pytorch/issues/47702#issuecomment-965625139
TL;DR: a public beta is at least 4 months out.

For those who couldn't install using conda like me use pip as following:-
Requirement:
Any Macbook with apple silicon chip
macOS version 12.3+
Installation:
pip3 install --pre torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/nightly/cpu
Update:
No need of nightly version. Pytorch version 1.12 now supports GPU acceleration in apple silicon. Simply install using following command:-
pip3 install torch torchvision torchaudio
You may follow other instructions for using pytorch in apple silicon and getting your benchmark.
Usage:
Make sure you use mps as your device as following:
device = torch.device('mps')
# Send you tensor to GPU
my_tensor = my_tensor.to(device)
Benchmarking (on M1 Max, 10-core CPU, 24-core GPU):
Without using GPU
Using GPU (5x faster)

Related

How to solve the error reported by installing pytorch on the Raspberry Pi?

ERROR Image
At the beginning, the Raspberry Pi environment was 3.9.2 (armv7l).
After searching the Internet, pytorch found that it only supports 3.8; after installing the conda environment, install pytorch under the (py36) python version 3.6.6; but as shown in the picture,
it can't Install.
Is there anyone on the forum who has encountered this problem and solved it?
Thanks.
At the beginning, when installing pytorch according to the official process, it will report that /home/pi/pytorch/third_party/eigen lacks the extraction file and cannot be installed.
After finding and installing the pytorch.whl file on the Internet and reporting an error, try to install the conda environment; but in conda Environment can't be installed.
Off topic, my second question is if I go to train mobilenet SSD v2 after success, can I directly use the lens for visual recognition?
Before using the public mobilenet to detect, the number of frames was only about 4.4. Later, because I felt that the FPS was too low, I wanted to train mibilenet by myself.
How about someone who has used Raspberry Pi to use FPS after training? If the FPS is too low, is it better to use NVIDIA Jetson Nano for training and detection?

Google Colab GPU environment establish

I'm trying to run my deep learning code in Google Colab, I have installed cuda10.0.130 and cudnn7.6.4 for tensorflow 1.14.0, but the result of tf.test.is_gpu_available() is still false, I don't know what can I do now, can somebody give me some instructions? Here is the output of !sudo lsb_release -a and !nvidia-smi
Supported and Tested configurations for GPU versions are given here in this link
Supported Version for Cuda 10.1 and Cudnn 7.6 will be tensorflow_gpu-2.3.0
Also for TF 1.X versions CPU and GPU support are different
So you should do
!pip install tensorflow_gpu==1.14.0
for using GPU version of Tensorflow
Ref- https://www.tensorflow.org/install/gpu#older_versions_of_tensorflow

Can I use high version torch and low version cuda?

I'm setting up my Conda environment with a remote GPU to use Pytorch.
The GPU I use is only NVIDIA-SMI 396.54, so I can only use cuda version 9.2
However, I need to use a higher version torch to be able to use some attributes.
I tried
conda install pytorch==1.7.1 torchvision==0.8.2 cudatoolkit=9.2
But this results in
print(torch.version.cuda)>> None
torch.cuda.is_available() >> False
There are two things I would check.
You may have unintentionally installed the pytorch cpu version or had it in your environment first, before running the above command. Even if you install the gpu version of Pytorch, if you already have the cpu version of pytorch then torch.cuda.is_available() will return False. Therefore I suggest checking out this link:
Forum on why Pytorch is CPU version even after installing cudatoolkit version
Although, I am pretty sure the above thing is your problem, I suggest looking at this second thing.
For understanding how to download previous version of Pytorch refer to this link. https://pytorch.org/get-started/previous-versions/
After looking at this, I suggest starting a new conda env and running your conda install command first.
Sarthak Jain

How to run tensorflow using GPU?

Tensorflow version - 1.0.1
GPU version - 1.0.1
python - 3.6
Graphics(Ubuntu 18.04) - IntelĀ® Sandybridge Desktop
We are trying to run a python code using Tensorflow. But it was very slow. So, we would like to run the code on GPU. Can anyone suggest how to use GPU for running python code?
You can uninstall tensor flow and install the GPU compatible version with
pip install tensorflow-gpu
config = tf.compat.v1.ConfigProto(gpu_options = tf.compat.v1.GPUOptions(per_process_gpu_memory_fraction=0.8))
config.gpu_options.allow_growth = True
session = tf.compat.v1.Session(config=config)
tf.compat.v1.keras.backend.set_session(session)
Place this code before your python code and also make sure you have all the cuDNN dlls installed.

Installing a CPU-based library version on a GPU enabled machine

I want to install a CPU version of PyTorch on a server which is equipped with a nVIDIA Tesla GPU. Will it work or can I only install a GPU version (with CUDA) on this server for PyTorch to function properly?
The version of PyTorch with GPU support also works with CPU (but the cpu training of neural networks is really slow). So you can install the GPU version. Make sure you install PyTorch compiled with the correct cuda version (cuda 7.5, cuda 8.0 or cuda 9.0).

Resources