win10 笔记本电脑安装 pytorch+cuda+gpu 大模型开发环境过程记录
文章部分内容参考 deepseek。
以下使用命令行工具 mingw64。
安装 Anaconda
安装位置:
/c/DEVPACK/Anaconda3
然后安装 Python 3.10.16
(略)
$ conda create -n pytorch_env python=3.10
$ conda activate pytorch_env
安装 CUDA cuDNN
安装 cuda toolkit 会更新系统的显卡驱动。尽管我的 Thinkpad X1 驱动已经是最新,但是仍然比 Nvidia 提供的旧。所以要更新 N 卡驱动。因为 pytorch 支持 12.6,所以我选择 cuda12.6。
下载:
cuda_12.6.3_561.17_windows.exe
cudnn_9.6.0_windows.exe
然后安装位置:
C:\NVIDIA\cuda126
C:\NVIDIA\cudnn9.6
系统环境变量设置如下:
CUDA_PATH=C:\NVIDIA\cuda126
CUDA_PATH_V12_6=C:\NVIDIA\cuda126
Path=C:\NVIDIA\cuda126;C:\NVIDIA\cuda126\bin;C:\NVIDIA\cuda126\libnvvp;C:\NVIDIA\cuda126\lib\x64;C:\NVIDIA\cudnn9.6\bin\12.6;C:\NVIDIA\cudnn9.6\lib\12.6\x64;...
安装 CUDA 时,不要勾选(名字记不清了,大概意思):
Nsight SVE
Cuda Visual Studio 整合
安装完毕测试:
$ nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2024 NVIDIA Corporation
Built on Wed_Oct_30_01:18:48_Pacific_Daylight_Time_2024
Cuda compilation tools, release 12.6, V12.6.85
Build cuda_12.6.r12.6/compiler.35059454_0
$ nvidia-smi
Sat Apr 5 16:43:53 2025
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 561.17 Driver Version: 561.17 CUDA Version: 12.6 |
|-----------------------------------------+------------------------+----------------------+
| GPU Name Driver-Model | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|=========================================+========================+======================|
| 0 NVIDIA GeForce GTX 1650 WDDM | 00000000:01:00.0 Off | N/A |
| N/A 56C P8 4W / 35W | 81MiB / 4096MiB | 0% Default |
| | | N/A |
+-----------------------------------------+------------------------+----------------------+
说明cuda显卡驱动和开发环境安装成功。
安装 pytorch
访问 PyTorch官网 https://pytorch.org/get-started/locally/,选择下图橙色所示部分,注意安装的Python和CUDA版本。
打开 mingw64 命令行工具窗口,运行下面命令:
$ conda config --set show_channel_urls yes
$ conda config --set always_yes yes
# 默认重试次数为 30
$ conda config --set remote_max_retries 30
# 连接超时
$ conda config --set remote_connect_timeout_secs 600
# 读取超时
$ conda config --set remote_read_timeout_secs 1200
$ conda activate pytorch_env
$ pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu126
漫长的等待之后安装成功。
Downloading https://download.pytorch.org/whl/cu126/torch-2.6.0%2Bcu126-cp310-cp310-win_amd64.whl (2496.1 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.5/2.5 GB 236.1 kB/s eta 0:00:00
Downloading https://download.pytorch.org/whl/cu126/torchaudio-2.6.0%2Bcu126-cp310-cp310-win_amd64.whl (4.2 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4.2/4.2 MB 276.7 kB/s eta 0:00:00
Downloading https://download.pytorch.org/whl/fsspec-2024.6.1-py3-none-any.whl (177 kB)
Installing collected packages: sympy, fsspec, torch, torchvision, torchaudio
Attempting uninstall: sympy
Found existing installation: sympy 1.13.3
Uninstalling sympy-1.13.3:
Successfully uninstalled sympy-1.13.3
Attempting uninstall: torch
Found existing installation: torch 2.5.1
Uninstalling torch-2.5.1:
Successfully uninstalled torch-2.5.1
Successfully installed fsspec-2024.6.1 sympy-1.13.1 torch-2.6.0+cu126 torchaudio-2.6.0+cu126 torchvision-0.21.0+cu126
(pytorch_env)
编写一个测试文件:check_pytorch_env.py
import torch
print(torch.__version__) # 查看 PyTorch 版本
print(torch.cuda.is_available()) # 输出 True 表示 CUDA 可用
print(torch.cuda.get_device_name(0)) # 显示 GPU 型号(如 NVIDIA RTX 3090)
运行测试:
$ python check_pytorch_env.py
2.6.0+cu126
True
NVIDIA GeForce GTX 1650
(pytorch_env)
说明 pytorch+cuda+gpu 安装成功。
安装 hugging-face
在安装好 PyTorch 后,安装 Hugging Face 的 transformers 库(核心工具库)和其他相关依赖的步骤如下。
系统环境变量增加:
HF_ENDPOINT=https://hf-mirror.com # 模型下载网站
HF_HOME=D:\huggingface # hf 主目录。下载缓存目录自动在: $HF_HOME/hub
HF_MODELS=D:\huggingface\models # 模型保存的本地目录
HF_HUB_DISABLE_SYMLINKS_WARNING=1
完整安装命令:
$ conda activate pytorch_env
$ pip3 install transformers datasets tokenizers accelerate peft safetensors soundfile librosa Pillow huggingface_hub python-dotenv
说明:
- transformers(模型加载与推理):支持加载预训练模型(如 BERT、GPT、T5)、进行文本生成、分类、翻译等任务。
- datasets(数据集加载与处理):快速加载和处理 NLP、CV、语音等公开数据集(如 GLUE、SQuAD)。
- tokenizers(高性能分词工具):用于文本分词(支持 BPE、WordPiece 等算法),比 transformers 内置分词器更快。
- huggingface_hub:通过镜像站下载模型:huggingface-cli download --resume-download --cache-dir “本地路径” 模型名称
- python-dotenv:读取 .env 保存的环境变量
可选:
- accelerate(分布式训练):加速库。简化多 GPU/TPU 训练代码,支持混合精度训练。
- evaluate:模型评估库。提供标准评估指标(如准确率、F1、BLEU)。
- peft:模型压缩库。支持参数高效微调(如 LoRA、Prefix Tuning)。
- safetensors:模型序列化库。安全高效的模型权重序列化格式,替代传统 pickle。
- soundfile、librosa:音频处理
- Pillow:图像处理
编写一个测试文件:check_huggingface.py
import transformers
from transformers import pipeline
# 应输出版本号(如 4.50.3)
print(transformers.__version__)
# 示例:情感分析管道
classifier = pipeline("sentiment-analysis")
result = classifier("Hugging Face is amazing!")
print(result) # 输出示例:[{'label': 'POSITIVE', 'score': 0.9998}]
运行测试:
$ python check_huggingface.py
报错。因为 https://huggingface.co 无法访问,所以需要使用Hugging Face Pipeline在本地运行模型。模型下载脚本文件:
$HF_HOME/scriptes/download_models.py
import os
import transformers
from transformers import AutoModelForSequenceClassification, AutoTokenizer
hf_models_dir = os.getenv("HF_MODELS")
# 指定模型名称
model_name = "nlptown/bert-base-multilingual-uncased-sentiment"
local_dir = os.path.join(hf_models_dir, "bert-base-multilingual-uncased-sentiment")
print("local_dir=", local_dir)
# 下载模型和分词器
model = AutoModelForSequenceClassification.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
# 保存到本地
model.save_pretrained(local_dir)
tokenizer.save_pretrained(local_dir)
执行上面的脚本: H F _ H O M E / s c r i p t e s / d o w n l o a d _ m o d e l s . p y ,模型下载到本地目录: HF\_HOME/scriptes/download\_models.py,模型下载到本地目录: HF_HOME/scriptes/download_models.py,模型下载到本地目录:HF_MODELS/bert-base-multilingual-uncased-sentiment
情感分析测试 sentiment-analysis.py:
import os
from transformers import pipeline
# D:\huggingface\models
hf_models_dir = os.environ["HF_MODELS"]
print("hf_models_dir=", hf_models_dir)
# 加载本地模型
classifier = pipeline(
"sentiment-analysis",
model = os.path.join(hf_models_dir, "bert-base-multilingual-uncased-sentiment"),
tokenizer= os.path.join(hf_models_dir, "bert-base-multilingual-uncased-sentiment")
)
print("classifier ok")
# 测试文本
test_lines = [
"傻!",
"关税战没有赢家",
"中国国必胜。yeah yeah"
]
for l in test_lines:
result = classifier(l)
print(l, result)
print("test exit ok")
执行:python sentiment-analysis.py