下载Huggingface Transformer 在苹果M1芯片上 (保姆教程,我可以,你也可以)

文章的reference:

https://developer.apple.com/metal/tensorflow-plugin/

https://www.youtube.com/watch?v=17gDhXU55oU 和 https://medium.com/@faizififita1/huggingface-installation-on-apple-silicon-2022-m1-pro-max-ultra-m2-9c449b9b4c14

芯片: arm64 : Apple silicon

我自建的envname: (ml_gpu_env)

======================= 华丽丽的分割线 =======================

前提要求: Step by step: https://developer.apple.com/metal/tensorflow-plugin/

前提1. Install wget - 通过homebrew

brew install wget

前提2. Download Conda environment

Miniconda3 will now be installed into this location: /Users/name/miniconda3

wget https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-arm64.sh -O ~/miniconda.sh
bash ~/miniconda.sh -b -p $HOME/miniconda
source ~/miniconda/bin/activate

结束。

题外话:

List all conda environments

conda env list

Activate an conda environment (举例)

conda activate /Users/name/anaconda3/envs/ml_env

Remove an environment with name torch-gpu

conda env remove --name torch-gpu

======================= 华丽丽的分割线 =======================

0. 打开有Rosetta的Terminal

arch

output: i386 (用了Rosetta的Terminal)
output: arm64(没用Rosetta的Terminal)
  1. 下载Rust - in i386 Terminal (from Rust's website)

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Proceed with 1) default

题外话:

Check rust is installed

which rustc

output: /Users/name/.cargo/bin/rustc (那就是有了)

Uninstall rust

rustup self uninstall

  1. 建立和激活conda environment with python version 3.8.x

conda create --name ml_env python=3.8
conda activate ml_env

(我的是python 3.8.8)

题外话:

To activate an environment with name ml_env

conda activate ml_env

To deactivate an active environment

conda deactivate

  1. Install Tensorflow

(前提必须要做,要不dependencies下载不了)

Install dependencies

conda install -c apple tensorflow-deps

Install base Tensorflow

python -m pip install tensorflow-macos

Install Tensorflow-metal plug-in

(Allow Tensorflow leverage GPU on chip)

python -m pip install tensorflow-metal

  1. Install PyTorch

conda install pytorch torchvision torchaudio -c pytorch-nightly

Verify installs (在ml_env的conda environment)

python
import torch, tensorflow as tf

# Both should return 'True'
print(torch.backends.mps.is_available())
print(torch.backends.mps.is_built())

# Should recognize 1 GPU
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))

  1. Install Tokenizers - HuggingFace

conda install -c conda-forge tokenizers=0.11.1
  1. Install Transformers - HuggingFace

conda install -c huggingface transformers
  1. Verify that all working

python -c "from transformers import pipeline; print(pipeline('sentiment-analysis')('we love you'))"

output: [{'label': 'POSITIVE', 'score': 0.9998704791069031}] (成功了!!!!!)

祝贺你!!!!!成功完成了!!!!

有需要我可以出视频教程。再见啦!

你可能感兴趣的:(tensorflow,pytorch,macos)