大語言模型快速入門(理論學習與微調實戰)
請使用requirements.txt文件進行Python 依賴包安裝:
pip install -r requirements.txt通常,GPU 驅動和CUDA 版本都是需要滿足安裝的PyTorch 和TensorFlow 版本。
大多數新發布的大語言模型使用了較新的PyTorch v2.0+ 版本,Pytorch 官方認為CUDA 最低版本是11.8 以及匹配的GPU 驅動版本。詳情見Pytorch官方提供的CUDA 最低版本要求回复。
簡而言之,建議直接安裝當前最新的CUDA 12.3 版本,詳情見Nvidia 官方安裝包。
安裝完成後,使用nvidia-smi指令查看版本:
nvidia-smiFri Mar 1 11:16:55 2024
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 529.08 Driver Version: 529.08 CUDA Version: 12.0 |
| -------------------------------+----------------------+----------------------+
| GPU Name TCC/WDDM | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
| ===============================+======================+====================== |
| 0 NVIDIA GeForce ... WDDM | 00000000:01:00.0 Off | N/A |
| N/A 45C P8 6W / 30W | 0MiB / 4096MiB | 0% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
| ============================================================================= |
+-----------------------------------------------------------------------------+為了使用OpenAI API,你需要從OpenAI控制台獲取一個API密鑰。一旦你有了密鑰,你可以將其設置為環境變量:
對於基於Unix的系統(如Ubuntu或MacOS),你可以在終端中運行以下命令:
export OPENAI_API_KEY= '你的-api-key '對於Windows,你可以在命令提示符中使用以下命令:
set OPENAI_API_KEY=你的-api-key關於requirements,大家可以視情況下載
pip install -r requirements.txt開發環境搭建包含幾個部分
Miniconda 是一個Python 環境管理工具,可以用來創建、管理多個Python 環境。它是Anaconda 的輕量級替代品,不包含任何IDE 工具。 Miniconda可以從官網下載安裝包。也可以從鏡像網站下載:
# 下载 Miniconda 安装包
$ wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-latest-Linux-x86_64.sh
# 也可以使用curl命令下载
$ curl -O https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-latest-Linux-x86_64.sh
# 安装 Miniconda
$ bash Miniconda3-latest-Linux-x86_64.sh安裝過程中,需要回答一些問題,如安裝路徑、是否將Miniconda 添加到環境變量等。安裝完成後,需要重啟終端,使環境變量生效。
可以使用以下命令來驗證Miniconda 是否安裝成功:
$ conda --versionMiniconda的配置文件存放在~/.condarc,可以參考文檔手工修改,也可以使用conda config命令來修改。
# 配置清华镜像
$ 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/conda-forge/
$ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
$ conda config --set show_channel_urls yes
# 查看~/.condarc配置
$ conda config --show-sources
channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
- defaults
show_channel_urls: True # 安装mamba
$ conda install -n base -c conda-forge mamba
# 安装micromamba
$ conda install -n base -c conda-forge micromamba之後可以使用mamba或者micromamba命令代替conda命令。
# 创建虚拟环境,指定 Python 版本为 3.11
(base) $ conda create -n transformers python=3.11
# 激活 openai 环境
$ conda activate transformers以下若無特殊說明,均在這里新建的openai環境中進行。
Jupyter Lab 是一個交互式的開發環境,可以在瀏覽器中運行。它支持多種編程語言,包括Python、R、Julia 等。 Jupyter Lab由conda-forge提供,請先配置鏡像,然後使用以下命令安裝:
(transformers) $ conda install jupyterlabHugging Face Transformers 是一個基於PyTorch 和TensorFlow 的自然語言處理工具包,提供了大量預訓練模型,可以用來完成多種NLP 任務。 Hugging Face Transformers 可以通過conda 安裝:
(transformers) $ conda install -c huggingface transformers安裝文檔:Hugging Face Transformers
Transformers需要使用tensorflow進行實際的模型推理,以下命令安裝tensorflow的CPU和GPU版本:
(transformers) $ pip install tensorflow若是使用Mac,對M1/M2芯片可以安裝Metal插件,一些小一些的模型也可以嘗試:
(transformers) $ pip install tensorflow-metal安裝文檔:
Transformers需要使用pytorch進行實際的模型推理,在前面已經配置了使用的pytorch和conda-forge鏡像源,可以使用下命令安裝和CUDA版本對應的Pytorch版本:
# Linux
# CUDA 11.8
(transformers) $ conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c nvidia
# CUDA 12.1
(transformers) $ conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c nvidia
# Mac
(transformers) $ conda install pytorch::pytorch torchvision torchaudio安裝文檔:pytorch
在處理圖像、音頻等數據時,需要使用到其他的依賴包,包括:
(transformers) $ conda install tqdm iprogress ffmpeg ffmpeg-python pillow祝學習進步