A卡2023最新AI画图教程:3A主机安装ROCm运行Stable Diffusion画图

目录

硬件平台:3A主机(内存16G)

显卡:AMD RX6700 XT 12GB 华擎幻影电竞
处理器:AMD R5 3500X 6C6T
主板: 华硕TUF B450M-PRO GAMING

  • 安装Ubuntu22.04.2 LTS系统

更换系统源、安装基本环境

安装git、vim

1. 首先备份一下源列表文件

cp /etc/apt/sources.list /etc/apt/sources.list.bak

2.然后编辑修改为清华源

sudo gedit sources.list

——————-删掉原来全部内容,把下面这段复制进去,最后记得保存!!!—————————————-

# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse

# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
# # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse

deb http://security.ubuntu.com/ubuntu/ jammy-security multiverse main universe restricted
# deb-src http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
# # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates multiverse main universe restricted
3.最后关闭文本编辑器,执行以下3个命令:
sudo apt-get update

sudo apt-get upgrade

sudo apt-get install wget git vim python3-pip python3-venv
4.Ubuntu修改pypi源为豆瓣(强烈建议用豆瓣,如果你用其他源–例如清华源,曾出现过 找不到gfpgan等核心依赖的情况),复制以下整行命令,执行即可
sudo mkdir ~/.pip&&sudo touch ~/.pip/pip.conf&&echo -e '[global]\nindex-url=https://pypi.douban.com/simple\ntimeout = 6000\n[install]\ntrusted-host=pypi.douban.com\n#disable-pip-version-check = true' > ~/.pip/pip.conf

安装对应Ubuntu 22.04版本的A卡驱动(ROCm=5.6)

wget https://repo.radeon.com/amdgpu-install/5.6.1/ubuntu/jammy/amdgpu-install_5.6.50601-1_all.deb

sudo apt-get install ./amdgpu-install_5.6.50601-1_all.deb

—————注意!!!如果你是其他Ubuntu系统版本,需要自己根据版本号,去这个链接找:http://repo.radeon.com/amdgpu-install/

安装Rocm

1.安装Rocm和rocm-smi-lib【需要使用其他包的可以用这个命令查出来有哪些包可以装sudo amdgpu-install –list-usecase】
sudo amdgpu-install --no-dkms
sudo apt-get install rocm-smi-lib
2.添加调用权限
sudo usermod -a -G video $LOGNAME 
sudo usermod -a -G render $LOGNAME
3.使用以下3个命令检查ROCm环境是否安装成功
/opt/rocm/bin/rocm-smi

/opt/rocm/bin/rocminfo

/opt/rocm/opencl/bin/clinfo
4.将ROCm路径加入系统PATH环境变量,执行以下命令
echo 'export PATH=$PATH:/opt/rocm/bin:/opt/rocm/profiler/bin:/opt/rocm/opencl/bin/x86_64' | sudo tee -a /etc/profile.d/rocm.sh
5.至此ROCm基础环境就算安装完成了。可以使用一下命令查看ROCm版本:
apt show rocm-libs -a  #查看ROCm版本
watch -n 1 /opt/rocm/bin/rocm-smi   #实时显卡监控
6.添加调用权限
sudo usermod -a -G video $LOGNAME 
sudo usermod -a -G render $LOGNAME
7.使用以下命令可以让你以后为系统新添加的用户都有 GPU 访问权:
echo 'ADD_EXTRA_GROUPS=1' | sudo tee -a /etc/adduser.conf echo 'EXTRA_GROUPS=video' | sudo tee -a /etc/adduser.conf

开启A卡高性能模式(可选)

/opt/rocm/bin/rocm-smi --setperflevel high

————注意:这将会让你的卡保持高频率运转,如果你短期不想画图了,可以设为auto,更省电

/opt/rocm/bin/rocm-smi --setperflevel auto

开启CPU高性能模式(可选)

sudo apt install cpupower-gui

主角登场,开始安装Stable Diffusion Webui

