详情页标题前

腾讯TencentOS ServerTencentOS Server 3.1 安装 pytorch 及运行 AI 相关模型

详情页1

环境准备

GPU 机型要求

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

腾讯TencentOS ServerTencentOS Server 3.1 安装 pytorch 及运行 AI 相关模型



Python 3 版本要求

必须为 3.8 及以上版本,检查方法如下:

[root@VM-0-21-tencentos ~]# python3 -VPython 3.8.16[root@VM-0-21-tencentos ~]# pip3 -Vpip 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 torchtorch                         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 StableDiffusionPipelineimport 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 脚本。

腾讯TencentOS ServerTencentOS Server 3.1 安装 pytorch 及运行 AI 相关模型

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

腾讯TencentOS ServerTencentOS Server 3.1 安装 pytorch 及运行 AI 相关模型



百川 13B 对话模型

网站地址:https://modelscope.cn/models/baichuan-inc/Baichuan-13B-Chat/summary说明:该网站为国内模型网站,下载速度较快。1. 安装依赖的软件包

pip3 install modelscopepip3 install pip --upgrade      #运行过程中会失败,需要升级pip3 install sentencepiece

2. 运行如下 Python 脚本。

import torchfrom modelscope import snapshot_download, Modelmodel_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)

模型运行结果如下:

腾讯TencentOS ServerTencentOS Server 3.1 安装 pytorch 及运行 AI 相关模型

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

腾讯TencentOS ServerTencentOS Server 3.1 安装 pytorch 及运行 AI 相关模型



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 installgit 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

下载过程可能较慢,其中有三个大文件,需要耐心等待。

腾讯TencentOS ServerTencentOS Server 3.1 安装 pytorch 及运行 AI 相关模型



3. 运行如下脚本:

from diffusers import StableDiffusionPipelineimport torchmodel_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")

运行结果如下:

腾讯TencentOS ServerTencentOS Server 3.1 安装 pytorch 及运行 AI 相关模型


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

腾讯TencentOS ServerTencentOS Server 3.1 安装 pytorch 及运行 AI 相关模型


注意:如果出现类似如下报错,说明 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 byPyTorch) If reserved memory is >> allocated memory try setting max_split_size_mb to avoid fragmentation.  See documentation for Memory Management



官网1折活动,限时活动,即将结束,速速收藏
同尘科技腾讯云授权服务中心。
购买腾讯云产品享受折上折,更有现金返利。同意关联立享优惠

转转请注明出处:https://www.yunxiaoer.com/146326.html

(0)
上一篇 2023年12月9日 上午11:05
下一篇 2023年12月9日 上午11:05
详情页2

相关推荐

  • 腾讯云CVM服务器关于不再支持 SUSE 商业版镜像公告

    腾讯云将于2022年1月1日起,停止对 SUSE 商业版公有镜像及对应认证服务的新增支持。版本如下:SUSE Linux Enterprise Server 12 SP3SUSE Linux Enterprise Server 12SUSE Linux Enterprise Server 10如存量用户希望继续使用公有 SUSE 镜像,请联系 SUSE 客户…

    腾讯云 2023年12月9日
  • DMS的这个实例里面这个报错是啥意思?看着左侧的表搜索不到。-云小二-阿里云

    DMS的这个实例里面这个报错是啥意思?看着左侧的表搜索不到。搜索表失败(以下为数据库报错):[174e164900403000][11.119.7.242:3154][cloud_siem_pre]ERR-CODE: [TDDL-4614][ERR_EXECUTE_ON_MYSQL] Error occurs when execute on GROUP &#…

    2024年1月9日
  • 阿里云日志服务SLS多目标Logstore数据分发-云淘科技

    本文介绍多目标Logstore数据分发的各种场景(动态分发、跨账号分发、跨账号动态分发、复杂场景动态分发等)及操作步骤。 背景信息 日志服务数据加工功能支持将加工结果分发到多个目标Logstore中。您可以在保存数据加工结果时,设置不同账号的AccessKey信息,将加工结果分发到不同账号的Logstore中;您也可以使用e_output函数、e_coutp…

    2023年12月10日
  • 信息流广告,信息流部分建议宽度830px,只针对默认列表样式,顺序随机
  • 腾讯云容器服务采集超级节点上的 Pod 日志同尘科技

    本文主要介绍 TKE 集群中调度至超级节点的 Pod 如何采集日志,包括:采集日志至 CLS采集日志至 Kafka 采集日志至 CLS 服务角色授权 在采集超级节点上的 Pod 日志至 CLS 之前,需要进行服务角色授权,以保证将日志正常上传到 CLS。操作步骤如下:1. 登录访问管理控制台 > 角色。2. 在角色页面单击新建角色。3. 在选择角色载体…

    2023年12月9日
  • 腾讯云TDSQL-C MySQL版创建数据库

    TDSQL-C MySQL 版支持通过控制台的数据库管理页创建数据库,并支持对数据库授权账号访问。本文为您介绍数据库账号权限以及如何通过控制台创建数据库。 操作步骤 1. 登录 TDSQL-C MySQL 版控制台,在集群列表,找到目标集群,单击集群 ID 或操作列的管理,进入集群管理页面。2. 在集群管理页面,选择数据库管理页,单击创建数据库。 3. 在…

    2023年12月9日

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
本站为广大会员提供阿里云、腾讯云、华为云、百度云等一线大厂的购买,续费优惠,保证底价,买贵退差。