一 环境准备与版本选择
二 安装 PyTorch
pip3 install torch torchvision torchaudiopip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu118conda install pytorch torchvision torchaudio cpuonly -c pytorchconda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia三 验证安装与基础推理
python3 - <<'PY' import torch print("PyTorch version:", torch.__version__) print("CUDA available:", torch.cuda.is_available()) if torch.cuda.is_available(): print("CUDA device:", torch.cuda.get_device_name(0)) PYpython3 - <<'PY' import torch from model import MyModel # 你的模型定义 model = MyModel(); model.load_state_dict(torch.load('model.pth', map_location='cpu')) model.eval(); device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model.to(device) x = torch.randn(1, 3, 224, 224).to(device) with torch.no_grad(): y = model(x) print("Output:", y.shape) PYmodel.to(device) 且输入张量在同一设备;使用 with torch.no_grad(): 关闭梯度以提升性能。四 生产部署与服务化
python3 - <<'PY' from flask import Flask, request, jsonify import torch, json from model import MyModel app = Flask(__name__) model = MyModel(); model.load_state_dict(torch.load('model.pth', map_location='cpu')) model.eval(); device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model.to(device) @app.route('/predict', methods=['POST']) def predict(): data = request.json['input_data'] x = torch.tensor(data, dtype=torch.float32).unsqueeze(0).to(device) with torch.no_grad(): y = model(x) return jsonify(y.cpu().tolist()) if __name__ == '__main__': app.run(host='0.0.0.0', port=5000) PYFROM python:3.10-slimWORKDIR /appCOPY requirements.txt .RUN pip install -r requirements.txtCOPY . .CMD ["gunicorn", "app:app", "-b", "0.0.0.0:5000"]docker build -t my-pytorch-app .docker run -p 5000:5000 my-pytorch-appdocker run --gpus all -p 5000:5000 my-pytorch-app五 常见问题与排障
libopenblas-dev 等)或使用虚拟环境隔离。pip install torch==2.4.0 ...)。torch.__version__、torch.cuda.is_available()、驱动版本与容器是否启用 –gpus。