win11+wsl2+dockerDesktop,使用intelArc77016G部署推理stable-diffusion的详细步骤

由于本地环境的原因,Aloereed的自动脚本总是执行不成功,懒得调了,所以参照网上大佬们的博客自己走了一遍流程
自动脚本的教程网上有不少,这里贴一篇我搜到的:stable diffusion本地部署教程

总的来说,就是在win11上安装显卡驱动,在docker里拉取intel官方配置好环境的镜像,然后把绘图模型拉下来,最后部署推理模型。部署jupyter是为了调测自己的代码。使用wsl是因为docker是之前安装好的,其实不需要wsl也可以

具体步骤:

1. win11 安装Arc显卡驱动, 已安装,步骤省略

2. 安装wsl2 ,已安装,步骤省略

#在cmd/powershell中查看wsl版本
wsl -l -v

3. 安装基于WSL2的 windows Docker,已安装,步骤省略

4. 在宿主机新建一个目录,用于容器挂载。在目录中下载stable-diffusion模型

mkdir c:\data
cd c:\data
#安装lfs,先拉取代码,后面再拉取大文件
git lfs install --skip-smudge
#从git上拉取
git clone https://huggingface.co/runwayml/stable-diffusion-v1-5
#git配置socket代理地址
git config --global http.https://github.com.proxy socks5://127.0.0.1:7890
#拉取大文件
git lfs pull
#重试
git lfs install --force

5. 运行官方镜像启动一个容器,留了一个端口给Jupter Notebook开发,另一个用于ssh连接控制

Arc独显用户理论上应该使用gpu这个镜像,但也可以试试latest。镜像中已经包含了OneAPI Toolkit等环境依赖,比较方便。

docker run -it --device /dev/dxg -v /usr/lib/wsl:/usr/lib/wsl -v c:\info:/info -v g:\data:/data -p 9999:9999 -p 9922:22  intel/intel-extension-for-pytorch:gpu

6. 在dockerDesktop的容器终端中,给apt换源,安装sshd服务并配置root用户远程密码登录

#备份apt源配置文件
cp /etc/apt/sources.list /etc/apt/sources.list.bak

换源:

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 https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports 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

sources.list.d/deadsnakes-ubuntu-ppa-jammy.list :

deb https://launchpad.proxy.ustclug.org/deadsnakes/ppa/ubuntu/ jammy main

安装sshd服务:

apt update
apt-get install openssh-server
#确认sshserver是否启动了
ps -e |grep ssh
如果没有启动,手动启动
/etc/init.d/ssh start

修改配置文件 /etc/ssh/sshd_config

#PermitRootLogin prohibit-password
改为:
PermitRootLogin yes

重启sshd服务

service ssh restart

初始化root用户密码

passwd root

安装编辑器(习惯用nano)

apt install nano

配置自动启动sshd

nano ~/.bashrc
#在最后一行添加
service ssh start

ssh连接配置:

url: 127.0.0.1
port: 9922
userName: root
passWord: root

7. 通过python命令行检查docker是否连接到A770显卡

python -c "import torch;import intel_extension_for_pytorch as ipex;print(ipex.xpu.get_device_name(0))"

8. 通过声明的9999端口启动Jupyter Notebook远程连接

生成配置文件,使用脚本自动启动:

cd c:/data
pip install -q jupyter
# 生成配置文件,使用脚本自动启动
jupyter notebook --generate-config --allow-root
配置文件生成在 ~/.jupyter/jupyter_notebook_config.py
生成用于登录的密码
jupyter notebook password
密码:root

编辑启动脚本:
nano startjupyter.sh
chmod u+x startjupyter.sh

文件内容:
#!/bin/bash
jupyter notebook --allow-root --ip 0.0.0.0 --port 9999 &

配置自动启动:
nano ~/.bashrc
#在最后一行添加
service ssh start

10. 用浏览器打开Jupyter给的URL来进行开发。运行SD模型

pip install -q diffusers transformers accelerate

简单的样例代码:

import intel_extension_for_pytorch as ipex

import torch

from diffusers import StableDiffusionPipeline

# check Intel GPU

