Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
280 changes: 280 additions & 0 deletions README-zh_CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,280 @@
<div align="center">

![project hero](https://github.com/invoke-ai/InvokeAI/assets/31807370/6e3728c7-e90e-4711-905c-3b55844ff5be)

# InvokeAI — Intel Arc (XPU) 社区分支

[![discord badge]][discord link] [![latest release badge]][latest release link] [![github stars badge]][github stars link] [![github forks badge]][github forks link]

> **社区分支** — 基于 [InvokeAI](https://github.com/invoke-ai/InvokeAI) 官方仓库,添加 **Intel Arc GPU (XPU)** 支持。
> 使用 PyTorch 2.13+ 原生 XPU 后端,**无需 IPEX**。
> 跟踪上游分支:[invoke-ai/InvokeAI](https://github.com/invoke-ai/InvokeAI) `main`。

[English](./README.md) | **中文**

</div>

---

## ✨ 分支特性

本分支通过 PyTorch 原生 XPU 后端为 InvokeAI 添加 **Intel Arc GPU 加速**,支持在 Intel 独显和核显上运行 Stable Diffusion 推理。

| 类别 | 说明 |
|------|------|
| **`scripts/patch_xpu.py`** | 统一补丁脚本(54 处 `patch()` 调用,覆盖 28 个文件 + pyproject.toml),可对干净的上游代码一键添加 XPU 支持 |
| **`scripts/sync_and_patch.py`** | 自动同步上游:拉取 `origin/main` → 硬重置 → 恢复 fork 文件 → 应用补丁 → 提交 |
| **核心设备处理** | `devices.py`、`model_cache.py`、`session_processor_default.py` 等 — 完整的 `torch.xpu` 设备选择、VRAM 查询、OOM 处理 |
| **显存与统计** | `memory_snapshot.py`、`invocation_stats_default.py`、`dev_utils.py` — XPU 显存占用追踪与显示 |
| **注意力后端** | `attention.py`、`ip_adapter.py`、`flux_ip_adapter.py` — XPU SDPA + 内存操作 |
| **Diffusers 集成** | `diffusers_pipeline.py`、`controlnet_utils.py` — XPU 图设备处理 |
| **量化支持** | `ggml_tensor.py`(GGUF)、`bnb_llm_int8.py`、`bnb_nf4.py` — XPU 量化守卫 |
| **PID/PixelDiT** | `pid_distill_model.py`、`pixeldit_model.py`、`decode.py`、`pipeline_registry.py` — 设备无关推理 |
| **构建配置** | `pyproject.toml` — `[xpu]` 可选依赖组 + UV 索引源 + 冲突声明 |
| **前端** | 系统信息 API (`app_info.py`) + 事件标签 (`events_common.py`) 显示 XPU 设备;完整简体中文翻译 |

## 🖥️ 支持的硬件

| 系列 | 型号 |
|------|------|
| **Arc A 系列** | A770、A750、A730M、A580、A380 等 |
| **Arc B 系列** | B580、B570 等 |
| **Core Ultra 核显** | Meteor Lake、Lunar Lake、Arrow Lake |

> 需要最新 Intel GPU 驱动 + PyTorch 2.13+(原生 XPU 支持,无需 IPEX)

---

## 🚀 快速开始

### 前置条件

1. **Intel GPU 驱动** — 从 [Intel 下载中心](https://www.intel.com/content/www/us/en/download-center/home.html) 获取最新版
2. **Python 3.12**(推荐)
3. **uv 包管理器**(推荐)或 pip

### 安装步骤

```bash
# 1. 克隆本分支
git clone -b intel-gpu-support https://github.com/wesd6r/InvokeAI.git
cd InvokeAI

# 2. 安装 PyTorch XPU
pip install torch torchvision --index-url https://download.pytorch.org/whl/xpu

# 3. 安装 InvokeAI(含 XPU 依赖)
pip install -e ".[xpu]"

# 4. 启动
python scripts/invokeai-web.py
```

### 使用 UV(推荐)

```bash
uv pip install torch torchvision --index-url https://download.pytorch.org/whl/xpu
uv pip install -e ".[xpu]"
```

> **首次启动**会下载基础模型和依赖,请耐心等待。

### 对已有安装添加 XPU 支持

如果你已经有 InvokeAI 源码安装:

```bash
# 在 InvokeAI 源码根目录下运行:
python scripts/patch_xpu.py
```

补丁脚本幂等安全,可重复运行。

---

## 🔄 同步上游更新

本分支跟踪 `invoke-ai/InvokeAI` 的 `main` 分支,同步流程如下:

```
upstream/main ──(sync_and_patch.py)──> intel-gpu-support 分支
├─ git fetch upstream
├─ git reset --hard upstream/main ⚠️ 丢弃本地修改
├─ 恢复 fork 专属文件(patch_xpu.py, sync_and_patch.py, README.md)
├─ 运行 patch_xpu.py 应用全部 XPU 补丁
└─ git commit + push
```

```bash
# 同步并推送到 fork:
python scripts/sync_and_patch.py --push
```

**⚠️ 警告**:`sync_and_patch.py` 会执行 `git reset --hard`,所有未提交的修改将丢失。

---

## 📋 补丁文件清单

<details>
<summary>点击展开完整列表(28 个文件,54 处补丁)</summary>

### 核心设备与显存

| # | 文件 | 补丁数 | 改动说明 |
|---|------|--------|----------|
| 1 | `invokeai/backend/util/devices.py` | 7 | `XPU_DEVICE` 常量、`is_xpu_available()`、`choose_torch_device/dtype/empty_cache`、`choose_bfloat16_safe_dtype`、多 GPU API |
| 2 | `invokeai/app/services/config/config_default.py` | 2 | device 字段接受 `xpu`(sections 2 + 22) |
| 3 | `invokeai/backend/model_manager/load/model_cache/model_cache.py` | 8 | OOM 处理、VRAM 查询(`_get_vram_available`/`_get_vram_in_use`)、缓存淘汰、XPU 日志、import |
| 4 | `invokeai/backend/util/attention.py` | 1 | psutil mem_free 回退 XPU |
| 5 | `invokeai/backend/model_manager/load/memory_snapshot.py` | 1 | XPU `memory_allocated` |
| 6 | `invokeai/app/services/invocation_stats/invocation_stats_default.py` | 4 | `_get_device_vram_allocated` 辅助函数 + 统计显示 |
| 7 | `invokeai/backend/model_manager/load/model_cache/dev_utils.py` | 2 | `_get_device_api` 辅助函数 |
| 8 | `invokeai/backend/stable_diffusion/diffusers_pipeline.py` | 1 | `torch.xpu.mem_get_info()` 显存查询 |
| 9 | `invokeai/app/services/session_processor/session_processor_default.py` | 1 | XPU `set_device` |

### 模型加载与量化

| # | 文件 | 补丁数 | 改动说明 |
|---|------|--------|----------|
| 10 | `invokeai/backend/textual_inversion.py` | 1 | `.to()` 短路修复 |
| 11 | `invokeai/app/invocations/blend_latents.py` | 1 | `empty_cache` → `TorchDevice.empty_cache()` |
| 12 | `invokeai/app/invocations/qwen_image_text_encoder.py` | 1 | `empty_cache` → `TorchDevice.empty_cache()` |
| 13 | `invokeai/backend/image_util/pbr_maps/pbr_maps.py` | 1 | XPU `empty_cache` |
| 14 | `invokeai/backend/quantization/bnb_nf4.py` | 1 | 修复 docstring |
| 15 | `invokeai/backend/quantization/gguf/ggml_tensor.py` | 1 | GGUF dispatch 表添加 XPU |
| 16 | `invokeai/backend/quantization/bnb_llm_int8.py` | 1 | bitsandbytes XPU 守卫 |

### 推理管线

| # | 文件 | 补丁数 | 改动说明 |
|---|------|--------|----------|
| 17 | `invokeai/app/run_app.py` | 1 | CUDA allocator 守卫(非 XPU) |
| 18 | `invokeai/app/invocations/anima_latents_to_image.py` | 2 | XPU OOM 检测 + 分块解码 |
| 19 | `invokeai/backend/image_util/pidi/model.py` | 1 | 设备无关 `FloatTensor` |
| 20 | `invokeai/backend/util/hotfixes.py` | 1 | XPU 禁用 xformers |
| 21 | `invokeai/backend/pid/_src/models/pid_distill_model.py` | 2 | 设备无关推理 |
| 22 | `invokeai/backend/pid/_src/models/pixeldit_model.py` | 1 | `.to("cuda")` → `.to(self.net.device)` |
| 23 | `invokeai/backend/pid/decode.py` | 2 | autocast XPU 支持 |
| 24 | `invokeai/backend/pid/_src/inference/pipeline_registry.py` | 1 | 设备无关 FPD eval 管线 |

### 前端与 API

| # | 文件 | 补丁数 | 改动说明 |
|---|------|--------|----------|
| 25 | `invokeai/backend/util/test_utils.py` | 1 | `torch_device` fixture 添加 XPU |
| 26 | `invokeai/app/api/routers/app_info.py` | 2 | `GenerationDevice` 枚举 + 设备列表 |
| 27 | `invokeai/app/services/events/events_common.py` | 2 | 设备标签检测 |

### 构建配置

| # | 文件 | 补丁数 | 改动说明 |
|---|------|--------|----------|
| 28 | `pyproject.toml` | 5 | `[xpu]` 可选依赖组、UV 冲突声明、torch/torchvision/triton-xpu 源映射、torch-xpu UV 索引 |

### 其他

- **`patch_locale()`**:为 `zh-CN.json` 添加中文翻译补丁(可选)

</details>

---

## 🐛 已知限制

| 问题 | 说明 |
|------|------|
| **fp16 稳定性** | 部分模型在 Intel Arc 上使用 `float16` 可能出噪点,补丁包含 `BF16_FALLBACK` 自动回退逻辑 |
| **VRAM 报告** | `torch.xpu.memory_allocated()` 在部分驱动版本上可能不够精确 |
| **功能覆盖** | ControlNet、IP-Adapter 等功能仅轻度测试 |
| **无安装程序** | 目前仅支持源码安装,无 Windows 安装包 |

## 🧪 测试状态

| 测试项 | 状态 | 详情 |
|--------|------|------|
| SD-1 txt2img (512×512, 15步) | ✅ 通过 | Intel Arc A730M,18 秒,VRAM 2.01 GB |
| XPU 设备识别 | ✅ 通过 | `torch.device("xpu")` 正确检测到 A730M |
| 显存管理 | ✅ 通过 | VRAM 查询、OOM 检测、缓存淘汰正常 |
| 多模型切换 | ✅ 通过 | SD-1 模型加载/卸载循环正常 |

## 🤝 参与贡献

欢迎社区贡献:

1. Fork 本仓库
2. 创建功能分支
3. 提交 PR 到 `intel-gpu-support`

上游 InvokeAI 贡献请访问 [invoke-ai/InvokeAI](https://github.com/invoke-ai/InvokeAI)。

## 📄 许可证

与上游 InvokeAI 相同 — [Apache 2.0](LICENSE)。

## 🙏 致谢

- [InvokeAI](https://github.com/invoke-ai/InvokeAI) — 优秀的 AI 创作引擎
- [PyTorch XPU 后端](https://pytorch.org/docs/stable/notes/get_start_xpu.html) — 原生 Intel GPU 支持

---

<div align="center">

**以下为 InvokeAI 原始 README** — [完整文档](https://invoke.ai) · [Discord](https://discord.gg/ZmtBAhwWhy)

</div>

---

## 原始:Invoke — 专业 AI 视觉创作工具

[Invoke](https://www.invoke.com) is a leading creative engine built to empower professionals and enthusiasts alike. Generate and create stunning visual media using the latest AI-driven technologies. Invoke offers an industry leading web-based UI, and serves as the foundation for multiple commercial products.

- Free to use under a commercially-friendly license
- Download and install on compatible hardware
- Generate, refine, iterate on images, and build workflows

![Highlighted Features - Canvas and Workflows](https://github.com/invoke-ai/InvokeAI/assets/31807370/708f7a82-084f-4860-bfbe-e2588c53548d)

### 模型支持
- SD 1.5、SD 2.0、SDXL、SD 3.5 Medium/Large
- CogView 4、Flux.1 Dev/Schnell/Kontext/Krea、Flux Redux/Fill
- Flux.2 Klein 4B/9B、Z-Image Turbo/Base、Krea 2 Turbo/Raw
- Anima、Qwen Image/Edit、Ideogram 4、ERNIE-Image/Turbo
- 以及更多...

### 其他功能
- 支持 ckpt、diffusers 及部分 gguf 模型
- 图片放大、Embedding 管理、模型管理
- 工作流创建与管理、节点架构
- 目标分割与选择模型(SAM / SAM2)

[features docs]: https://invoke.ai/
[faq]: https://invoke.ai/troubleshooting/faq/
[contributors]: https://invoke.ai/contributing/contributors/
[github issues]: https://github.com/invoke-ai/InvokeAI/issues
[docs home]: https://invoke.ai
[installation docs]: https://invoke.ai/start-here/installation/
[sponsor link]: https://github.com/sponsors/invoke-ai
[#dev-chat]: https://discord.com/channels/1020123559063990373/1049495067846524939
[contributing docs]: https://invoke.ai/contributing/
[CI checks on main badge]: https://flat.badgen.net/github/checks/invoke-ai/InvokeAI/main?label=CI%20status%20on%20main&cache=900&icon=github
[CI checks on main link]: https://github.com/invoke-ai/InvokeAI/actions?query=branch%3Amain
[discord badge]: https://flat.badgen.net/discord/members/ZmtBAhwWhy?icon=discord
[discord link]: https://discord.gg/ZmtBAhwWhy
[github forks badge]: https://flat.badgen.net/github/forks/wesd6r/InvokeAI?icon=github
[github forks link]: https://useful-forks.github.io/?repo=wesd6r%2FInvokeAI
[github open issues badge]: https://flat.badgen.net/github/open-issues/wesd6r/InvokeAI?icon=github
[github open issues link]: https://github.com/wesd6r/InvokeAI/issues?q=is%3Aissue+is%3Aopen
[github open prs badge]: https://flat.badgen.net/github/open-prs/wesd6r/InvokeAI?icon=github
[github open prs link]: https://github.com/wesd6r/InvokeAI/pulls?q=is%3Apr+is%3Aopen
[github stars badge]: https://flat.badgen.net/github/stars/wesd6r/InvokeAI?icon=github
[github stars link]: https://github.com/wesd6r/InvokeAI/stargazers
[latest commit to main badge]: https://flat.badgen.net/github/last-commit/wesd6r/InvokeAI/intel-gpu-support?icon=github&color=yellow&label=last%20commit&cache=900
[latest commit to main link]: https://github.com/wesd6r/InvokeAI/commits/intel-gpu-support
[latest release badge]: https://flat.badgen.net/github/release/wesd6r/InvokeAI/intel-gpu-support?icon=github
[latest release link]: https://github.com/wesd6r/InvokeAI/releases/latest
[translation status badge]: https://hosted.weblate.org/widgets/invokeai/-/svg-badge.svg
[translation status link]: https://hosted.weblate.org/engage/invokeai/
Loading