上云无忧 > 文档中心 > 腾讯云 TencentOS Server 3.1 安装 pytorch 及运行 AI 相关模型
TencentOS Server
腾讯云 TencentOS Server 3.1 安装 pytorch 及运行 AI 相关模型

文档简介:
环境准备: GPU 机型要求: 购买 GPU 机型时,根据需求选择下图所示的 GPU 驱动版本、CUDA 版本、cuDNN 版本。
*此产品及展示信息均由腾讯云官方提供。免费试用 咨询热线:400-826-7010,为您提供专业的售前咨询,让您快速了解云产品,助您轻松上云! 微信咨询
  免费试用、价格特惠

环境准备

GPU 机型要求

购买 GPU 机型时,根据需求选择下图所示的 GPU 驱动版本、CUDA 版本、cuDNN 版本。

Python 3 版本要求

必须为 3.8 及以上版本,检查方法如下:
		
[root@VM-0-21-tencentos ~]# python3 -V
Python 3.8.16
[root@VM-0-21-tencentos ~]# pip3 -V
pip 19.3.1 from /usr/lib/python3.8/site-packages/pip (python 3.8)
[root@VM-0-21-tencentos ~]#
如果 Python 版本不满足要求,请按照如下步骤操作:
1. 安装 Python 3.8。
		
yum install -y python3.8
2. 配置 Python 3.8 为默认的 Python 3 版本。
		
cd /usr/bin/ && rm /usr/bin/python3 && ln -s python3.8 python3
3. 配置 pip 3.8 为默认的 pip 3 版本。
		
cd /usr/bin/ && rm /usr/bin/pip3 && ln -s pip3.8 pip3

安装 pytorch

您可以执行如下命令安装 pytorch。
		
pip3 install torch==1.12
安装 pytorch 后,您也可以执行如下命令检查 pytorch 是否安装成功。
		
[root@VM-0-21-tencentos ~]# pip3 list | grep torch
torch 1.12.0
[root@VM-0-21-tencentos ~]#

典型模型示例

Stable Diffusion

网站地址:https://huggingface.co/runwayml/stable-diffusion-v1-5
说明:
该网站为国外网站,下载速度可能较慢,取决于您的网络性能。
1. 安装依赖的软件包。
		
pip3 install diffusers transformers
2. 将如下 Python 代码保存为 Python 脚本。假设脚本名称为:stable_diffusion.py。
		
from diffusers import StableDiffusionPipeline
import torch
model_id = "runwayml/stable-diffusion-v1-5"
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
pipe = pipe.to("cuda")
prompt = "a photo of an astronaut riding a horse on mars"
image = pipe(prompt).images[0]
image.save("astronaut_rides_horse.png")
3. 运行上一步保存的 Python 脚本。

上图展示的是等待下载模型所需要的资源,最后会在当前目录生成一张图片:astronaut_rides_horse.png。
训练结果如下:

百川 13B 对话模型

网站地址:https://modelscope.cn/models/baichuan-inc/Baichuan-13B-Chat/summary
说明:
该网站为国内模型网站,下载速度较快。
1. 安装依赖的软件包
		
pip3 install modelscope
pip3 install pip --upgrade #运行过程中会失败,需要升级
pip3 install sentencepiece
2. 运行如下 Python 脚本。
		
import torch
from modelscope import snapshot_download, Model
model_dir = snapshot_download("baichuan-inc/Baichuan-13B-Chat", revision='v1.0.3')
model = Model.from_pretrained(model_dir, device_map="balanced", trust_remote_code=True, torch_dtype=torch.float16)
messages = []
messages.append({"role": "user", "content": "世界上第二高的山峰是哪一座?"})
response = model(messages)
print(response)
模型运行结果如下:

您也可以替换脚本中的提问,例如:北京申奥成功是哪一年?返回结果如下:

openjourney

网站地址:prompthero/openjourney · Hugging Face
该例子使用源码的方式训练
1. 安装软件:
		
yum install git-lfs -y
2. 下载 openjourney 对应的代码。
		
# Make sure you have git-lfs installed (https://git-lfs.com)
git lfs install
git clone https://huggingface.co/prompthero/openjourney
# if you want to clone without large files – just their pointers
# prepend your git clone with the following env var:
GIT_LFS_SKIP_SMUDGE=1
下载过程可能较慢,其中有三个大文件,需要耐心等待。

3. 运行如下脚本:
		
from diffusers import StableDiffusionPipeline
import torch
model_id = "/root/openjourney_code/openjourney" #修改为你本地的目录即可
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
pipe = pipe.to("cuda")
prompt = "retro serie of different cars with different colors and shapes, mdjrny-v4 style"
image = pipe(prompt).images[0]
image.save("./retro_cars.png")
运行结果如下:

运行完毕会生成一张图片,如下图所示:

注意:
如果出现类似如下报错,说明 GPU 显存不够,需要更高配置的 GPU 机型。

			

RuntimeError: CUDA out of memory. Tried to allocate 1.25 GiB (GPU 0; 14.76 GiB total capacity; 12.96 GiB

already allocated; 993.75 MiB free; 12.96 GiB reserved in total by

PyTorch) If reserved memory is >> allocated memory try setting max_split_size_mb to avoid fragmentation. See documenta

相似文档
  • 环境准备: GPU 机型要求: 购买 GPU 机型时,根据需求选择下图所示的 GPU 驱动版本、CUDA 版本、cuDNN 版本。
  • Q:什么是 TencentOS Server ? A:TencentOS Server 是腾讯云推出的一款企业级服务器 Linux 操作系统产品。TencentOS Server 自研内核核心技术,支持主流硬件平台,具备高性能和高可靠性的优点,满足企业级要求的操作系统产品。
  • 操作场景: CentOS 官方计划停止维护 CentOS Linux 项目,CentOS 8及 CentOS 7维护情况如下表格。如需了解更多信息,请参见 CentOS 官方公告。
  • 您可在云上或在本地快速开始使用 TencentOS Server。 云上使用: 您可在创建实例,或重装已有实例操作系统时,选择公共镜像,并选择使用 TencentOS Server 的相应版本。操作详情请参见 创建实例 及 重装系统。
  • 镜像更新记录以发布时间为顺序。 镜像发布按照地域灰度。若创建云服务器时,镜像不是更新记录中的最新版本,则可能是还未发布至该地域。
官方微信
联系客服
400-826-7010
7x24小时客服热线
分享
  • QQ好友
  • QQ空间
  • 微信
  • 微博
返回顶部