print(ipex.xpu.get_device_name(0))

# load the Stable Diffusion model

pipe = StableDiffusionPipeline.from_pretrained("./stable-diffusion-v1-5", revision="fp16", torch_dtype=torch.float16)

# move the model to Intel Arc GPU

pipe = pipe.to("xpu")

# model is ready for submitting queries

pipe("an astronaut riding a horse on mars").images[0]

# run another query

pipe("cat sitting on a park bench").images[0]

注意:如果不是特别熟悉wsl、ubuntu,配置环境,且擅长处理包冲突的问题,就不要更新容器里的包了。一定要更新的话,记得一定要提前保存下容器,不然会有各种奇怪的问题,消耗你大量时间!

AI绘画Prompt提示词

可以参考以下网站:

ArtHubLexica

参考的博客和代码:

https://github.com/Aloereed/stable-diffusion-webui-ipex-arc
https://huggingface.co/runwayml/stable-diffusion-v1-5/tree/main
https://blog.csdn.net/coroutines/article/details/109325210
https://ericclose.github.io/git-proxy-config.html
http://kerwenzhang.github.io/git/2020/04/14/Git-LFS-issue/
https://blog.csdn.net/qq_41699621/article/details/103064684
https://blog.csdn.net/winter2121/article/details/119892416

### 配置 Windows 11WSL2 的 Ubuntu 发行版 Visual Studio Code 的集成 要在 Windows 11 系统中实现 WSL2 的 Ubuntu 发行版 Visual Studio Code 的无缝集成,可以按照以下方法完成: #### 1. 安装必要的软件 确保已安装最新版本的 Visual Studio Code 和适用于 Linux 的扩展包。通过 Microsoft Store 或官方文档指南安装 WSL2 并启用其支持功能[^4]。 #### 2. 启动 WSL2 中的 Ubuntu 在 PowerShell 或命令提示符中运行 `wsl --install` 命令来初始化 WSL2 及默认发行版(通常是 Ubuntu)。如果已经安装了特定版本如 Ubuntu 18.04,则无需重新安装[^1]。 #### 3. 设置 VSCode 远程开发环境 - 打开 VSCode,进入 Extensions Marketplace (`Ctrl+Shift+X`) 搜索 “Remote – WSL”,然后点击安装该插件。 - 安装完成后重启应用程序以应用更改。 #### 4. 使用 VSCode 打开 WSL 文件夹 可以通过两种方式启动基于 WSL 的项目工作区: - 方法一:按快捷键组合 `Ctrl+K`, 接着按下 `Ctrl+W` 来切换到 WSL 工作空间; - 方法二:直接从终端内部执行命令 `code .` 将当前目录加载至编辑器实例内[^2]。 #### 5. 调试 C/C++ 应用程序 为了能够在 WSL 下顺利编译并调试 C/C++ 类型的应用程序,请先确认 GCC 编译工具链已被正确预设好。接着参照官方说明调整 launch.json 参数配置文件以便适配目标平台特性需求[^5]。 ```json { "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/a.out", "args": [], "stopAtEntry": false, "cwd": "${fileDirname}", "environment": [], "externalConsole": true, "MIMode": "gdb", "miDebuggerPath": "/usr/bin/gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "build" } ] } ``` 上述 JSON 片段定义了一个基本的 GDB 调试会话模板,其中包含了若干重要字段解释如下表所示: | 字段名 | 描述 | |------------------|----------------------------------------------------------------------| | name | 显示于 UI 列表中的名称 | | type | 表明使用的调试协议 | | request | 请求模式 | | program | 待调试可执行文件路径 | | args | 提供给被测程序的参数数组 | | stopAtEntry | 是否暂停于入口处 | | cwd | 当前工作目录 | | environment | 自定义环境变量 | | externalConsole | 控制台显示选项 | | MIMode | MI(Machine Interface)模式下的调试器类型 | | miDebuggerPath | 对应调试器的实际位置 | #### 注意事项 当遇到某些特定场景比如 GPU 加速计算框架 PyTorch CUDA 支持时可能还需要额外处理驱动兼容性和库链接等问题。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

生命不止折腾不停

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值