git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui
1.查看安装脚本 (不懂代码的可以跳过,直接跳到下面第4步)
less webui.sh
2.这段脚本的作用是自动帮你导入HSA_OVERRIDE_GFX_VERSION变量(别执行,仅分析和讲解)
# Check prerequisites gpu_info=$(lspci 2>/dev/null | grep VGA) case "$gpu_info" in    *"Navi 1"*|*"Navi 2"*) export HSA_OVERRIDE_GFX_VERSION=10.3.0    ;;    *"Renoir"*) export HSA_OVERRIDE_GFX_VERSION=9.0.0        printf "\n%s\n" "${delimiter}"
3.然后它自动帮你安装torch==2.0.1+rocm5.4.2(别执行,仅分析和讲解)
if echo "$gpu_info" | grep -q "AMD" && [[ -z "${TORCH_COMMAND}" ]] then    export TORCH_COMMAND="pip install torch==2.0.1+rocm5.4.2 torchvision==0.15.2+rocm5.4.2 --index-url https://download.pytorch.org/whl/rocm5.4.2" fi
4.进入stable-diffusion-webui项目文件夹
cd stable-diffusion-webui/
5.创建venv环境
python3 -m venv venv

启动吧! stable-diffusion-webui

./webui.sh
6.如果出现类似下面的报错,重试执行./webui.sh即可,直到成功为止(有科学上网条件的同学应该不会遇到此问题)
RuntimeError: Couldn't clone K-diffusion. Command: "git" clone "https://github.com/crowsonkb/k-diffusion.git" "/home/terryjay/SD/stable-diffusion-webui/repositories/k-diffusion" Error code: 128 stderr: 正克隆到 '/home/terryjay/SD/stable-diffusion-webui/repositories/k-diffusion'... fatal: 无法访问 'https://github.com/crowsonkb/k-diffusion.git/':Failed to connect to github.com port 443 after 130035 ms: 连接超时`
7.完成了即可看到这样的输出
No checkpoints found. When searching for checkpoints, looked at: - file /home/terryjay/SD/stable-diffusion-webui/model.ckpt - directory /home/terryjay/SD/stable-diffusion-webui/models/Stable-diffusion Can't run without a checkpoint. Find and place a .ckpt or .safetensors file into any of those locations. The program will exit. Running on local URL:  http://127.0.0.1:7860 To create a public link, set `share=True` in `launch()`. Startup time: 4.6s (import torch: 0.7s, import gradio: 1.3s, import ldm: 1.0s, other imports: 0.5s, list SD models: 0.2s, load scripts: 0.4s, create ui: 0.3s).
8.此时环境OK,但还没有模型,你需要下载模型:
1. 下载的是checkpoint (缩写ckpt) 或safetensors 类型的文件,都放入“stable-diffusion-webui\models\Stable-diffusion”中

2. LoRA类型放入“stable-diffusion-webui\models\Lora”中

3. hypernetwork类型放在“stable-diffusion-webui\models\hypernetworks”中
9.另外,插件相关的:
controlnet放在“/stable-diffusion-webui/extensions ”中
10.放好你的模型后即可进入http://127.0.0.1:7860开始你的作图之旅~

如果使用SD的过程中发现还是有环境问题,可先手动验证pytorh库:

  1. 在你的stable-diffusion-webui\目录下,进入python3 env解释器环境:
. ./venv/bin/python3
  1. 复制执行下面的torch代码:
import torch
torch.cuda.is_available()

3.安装SSH服务,以便在其他设备上登录管理Ubuntu:

安装SSH:
    sudo apt-get install openssh-server
启动SSH服务:
    sudo service ssh start
检查SSH服务已经启动
    sudo service ssh status
在其他电脑上登录该Ubuntu(用户名和IP需要根据实际情况修改)
    ssh 用户名@192.168.*.*

全文完

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
扎眼的阳光的头像扎眼的阳光普通用户
上一篇 2023年12月11日
下一篇 2023年12月11日

相关推荐