安裝
// 首先,創建一個虛擬環境 conda create -n my_paddle python=3.8 // 安裝Paddle GPU版 // 沒有加版本號會自動下載最新版 pip install paddlepaddle-gpu // 安裝PaddleNLP pip install paddlenlp
例子:
以官方的Example: sentence_transformers為例
// 下載源碼 git clone https://github.com/PaddlePaddle/PaddleNLP.git // 進入項目根目錄 cd PaddleNLP/examples/text_matching/sentence_transformers // 運行 // 先指定gpu 編號, 從0開始的 export CUDA_VISIBLE_DEVICES=2 // 訓練 由於只有一個GPU可見,默認用可用列表中的第一個 python train.py --device gpu --save_dir ./checkpoint
// 預測
python predict.py --device gpu --params_path checkpoint/model_9900/model_state.pdparams
paddle還支持多gpu並行訓練,使用 paddle.distributed.launch
paddle.distributed.launch
啟動多卡訓練時,設置 --log_dir
參數會將每張卡的日志保存在設置的文件夾下。
// 訓練 python -m paddle.distributed.launch train.py --device gpu --save_dir ./checkpoint // 預測 python -m paddle.distributed.launch predict.py --device gpu --params_path checkpoints/model_9900/model_state.pdparams
用的默認的 ernie-tlny
模型和 LCQMC
數據集,
離譜,每次預測結果都不相同xs
最后記錄一下GPU的使用
為了不讓自己霸占所有的GPU,要手動指定GPU
先看看GPU使用情況: nvidia-smi<c/ode>
設置GPU可見情況: export CUDA_VISIBLE_DEVICES=2
或者多個 export CUDA_VISIBLE_DEVICES=0,2
等等
查看CUDA_VISIBLE_DEVICES: echo $CUDA_VISIBLE_DEVICES
用pytorch查看可用的gpu列表:
>>> import torch >>> available_gpus = [torch.cuda.device(i) for i in range(torch.cuda.device_count())] >>> available_gpus [<torch.cuda.device object at 0x7f2585882b50>] # 獲取可用數量 print(torch.cuda.device_count())
監控系統資源:htop
,比top
更直觀一點
監控GPU使用情況: watch -n 1 nvidia-smi
,這里是1s打印一次
參考鏈接:
1. stack overflow_Change default GPU in TensorFlow