Computer system: MacBook Pro M1 + Mac Ventura 13.5
Installation: Miniconda + tensorflow-macos-2.13.0 + torch-2.0.1
1. Install Miniconda
Miniconda is a lightweight version of Anaconda. If you want to save hard disk storage space, you can consider installing Miniconda instead of Anaconda. But Anaconda has a visual interface and the pre-installed packages are relatively complete (some packages we may never use in our lifetime), which is more friendly for first-time users.
For details on how to install Miniconda and Anaconda, please refer to: link.
Only Miniconda is installed here.
1.1 Download Miniconda
Miniconda download address: link. Select the M1 version bash file to download.
1.2 Install Miniconda
After downloading, open the computer Terminal (terminal), enter the Download (download) folder, and install Miniconda.
enter:
cd Downloads # enter the Download folder bash Miniconda3-latest-MacOSX-arm64.sh #Install Miniconda
At this point, you will be asked to view the agreement, enter: ENTER
After reviewing the agreement, enter: yes.
At this point, the installation path of Miniconda will be prompted, just press “ENTER”.
So start the installation, after the installation is complete, enter: yes to complete the initialization.
It can be found that the environment variable has been modified.
You can check the .zshrc file, the content is added as follows:
Restart Terminal, and see (base) displayed at the beginning.
To view the version number of conda, enter in Terminal:
conda --version
At this time, the version of conda is returned, indicating that the installation is successful.
If you don’t want to automatically activate the conda base environment every time you open the terminal, you can set the auto_activate_base parameter to false. Type in Terminal (this step is optional):
conda config --set auto_activate_base false
1.3 Configuration source
1.3.1 Configure conda source
To view the current source configuration, enter in Terminal:
conda config --show channels
It can be found that it is currently defaults, which means that the image source currently used by conda is the default image source.
Go ahead and type:
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/menpo/ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/ conda config --set show_channel_urls yes
The above configuration of the source is actually to modify the .condarc file under the home path.
1.3.2 Configure pip source
Enter in Terminal:
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
return result:
To view the source configuration, enter in Terminal:
pip config list -v
return result:
1.4 Conda related commands
Function | Command | Example |
Create virtual Environment | conda create -n env_name python==version | conda create -n tf2.8 python== 3.8 |
Activate virtual environment | conda activate env_name | conda activate tf2.8 |
Exit the virtual environment | conda deactivate | conda deactivate |
Delete virtual environment | conda remove -n env_name –all | conda remove -n tf2.8 –all |
View all created virtual environments | conda env list / conda info -e | conda env list / conda info -e |
installation package | conda install package_name | conda install numpy |
View all installed packages | conda list | conda list |
upgrade package version | conda update package_name | conda update numpy |
2. Install Xcode
Enter in Terminal:
xcode-select --install
Just select “Install”, the installation process is a bit long, please wait patiently~
3. Install TensorFlow2.10-GPU
Installation prerequisites:
Requirements
- Mac computers with Apple silicon or AMD GPUs (with GPU)
- macOS 12.0 or later (Get the latest beta) (operating system above 12.0)
- Python 3.8 or later (python above 3.8)
- Xcode command-line tools:
xcode-select --install (install Xcode, previously installed)
Reference link:
Get Started with tensorflow-metal
3.1 Create a virtual environment
Enter in the base environment:
conda create -n tf2.13-gpu python==3.10
When prompted: Proceed ([y]/n)?, enter: y.
To activate the virtual environment, enter:
conda activate tf2.13-gpu
3.2 Install tensorflow-macos 2.13.0
Enter in the tf2.10-gpu environment:
python -m pip install tensorflow
return result:
Successfully installed MarkupSafe-2.1.3 absl-py-1.4.0 astunparse-1.6.3 cachetools-5.3.1 certifi-2023.7.22 charset-normalizer-3.2.0 flatbuffers-23.5.26 gast-0.4.0 google-auth- 2.22.0 google-auth-oauthlib-1.0.0 google-pasta-0.2.0 grpcio-1.56.2 h5py-3.9.0 idna-3.4 keras-2.13.1 libclang-16.0.6 markdown-3.4.4 numpy-1.24 .3 oauthlib-3.2.2 opt-einsum-3.3.0 packaging-23.1 protobuf-4.23.4 pyasn1-0.5.0 pyasn1-modules-0.3.0 requests-2.31.0 requests-oauthlib-1.3.1 rsa-4.9 six -1.16.0 tensorboard-2.13.0 tensorboard-data-server-0.7.1 tensorflow-2.13.0 tensorflow-estimator-2.13.0 tensorflow-macos-2.13.0 termcolor-2.3.0 typing-extensions-4.5.0 urllib3 -1.26.16 werkzeug-2.3.6 wrapt-1.15.0
3.3 Install tensorflow-metal plugin
Enter in the tf2.10-gpu environment:
python -m pip install tensorflow-metal
return result:
Successfully installed tensorflow-metal-1.0.1
3.4 Verification
You can verify that the installation was successful with the following code:
import tensorflow as tf cifar = tf.keras.datasets.cifar100 (x_train, y_train), (x_test, y_test) = cifar.load_data() model = tf.keras.applications.ResNet50( include_top=True, weights=None, input_shape=(32, 32, 3), classes=100,) loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False) model.compile(optimizer="adam", loss=loss_fn, metrics=["accuracy"]) model.fit(x_train, y_train, epochs=5, batch_size=64)
return result:
2023-08-06 15:20:02.934449: I metal_plugin/src/device/metal_device.cc:1154] Metal device set to: Apple M1
2023-08-06 15:20:02.934483: I metal_plugin/src/device/metal_device.cc:296] systemMemory: 16.00 GB
2023-08-06 15:20:02.934488: I metal_plugin/src/device/metal_device.cc:313] maxCacheSize: 5.33 GB
2023-08-06 15:20:02.934545: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:303] Could not identify NUMA node of platform GPU ID 0, defaulting to 0. Your kernel may not have been built with NUMA support.
2023-08-06 15:20:02.934577: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:269] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 0 MB memory) -> physical PluggableDevice (device: 0, name: METAL, pci bus id:
Epoch 1/5
2023-08-06 15:20:05.991958: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:114] Plugin optimizer for device_type GPU is enabled.
782/782 [===============================] – 123s 146ms/step – loss: 4.6828 – accuracy: 0.0756
Epoch 2/5
782/782 [===============================] – 112s 143ms/step – loss: 3.9808 – accuracy: 0.1352
Epoch 3/5
782/782 [===============================] – 112s 143ms/step – loss: 3.6236 – accuracy: 0.1873
Epoch 4/5
782/782 [===============================] – 112s 143ms/step – loss: 3.4396 – accuracy: 0.2140
Epoch 5/5
782/782 [===============================] – 112s 143ms/step – loss: 3.1431 – accuracy: 0.2644
You’re done! ! !
I don’t think it’s much faster, hahaha, let’s see later~~ (the computer is already a little hot)
3.5 Other attempts
Some attempts: both tensorflow-macos 2.10.0 and tensflow-metal 1.0.1/1.0.0/0.8.0/0.7.1 will have problems, which have not been resolved yet. The problem is: symbol not found in flat namespace ‘_TF_GetInputPropertiesList’.
Maybe install tensorflow-deps? Not tried.
4. Install PyTorch
Installation prerequisites:
Requirements
- Mac computers with Apple silicon or AMD GPUs
- macOS 12.3 or later
- Python 3.7 or later
- Xcode command-line tools:
xcode-select --install
Reference link:
Accelerated PyTorch training on Mac
Improve 5-7 times, use Mac M1 chip to accelerate PyTorch
4.1 Create a virtual environment
Enter in Terminal:
conda create -n pytorch2.0.1 python==3.10.0
When prompted: Proceed ([y]/n)?, enter: y.
To activate the virtual environment, enter:
conda activate pytorch2.0.1
4.2 Install PyTorch 2.0.1
Here you can refer to the official website to generate the installation command. Official website: link.
Enter in the pytorch2.0.1 environment:
# MPS acceleration is available on MacOS 12.3+ pip install torch torchvision torchaudio
return result:
Successfully installed MarkupSafe-2.1.3 certifi-2023.7.22 charset-normalizer-3.2.0 filelock-3.12.2 idna-3.4 jinja2-3.1.2 mpmath-1.3.0 networkx-3.1 numpy-1.25.2 pillow-10.0.0 requests-2.31.0 sympy-1.12 torch-2.0.1 torchaudio-2.0.2 torchvision-0.15.2 typing-extensions-4.7.1 urllib3-2.0.4
4.3 Verification
You can verify that the installation was successful with the following code:
import torch if torch.backends.mps.is_available(): mps_device = torch.device("mps") x = torch.ones(1, device=mps_device) print (x) else: print ("MPS device not found.")
return result:
tensor([1.], device=’mps:0′)
Note: On Mac M1, device is “mps”, not “cuda”.
You’re done! ! !
If you have any questions, you can discuss them together~~