mirror of
https://github.com/modelscope/DiffSynth-Studio.git
synced 2026-03-19 14:58:12 +00:00
Compare commits
28 Commits
diffsynth-
...
ascend
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7c6905a432 | ||
|
|
2883bc1b76 | ||
|
|
78d8842ddf | ||
|
|
5821a664a0 | ||
|
|
ab9aa1a087 | ||
|
|
e1f5db5f5c | ||
|
|
e316fb717f | ||
|
|
64c5139502 | ||
|
|
5da9611a74 | ||
|
|
733750d01b | ||
|
|
edc95359d0 | ||
|
|
f2d0241e26 | ||
|
|
7b5d7f4af5 | ||
|
|
1fa9a6c60c | ||
|
|
51efa128d3 | ||
|
|
421c6a5fce | ||
|
|
864080d8f2 | ||
|
|
ba372dd295 | ||
|
|
1ceb02f673 | ||
|
|
30f93161fb | ||
|
|
3ee3cc3104 | ||
|
|
c2218f5c73 | ||
|
|
72af7122b3 | ||
|
|
afd101f345 | ||
|
|
1313f4dd63 | ||
|
|
8332ecebb7 | ||
|
|
401d7d74a5 | ||
|
|
b8d7d55568 |
19
README.md
19
README.md
@@ -33,6 +33,8 @@ We believe that a well-developed open-source code framework can lower the thresh
|
|||||||
|
|
||||||
> Currently, the development personnel of this project are limited, with most of the work handled by [Artiprocher](https://github.com/Artiprocher). Therefore, the progress of new feature development will be relatively slow, and the speed of responding to and resolving issues is limited. We apologize for this and ask developers to understand.
|
> Currently, the development personnel of this project are limited, with most of the work handled by [Artiprocher](https://github.com/Artiprocher). Therefore, the progress of new feature development will be relatively slow, and the speed of responding to and resolving issues is limited. We apologize for this and ask developers to understand.
|
||||||
|
|
||||||
|
- **December 9, 2025** We release a wild model based on DiffSynth-Studio 2.0: [Qwen-Image-i2L](https://www.modelscope.cn/models/DiffSynth-Studio/Qwen-Image-i2L) (Image-to-LoRA). This model takes an image as input and outputs a LoRA. Although this version still has significant room for improvement in terms of generalization, detail preservation, and other aspects, we are open-sourcing these models to inspire more innovative research.
|
||||||
|
|
||||||
- **December 4, 2025** DiffSynth-Studio 2.0 released! Many new features online
|
- **December 4, 2025** DiffSynth-Studio 2.0 released! Many new features online
|
||||||
- [Documentation](/docs/en/README.md) online: Our documentation is still continuously being optimized and updated
|
- [Documentation](/docs/en/README.md) online: Our documentation is still continuously being optimized and updated
|
||||||
- [VRAM Management](/docs/en/Pipeline_Usage/VRAM_management.md) module upgraded, supporting layer-level disk offload, releasing both memory and VRAM simultaneously
|
- [VRAM Management](/docs/en/Pipeline_Usage/VRAM_management.md) module upgraded, supporting layer-level disk offload, releasing both memory and VRAM simultaneously
|
||||||
@@ -187,21 +189,7 @@ cd DiffSynth-Studio
|
|||||||
pip install -e .
|
pip install -e .
|
||||||
```
|
```
|
||||||
|
|
||||||
<details>
|
For more installation methods and instructions for non-NVIDIA GPUs, please refer to the [Installation Guide](/docs/en/Pipeline_Usage/Setup.md).
|
||||||
<summary>Other installation methods</summary>
|
|
||||||
|
|
||||||
Install from PyPI (version updates may be delayed; for latest features, install from source)
|
|
||||||
|
|
||||||
```
|
|
||||||
pip install diffsynth
|
|
||||||
```
|
|
||||||
|
|
||||||
If you meet problems during installation, they might be caused by upstream dependencies. Please check the docs of these packages:
|
|
||||||
|
|
||||||
* [torch](https://pytorch.org/get-started/locally/)
|
|
||||||
* [sentencepiece](https://github.com/google/sentencepiece)
|
|
||||||
* [cmake](https://cmake.org)
|
|
||||||
* [cupy](https://docs.cupy.dev/en/stable/install.html)
|
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
@@ -420,6 +408,7 @@ Example code for Qwen-Image is available at: [/examples/qwen_image/](/examples/q
|
|||||||
|[DiffSynth-Studio/Qwen-Image-Blockwise-ControlNet-Inpaint](https://modelscope.cn/models/DiffSynth-Studio/Qwen-Image-Blockwise-ControlNet-Inpaint)|[code](/examples/qwen_image/model_inference/Qwen-Image-Blockwise-ControlNet-Inpaint.py)|[code](/examples/qwen_image/model_inference_low_vram/Qwen-Image-Blockwise-ControlNet-Inpaint.py)|[code](/examples/qwen_image/model_training/full/Qwen-Image-Blockwise-ControlNet-Inpaint.sh)|[code](/examples/qwen_image/model_training/validate_full/Qwen-Image-Blockwise-ControlNet-Inpaint.py)|[code](/examples/qwen_image/model_training/lora/Qwen-Image-Blockwise-ControlNet-Inpaint.sh)|[code](/examples/qwen_image/model_training/validate_lora/Qwen-Image-Blockwise-ControlNet-Inpaint.py)|
|
|[DiffSynth-Studio/Qwen-Image-Blockwise-ControlNet-Inpaint](https://modelscope.cn/models/DiffSynth-Studio/Qwen-Image-Blockwise-ControlNet-Inpaint)|[code](/examples/qwen_image/model_inference/Qwen-Image-Blockwise-ControlNet-Inpaint.py)|[code](/examples/qwen_image/model_inference_low_vram/Qwen-Image-Blockwise-ControlNet-Inpaint.py)|[code](/examples/qwen_image/model_training/full/Qwen-Image-Blockwise-ControlNet-Inpaint.sh)|[code](/examples/qwen_image/model_training/validate_full/Qwen-Image-Blockwise-ControlNet-Inpaint.py)|[code](/examples/qwen_image/model_training/lora/Qwen-Image-Blockwise-ControlNet-Inpaint.sh)|[code](/examples/qwen_image/model_training/validate_lora/Qwen-Image-Blockwise-ControlNet-Inpaint.py)|
|
||||||
|[DiffSynth-Studio/Qwen-Image-In-Context-Control-Union](https://www.modelscope.cn/models/DiffSynth-Studio/Qwen-Image-In-Context-Control-Union)|[code](/examples/qwen_image/model_inference/Qwen-Image-In-Context-Control-Union.py)|[code](/examples/qwen_image/model_inference_low_vram/Qwen-Image-In-Context-Control-Union.py)|-|-|[code](/examples/qwen_image/model_training/lora/Qwen-Image-In-Context-Control-Union.sh)|[code](/examples/qwen_image/model_training/validate_lora/Qwen-Image-In-Context-Control-Union.py)|
|
|[DiffSynth-Studio/Qwen-Image-In-Context-Control-Union](https://www.modelscope.cn/models/DiffSynth-Studio/Qwen-Image-In-Context-Control-Union)|[code](/examples/qwen_image/model_inference/Qwen-Image-In-Context-Control-Union.py)|[code](/examples/qwen_image/model_inference_low_vram/Qwen-Image-In-Context-Control-Union.py)|-|-|[code](/examples/qwen_image/model_training/lora/Qwen-Image-In-Context-Control-Union.sh)|[code](/examples/qwen_image/model_training/validate_lora/Qwen-Image-In-Context-Control-Union.py)|
|
||||||
|[DiffSynth-Studio/Qwen-Image-Edit-Lowres-Fix](https://www.modelscope.cn/models/DiffSynth-Studio/Qwen-Image-Edit-Lowres-Fix)|[code](/examples/qwen_image/model_inference/Qwen-Image-Edit-Lowres-Fix.py)|[code](/examples/qwen_image/model_inference_low_vram/Qwen-Image-Edit-Lowres-Fix.py)|-|-|-|-|
|
|[DiffSynth-Studio/Qwen-Image-Edit-Lowres-Fix](https://www.modelscope.cn/models/DiffSynth-Studio/Qwen-Image-Edit-Lowres-Fix)|[code](/examples/qwen_image/model_inference/Qwen-Image-Edit-Lowres-Fix.py)|[code](/examples/qwen_image/model_inference_low_vram/Qwen-Image-Edit-Lowres-Fix.py)|-|-|-|-|
|
||||||
|
|[DiffSynth-Studio/Qwen-Image-i2L](https://www.modelscope.cn/models/DiffSynth-Studio/Qwen-Image-i2L)|[code](/examples/qwen_image/model_inference/Qwen-Image-i2L.py)|[code](/examples/qwen_image/model_inference_low_vram/Qwen-Image-i2L.py)|-|-|-|-|
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
|
|||||||
19
README_zh.md
19
README_zh.md
@@ -33,6 +33,8 @@ DiffSynth 目前包括两个开源项目:
|
|||||||
|
|
||||||
> 目前本项目的开发人员有限,大部分工作由 [Artiprocher](https://github.com/Artiprocher) 负责,因此新功能的开发进展会比较缓慢,issue 的回复和解决速度有限,我们对此感到非常抱歉,请各位开发者理解。
|
> 目前本项目的开发人员有限,大部分工作由 [Artiprocher](https://github.com/Artiprocher) 负责,因此新功能的开发进展会比较缓慢,issue 的回复和解决速度有限,我们对此感到非常抱歉,请各位开发者理解。
|
||||||
|
|
||||||
|
- **2025年12月9日** 我们基于 DiffSynth-Studio 2.0 训练了一个疯狂的模型:[Qwen-Image-i2L](https://www.modelscope.cn/models/DiffSynth-Studio/Qwen-Image-i2L)(Image to LoRA)。这一模型以图像为输入,以 LoRA 为输出。尽管这个版本的模型在泛化能力、细节保持能力等方面还有很大改进空间,我们将这些模型开源,以启发更多创新性的研究工作。
|
||||||
|
|
||||||
- **2025年12月4日** DiffSynth-Studio 2.0 发布!众多新功能上线
|
- **2025年12月4日** DiffSynth-Studio 2.0 发布!众多新功能上线
|
||||||
- [文档](/docs/zh/README.md)上线:我们的文档还在持续优化更新中
|
- [文档](/docs/zh/README.md)上线:我们的文档还在持续优化更新中
|
||||||
- [显存管理](/docs/zh/Pipeline_Usage/VRAM_management.md)模块升级,支持 Layer 级别的 Disk Offload,同时释放内存与显存
|
- [显存管理](/docs/zh/Pipeline_Usage/VRAM_management.md)模块升级,支持 Layer 级别的 Disk Offload,同时释放内存与显存
|
||||||
@@ -187,21 +189,7 @@ cd DiffSynth-Studio
|
|||||||
pip install -e .
|
pip install -e .
|
||||||
```
|
```
|
||||||
|
|
||||||
<details>
|
更多安装方式,以及非 NVIDIA GPU 的安装,请参考[安装文档](/docs/zh/Pipeline_Usage/Setup.md)。
|
||||||
<summary>其他安装方式</summary>
|
|
||||||
|
|
||||||
从 pypi 安装(存在版本更新延迟,如需使用最新功能,请从源码安装)
|
|
||||||
|
|
||||||
```
|
|
||||||
pip install diffsynth
|
|
||||||
```
|
|
||||||
|
|
||||||
如果在安装过程中遇到问题,可能是由上游依赖包导致的,请参考这些包的文档:
|
|
||||||
|
|
||||||
* [torch](https://pytorch.org/get-started/locally/)
|
|
||||||
* [sentencepiece](https://github.com/google/sentencepiece)
|
|
||||||
* [cmake](https://cmake.org)
|
|
||||||
* [cupy](https://docs.cupy.dev/en/stable/install.html)
|
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
@@ -420,6 +408,7 @@ Qwen-Image 的示例代码位于:[/examples/qwen_image/](/examples/qwen_image/
|
|||||||
|[DiffSynth-Studio/Qwen-Image-Blockwise-ControlNet-Inpaint](https://modelscope.cn/models/DiffSynth-Studio/Qwen-Image-Blockwise-ControlNet-Inpaint)|[code](/examples/qwen_image/model_inference/Qwen-Image-Blockwise-ControlNet-Inpaint.py)|[code](/examples/qwen_image/model_inference_low_vram/Qwen-Image-Blockwise-ControlNet-Inpaint.py)|[code](/examples/qwen_image/model_training/full/Qwen-Image-Blockwise-ControlNet-Inpaint.sh)|[code](/examples/qwen_image/model_training/validate_full/Qwen-Image-Blockwise-ControlNet-Inpaint.py)|[code](/examples/qwen_image/model_training/lora/Qwen-Image-Blockwise-ControlNet-Inpaint.sh)|[code](/examples/qwen_image/model_training/validate_lora/Qwen-Image-Blockwise-ControlNet-Inpaint.py)|
|
|[DiffSynth-Studio/Qwen-Image-Blockwise-ControlNet-Inpaint](https://modelscope.cn/models/DiffSynth-Studio/Qwen-Image-Blockwise-ControlNet-Inpaint)|[code](/examples/qwen_image/model_inference/Qwen-Image-Blockwise-ControlNet-Inpaint.py)|[code](/examples/qwen_image/model_inference_low_vram/Qwen-Image-Blockwise-ControlNet-Inpaint.py)|[code](/examples/qwen_image/model_training/full/Qwen-Image-Blockwise-ControlNet-Inpaint.sh)|[code](/examples/qwen_image/model_training/validate_full/Qwen-Image-Blockwise-ControlNet-Inpaint.py)|[code](/examples/qwen_image/model_training/lora/Qwen-Image-Blockwise-ControlNet-Inpaint.sh)|[code](/examples/qwen_image/model_training/validate_lora/Qwen-Image-Blockwise-ControlNet-Inpaint.py)|
|
||||||
|[DiffSynth-Studio/Qwen-Image-In-Context-Control-Union](https://www.modelscope.cn/models/DiffSynth-Studio/Qwen-Image-In-Context-Control-Union)|[code](/examples/qwen_image/model_inference/Qwen-Image-In-Context-Control-Union.py)|[code](/examples/qwen_image/model_inference_low_vram/Qwen-Image-In-Context-Control-Union.py)|-|-|[code](/examples/qwen_image/model_training/lora/Qwen-Image-In-Context-Control-Union.sh)|[code](/examples/qwen_image/model_training/validate_lora/Qwen-Image-In-Context-Control-Union.py)|
|
|[DiffSynth-Studio/Qwen-Image-In-Context-Control-Union](https://www.modelscope.cn/models/DiffSynth-Studio/Qwen-Image-In-Context-Control-Union)|[code](/examples/qwen_image/model_inference/Qwen-Image-In-Context-Control-Union.py)|[code](/examples/qwen_image/model_inference_low_vram/Qwen-Image-In-Context-Control-Union.py)|-|-|[code](/examples/qwen_image/model_training/lora/Qwen-Image-In-Context-Control-Union.sh)|[code](/examples/qwen_image/model_training/validate_lora/Qwen-Image-In-Context-Control-Union.py)|
|
||||||
|[DiffSynth-Studio/Qwen-Image-Edit-Lowres-Fix](https://www.modelscope.cn/models/DiffSynth-Studio/Qwen-Image-Edit-Lowres-Fix)|[code](/examples/qwen_image/model_inference/Qwen-Image-Edit-Lowres-Fix.py)|[code](/examples/qwen_image/model_inference_low_vram/Qwen-Image-Edit-Lowres-Fix.py)|-|-|-|-|
|
|[DiffSynth-Studio/Qwen-Image-Edit-Lowres-Fix](https://www.modelscope.cn/models/DiffSynth-Studio/Qwen-Image-Edit-Lowres-Fix)|[code](/examples/qwen_image/model_inference/Qwen-Image-Edit-Lowres-Fix.py)|[code](/examples/qwen_image/model_inference_low_vram/Qwen-Image-Edit-Lowres-Fix.py)|-|-|-|-|
|
||||||
|
|[DiffSynth-Studio/Qwen-Image-i2L](https://www.modelscope.cn/models/DiffSynth-Studio/Qwen-Image-i2L)|[code](/examples/qwen_image/model_inference/Qwen-Image-i2L.py)|[code](/examples/qwen_image/model_inference_low_vram/Qwen-Image-i2L.py)|-|-|-|-|
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
|
|||||||
@@ -31,6 +31,38 @@ qwen_image_series = [
|
|||||||
"model_class": "diffsynth.models.qwen_image_controlnet.QwenImageBlockWiseControlNet",
|
"model_class": "diffsynth.models.qwen_image_controlnet.QwenImageBlockWiseControlNet",
|
||||||
"extra_kwargs": {"additional_in_dim": 4},
|
"extra_kwargs": {"additional_in_dim": 4},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
# Example: ModelConfig(model_id="DiffSynth-Studio/General-Image-Encoders", origin_file_pattern="SigLIP2-G384/model.safetensors")
|
||||||
|
"model_hash": "469c78b61e3e31bc9eec0d0af3d3f2f8",
|
||||||
|
"model_name": "siglip2_image_encoder",
|
||||||
|
"model_class": "diffsynth.models.siglip2_image_encoder.Siglip2ImageEncoder",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
# Example: ModelConfig(model_id="DiffSynth-Studio/General-Image-Encoders", origin_file_pattern="DINOv3-7B/model.safetensors")
|
||||||
|
"model_hash": "5722b5c873720009de96422993b15682",
|
||||||
|
"model_name": "dinov3_image_encoder",
|
||||||
|
"model_class": "diffsynth.models.dinov3_image_encoder.DINOv3ImageEncoder",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
# Example:
|
||||||
|
"model_hash": "a166c33455cdbd89c0888a3645ca5c0f",
|
||||||
|
"model_name": "qwen_image_image2lora_coarse",
|
||||||
|
"model_class": "diffsynth.models.qwen_image_image2lora.QwenImageImage2LoRAModel",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
# Example:
|
||||||
|
"model_hash": "a5476e691767a4da6d3a6634a10f7408",
|
||||||
|
"model_name": "qwen_image_image2lora_fine",
|
||||||
|
"model_class": "diffsynth.models.qwen_image_image2lora.QwenImageImage2LoRAModel",
|
||||||
|
"extra_kwargs": {"residual_length": 37*37+7, "residual_mid_dim": 64}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
# Example:
|
||||||
|
"model_hash": "0aad514690602ecaff932c701cb4b0bb",
|
||||||
|
"model_name": "qwen_image_image2lora_style",
|
||||||
|
"model_class": "diffsynth.models.qwen_image_image2lora.QwenImageImage2LoRAModel",
|
||||||
|
"extra_kwargs": {"compress_dim": 64, "use_residual": False}
|
||||||
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
wan_series = [
|
wan_series = [
|
||||||
|
|||||||
@@ -32,6 +32,25 @@ VRAM_MANAGEMENT_MODULE_MAPS = {
|
|||||||
"diffsynth.models.qwen_image_dit.RMSNorm": "diffsynth.core.vram.layers.AutoWrappedModule",
|
"diffsynth.models.qwen_image_dit.RMSNorm": "diffsynth.core.vram.layers.AutoWrappedModule",
|
||||||
"torch.nn.Linear": "diffsynth.core.vram.layers.AutoWrappedLinear",
|
"torch.nn.Linear": "diffsynth.core.vram.layers.AutoWrappedLinear",
|
||||||
},
|
},
|
||||||
|
"diffsynth.models.siglip2_image_encoder.Siglip2ImageEncoder": {
|
||||||
|
"transformers.models.siglip.modeling_siglip.SiglipVisionEmbeddings": "diffsynth.core.vram.layers.AutoWrappedModule",
|
||||||
|
"transformers.models.siglip.modeling_siglip.SiglipMultiheadAttentionPoolingHead": "diffsynth.core.vram.layers.AutoWrappedModule",
|
||||||
|
"torch.nn.Conv2d": "diffsynth.core.vram.layers.AutoWrappedModule",
|
||||||
|
"torch.nn.Embedding": "diffsynth.core.vram.layers.AutoWrappedModule",
|
||||||
|
"torch.nn.LayerNorm": "diffsynth.core.vram.layers.AutoWrappedModule",
|
||||||
|
"torch.nn.Linear": "diffsynth.core.vram.layers.AutoWrappedLinear",
|
||||||
|
},
|
||||||
|
"diffsynth.models.dinov3_image_encoder.DINOv3ImageEncoder": {
|
||||||
|
"transformers.models.dinov3_vit.modeling_dinov3_vit.DINOv3ViTLayerScale": "diffsynth.core.vram.layers.AutoWrappedModule",
|
||||||
|
"transformers.models.dinov3_vit.modeling_dinov3_vit.DINOv3ViTRopePositionEmbedding": "diffsynth.core.vram.layers.AutoWrappedModule",
|
||||||
|
"transformers.models.dinov3_vit.modeling_dinov3_vit.DINOv3ViTEmbeddings": "diffsynth.core.vram.layers.AutoWrappedModule",
|
||||||
|
"torch.nn.Conv2d": "diffsynth.core.vram.layers.AutoWrappedModule",
|
||||||
|
"torch.nn.LayerNorm": "diffsynth.core.vram.layers.AutoWrappedModule",
|
||||||
|
"torch.nn.Linear": "diffsynth.core.vram.layers.AutoWrappedLinear",
|
||||||
|
},
|
||||||
|
"diffsynth.models.qwen_image_image2lora.QwenImageImage2LoRAModel": {
|
||||||
|
"torch.nn.Linear": "diffsynth.core.vram.layers.AutoWrappedLinear",
|
||||||
|
},
|
||||||
"diffsynth.models.wan_video_animate_adapter.WanAnimateAdapter": {
|
"diffsynth.models.wan_video_animate_adapter.WanAnimateAdapter": {
|
||||||
"diffsynth.models.wan_video_animate_adapter.FaceEncoder": "diffsynth.core.vram.layers.AutoWrappedModule",
|
"diffsynth.models.wan_video_animate_adapter.FaceEncoder": "diffsynth.core.vram.layers.AutoWrappedModule",
|
||||||
"diffsynth.models.wan_video_animate_adapter.EqualLinear": "diffsynth.core.vram.layers.AutoWrappedModule",
|
"diffsynth.models.wan_video_animate_adapter.EqualLinear": "diffsynth.core.vram.layers.AutoWrappedModule",
|
||||||
|
|||||||
@@ -3,3 +3,4 @@ from .data import *
|
|||||||
from .gradient import *
|
from .gradient import *
|
||||||
from .loader import *
|
from .loader import *
|
||||||
from .vram import *
|
from .vram import *
|
||||||
|
from .device import *
|
||||||
|
|||||||
1
diffsynth/core/device/__init__.py
Normal file
1
diffsynth/core/device/__init__.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
from .npu_compatible_device import parse_device_type, parse_nccl_backend, get_available_device_type
|
||||||
107
diffsynth/core/device/npu_compatible_device.py
Normal file
107
diffsynth/core/device/npu_compatible_device.py
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
import importlib
|
||||||
|
import torch
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
|
||||||
|
def is_torch_npu_available():
|
||||||
|
return importlib.util.find_spec("torch_npu") is not None
|
||||||
|
|
||||||
|
|
||||||
|
IS_CUDA_AVAILABLE = torch.cuda.is_available()
|
||||||
|
IS_NPU_AVAILABLE = is_torch_npu_available() and torch.npu.is_available()
|
||||||
|
|
||||||
|
if IS_NPU_AVAILABLE:
|
||||||
|
import torch_npu
|
||||||
|
|
||||||
|
torch.npu.config.allow_internal_format = False
|
||||||
|
|
||||||
|
|
||||||
|
def get_device_type() -> str:
|
||||||
|
"""Get device type based on current machine, currently only support CPU, CUDA, NPU."""
|
||||||
|
if IS_CUDA_AVAILABLE:
|
||||||
|
device = "cuda"
|
||||||
|
elif IS_NPU_AVAILABLE:
|
||||||
|
device = "npu"
|
||||||
|
else:
|
||||||
|
device = "cpu"
|
||||||
|
|
||||||
|
return device
|
||||||
|
|
||||||
|
|
||||||
|
def get_torch_device() -> Any:
|
||||||
|
"""Get torch attribute based on device type, e.g. torch.cuda or torch.npu"""
|
||||||
|
device_name = get_device_type()
|
||||||
|
|
||||||
|
try:
|
||||||
|
return getattr(torch, device_name)
|
||||||
|
except AttributeError:
|
||||||
|
print(f"Device namespace '{device_name}' not found in torch, try to load 'torch.cuda'.")
|
||||||
|
return torch.cuda
|
||||||
|
|
||||||
|
|
||||||
|
def get_device_id() -> int:
|
||||||
|
"""Get current device id based on device type."""
|
||||||
|
return get_torch_device().current_device()
|
||||||
|
|
||||||
|
|
||||||
|
def get_device_name() -> str:
|
||||||
|
"""Get current device name based on device type."""
|
||||||
|
return f"{get_device_type()}:{get_device_id()}"
|
||||||
|
|
||||||
|
|
||||||
|
def synchronize() -> None:
|
||||||
|
"""Execute torch synchronize operation."""
|
||||||
|
get_torch_device().synchronize()
|
||||||
|
|
||||||
|
|
||||||
|
def empty_cache() -> None:
|
||||||
|
"""Execute torch empty cache operation."""
|
||||||
|
get_torch_device().empty_cache()
|
||||||
|
|
||||||
|
|
||||||
|
def get_nccl_backend() -> str:
|
||||||
|
"""Return distributed communication backend type based on device type."""
|
||||||
|
if IS_CUDA_AVAILABLE:
|
||||||
|
return "nccl"
|
||||||
|
elif IS_NPU_AVAILABLE:
|
||||||
|
return "hccl"
|
||||||
|
else:
|
||||||
|
raise RuntimeError(f"No available distributed communication backend found on device type {get_device_type()}.")
|
||||||
|
|
||||||
|
|
||||||
|
def enable_high_precision_for_bf16():
|
||||||
|
"""
|
||||||
|
Set high accumulation dtype for matmul and reduction.
|
||||||
|
"""
|
||||||
|
if IS_CUDA_AVAILABLE:
|
||||||
|
torch.backends.cuda.matmul.allow_tf32 = False
|
||||||
|
torch.backends.cuda.matmul.allow_bf16_reduced_precision_reduction = False
|
||||||
|
|
||||||
|
if IS_NPU_AVAILABLE:
|
||||||
|
torch.npu.matmul.allow_tf32 = False
|
||||||
|
torch.npu.matmul.allow_bf16_reduced_precision_reduction = False
|
||||||
|
|
||||||
|
|
||||||
|
def parse_device_type(device):
|
||||||
|
if isinstance(device, str):
|
||||||
|
if device.startswith("cuda"):
|
||||||
|
return "cuda"
|
||||||
|
elif device.startswith("npu"):
|
||||||
|
return "npu"
|
||||||
|
else:
|
||||||
|
return "cpu"
|
||||||
|
elif isinstance(device, torch.device):
|
||||||
|
return device.type
|
||||||
|
|
||||||
|
|
||||||
|
def parse_nccl_backend(device_type):
|
||||||
|
if device_type == "cuda":
|
||||||
|
return "nccl"
|
||||||
|
elif device_type == "npu":
|
||||||
|
return "hccl"
|
||||||
|
else:
|
||||||
|
raise RuntimeError(f"No available distributed communication backend found on device type {device_type}.")
|
||||||
|
|
||||||
|
|
||||||
|
def get_available_device_type():
|
||||||
|
return get_device_type()
|
||||||
@@ -2,6 +2,7 @@ import torch, copy
|
|||||||
from typing import Union
|
from typing import Union
|
||||||
from .initialization import skip_model_initialization
|
from .initialization import skip_model_initialization
|
||||||
from .disk_map import DiskMap
|
from .disk_map import DiskMap
|
||||||
|
from ..device import parse_device_type
|
||||||
|
|
||||||
|
|
||||||
class AutoTorchModule(torch.nn.Module):
|
class AutoTorchModule(torch.nn.Module):
|
||||||
@@ -32,6 +33,7 @@ class AutoTorchModule(torch.nn.Module):
|
|||||||
)
|
)
|
||||||
self.state = 0
|
self.state = 0
|
||||||
self.name = ""
|
self.name = ""
|
||||||
|
self.computation_device_type = parse_device_type(self.computation_device)
|
||||||
|
|
||||||
def set_dtype_and_device(
|
def set_dtype_and_device(
|
||||||
self,
|
self,
|
||||||
@@ -61,7 +63,7 @@ class AutoTorchModule(torch.nn.Module):
|
|||||||
return r
|
return r
|
||||||
|
|
||||||
def check_free_vram(self):
|
def check_free_vram(self):
|
||||||
gpu_mem_state = torch.cuda.mem_get_info(self.computation_device)
|
gpu_mem_state = getattr(torch, self.computation_device_type).mem_get_info(self.computation_device)
|
||||||
used_memory = (gpu_mem_state[1] - gpu_mem_state[0]) / (1024**3)
|
used_memory = (gpu_mem_state[1] - gpu_mem_state[0]) / (1024**3)
|
||||||
return used_memory < self.vram_limit
|
return used_memory < self.vram_limit
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import torch
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
from einops import repeat, reduce
|
from einops import repeat, reduce
|
||||||
from typing import Union
|
from typing import Union
|
||||||
from ..core import AutoTorchModule, AutoWrappedLinear, load_state_dict, ModelConfig
|
from ..core import AutoTorchModule, AutoWrappedLinear, load_state_dict, ModelConfig, parse_device_type
|
||||||
from ..utils.lora import GeneralLoRALoader
|
from ..utils.lora import GeneralLoRALoader
|
||||||
from ..models.model_loader import ModelPool
|
from ..models.model_loader import ModelPool
|
||||||
from ..utils.controlnet import ControlNetInput
|
from ..utils.controlnet import ControlNetInput
|
||||||
@@ -68,6 +68,7 @@ class BasePipeline(torch.nn.Module):
|
|||||||
# The device and torch_dtype is used for the storage of intermediate variables, not models.
|
# The device and torch_dtype is used for the storage of intermediate variables, not models.
|
||||||
self.device = device
|
self.device = device
|
||||||
self.torch_dtype = torch_dtype
|
self.torch_dtype = torch_dtype
|
||||||
|
self.device_type = parse_device_type(device)
|
||||||
# The following parameters are used for shape check.
|
# The following parameters are used for shape check.
|
||||||
self.height_division_factor = height_division_factor
|
self.height_division_factor = height_division_factor
|
||||||
self.width_division_factor = width_division_factor
|
self.width_division_factor = width_division_factor
|
||||||
@@ -154,7 +155,7 @@ class BasePipeline(torch.nn.Module):
|
|||||||
for module in model.modules():
|
for module in model.modules():
|
||||||
if hasattr(module, "offload"):
|
if hasattr(module, "offload"):
|
||||||
module.offload()
|
module.offload()
|
||||||
torch.cuda.empty_cache()
|
getattr(torch, self.device_type).empty_cache()
|
||||||
# onload models
|
# onload models
|
||||||
for name, model in self.named_children():
|
for name, model in self.named_children():
|
||||||
if name in model_names:
|
if name in model_names:
|
||||||
@@ -176,7 +177,7 @@ class BasePipeline(torch.nn.Module):
|
|||||||
|
|
||||||
|
|
||||||
def get_vram(self):
|
def get_vram(self):
|
||||||
return torch.cuda.mem_get_info(self.device)[1] / (1024 ** 3)
|
return getattr(torch, self.device_type).mem_get_info(self.device)[1] / (1024 ** 3)
|
||||||
|
|
||||||
def get_module(self, model, name):
|
def get_module(self, model, name):
|
||||||
if "." in name:
|
if "." in name:
|
||||||
|
|||||||
94
diffsynth/models/dinov3_image_encoder.py
Normal file
94
diffsynth/models/dinov3_image_encoder.py
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
from transformers import DINOv3ViTModel, DINOv3ViTImageProcessorFast
|
||||||
|
from transformers.models.dinov3_vit.modeling_dinov3_vit import DINOv3ViTConfig
|
||||||
|
import torch
|
||||||
|
|
||||||
|
|
||||||
|
class DINOv3ImageEncoder(DINOv3ViTModel):
|
||||||
|
def __init__(self):
|
||||||
|
config = DINOv3ViTConfig(
|
||||||
|
architectures = [
|
||||||
|
"DINOv3ViTModel"
|
||||||
|
],
|
||||||
|
attention_dropout = 0.0,
|
||||||
|
drop_path_rate = 0.0,
|
||||||
|
dtype = "float32",
|
||||||
|
hidden_act = "silu",
|
||||||
|
hidden_size = 4096,
|
||||||
|
image_size = 224,
|
||||||
|
initializer_range = 0.02,
|
||||||
|
intermediate_size = 8192,
|
||||||
|
key_bias = False,
|
||||||
|
layer_norm_eps = 1e-05,
|
||||||
|
layerscale_value = 1.0,
|
||||||
|
mlp_bias = True,
|
||||||
|
model_type = "dinov3_vit",
|
||||||
|
num_attention_heads = 32,
|
||||||
|
num_channels = 3,
|
||||||
|
num_hidden_layers = 40,
|
||||||
|
num_register_tokens = 4,
|
||||||
|
patch_size = 16,
|
||||||
|
pos_embed_jitter = None,
|
||||||
|
pos_embed_rescale = 2.0,
|
||||||
|
pos_embed_shift = None,
|
||||||
|
proj_bias = True,
|
||||||
|
query_bias = False,
|
||||||
|
rope_theta = 100.0,
|
||||||
|
transformers_version = "4.56.1",
|
||||||
|
use_gated_mlp = True,
|
||||||
|
value_bias = False
|
||||||
|
)
|
||||||
|
super().__init__(config)
|
||||||
|
self.processor = DINOv3ViTImageProcessorFast(
|
||||||
|
crop_size = None,
|
||||||
|
data_format = "channels_first",
|
||||||
|
default_to_square = True,
|
||||||
|
device = None,
|
||||||
|
disable_grouping = None,
|
||||||
|
do_center_crop = None,
|
||||||
|
do_convert_rgb = None,
|
||||||
|
do_normalize = True,
|
||||||
|
do_rescale = True,
|
||||||
|
do_resize = True,
|
||||||
|
image_mean = [
|
||||||
|
0.485,
|
||||||
|
0.456,
|
||||||
|
0.406
|
||||||
|
],
|
||||||
|
image_processor_type = "DINOv3ViTImageProcessorFast",
|
||||||
|
image_std = [
|
||||||
|
0.229,
|
||||||
|
0.224,
|
||||||
|
0.225
|
||||||
|
],
|
||||||
|
input_data_format = None,
|
||||||
|
resample = 2,
|
||||||
|
rescale_factor = 0.00392156862745098,
|
||||||
|
return_tensors = None,
|
||||||
|
size = {
|
||||||
|
"height": 224,
|
||||||
|
"width": 224
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
def forward(self, image, torch_dtype=torch.bfloat16, device="cuda"):
|
||||||
|
inputs = self.processor(images=image, return_tensors="pt")
|
||||||
|
pixel_values = inputs["pixel_values"].to(dtype=torch_dtype, device=device)
|
||||||
|
bool_masked_pos = None
|
||||||
|
head_mask = None
|
||||||
|
|
||||||
|
pixel_values = pixel_values.to(torch_dtype)
|
||||||
|
hidden_states = self.embeddings(pixel_values, bool_masked_pos=bool_masked_pos)
|
||||||
|
position_embeddings = self.rope_embeddings(pixel_values)
|
||||||
|
|
||||||
|
for i, layer_module in enumerate(self.layer):
|
||||||
|
layer_head_mask = head_mask[i] if head_mask is not None else None
|
||||||
|
hidden_states = layer_module(
|
||||||
|
hidden_states,
|
||||||
|
attention_mask=layer_head_mask,
|
||||||
|
position_embeddings=position_embeddings,
|
||||||
|
)
|
||||||
|
|
||||||
|
sequence_output = self.norm(hidden_states)
|
||||||
|
pooled_output = sequence_output[:, 0, :]
|
||||||
|
|
||||||
|
return pooled_output
|
||||||
128
diffsynth/models/qwen_image_image2lora.py
Normal file
128
diffsynth/models/qwen_image_image2lora.py
Normal file
@@ -0,0 +1,128 @@
|
|||||||
|
import torch
|
||||||
|
|
||||||
|
|
||||||
|
class CompressedMLP(torch.nn.Module):
|
||||||
|
def __init__(self, in_dim, mid_dim, out_dim, bias=False):
|
||||||
|
super().__init__()
|
||||||
|
self.proj_in = torch.nn.Linear(in_dim, mid_dim, bias=bias)
|
||||||
|
self.proj_out = torch.nn.Linear(mid_dim, out_dim, bias=bias)
|
||||||
|
|
||||||
|
def forward(self, x, residual=None):
|
||||||
|
x = self.proj_in(x)
|
||||||
|
if residual is not None: x = x + residual
|
||||||
|
x = self.proj_out(x)
|
||||||
|
return x
|
||||||
|
|
||||||
|
|
||||||
|
class ImageEmbeddingToLoraMatrix(torch.nn.Module):
|
||||||
|
def __init__(self, in_dim, compress_dim, lora_a_dim, lora_b_dim, rank):
|
||||||
|
super().__init__()
|
||||||
|
self.proj_a = CompressedMLP(in_dim, compress_dim, lora_a_dim * rank)
|
||||||
|
self.proj_b = CompressedMLP(in_dim, compress_dim, lora_b_dim * rank)
|
||||||
|
self.lora_a_dim = lora_a_dim
|
||||||
|
self.lora_b_dim = lora_b_dim
|
||||||
|
self.rank = rank
|
||||||
|
|
||||||
|
def forward(self, x, residual=None):
|
||||||
|
lora_a = self.proj_a(x, residual).view(self.rank, self.lora_a_dim)
|
||||||
|
lora_b = self.proj_b(x, residual).view(self.lora_b_dim, self.rank)
|
||||||
|
return lora_a, lora_b
|
||||||
|
|
||||||
|
|
||||||
|
class SequencialMLP(torch.nn.Module):
|
||||||
|
def __init__(self, length, in_dim, mid_dim, out_dim, bias=False):
|
||||||
|
super().__init__()
|
||||||
|
self.proj_in = torch.nn.Linear(in_dim, mid_dim, bias=bias)
|
||||||
|
self.proj_out = torch.nn.Linear(length * mid_dim, out_dim, bias=bias)
|
||||||
|
self.length = length
|
||||||
|
self.in_dim = in_dim
|
||||||
|
self.mid_dim = mid_dim
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
x = x.view(self.length, self.in_dim)
|
||||||
|
x = self.proj_in(x)
|
||||||
|
x = x.view(1, self.length * self.mid_dim)
|
||||||
|
x = self.proj_out(x)
|
||||||
|
return x
|
||||||
|
|
||||||
|
|
||||||
|
class LoRATrainerBlock(torch.nn.Module):
|
||||||
|
def __init__(self, lora_patterns, in_dim=1536+4096, compress_dim=128, rank=4, block_id=0, use_residual=True, residual_length=64+7, residual_dim=3584, residual_mid_dim=1024):
|
||||||
|
super().__init__()
|
||||||
|
self.lora_patterns = lora_patterns
|
||||||
|
self.block_id = block_id
|
||||||
|
self.layers = []
|
||||||
|
for name, lora_a_dim, lora_b_dim in self.lora_patterns:
|
||||||
|
self.layers.append(ImageEmbeddingToLoraMatrix(in_dim, compress_dim, lora_a_dim, lora_b_dim, rank))
|
||||||
|
self.layers = torch.nn.ModuleList(self.layers)
|
||||||
|
if use_residual:
|
||||||
|
self.proj_residual = SequencialMLP(residual_length, residual_dim, residual_mid_dim, compress_dim)
|
||||||
|
else:
|
||||||
|
self.proj_residual = None
|
||||||
|
|
||||||
|
def forward(self, x, residual=None):
|
||||||
|
lora = {}
|
||||||
|
if self.proj_residual is not None: residual = self.proj_residual(residual)
|
||||||
|
for lora_pattern, layer in zip(self.lora_patterns, self.layers):
|
||||||
|
name = lora_pattern[0]
|
||||||
|
lora_a, lora_b = layer(x, residual=residual)
|
||||||
|
lora[f"transformer_blocks.{self.block_id}.{name}.lora_A.default.weight"] = lora_a
|
||||||
|
lora[f"transformer_blocks.{self.block_id}.{name}.lora_B.default.weight"] = lora_b
|
||||||
|
return lora
|
||||||
|
|
||||||
|
|
||||||
|
class QwenImageImage2LoRAModel(torch.nn.Module):
|
||||||
|
def __init__(self, num_blocks=60, use_residual=True, compress_dim=128, rank=4, residual_length=64+7, residual_mid_dim=1024):
|
||||||
|
super().__init__()
|
||||||
|
self.lora_patterns = [
|
||||||
|
[
|
||||||
|
("attn.to_q", 3072, 3072),
|
||||||
|
("attn.to_k", 3072, 3072),
|
||||||
|
("attn.to_v", 3072, 3072),
|
||||||
|
("attn.to_out.0", 3072, 3072),
|
||||||
|
],
|
||||||
|
[
|
||||||
|
("img_mlp.net.2", 3072*4, 3072),
|
||||||
|
("img_mod.1", 3072, 3072*6),
|
||||||
|
],
|
||||||
|
[
|
||||||
|
("attn.add_q_proj", 3072, 3072),
|
||||||
|
("attn.add_k_proj", 3072, 3072),
|
||||||
|
("attn.add_v_proj", 3072, 3072),
|
||||||
|
("attn.to_add_out", 3072, 3072),
|
||||||
|
],
|
||||||
|
[
|
||||||
|
("txt_mlp.net.2", 3072*4, 3072),
|
||||||
|
("txt_mod.1", 3072, 3072*6),
|
||||||
|
],
|
||||||
|
]
|
||||||
|
self.num_blocks = num_blocks
|
||||||
|
self.blocks = []
|
||||||
|
for lora_patterns in self.lora_patterns:
|
||||||
|
for block_id in range(self.num_blocks):
|
||||||
|
self.blocks.append(LoRATrainerBlock(lora_patterns, block_id=block_id, use_residual=use_residual, compress_dim=compress_dim, rank=rank, residual_length=residual_length, residual_mid_dim=residual_mid_dim))
|
||||||
|
self.blocks = torch.nn.ModuleList(self.blocks)
|
||||||
|
self.residual_scale = 0.05
|
||||||
|
self.use_residual = use_residual
|
||||||
|
|
||||||
|
def forward(self, x, residual=None):
|
||||||
|
if residual is not None:
|
||||||
|
if self.use_residual:
|
||||||
|
residual = residual * self.residual_scale
|
||||||
|
else:
|
||||||
|
residual = None
|
||||||
|
lora = {}
|
||||||
|
for block in self.blocks:
|
||||||
|
lora.update(block(x, residual))
|
||||||
|
return lora
|
||||||
|
|
||||||
|
def initialize_weights(self):
|
||||||
|
state_dict = self.state_dict()
|
||||||
|
for name in state_dict:
|
||||||
|
if ".proj_a." in name:
|
||||||
|
state_dict[name] = state_dict[name] * 0.3
|
||||||
|
elif ".proj_b.proj_out." in name:
|
||||||
|
state_dict[name] = state_dict[name] * 0
|
||||||
|
elif ".proj_residual.proj_out." in name:
|
||||||
|
state_dict[name] = state_dict[name] * 0.3
|
||||||
|
self.load_state_dict(state_dict)
|
||||||
70
diffsynth/models/siglip2_image_encoder.py
Normal file
70
diffsynth/models/siglip2_image_encoder.py
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
from transformers.models.siglip.modeling_siglip import SiglipVisionTransformer, SiglipVisionConfig
|
||||||
|
from transformers import SiglipImageProcessor
|
||||||
|
import torch
|
||||||
|
|
||||||
|
|
||||||
|
class Siglip2ImageEncoder(SiglipVisionTransformer):
|
||||||
|
def __init__(self):
|
||||||
|
config = SiglipVisionConfig(
|
||||||
|
attention_dropout = 0.0,
|
||||||
|
dtype = "float32",
|
||||||
|
hidden_act = "gelu_pytorch_tanh",
|
||||||
|
hidden_size = 1536,
|
||||||
|
image_size = 384,
|
||||||
|
intermediate_size = 6144,
|
||||||
|
layer_norm_eps = 1e-06,
|
||||||
|
model_type = "siglip_vision_model",
|
||||||
|
num_attention_heads = 16,
|
||||||
|
num_channels = 3,
|
||||||
|
num_hidden_layers = 40,
|
||||||
|
patch_size = 16,
|
||||||
|
transformers_version = "4.56.1",
|
||||||
|
_attn_implementation = "sdpa"
|
||||||
|
)
|
||||||
|
super().__init__(config)
|
||||||
|
self.processor = SiglipImageProcessor(
|
||||||
|
do_convert_rgb = None,
|
||||||
|
do_normalize = True,
|
||||||
|
do_rescale = True,
|
||||||
|
do_resize = True,
|
||||||
|
image_mean = [
|
||||||
|
0.5,
|
||||||
|
0.5,
|
||||||
|
0.5
|
||||||
|
],
|
||||||
|
image_processor_type = "SiglipImageProcessor",
|
||||||
|
image_std = [
|
||||||
|
0.5,
|
||||||
|
0.5,
|
||||||
|
0.5
|
||||||
|
],
|
||||||
|
processor_class = "SiglipProcessor",
|
||||||
|
resample = 2,
|
||||||
|
rescale_factor = 0.00392156862745098,
|
||||||
|
size = {
|
||||||
|
"height": 384,
|
||||||
|
"width": 384
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
def forward(self, image, torch_dtype=torch.bfloat16, device="cuda"):
|
||||||
|
pixel_values = self.processor(images=[image], return_tensors="pt")["pixel_values"]
|
||||||
|
pixel_values = pixel_values.to(device=device, dtype=torch_dtype)
|
||||||
|
output_attentions = False
|
||||||
|
output_hidden_states = False
|
||||||
|
interpolate_pos_encoding = False
|
||||||
|
|
||||||
|
hidden_states = self.embeddings(pixel_values, interpolate_pos_encoding=interpolate_pos_encoding)
|
||||||
|
|
||||||
|
encoder_outputs = self.encoder(
|
||||||
|
inputs_embeds=hidden_states,
|
||||||
|
output_attentions=output_attentions,
|
||||||
|
output_hidden_states=output_hidden_states,
|
||||||
|
)
|
||||||
|
|
||||||
|
last_hidden_state = encoder_outputs.last_hidden_state
|
||||||
|
last_hidden_state = self.post_layernorm(last_hidden_state)
|
||||||
|
|
||||||
|
pooler_output = self.head(last_hidden_state) if self.use_head else None
|
||||||
|
|
||||||
|
return pooler_output
|
||||||
@@ -8,11 +8,15 @@ import numpy as np
|
|||||||
from ..diffusion import FlowMatchScheduler
|
from ..diffusion import FlowMatchScheduler
|
||||||
from ..core import ModelConfig, gradient_checkpoint_forward
|
from ..core import ModelConfig, gradient_checkpoint_forward
|
||||||
from ..diffusion.base_pipeline import BasePipeline, PipelineUnit, ControlNetInput
|
from ..diffusion.base_pipeline import BasePipeline, PipelineUnit, ControlNetInput
|
||||||
|
from ..utils.lora.merge import merge_lora
|
||||||
|
|
||||||
from ..models.qwen_image_dit import QwenImageDiT
|
from ..models.qwen_image_dit import QwenImageDiT
|
||||||
from ..models.qwen_image_text_encoder import QwenImageTextEncoder
|
from ..models.qwen_image_text_encoder import QwenImageTextEncoder
|
||||||
from ..models.qwen_image_vae import QwenImageVAE
|
from ..models.qwen_image_vae import QwenImageVAE
|
||||||
from ..models.qwen_image_controlnet import QwenImageBlockWiseControlNet
|
from ..models.qwen_image_controlnet import QwenImageBlockWiseControlNet
|
||||||
|
from ..models.siglip2_image_encoder import Siglip2ImageEncoder
|
||||||
|
from ..models.dinov3_image_encoder import DINOv3ImageEncoder
|
||||||
|
from ..models.qwen_image_image2lora import QwenImageImage2LoRAModel
|
||||||
|
|
||||||
|
|
||||||
class QwenImagePipeline(BasePipeline):
|
class QwenImagePipeline(BasePipeline):
|
||||||
@@ -30,6 +34,11 @@ class QwenImagePipeline(BasePipeline):
|
|||||||
self.vae: QwenImageVAE = None
|
self.vae: QwenImageVAE = None
|
||||||
self.blockwise_controlnet: QwenImageBlockwiseMultiControlNet = None
|
self.blockwise_controlnet: QwenImageBlockwiseMultiControlNet = None
|
||||||
self.tokenizer: Qwen2Tokenizer = None
|
self.tokenizer: Qwen2Tokenizer = None
|
||||||
|
self.siglip2_image_encoder: Siglip2ImageEncoder = None
|
||||||
|
self.dinov3_image_encoder: DINOv3ImageEncoder = None
|
||||||
|
self.image2lora_style: QwenImageImage2LoRAModel = None
|
||||||
|
self.image2lora_coarse: QwenImageImage2LoRAModel = None
|
||||||
|
self.image2lora_fine: QwenImageImage2LoRAModel = None
|
||||||
self.processor: Qwen2VLProcessor = None
|
self.processor: Qwen2VLProcessor = None
|
||||||
self.in_iteration_models = ("dit", "blockwise_controlnet")
|
self.in_iteration_models = ("dit", "blockwise_controlnet")
|
||||||
self.units = [
|
self.units = [
|
||||||
@@ -72,6 +81,11 @@ class QwenImagePipeline(BasePipeline):
|
|||||||
processor_config.download_if_necessary()
|
processor_config.download_if_necessary()
|
||||||
from transformers import Qwen2VLProcessor
|
from transformers import Qwen2VLProcessor
|
||||||
pipe.processor = Qwen2VLProcessor.from_pretrained(processor_config.path)
|
pipe.processor = Qwen2VLProcessor.from_pretrained(processor_config.path)
|
||||||
|
pipe.siglip2_image_encoder = model_pool.fetch_model("siglip2_image_encoder")
|
||||||
|
pipe.dinov3_image_encoder = model_pool.fetch_model("dinov3_image_encoder")
|
||||||
|
pipe.image2lora_style = model_pool.fetch_model("qwen_image_image2lora_style")
|
||||||
|
pipe.image2lora_coarse = model_pool.fetch_model("qwen_image_image2lora_coarse")
|
||||||
|
pipe.image2lora_fine = model_pool.fetch_model("qwen_image_image2lora_fine")
|
||||||
|
|
||||||
# VRAM Management
|
# VRAM Management
|
||||||
pipe.vram_management_enabled = pipe.check_vram_management_state()
|
pipe.vram_management_enabled = pipe.check_vram_management_state()
|
||||||
@@ -515,6 +529,116 @@ class QwenImageUnit_EditImageEmbedder(PipelineUnit):
|
|||||||
return {"edit_latents": edit_latents, "edit_image": resized_edit_image}
|
return {"edit_latents": edit_latents, "edit_image": resized_edit_image}
|
||||||
|
|
||||||
|
|
||||||
|
class QwenImageUnit_Image2LoRAEncode(PipelineUnit):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__(
|
||||||
|
input_params=("image2lora_images",),
|
||||||
|
output_params=("image2lora_x", "image2lora_residual", "image2lora_residual_highres"),
|
||||||
|
onload_model_names=("siglip2_image_encoder", "dinov3_image_encoder", "text_encoder"),
|
||||||
|
)
|
||||||
|
from ..core.data.operators import ImageCropAndResize
|
||||||
|
self.processor_lowres = ImageCropAndResize(height=28*8, width=28*8)
|
||||||
|
self.processor_highres = ImageCropAndResize(height=1024, width=1024)
|
||||||
|
|
||||||
|
def extract_masked_hidden(self, hidden_states: torch.Tensor, mask: torch.Tensor):
|
||||||
|
bool_mask = mask.bool()
|
||||||
|
valid_lengths = bool_mask.sum(dim=1)
|
||||||
|
selected = hidden_states[bool_mask]
|
||||||
|
split_result = torch.split(selected, valid_lengths.tolist(), dim=0)
|
||||||
|
return split_result
|
||||||
|
|
||||||
|
def encode_prompt_edit(self, pipe: QwenImagePipeline, prompt, edit_image):
|
||||||
|
prompt = [prompt]
|
||||||
|
template = "<|im_start|>system\nDescribe the key features of the input image (color, shape, size, texture, objects, background), then explain how the user's text instruction should alter or modify the image. Generate a new image that meets the user's requirements while maintaining consistency with the original input where appropriate.<|im_end|>\n<|im_start|>user\n<|vision_start|><|image_pad|><|vision_end|>{}<|im_end|>\n<|im_start|>assistant\n"
|
||||||
|
drop_idx = 64
|
||||||
|
txt = [template.format(e) for e in prompt]
|
||||||
|
model_inputs = pipe.processor(text=txt, images=edit_image, padding=True, return_tensors="pt").to(pipe.device)
|
||||||
|
hidden_states = pipe.text_encoder(input_ids=model_inputs.input_ids, attention_mask=model_inputs.attention_mask, pixel_values=model_inputs.pixel_values, image_grid_thw=model_inputs.image_grid_thw, output_hidden_states=True,)[-1]
|
||||||
|
split_hidden_states = self.extract_masked_hidden(hidden_states, model_inputs.attention_mask)
|
||||||
|
split_hidden_states = [e[drop_idx:] for e in split_hidden_states]
|
||||||
|
max_seq_len = max([e.size(0) for e in split_hidden_states])
|
||||||
|
prompt_embeds = torch.stack([torch.cat([u, u.new_zeros(max_seq_len - u.size(0), u.size(1))]) for u in split_hidden_states])
|
||||||
|
prompt_embeds = prompt_embeds.to(dtype=pipe.torch_dtype, device=pipe.device)
|
||||||
|
return prompt_embeds.view(1, -1)
|
||||||
|
|
||||||
|
def encode_images_using_siglip2(self, pipe: QwenImagePipeline, images: list[Image.Image]):
|
||||||
|
pipe.load_models_to_device(["siglip2_image_encoder"])
|
||||||
|
embs = []
|
||||||
|
for image in images:
|
||||||
|
image = self.processor_highres(image)
|
||||||
|
embs.append(pipe.siglip2_image_encoder(image).to(pipe.torch_dtype))
|
||||||
|
embs = torch.stack(embs)
|
||||||
|
return embs
|
||||||
|
|
||||||
|
def encode_images_using_dinov3(self, pipe: QwenImagePipeline, images: list[Image.Image]):
|
||||||
|
pipe.load_models_to_device(["dinov3_image_encoder"])
|
||||||
|
embs = []
|
||||||
|
for image in images:
|
||||||
|
image = self.processor_highres(image)
|
||||||
|
embs.append(pipe.dinov3_image_encoder(image).to(pipe.torch_dtype))
|
||||||
|
embs = torch.stack(embs)
|
||||||
|
return embs
|
||||||
|
|
||||||
|
def encode_images_using_qwenvl(self, pipe: QwenImagePipeline, images: list[Image.Image], highres=False):
|
||||||
|
pipe.load_models_to_device(["text_encoder"])
|
||||||
|
embs = []
|
||||||
|
for image in images:
|
||||||
|
image = self.processor_highres(image) if highres else self.processor_lowres(image)
|
||||||
|
embs.append(self.encode_prompt_edit(pipe, prompt="", edit_image=image))
|
||||||
|
embs = torch.stack(embs)
|
||||||
|
return embs
|
||||||
|
|
||||||
|
def encode_images(self, pipe: QwenImagePipeline, images: list[Image.Image]):
|
||||||
|
if images is None:
|
||||||
|
return {}
|
||||||
|
if not isinstance(images, list):
|
||||||
|
images = [images]
|
||||||
|
embs_siglip2 = self.encode_images_using_siglip2(pipe, images)
|
||||||
|
embs_dinov3 = self.encode_images_using_dinov3(pipe, images)
|
||||||
|
x = torch.concat([embs_siglip2, embs_dinov3], dim=-1)
|
||||||
|
residual = None
|
||||||
|
residual_highres = None
|
||||||
|
if pipe.image2lora_coarse is not None:
|
||||||
|
residual = self.encode_images_using_qwenvl(pipe, images, highres=False)
|
||||||
|
if pipe.image2lora_fine is not None:
|
||||||
|
residual_highres = self.encode_images_using_qwenvl(pipe, images, highres=True)
|
||||||
|
return x, residual, residual_highres
|
||||||
|
|
||||||
|
def process(self, pipe: QwenImagePipeline, image2lora_images):
|
||||||
|
if image2lora_images is None:
|
||||||
|
return {}
|
||||||
|
x, residual, residual_highres = self.encode_images(pipe, image2lora_images)
|
||||||
|
return {"image2lora_x": x, "image2lora_residual": residual, "image2lora_residual_highres": residual_highres}
|
||||||
|
|
||||||
|
|
||||||
|
class QwenImageUnit_Image2LoRADecode(PipelineUnit):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__(
|
||||||
|
input_params=("image2lora_x", "image2lora_residual", "image2lora_residual_highres"),
|
||||||
|
output_params=("lora",),
|
||||||
|
onload_model_names=("image2lora_coarse", "image2lora_fine", "image2lora_style"),
|
||||||
|
)
|
||||||
|
|
||||||
|
def process(self, pipe: QwenImagePipeline, image2lora_x, image2lora_residual, image2lora_residual_highres):
|
||||||
|
if image2lora_x is None:
|
||||||
|
return {}
|
||||||
|
loras = []
|
||||||
|
if pipe.image2lora_style is not None:
|
||||||
|
pipe.load_models_to_device(["image2lora_style"])
|
||||||
|
for x in image2lora_x:
|
||||||
|
loras.append(pipe.image2lora_style(x=x, residual=None))
|
||||||
|
if pipe.image2lora_coarse is not None:
|
||||||
|
pipe.load_models_to_device(["image2lora_coarse"])
|
||||||
|
for x, residual in zip(image2lora_x, image2lora_residual):
|
||||||
|
loras.append(pipe.image2lora_coarse(x=x, residual=residual))
|
||||||
|
if pipe.image2lora_fine is not None:
|
||||||
|
pipe.load_models_to_device(["image2lora_fine"])
|
||||||
|
for x, residual in zip(image2lora_x, image2lora_residual_highres):
|
||||||
|
loras.append(pipe.image2lora_fine(x=x, residual=residual))
|
||||||
|
lora = merge_lora(loras, alpha=1 / len(image2lora_x))
|
||||||
|
return {"lora": lora}
|
||||||
|
|
||||||
|
|
||||||
class QwenImageUnit_ContextImageEmbedder(PipelineUnit):
|
class QwenImageUnit_ContextImageEmbedder(PipelineUnit):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__(
|
super().__init__(
|
||||||
|
|||||||
@@ -126,7 +126,7 @@ class WanVideoPipeline(BasePipeline):
|
|||||||
pipe = WanVideoPipeline(device=device, torch_dtype=torch_dtype)
|
pipe = WanVideoPipeline(device=device, torch_dtype=torch_dtype)
|
||||||
if use_usp:
|
if use_usp:
|
||||||
from ..utils.xfuser import initialize_usp
|
from ..utils.xfuser import initialize_usp
|
||||||
initialize_usp()
|
initialize_usp(device)
|
||||||
model_pool = pipe.download_and_load_models(model_configs, vram_limit)
|
model_pool = pipe.download_and_load_models(model_configs, vram_limit)
|
||||||
|
|
||||||
# Fetch models
|
# Fetch models
|
||||||
|
|||||||
@@ -1 +1,3 @@
|
|||||||
from .general import GeneralLoRALoader
|
from .general import GeneralLoRALoader
|
||||||
|
from .merge import merge_lora
|
||||||
|
from .reset_rank import reset_lora_rank
|
||||||
@@ -202,3 +202,99 @@ class FluxLoRALoader(GeneralLoRALoader):
|
|||||||
state_dict_.pop(name.replace(f".{component}_to_q.", f".{component}_to_k."))
|
state_dict_.pop(name.replace(f".{component}_to_q.", f".{component}_to_k."))
|
||||||
state_dict_.pop(name.replace(f".{component}_to_q.", f".{component}_to_v."))
|
state_dict_.pop(name.replace(f".{component}_to_q.", f".{component}_to_v."))
|
||||||
return state_dict_
|
return state_dict_
|
||||||
|
|
||||||
|
|
||||||
|
class FluxLoRAConverter:
|
||||||
|
def __init__(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def align_to_opensource_format(state_dict, alpha=None):
|
||||||
|
prefix_rename_dict = {
|
||||||
|
"single_blocks": "lora_unet_single_blocks",
|
||||||
|
"blocks": "lora_unet_double_blocks",
|
||||||
|
}
|
||||||
|
middle_rename_dict = {
|
||||||
|
"norm.linear": "modulation_lin",
|
||||||
|
"to_qkv_mlp": "linear1",
|
||||||
|
"proj_out": "linear2",
|
||||||
|
|
||||||
|
"norm1_a.linear": "img_mod_lin",
|
||||||
|
"norm1_b.linear": "txt_mod_lin",
|
||||||
|
"attn.a_to_qkv": "img_attn_qkv",
|
||||||
|
"attn.b_to_qkv": "txt_attn_qkv",
|
||||||
|
"attn.a_to_out": "img_attn_proj",
|
||||||
|
"attn.b_to_out": "txt_attn_proj",
|
||||||
|
"ff_a.0": "img_mlp_0",
|
||||||
|
"ff_a.2": "img_mlp_2",
|
||||||
|
"ff_b.0": "txt_mlp_0",
|
||||||
|
"ff_b.2": "txt_mlp_2",
|
||||||
|
}
|
||||||
|
suffix_rename_dict = {
|
||||||
|
"lora_B.weight": "lora_up.weight",
|
||||||
|
"lora_A.weight": "lora_down.weight",
|
||||||
|
}
|
||||||
|
state_dict_ = {}
|
||||||
|
for name, param in state_dict.items():
|
||||||
|
names = name.split(".")
|
||||||
|
if names[-2] != "lora_A" and names[-2] != "lora_B":
|
||||||
|
names.pop(-2)
|
||||||
|
prefix = names[0]
|
||||||
|
middle = ".".join(names[2:-2])
|
||||||
|
suffix = ".".join(names[-2:])
|
||||||
|
block_id = names[1]
|
||||||
|
if middle not in middle_rename_dict:
|
||||||
|
continue
|
||||||
|
rename = prefix_rename_dict[prefix] + "_" + block_id + "_" + middle_rename_dict[middle] + "." + suffix_rename_dict[suffix]
|
||||||
|
state_dict_[rename] = param
|
||||||
|
if rename.endswith("lora_up.weight"):
|
||||||
|
lora_alpha = alpha if alpha is not None else param.shape[-1]
|
||||||
|
state_dict_[rename.replace("lora_up.weight", "alpha")] = torch.tensor((lora_alpha,))[0]
|
||||||
|
return state_dict_
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def align_to_diffsynth_format(state_dict):
|
||||||
|
rename_dict = {
|
||||||
|
"lora_unet_double_blocks_blockid_img_mod_lin.lora_down.weight": "blocks.blockid.norm1_a.linear.lora_A.default.weight",
|
||||||
|
"lora_unet_double_blocks_blockid_img_mod_lin.lora_up.weight": "blocks.blockid.norm1_a.linear.lora_B.default.weight",
|
||||||
|
"lora_unet_double_blocks_blockid_txt_mod_lin.lora_down.weight": "blocks.blockid.norm1_b.linear.lora_A.default.weight",
|
||||||
|
"lora_unet_double_blocks_blockid_txt_mod_lin.lora_up.weight": "blocks.blockid.norm1_b.linear.lora_B.default.weight",
|
||||||
|
"lora_unet_double_blocks_blockid_img_attn_qkv.lora_down.weight": "blocks.blockid.attn.a_to_qkv.lora_A.default.weight",
|
||||||
|
"lora_unet_double_blocks_blockid_img_attn_qkv.lora_up.weight": "blocks.blockid.attn.a_to_qkv.lora_B.default.weight",
|
||||||
|
"lora_unet_double_blocks_blockid_txt_attn_qkv.lora_down.weight": "blocks.blockid.attn.b_to_qkv.lora_A.default.weight",
|
||||||
|
"lora_unet_double_blocks_blockid_txt_attn_qkv.lora_up.weight": "blocks.blockid.attn.b_to_qkv.lora_B.default.weight",
|
||||||
|
"lora_unet_double_blocks_blockid_img_attn_proj.lora_down.weight": "blocks.blockid.attn.a_to_out.lora_A.default.weight",
|
||||||
|
"lora_unet_double_blocks_blockid_img_attn_proj.lora_up.weight": "blocks.blockid.attn.a_to_out.lora_B.default.weight",
|
||||||
|
"lora_unet_double_blocks_blockid_txt_attn_proj.lora_down.weight": "blocks.blockid.attn.b_to_out.lora_A.default.weight",
|
||||||
|
"lora_unet_double_blocks_blockid_txt_attn_proj.lora_up.weight": "blocks.blockid.attn.b_to_out.lora_B.default.weight",
|
||||||
|
"lora_unet_double_blocks_blockid_img_mlp_0.lora_down.weight": "blocks.blockid.ff_a.0.lora_A.default.weight",
|
||||||
|
"lora_unet_double_blocks_blockid_img_mlp_0.lora_up.weight": "blocks.blockid.ff_a.0.lora_B.default.weight",
|
||||||
|
"lora_unet_double_blocks_blockid_img_mlp_2.lora_down.weight": "blocks.blockid.ff_a.2.lora_A.default.weight",
|
||||||
|
"lora_unet_double_blocks_blockid_img_mlp_2.lora_up.weight": "blocks.blockid.ff_a.2.lora_B.default.weight",
|
||||||
|
"lora_unet_double_blocks_blockid_txt_mlp_0.lora_down.weight": "blocks.blockid.ff_b.0.lora_A.default.weight",
|
||||||
|
"lora_unet_double_blocks_blockid_txt_mlp_0.lora_up.weight": "blocks.blockid.ff_b.0.lora_B.default.weight",
|
||||||
|
"lora_unet_double_blocks_blockid_txt_mlp_2.lora_down.weight": "blocks.blockid.ff_b.2.lora_A.default.weight",
|
||||||
|
"lora_unet_double_blocks_blockid_txt_mlp_2.lora_up.weight": "blocks.blockid.ff_b.2.lora_B.default.weight",
|
||||||
|
"lora_unet_single_blocks_blockid_modulation_lin.lora_down.weight": "single_blocks.blockid.norm.linear.lora_A.default.weight",
|
||||||
|
"lora_unet_single_blocks_blockid_modulation_lin.lora_up.weight": "single_blocks.blockid.norm.linear.lora_B.default.weight",
|
||||||
|
"lora_unet_single_blocks_blockid_linear1.lora_down.weight": "single_blocks.blockid.to_qkv_mlp.lora_A.default.weight",
|
||||||
|
"lora_unet_single_blocks_blockid_linear1.lora_up.weight": "single_blocks.blockid.to_qkv_mlp.lora_B.default.weight",
|
||||||
|
"lora_unet_single_blocks_blockid_linear2.lora_down.weight": "single_blocks.blockid.proj_out.lora_A.default.weight",
|
||||||
|
"lora_unet_single_blocks_blockid_linear2.lora_up.weight": "single_blocks.blockid.proj_out.lora_B.default.weight",
|
||||||
|
}
|
||||||
|
def guess_block_id(name):
|
||||||
|
names = name.split("_")
|
||||||
|
for i in names:
|
||||||
|
if i.isdigit():
|
||||||
|
return i, name.replace(f"_{i}_", "_blockid_")
|
||||||
|
return None, None
|
||||||
|
state_dict_ = {}
|
||||||
|
for name, param in state_dict.items():
|
||||||
|
block_id, source_name = guess_block_id(name)
|
||||||
|
if source_name in rename_dict:
|
||||||
|
target_name = rename_dict[source_name]
|
||||||
|
target_name = target_name.replace(".blockid.", f".{block_id}.")
|
||||||
|
state_dict_[target_name] = param
|
||||||
|
else:
|
||||||
|
state_dict_[name] = param
|
||||||
|
return state_dict_
|
||||||
|
|||||||
20
diffsynth/utils/lora/reset_rank.py
Normal file
20
diffsynth/utils/lora/reset_rank.py
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
import torch
|
||||||
|
|
||||||
|
def decomposite(tensor_A, tensor_B, rank):
|
||||||
|
dtype, device = tensor_A.dtype, tensor_A.device
|
||||||
|
weight = tensor_B @ tensor_A
|
||||||
|
U, S, V = torch.pca_lowrank(weight.float(), q=rank)
|
||||||
|
tensor_A = (V.T).to(dtype=dtype, device=device).contiguous()
|
||||||
|
tensor_B = (U @ torch.diag(S)).to(dtype=dtype, device=device).contiguous()
|
||||||
|
return tensor_A, tensor_B
|
||||||
|
|
||||||
|
def reset_lora_rank(lora, rank):
|
||||||
|
lora_merged = {}
|
||||||
|
keys = [i for i in lora.keys() if ".lora_A." in i]
|
||||||
|
for key in keys:
|
||||||
|
tensor_A = lora[key]
|
||||||
|
tensor_B = lora[key.replace(".lora_A.", ".lora_B.")]
|
||||||
|
tensor_A, tensor_B = decomposite(tensor_A, tensor_B, rank)
|
||||||
|
lora_merged[key] = tensor_A
|
||||||
|
lora_merged[key.replace(".lora_A.", ".lora_B.")] = tensor_B
|
||||||
|
return lora_merged
|
||||||
@@ -5,19 +5,20 @@ from xfuser.core.distributed import (get_sequence_parallel_rank,
|
|||||||
get_sequence_parallel_world_size,
|
get_sequence_parallel_world_size,
|
||||||
get_sp_group)
|
get_sp_group)
|
||||||
from xfuser.core.long_ctx_attention import xFuserLongContextAttention
|
from xfuser.core.long_ctx_attention import xFuserLongContextAttention
|
||||||
|
from ...core.device import parse_nccl_backend, parse_device_type
|
||||||
|
|
||||||
|
|
||||||
def initialize_usp():
|
def initialize_usp(device_type):
|
||||||
import torch.distributed as dist
|
import torch.distributed as dist
|
||||||
from xfuser.core.distributed import initialize_model_parallel, init_distributed_environment
|
from xfuser.core.distributed import initialize_model_parallel, init_distributed_environment
|
||||||
dist.init_process_group(backend="nccl", init_method="env://")
|
dist.init_process_group(backend=parse_nccl_backend(device_type), init_method="env://")
|
||||||
init_distributed_environment(rank=dist.get_rank(), world_size=dist.get_world_size())
|
init_distributed_environment(rank=dist.get_rank(), world_size=dist.get_world_size())
|
||||||
initialize_model_parallel(
|
initialize_model_parallel(
|
||||||
sequence_parallel_degree=dist.get_world_size(),
|
sequence_parallel_degree=dist.get_world_size(),
|
||||||
ring_degree=1,
|
ring_degree=1,
|
||||||
ulysses_degree=dist.get_world_size(),
|
ulysses_degree=dist.get_world_size(),
|
||||||
)
|
)
|
||||||
torch.cuda.set_device(dist.get_rank())
|
getattr(torch, device_type).set_device(dist.get_rank())
|
||||||
|
|
||||||
|
|
||||||
def sinusoidal_embedding_1d(dim, position):
|
def sinusoidal_embedding_1d(dim, position):
|
||||||
@@ -141,5 +142,5 @@ def usp_attn_forward(self, x, freqs):
|
|||||||
x = x.flatten(2)
|
x = x.flatten(2)
|
||||||
|
|
||||||
del q, k, v
|
del q, k, v
|
||||||
torch.cuda.empty_cache()
|
getattr(torch, parse_device_type(x.device)).empty_cache()
|
||||||
return self.o(x)
|
return self.o(x)
|
||||||
@@ -93,6 +93,7 @@ graph LR;
|
|||||||
| [DiffSynth-Studio/Qwen-Image-Blockwise-ControlNet-Inpaint](https://modelscope.cn/models/DiffSynth-Studio/Qwen-Image-Blockwise-ControlNet-Inpaint) | [code](/examples/qwen_image/model_inference/Qwen-Image-Blockwise-ControlNet-Inpaint.py) | [code](/examples/qwen_image/model_inference_low_vram/Qwen-Image-Blockwise-ControlNet-Inpaint.py) | [code](/examples/qwen_image/model_training/full/Qwen-Image-Blockwise-ControlNet-Inpaint.sh) | [code](/examples/qwen_image/model_training/validate_full/Qwen-Image-Blockwise-ControlNet-Inpaint.py) | [code](/examples/qwen_image/model_training/lora/Qwen-Image-Blockwise-ControlNet-Inpaint.sh) | [code](/examples/qwen_image/model_training/validate_lora/Qwen-Image-Blockwise-ControlNet-Inpaint.py) |
|
| [DiffSynth-Studio/Qwen-Image-Blockwise-ControlNet-Inpaint](https://modelscope.cn/models/DiffSynth-Studio/Qwen-Image-Blockwise-ControlNet-Inpaint) | [code](/examples/qwen_image/model_inference/Qwen-Image-Blockwise-ControlNet-Inpaint.py) | [code](/examples/qwen_image/model_inference_low_vram/Qwen-Image-Blockwise-ControlNet-Inpaint.py) | [code](/examples/qwen_image/model_training/full/Qwen-Image-Blockwise-ControlNet-Inpaint.sh) | [code](/examples/qwen_image/model_training/validate_full/Qwen-Image-Blockwise-ControlNet-Inpaint.py) | [code](/examples/qwen_image/model_training/lora/Qwen-Image-Blockwise-ControlNet-Inpaint.sh) | [code](/examples/qwen_image/model_training/validate_lora/Qwen-Image-Blockwise-ControlNet-Inpaint.py) |
|
||||||
| [DiffSynth-Studio/Qwen-Image-In-Context-Control-Union](https://www.modelscope.cn/models/DiffSynth-Studio/Qwen-Image-In-Context-Control-Union) | [code](/examples/qwen_image/model_inference/Qwen-Image-In-Context-Control-Union.py) | [code](/examples/qwen_image/model_inference_low_vram/Qwen-Image-In-Context-Control-Union.py) | - | - | [code](/examples/qwen_image/model_training/lora/Qwen-Image-In-Context-Control-Union.sh) | [code](/examples/qwen_image/model_training/validate_lora/Qwen-Image-In-Context-Control-Union.py) |
|
| [DiffSynth-Studio/Qwen-Image-In-Context-Control-Union](https://www.modelscope.cn/models/DiffSynth-Studio/Qwen-Image-In-Context-Control-Union) | [code](/examples/qwen_image/model_inference/Qwen-Image-In-Context-Control-Union.py) | [code](/examples/qwen_image/model_inference_low_vram/Qwen-Image-In-Context-Control-Union.py) | - | - | [code](/examples/qwen_image/model_training/lora/Qwen-Image-In-Context-Control-Union.sh) | [code](/examples/qwen_image/model_training/validate_lora/Qwen-Image-In-Context-Control-Union.py) |
|
||||||
| [DiffSynth-Studio/Qwen-Image-Edit-Lowres-Fix](https://www.modelscope.cn/models/DiffSynth-Studio/Qwen-Image-Edit-Lowres-Fix) | [code](/examples/qwen_image/model_inference/Qwen-Image-Edit-Lowres-Fix.py) | [code](/examples/qwen_image/model_inference_low_vram/Qwen-Image-Edit-Lowres-Fix.py) | - | - | - | - |
|
| [DiffSynth-Studio/Qwen-Image-Edit-Lowres-Fix](https://www.modelscope.cn/models/DiffSynth-Studio/Qwen-Image-Edit-Lowres-Fix) | [code](/examples/qwen_image/model_inference/Qwen-Image-Edit-Lowres-Fix.py) | [code](/examples/qwen_image/model_inference_low_vram/Qwen-Image-Edit-Lowres-Fix.py) | - | - | - | - |
|
||||||
|
|[DiffSynth-Studio/Qwen-Image-i2L](https://www.modelscope.cn/models/DiffSynth-Studio/Qwen-Image-i2L)|[code](/examples/qwen_image/model_inference/Qwen-Image-i2L.py)|[code](/examples/qwen_image/model_inference_low_vram/Qwen-Image-i2L.py)|-|-|-|-|
|
||||||
|
|
||||||
Special Training Scripts:
|
Special Training Scripts:
|
||||||
|
|
||||||
|
|||||||
@@ -138,4 +138,4 @@ Training Tips:
|
|||||||
* Differential LoRA Training ([code](/examples/z_image/model_training/special/differential_training/)) + Acceleration Configuration Inference
|
* Differential LoRA Training ([code](/examples/z_image/model_training/special/differential_training/)) + Acceleration Configuration Inference
|
||||||
* An additional LoRA needs to be loaded in differential LoRA training, e.g., [ostris/zimage_turbo_training_adapter](https://www.modelscope.cn/models/ostris/zimage_turbo_training_adapter)
|
* An additional LoRA needs to be loaded in differential LoRA training, e.g., [ostris/zimage_turbo_training_adapter](https://www.modelscope.cn/models/ostris/zimage_turbo_training_adapter)
|
||||||
* Standard SFT Training ([code](/examples/z_image/model_training/lora/Z-Image-Turbo.sh)) + Trajectory Imitation Distillation Training ([code](/examples/z_image/model_training/special/trajectory_imitation/)) + Acceleration Configuration Inference
|
* Standard SFT Training ([code](/examples/z_image/model_training/lora/Z-Image-Turbo.sh)) + Trajectory Imitation Distillation Training ([code](/examples/z_image/model_training/special/trajectory_imitation/)) + Acceleration Configuration Inference
|
||||||
* Standard SFT Training ([code](/examples/z_image/model_training/lora/Z-Image-Turbo.sh)) + Load Distillation Acceleration LoRA During Inference ([model](https://www.modelscope.cn/models/DiffSynth-Studio/Z-Image-Turbo-DistillFix)) + Acceleration Configuration Inference
|
* Standard SFT Training ([code](/examples/z_image/model_training/lora/Z-Image-Turbo.sh)) + Load Distillation Acceleration LoRA During Inference ([model](https://www.modelscope.cn/models/DiffSynth-Studio/Z-Image-Turbo-DistillPatch)) + Acceleration Configuration Inference
|
||||||
|
|||||||
58
docs/en/Pipeline_Usage/GPU_support.md
Normal file
58
docs/en/Pipeline_Usage/GPU_support.md
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
# GPU/NPU Support
|
||||||
|
|
||||||
|
`DiffSynth-Studio` supports various GPUs and NPUs. This document explains how to run model inference and training on these devices.
|
||||||
|
|
||||||
|
Before you begin, please follow the [Installation Guide](/docs/en/Pipeline_Usage/Setup.md) to install the required GPU/NPU dependencies.
|
||||||
|
|
||||||
|
## NVIDIA GPU
|
||||||
|
|
||||||
|
All sample code provided by this project supports NVIDIA GPUs by default, requiring no additional modifications.
|
||||||
|
|
||||||
|
## AMD GPU
|
||||||
|
|
||||||
|
AMD provides PyTorch packages based on ROCm, so most models can run without code changes. A small number of models may not be compatible due to their reliance on CUDA-specific instructions.
|
||||||
|
|
||||||
|
## Ascend NPU
|
||||||
|
|
||||||
|
When using Ascend NPU, you need to replace `"cuda"` with `"npu"` in your code.
|
||||||
|
|
||||||
|
For example, here is the inference code for **Wan2.1-T2V-1.3B**, modified for Ascend NPU:
|
||||||
|
|
||||||
|
```diff
|
||||||
|
import torch
|
||||||
|
from diffsynth.utils.data import save_video, VideoData
|
||||||
|
from diffsynth.pipelines.wan_video import WanVideoPipeline, ModelConfig
|
||||||
|
|
||||||
|
vram_config = {
|
||||||
|
"offload_dtype": "disk",
|
||||||
|
"offload_device": "disk",
|
||||||
|
"onload_dtype": torch.bfloat16,
|
||||||
|
"onload_device": "cpu",
|
||||||
|
"preparing_dtype": torch.bfloat16,
|
||||||
|
- "preparing_device": "cuda",
|
||||||
|
+ "preparing_device": "npu",
|
||||||
|
"computation_dtype": torch.bfloat16,
|
||||||
|
- "computation_device": "cuda",
|
||||||
|
+ "computation_device": "npu",
|
||||||
|
}
|
||||||
|
pipe = WanVideoPipeline.from_pretrained(
|
||||||
|
torch_dtype=torch.bfloat16,
|
||||||
|
- device="cuda",
|
||||||
|
+ device="npu",
|
||||||
|
model_configs=[
|
||||||
|
ModelConfig(model_id="Wan-AI/Wan2.1-T2V-1.3B", origin_file_pattern="diffusion_pytorch_model*.safetensors", **vram_config),
|
||||||
|
ModelConfig(model_id="Wan-AI/Wan2.1-T2V-1.3B", origin_file_pattern="models_t5_umt5-xxl-enc-bf16.pth", **vram_config),
|
||||||
|
ModelConfig(model_id="Wan-AI/Wan2.1-T2V-1.3B", origin_file_pattern="Wan2.1_VAE.pth", **vram_config),
|
||||||
|
],
|
||||||
|
tokenizer_config=ModelConfig(model_id="Wan-AI/Wan2.1-T2V-1.3B", origin_file_pattern="google/umt5-xxl/"),
|
||||||
|
- vram_limit=torch.cuda.mem_get_info("cuda")[1] / (1024 ** 3) - 2,
|
||||||
|
+ vram_limit=torch.npu.mem_get_info("npu")[1] / (1024 ** 3) - 2,
|
||||||
|
)
|
||||||
|
|
||||||
|
video = pipe(
|
||||||
|
prompt="Documentary-style photography: a lively puppy running swiftly across lush green grass. The puppy has brownish-yellow fur, upright ears, and an alert, joyful expression. Sunlight bathes its body, making the fur appear exceptionally soft and shiny. The background is an open field with occasional wildflowers, and faint blue sky with scattered white clouds in the distance. Strong perspective captures the motion of the running puppy and the vitality of the surrounding grass. Mid-shot, side-moving viewpoint.",
|
||||||
|
negative_prompt="Overly vibrant colors, overexposed, static, blurry details, subtitles, artistic style, painting, still image, overall grayish tone, worst quality, low quality, JPEG artifacts, ugly, distorted, extra fingers, poorly drawn hands, poorly drawn faces, deformed, disfigured, fused fingers, motionless scene, cluttered background, three legs, many people in background, walking backward",
|
||||||
|
seed=0, tiled=True,
|
||||||
|
)
|
||||||
|
save_video(video, "video.mp4", fps=15, quality=5)
|
||||||
|
```
|
||||||
@@ -14,8 +14,35 @@ Install from PyPI (there may be delays in version updates; for latest features,
|
|||||||
pip install diffsynth
|
pip install diffsynth
|
||||||
```
|
```
|
||||||
|
|
||||||
If you encounter issues during installation, they may be caused by upstream dependency packages. Please refer to the documentation for these packages:
|
## GPU/NPU Support
|
||||||
|
|
||||||
|
* **NVIDIA GPU**
|
||||||
|
|
||||||
|
Install as described above.
|
||||||
|
|
||||||
|
* **AMD GPU**
|
||||||
|
|
||||||
|
You need to install the `torch` package with ROCm support. Taking ROCm 6.4 (as of the article update date: December 15, 2025) on Linux as an example, run the following command:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
pip install torch torchvision --index-url https://download.pytorch.org/whl/rocm6.4
|
||||||
|
```
|
||||||
|
|
||||||
|
* **Ascend NPU**
|
||||||
|
|
||||||
|
Ascend NPU support is provided via the `torch-npu` package. Taking version `2.1.0.post17` (as of the article update date: December 15, 2025) as an example, run the following command:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
pip install torch-npu==2.1.0.post17
|
||||||
|
```
|
||||||
|
|
||||||
|
When using Ascend NPU, please replace `"cuda"` with `"npu"` in your Python code. For details, see [NPU Support](/docs/en/Pipeline_Usage/GPU_support.md#ascend-npu).
|
||||||
|
|
||||||
|
## Other Installation Issues
|
||||||
|
|
||||||
|
If you encounter issues during installation, they may be caused by upstream dependencies. Please refer to the documentation for these packages:
|
||||||
|
|
||||||
* [torch](https://pytorch.org/get-started/locally/)
|
* [torch](https://pytorch.org/get-started/locally/)
|
||||||
|
* [Ascend/pytorch](https://github.com/Ascend/pytorch)
|
||||||
* [sentencepiece](https://github.com/google/sentencepiece)
|
* [sentencepiece](https://github.com/google/sentencepiece)
|
||||||
* [cmake](https://cmake.org)
|
* [cmake](https://cmake.org)
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ This section introduces the basic usage of `DiffSynth-Studio`, including how to
|
|||||||
* [VRAM Management](/docs/en/Pipeline_Usage/VRAM_management.md)
|
* [VRAM Management](/docs/en/Pipeline_Usage/VRAM_management.md)
|
||||||
* [Model Training](/docs/en/Pipeline_Usage/Model_Training.md)
|
* [Model Training](/docs/en/Pipeline_Usage/Model_Training.md)
|
||||||
* [Environment Variables](/docs/en/Pipeline_Usage/Environment_Variables.md)
|
* [Environment Variables](/docs/en/Pipeline_Usage/Environment_Variables.md)
|
||||||
|
* [GPU/NPU Support](/docs/en/Pipeline_Usage/GPU_support.md)
|
||||||
|
|
||||||
## Section 2: Model Details
|
## Section 2: Model Details
|
||||||
|
|
||||||
|
|||||||
@@ -93,6 +93,7 @@ graph LR;
|
|||||||
|[DiffSynth-Studio/Qwen-Image-Blockwise-ControlNet-Inpaint](https://modelscope.cn/models/DiffSynth-Studio/Qwen-Image-Blockwise-ControlNet-Inpaint)|[code](/examples/qwen_image/model_inference/Qwen-Image-Blockwise-ControlNet-Inpaint.py)|[code](/examples/qwen_image/model_inference_low_vram/Qwen-Image-Blockwise-ControlNet-Inpaint.py)|[code](/examples/qwen_image/model_training/full/Qwen-Image-Blockwise-ControlNet-Inpaint.sh)|[code](/examples/qwen_image/model_training/validate_full/Qwen-Image-Blockwise-ControlNet-Inpaint.py)|[code](/examples/qwen_image/model_training/lora/Qwen-Image-Blockwise-ControlNet-Inpaint.sh)|[code](/examples/qwen_image/model_training/validate_lora/Qwen-Image-Blockwise-ControlNet-Inpaint.py)|
|
|[DiffSynth-Studio/Qwen-Image-Blockwise-ControlNet-Inpaint](https://modelscope.cn/models/DiffSynth-Studio/Qwen-Image-Blockwise-ControlNet-Inpaint)|[code](/examples/qwen_image/model_inference/Qwen-Image-Blockwise-ControlNet-Inpaint.py)|[code](/examples/qwen_image/model_inference_low_vram/Qwen-Image-Blockwise-ControlNet-Inpaint.py)|[code](/examples/qwen_image/model_training/full/Qwen-Image-Blockwise-ControlNet-Inpaint.sh)|[code](/examples/qwen_image/model_training/validate_full/Qwen-Image-Blockwise-ControlNet-Inpaint.py)|[code](/examples/qwen_image/model_training/lora/Qwen-Image-Blockwise-ControlNet-Inpaint.sh)|[code](/examples/qwen_image/model_training/validate_lora/Qwen-Image-Blockwise-ControlNet-Inpaint.py)|
|
||||||
|[DiffSynth-Studio/Qwen-Image-In-Context-Control-Union](https://www.modelscope.cn/models/DiffSynth-Studio/Qwen-Image-In-Context-Control-Union)|[code](/examples/qwen_image/model_inference/Qwen-Image-In-Context-Control-Union.py)|[code](/examples/qwen_image/model_inference_low_vram/Qwen-Image-In-Context-Control-Union.py)|-|-|[code](/examples/qwen_image/model_training/lora/Qwen-Image-In-Context-Control-Union.sh)|[code](/examples/qwen_image/model_training/validate_lora/Qwen-Image-In-Context-Control-Union.py)|
|
|[DiffSynth-Studio/Qwen-Image-In-Context-Control-Union](https://www.modelscope.cn/models/DiffSynth-Studio/Qwen-Image-In-Context-Control-Union)|[code](/examples/qwen_image/model_inference/Qwen-Image-In-Context-Control-Union.py)|[code](/examples/qwen_image/model_inference_low_vram/Qwen-Image-In-Context-Control-Union.py)|-|-|[code](/examples/qwen_image/model_training/lora/Qwen-Image-In-Context-Control-Union.sh)|[code](/examples/qwen_image/model_training/validate_lora/Qwen-Image-In-Context-Control-Union.py)|
|
||||||
|[DiffSynth-Studio/Qwen-Image-Edit-Lowres-Fix](https://www.modelscope.cn/models/DiffSynth-Studio/Qwen-Image-Edit-Lowres-Fix)|[code](/examples/qwen_image/model_inference/Qwen-Image-Edit-Lowres-Fix.py)|[code](/examples/qwen_image/model_inference_low_vram/Qwen-Image-Edit-Lowres-Fix.py)|-|-|-|-|
|
|[DiffSynth-Studio/Qwen-Image-Edit-Lowres-Fix](https://www.modelscope.cn/models/DiffSynth-Studio/Qwen-Image-Edit-Lowres-Fix)|[code](/examples/qwen_image/model_inference/Qwen-Image-Edit-Lowres-Fix.py)|[code](/examples/qwen_image/model_inference_low_vram/Qwen-Image-Edit-Lowres-Fix.py)|-|-|-|-|
|
||||||
|
|[DiffSynth-Studio/Qwen-Image-i2L](https://www.modelscope.cn/models/DiffSynth-Studio/Qwen-Image-i2L)|[code](/examples/qwen_image/model_inference/Qwen-Image-i2L.py)|[code](/examples/qwen_image/model_inference_low_vram/Qwen-Image-i2L.py)|-|-|-|-|
|
||||||
|
|
||||||
特殊训练脚本:
|
特殊训练脚本:
|
||||||
|
|
||||||
|
|||||||
@@ -138,4 +138,4 @@ modelscope download --dataset DiffSynth-Studio/example_image_dataset --local_dir
|
|||||||
* 差分 LoRA 训练([code](/examples/z_image/model_training/special/differential_training/)) + 加速配置推理
|
* 差分 LoRA 训练([code](/examples/z_image/model_training/special/differential_training/)) + 加速配置推理
|
||||||
* 差分 LoRA 训练中需加载一个额外的 LoRA,例如 [ostris/zimage_turbo_training_adapter](https://www.modelscope.cn/models/ostris/zimage_turbo_training_adapter)
|
* 差分 LoRA 训练中需加载一个额外的 LoRA,例如 [ostris/zimage_turbo_training_adapter](https://www.modelscope.cn/models/ostris/zimage_turbo_training_adapter)
|
||||||
* 标准 SFT 训练([code](/examples/z_image/model_training/lora/Z-Image-Turbo.sh))+ 轨迹模仿蒸馏训练([code](/examples/z_image/model_training/special/trajectory_imitation/))+ 加速配置推理
|
* 标准 SFT 训练([code](/examples/z_image/model_training/lora/Z-Image-Turbo.sh))+ 轨迹模仿蒸馏训练([code](/examples/z_image/model_training/special/trajectory_imitation/))+ 加速配置推理
|
||||||
* 标准 SFT 训练([code](/examples/z_image/model_training/lora/Z-Image-Turbo.sh))+ 推理时加载蒸馏加速 LoRA([model](https://www.modelscope.cn/models/DiffSynth-Studio/Z-Image-Turbo-DistillFix)) + 加速配置推理
|
* 标准 SFT 训练([code](/examples/z_image/model_training/lora/Z-Image-Turbo.sh))+ 推理时加载蒸馏加速 LoRA([model](https://www.modelscope.cn/models/DiffSynth-Studio/Z-Image-Turbo-DistillPatch)) + 加速配置推理
|
||||||
|
|||||||
58
docs/zh/Pipeline_Usage/GPU_support.md
Normal file
58
docs/zh/Pipeline_Usage/GPU_support.md
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
# GPU/NPU 支持
|
||||||
|
|
||||||
|
`DiffSynth-Studio` 支持多种 GPU/NPU,本文介绍如何在这些设备上运行模型推理和训练。
|
||||||
|
|
||||||
|
在开始前,请参考[安装依赖](/docs/zh/Pipeline_Usage/Setup.md)安装好 GPU/NPU 相关的依赖包。
|
||||||
|
|
||||||
|
## NVIDIA GPU
|
||||||
|
|
||||||
|
本项目提供的所有样例代码默认支持 NVIDIA GPU,无需额外修改。
|
||||||
|
|
||||||
|
## AMD GPU
|
||||||
|
|
||||||
|
AMD 提供了基于 ROCm 的 torch 包,所以大多数模型无需修改代码即可运行,少数模型由于依赖特定的 cuda 指令无法运行。
|
||||||
|
|
||||||
|
## Ascend NPU
|
||||||
|
|
||||||
|
使用 Ascend NPU 时,需把代码中的 `"cuda"` 改为 `"npu"`。
|
||||||
|
|
||||||
|
例如,Wan2.1-T2V-1.3B 的推理代码:
|
||||||
|
|
||||||
|
```diff
|
||||||
|
import torch
|
||||||
|
from diffsynth.utils.data import save_video, VideoData
|
||||||
|
from diffsynth.pipelines.wan_video import WanVideoPipeline, ModelConfig
|
||||||
|
|
||||||
|
vram_config = {
|
||||||
|
"offload_dtype": "disk",
|
||||||
|
"offload_device": "disk",
|
||||||
|
"onload_dtype": torch.bfloat16,
|
||||||
|
"onload_device": "cpu",
|
||||||
|
"preparing_dtype": torch.bfloat16,
|
||||||
|
- "preparing_device": "cuda",
|
||||||
|
+ "preparing_device": "npu",
|
||||||
|
"computation_dtype": torch.bfloat16,
|
||||||
|
- "computation_device": "cuda",
|
||||||
|
+ "preparing_device": "npu",
|
||||||
|
}
|
||||||
|
pipe = WanVideoPipeline.from_pretrained(
|
||||||
|
torch_dtype=torch.bfloat16,
|
||||||
|
- device="cuda",
|
||||||
|
+ device="npu",
|
||||||
|
model_configs=[
|
||||||
|
ModelConfig(model_id="Wan-AI/Wan2.1-T2V-1.3B", origin_file_pattern="diffusion_pytorch_model*.safetensors", **vram_config),
|
||||||
|
ModelConfig(model_id="Wan-AI/Wan2.1-T2V-1.3B", origin_file_pattern="models_t5_umt5-xxl-enc-bf16.pth", **vram_config),
|
||||||
|
ModelConfig(model_id="Wan-AI/Wan2.1-T2V-1.3B", origin_file_pattern="Wan2.1_VAE.pth", **vram_config),
|
||||||
|
],
|
||||||
|
tokenizer_config=ModelConfig(model_id="Wan-AI/Wan2.1-T2V-1.3B", origin_file_pattern="google/umt5-xxl/"),
|
||||||
|
- vram_limit=torch.cuda.mem_get_info("cuda")[1] / (1024 ** 3) - 2,
|
||||||
|
+ vram_limit=torch.npu.mem_get_info("npu")[1] / (1024 ** 3) - 2,
|
||||||
|
)
|
||||||
|
|
||||||
|
video = pipe(
|
||||||
|
prompt="纪实摄影风格画面,一只活泼的小狗在绿茵茵的草地上迅速奔跑。小狗毛色棕黄,两只耳朵立起,神情专注而欢快。阳光洒在它身上,使得毛发看上去格外柔软而闪亮。背景是一片开阔的草地,偶尔点缀着几朵野花,远处隐约可见蓝天和几片白云。透视感鲜明,捕捉小狗奔跑时的动感和四周草地的生机。中景侧面移动视角。",
|
||||||
|
negative_prompt="色调艳丽,过曝,静态,细节模糊不清,字幕,风格,作品,画作,画面,静止,整体发灰,最差质量,低质量,JPEG压缩残留,丑陋的,残缺的,多余的手指,画得不好的手部,画得不好的脸部,畸形的,毁容的,形态畸形的肢体,手指融合,静止不动的画面,杂乱的背景,三条腿,背景人很多,倒着走",
|
||||||
|
seed=0, tiled=True,
|
||||||
|
)
|
||||||
|
save_video(video, "video.mp4", fps=15, quality=5)
|
||||||
|
```
|
||||||
@@ -14,8 +14,35 @@ pip install -e .
|
|||||||
pip install diffsynth
|
pip install diffsynth
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## GPU/NPU 支持
|
||||||
|
|
||||||
|
* NVIDIA GPU
|
||||||
|
|
||||||
|
按照以上方式安装即可。
|
||||||
|
|
||||||
|
* AMD GPU
|
||||||
|
|
||||||
|
需安装支持 ROCm 的 `torch` 包,以 ROCm 6.4(本文更新于 2025 年 12 月 15 日)、Linux 系统为例,请运行以下命令
|
||||||
|
|
||||||
|
```shell
|
||||||
|
pip install torch torchvision --index-url https://download.pytorch.org/whl/rocm6.4
|
||||||
|
```
|
||||||
|
|
||||||
|
* Ascend NPU
|
||||||
|
|
||||||
|
Ascend NPU 通过 `torch-npu` 包提供支持,以 `2.1.0.post17` 版本(本文更新于 2025 年 12 月 15 日)为例,请运行以下命令
|
||||||
|
|
||||||
|
```shell
|
||||||
|
pip install torch-npu==2.1.0.post17
|
||||||
|
```
|
||||||
|
|
||||||
|
使用 Ascend NPU 时,请将 Python 代码中的 `"cuda"` 改为 `"npu"`,详见[NPU 支持](/docs/zh/Pipeline_Usage/GPU_support.md#ascend-npu)。
|
||||||
|
|
||||||
|
## 其他安装问题
|
||||||
|
|
||||||
如果在安装过程中遇到问题,可能是由上游依赖包导致的,请参考这些包的文档:
|
如果在安装过程中遇到问题,可能是由上游依赖包导致的,请参考这些包的文档:
|
||||||
|
|
||||||
* [torch](https://pytorch.org/get-started/locally/)
|
* [torch](https://pytorch.org/get-started/locally/)
|
||||||
|
* [Ascend/pytorch](https://github.com/Ascend/pytorch)
|
||||||
* [sentencepiece](https://github.com/google/sentencepiece)
|
* [sentencepiece](https://github.com/google/sentencepiece)
|
||||||
* [cmake](https://cmake.org)
|
* [cmake](https://cmake.org)
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ graph LR;
|
|||||||
* [显存管理](/docs/zh/Pipeline_Usage/VRAM_management.md)
|
* [显存管理](/docs/zh/Pipeline_Usage/VRAM_management.md)
|
||||||
* [模型训练](/docs/zh/Pipeline_Usage/Model_Training.md)
|
* [模型训练](/docs/zh/Pipeline_Usage/Model_Training.md)
|
||||||
* [环境变量](/docs/zh/Pipeline_Usage/Environment_Variables.md)
|
* [环境变量](/docs/zh/Pipeline_Usage/Environment_Variables.md)
|
||||||
|
* [GPU/NPU 支持](/docs/zh/Pipeline_Usage/GPU_support.md)
|
||||||
|
|
||||||
## Section 2: 模型详解
|
## Section 2: 模型详解
|
||||||
|
|
||||||
|
|||||||
110
examples/qwen_image/model_inference/Qwen-Image-i2L.py
Normal file
110
examples/qwen_image/model_inference/Qwen-Image-i2L.py
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
from diffsynth.pipelines.qwen_image import (
|
||||||
|
QwenImagePipeline, ModelConfig,
|
||||||
|
QwenImageUnit_Image2LoRAEncode, QwenImageUnit_Image2LoRADecode
|
||||||
|
)
|
||||||
|
from diffsynth.utils.lora import merge_lora
|
||||||
|
from diffsynth import load_state_dict
|
||||||
|
from modelscope import snapshot_download
|
||||||
|
from safetensors.torch import save_file
|
||||||
|
import torch
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
|
|
||||||
|
def demo_style():
|
||||||
|
# Load models
|
||||||
|
pipe = QwenImagePipeline.from_pretrained(
|
||||||
|
torch_dtype=torch.bfloat16,
|
||||||
|
device="cuda",
|
||||||
|
model_configs=[
|
||||||
|
ModelConfig(model_id="DiffSynth-Studio/General-Image-Encoders", origin_file_pattern="SigLIP2-G384/model.safetensors"),
|
||||||
|
ModelConfig(model_id="DiffSynth-Studio/General-Image-Encoders", origin_file_pattern="DINOv3-7B/model.safetensors"),
|
||||||
|
ModelConfig(model_id="DiffSynth-Studio/Qwen-Image-i2L", origin_file_pattern="Qwen-Image-i2L-Style.safetensors"),
|
||||||
|
],
|
||||||
|
processor_config=ModelConfig(model_id="Qwen/Qwen-Image-Edit", origin_file_pattern="processor/"),
|
||||||
|
)
|
||||||
|
|
||||||
|
# Load images
|
||||||
|
snapshot_download(
|
||||||
|
model_id="DiffSynth-Studio/Qwen-Image-i2L",
|
||||||
|
allow_file_pattern="assets/style/1/*",
|
||||||
|
local_dir="data/examples"
|
||||||
|
)
|
||||||
|
images = [
|
||||||
|
Image.open("data/examples/assets/style/1/0.jpg"),
|
||||||
|
Image.open("data/examples/assets/style/1/1.jpg"),
|
||||||
|
Image.open("data/examples/assets/style/1/2.jpg"),
|
||||||
|
Image.open("data/examples/assets/style/1/3.jpg"),
|
||||||
|
Image.open("data/examples/assets/style/1/4.jpg"),
|
||||||
|
]
|
||||||
|
|
||||||
|
# Model inference
|
||||||
|
with torch.no_grad():
|
||||||
|
embs = QwenImageUnit_Image2LoRAEncode().process(pipe, image2lora_images=images)
|
||||||
|
lora = QwenImageUnit_Image2LoRADecode().process(pipe, **embs)["lora"]
|
||||||
|
save_file(lora, "model_style.safetensors")
|
||||||
|
|
||||||
|
|
||||||
|
def demo_coarse_fine_bias():
|
||||||
|
# Load models
|
||||||
|
pipe = QwenImagePipeline.from_pretrained(
|
||||||
|
torch_dtype=torch.bfloat16,
|
||||||
|
device="cuda",
|
||||||
|
model_configs=[
|
||||||
|
ModelConfig(model_id="Qwen/Qwen-Image", origin_file_pattern="text_encoder/model*.safetensors"),
|
||||||
|
ModelConfig(model_id="DiffSynth-Studio/General-Image-Encoders", origin_file_pattern="SigLIP2-G384/model.safetensors"),
|
||||||
|
ModelConfig(model_id="DiffSynth-Studio/General-Image-Encoders", origin_file_pattern="DINOv3-7B/model.safetensors"),
|
||||||
|
ModelConfig(model_id="DiffSynth-Studio/Qwen-Image-i2L", origin_file_pattern="Qwen-Image-i2L-Coarse.safetensors"),
|
||||||
|
ModelConfig(model_id="DiffSynth-Studio/Qwen-Image-i2L", origin_file_pattern="Qwen-Image-i2L-Fine.safetensors"),
|
||||||
|
],
|
||||||
|
processor_config=ModelConfig(model_id="Qwen/Qwen-Image-Edit", origin_file_pattern="processor/"),
|
||||||
|
)
|
||||||
|
|
||||||
|
# Load images
|
||||||
|
snapshot_download(
|
||||||
|
model_id="DiffSynth-Studio/Qwen-Image-i2L",
|
||||||
|
allow_file_pattern="assets/lora/3/*",
|
||||||
|
local_dir="data/examples"
|
||||||
|
)
|
||||||
|
images = [
|
||||||
|
Image.open("data/examples/assets/lora/3/0.jpg"),
|
||||||
|
Image.open("data/examples/assets/lora/3/1.jpg"),
|
||||||
|
Image.open("data/examples/assets/lora/3/2.jpg"),
|
||||||
|
Image.open("data/examples/assets/lora/3/3.jpg"),
|
||||||
|
Image.open("data/examples/assets/lora/3/4.jpg"),
|
||||||
|
Image.open("data/examples/assets/lora/3/5.jpg"),
|
||||||
|
]
|
||||||
|
|
||||||
|
# Model inference
|
||||||
|
with torch.no_grad():
|
||||||
|
embs = QwenImageUnit_Image2LoRAEncode().process(pipe, image2lora_images=images)
|
||||||
|
lora = QwenImageUnit_Image2LoRADecode().process(pipe, **embs)["lora"]
|
||||||
|
lora_bias = ModelConfig(model_id="DiffSynth-Studio/Qwen-Image-i2L", origin_file_pattern="Qwen-Image-i2L-Bias.safetensors")
|
||||||
|
lora_bias.download_if_necessary()
|
||||||
|
lora_bias = load_state_dict(lora_bias.path, torch_dtype=torch.bfloat16, device="cuda")
|
||||||
|
lora = merge_lora([lora, lora_bias])
|
||||||
|
save_file(lora, "model_coarse_fine_bias.safetensors")
|
||||||
|
|
||||||
|
|
||||||
|
def generate_image(lora_path, prompt, seed):
|
||||||
|
pipe = QwenImagePipeline.from_pretrained(
|
||||||
|
torch_dtype=torch.bfloat16,
|
||||||
|
device="cuda",
|
||||||
|
model_configs=[
|
||||||
|
ModelConfig(model_id="Qwen/Qwen-Image", origin_file_pattern="transformer/diffusion_pytorch_model*.safetensors"),
|
||||||
|
ModelConfig(model_id="Qwen/Qwen-Image", origin_file_pattern="text_encoder/model*.safetensors"),
|
||||||
|
ModelConfig(model_id="Qwen/Qwen-Image", origin_file_pattern="vae/diffusion_pytorch_model.safetensors"),
|
||||||
|
],
|
||||||
|
tokenizer_config=ModelConfig(model_id="Qwen/Qwen-Image", origin_file_pattern="tokenizer/"),
|
||||||
|
)
|
||||||
|
pipe.load_lora(pipe.dit, lora_path)
|
||||||
|
image = pipe(prompt, seed=seed, height=1024, width=1024, num_inference_steps=50)
|
||||||
|
return image
|
||||||
|
|
||||||
|
|
||||||
|
demo_style()
|
||||||
|
image = generate_image("model_style.safetensors", "a cat", 0)
|
||||||
|
image.save("image_1.jpg")
|
||||||
|
|
||||||
|
demo_coarse_fine_bias()
|
||||||
|
image = generate_image("model_coarse_fine_bias.safetensors", "bowl", 1)
|
||||||
|
image.save("image_2.jpg")
|
||||||
134
examples/qwen_image/model_inference_low_vram/Qwen-Image-i2L.py
Normal file
134
examples/qwen_image/model_inference_low_vram/Qwen-Image-i2L.py
Normal file
@@ -0,0 +1,134 @@
|
|||||||
|
from diffsynth.pipelines.qwen_image import (
|
||||||
|
QwenImagePipeline, ModelConfig,
|
||||||
|
QwenImageUnit_Image2LoRAEncode, QwenImageUnit_Image2LoRADecode
|
||||||
|
)
|
||||||
|
from diffsynth.utils.lora import merge_lora
|
||||||
|
from diffsynth import load_state_dict
|
||||||
|
from modelscope import snapshot_download
|
||||||
|
from safetensors.torch import save_file
|
||||||
|
import torch
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
|
|
||||||
|
vram_config = {
|
||||||
|
"offload_dtype": "disk",
|
||||||
|
"offload_device": "disk",
|
||||||
|
"onload_dtype": torch.bfloat16,
|
||||||
|
"onload_device": "cpu",
|
||||||
|
"preparing_dtype": torch.bfloat16,
|
||||||
|
"preparing_device": "cuda",
|
||||||
|
"computation_dtype": torch.bfloat16,
|
||||||
|
"computation_device": "cuda",
|
||||||
|
}
|
||||||
|
vram_config_disk_offload = {
|
||||||
|
"offload_dtype": "disk",
|
||||||
|
"offload_device": "disk",
|
||||||
|
"onload_dtype": "disk",
|
||||||
|
"onload_device": "disk",
|
||||||
|
"preparing_dtype": torch.bfloat16,
|
||||||
|
"preparing_device": "cuda",
|
||||||
|
"computation_dtype": torch.bfloat16,
|
||||||
|
"computation_device": "cuda",
|
||||||
|
}
|
||||||
|
|
||||||
|
def demo_style():
|
||||||
|
# Load models
|
||||||
|
pipe = QwenImagePipeline.from_pretrained(
|
||||||
|
torch_dtype=torch.bfloat16,
|
||||||
|
device="cuda",
|
||||||
|
model_configs=[
|
||||||
|
ModelConfig(model_id="DiffSynth-Studio/General-Image-Encoders", origin_file_pattern="SigLIP2-G384/model.safetensors", **vram_config_disk_offload),
|
||||||
|
ModelConfig(model_id="DiffSynth-Studio/General-Image-Encoders", origin_file_pattern="DINOv3-7B/model.safetensors", **vram_config_disk_offload),
|
||||||
|
ModelConfig(model_id="DiffSynth-Studio/Qwen-Image-i2L", origin_file_pattern="Qwen-Image-i2L-Style.safetensors", **vram_config_disk_offload),
|
||||||
|
],
|
||||||
|
processor_config=ModelConfig(model_id="Qwen/Qwen-Image-Edit", origin_file_pattern="processor/"),
|
||||||
|
vram_limit=torch.cuda.mem_get_info("cuda")[1] / (1024 ** 3) - 0.5,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Load images
|
||||||
|
snapshot_download(
|
||||||
|
model_id="DiffSynth-Studio/Qwen-Image-i2L",
|
||||||
|
allow_file_pattern="assets/style/1/*",
|
||||||
|
local_dir="data/examples"
|
||||||
|
)
|
||||||
|
images = [
|
||||||
|
Image.open("data/examples/assets/style/1/0.jpg"),
|
||||||
|
Image.open("data/examples/assets/style/1/1.jpg"),
|
||||||
|
Image.open("data/examples/assets/style/1/2.jpg"),
|
||||||
|
Image.open("data/examples/assets/style/1/3.jpg"),
|
||||||
|
Image.open("data/examples/assets/style/1/4.jpg"),
|
||||||
|
]
|
||||||
|
|
||||||
|
# Model inference
|
||||||
|
with torch.no_grad():
|
||||||
|
embs = QwenImageUnit_Image2LoRAEncode().process(pipe, image2lora_images=images)
|
||||||
|
lora = QwenImageUnit_Image2LoRADecode().process(pipe, **embs)["lora"]
|
||||||
|
save_file(lora, "model_style.safetensors")
|
||||||
|
|
||||||
|
|
||||||
|
def demo_coarse_fine_bias():
|
||||||
|
# Load models
|
||||||
|
pipe = QwenImagePipeline.from_pretrained(
|
||||||
|
torch_dtype=torch.bfloat16,
|
||||||
|
device="cuda",
|
||||||
|
model_configs=[
|
||||||
|
ModelConfig(model_id="Qwen/Qwen-Image", origin_file_pattern="text_encoder/model*.safetensors", **vram_config_disk_offload),
|
||||||
|
ModelConfig(model_id="DiffSynth-Studio/General-Image-Encoders", origin_file_pattern="SigLIP2-G384/model.safetensors", **vram_config_disk_offload),
|
||||||
|
ModelConfig(model_id="DiffSynth-Studio/General-Image-Encoders", origin_file_pattern="DINOv3-7B/model.safetensors", **vram_config_disk_offload),
|
||||||
|
ModelConfig(model_id="DiffSynth-Studio/Qwen-Image-i2L", origin_file_pattern="Qwen-Image-i2L-Coarse.safetensors", **vram_config_disk_offload),
|
||||||
|
ModelConfig(model_id="DiffSynth-Studio/Qwen-Image-i2L", origin_file_pattern="Qwen-Image-i2L-Fine.safetensors", **vram_config_disk_offload),
|
||||||
|
],
|
||||||
|
processor_config=ModelConfig(model_id="Qwen/Qwen-Image-Edit", origin_file_pattern="processor/"),
|
||||||
|
vram_limit=torch.cuda.mem_get_info("cuda")[1] / (1024 ** 3) - 0.5,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Load images
|
||||||
|
snapshot_download(
|
||||||
|
model_id="DiffSynth-Studio/Qwen-Image-i2L",
|
||||||
|
allow_file_pattern="assets/lora/3/*",
|
||||||
|
local_dir="data/examples"
|
||||||
|
)
|
||||||
|
images = [
|
||||||
|
Image.open("data/examples/assets/lora/3/0.jpg"),
|
||||||
|
Image.open("data/examples/assets/lora/3/1.jpg"),
|
||||||
|
Image.open("data/examples/assets/lora/3/2.jpg"),
|
||||||
|
Image.open("data/examples/assets/lora/3/3.jpg"),
|
||||||
|
Image.open("data/examples/assets/lora/3/4.jpg"),
|
||||||
|
Image.open("data/examples/assets/lora/3/5.jpg"),
|
||||||
|
]
|
||||||
|
|
||||||
|
# Model inference
|
||||||
|
with torch.no_grad():
|
||||||
|
embs = QwenImageUnit_Image2LoRAEncode().process(pipe, image2lora_images=images)
|
||||||
|
lora = QwenImageUnit_Image2LoRADecode().process(pipe, **embs)["lora"]
|
||||||
|
lora_bias = ModelConfig(model_id="DiffSynth-Studio/Qwen-Image-i2L", origin_file_pattern="Qwen-Image-i2L-Bias.safetensors")
|
||||||
|
lora_bias.download_if_necessary()
|
||||||
|
lora_bias = load_state_dict(lora_bias.path, torch_dtype=torch.bfloat16, device="cuda")
|
||||||
|
lora = merge_lora([lora, lora_bias])
|
||||||
|
save_file(lora, "model_coarse_fine_bias.safetensors")
|
||||||
|
|
||||||
|
|
||||||
|
def generate_image(lora_path, prompt, seed):
|
||||||
|
pipe = QwenImagePipeline.from_pretrained(
|
||||||
|
torch_dtype=torch.bfloat16,
|
||||||
|
device="cuda",
|
||||||
|
model_configs=[
|
||||||
|
ModelConfig(model_id="Qwen/Qwen-Image", origin_file_pattern="transformer/diffusion_pytorch_model*.safetensors", **vram_config),
|
||||||
|
ModelConfig(model_id="Qwen/Qwen-Image", origin_file_pattern="text_encoder/model*.safetensors", **vram_config),
|
||||||
|
ModelConfig(model_id="Qwen/Qwen-Image", origin_file_pattern="vae/diffusion_pytorch_model.safetensors", **vram_config),
|
||||||
|
],
|
||||||
|
tokenizer_config=ModelConfig(model_id="Qwen/Qwen-Image", origin_file_pattern="tokenizer/"),
|
||||||
|
vram_limit=torch.cuda.mem_get_info("cuda")[1] / (1024 ** 3) - 0.5,
|
||||||
|
)
|
||||||
|
pipe.load_lora(pipe.dit, lora_path)
|
||||||
|
image = pipe(prompt, seed=seed, height=1024, width=1024, num_inference_steps=50)
|
||||||
|
return image
|
||||||
|
|
||||||
|
|
||||||
|
demo_style()
|
||||||
|
image = generate_image("model_style.safetensors", "a cat", 0)
|
||||||
|
image.save("image_1.jpg")
|
||||||
|
|
||||||
|
demo_coarse_fine_bias()
|
||||||
|
image = generate_image("model_coarse_fine_bias.safetensors", "bowl", 1)
|
||||||
|
image.save("image_2.jpg")
|
||||||
26
examples/wanvideo/acceleration/unified_sequence_parallel.py
Normal file
26
examples/wanvideo/acceleration/unified_sequence_parallel.py
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
import torch
|
||||||
|
from PIL import Image
|
||||||
|
from diffsynth.utils.data import save_video, VideoData
|
||||||
|
from diffsynth.pipelines.wan_video import WanVideoPipeline, ModelConfig
|
||||||
|
import torch.distributed as dist
|
||||||
|
|
||||||
|
pipe = WanVideoPipeline.from_pretrained(
|
||||||
|
torch_dtype=torch.bfloat16,
|
||||||
|
device="cuda",
|
||||||
|
use_usp=True,
|
||||||
|
model_configs=[
|
||||||
|
ModelConfig(model_id="Wan-AI/Wan2.1-T2V-14B", origin_file_pattern="diffusion_pytorch_model*.safetensors"),
|
||||||
|
ModelConfig(model_id="Wan-AI/Wan2.1-T2V-14B", origin_file_pattern="models_t5_umt5-xxl-enc-bf16.pth"),
|
||||||
|
ModelConfig(model_id="Wan-AI/Wan2.1-T2V-14B", origin_file_pattern="Wan2.1_VAE.pth"),
|
||||||
|
],
|
||||||
|
tokenizer_config=ModelConfig(model_id="Wan-AI/Wan2.1-T2V-1.3B", origin_file_pattern="google/umt5-xxl/"),
|
||||||
|
)
|
||||||
|
|
||||||
|
# Text-to-video
|
||||||
|
video = pipe(
|
||||||
|
prompt="一名宇航员身穿太空服,面朝镜头骑着一匹机械马在火星表面驰骋。红色的荒凉地表延伸至远方,点缀着巨大的陨石坑和奇特的岩石结构。机械马的步伐稳健,扬起微弱的尘埃,展现出未来科技与原始探索的完美结合。宇航员手持操控装置,目光坚定,仿佛正在开辟人类的新疆域。背景是深邃的宇宙和蔚蓝的地球,画面既科幻又充满希望,让人不禁畅想未来的星际生活。",
|
||||||
|
negative_prompt="色调艳丽,过曝,静态,细节模糊不清,字幕,风格,作品,画作,画面,静止,整体发灰,最差质量,低质量,JPEG压缩残留,丑陋的,残缺的,多余的手指,画得不好的手部,画得不好的脸部,畸形的,毁容的,形态畸形的肢体,手指融合,静止不动的画面,杂乱的背景,三条腿,背景人很多,倒着走",
|
||||||
|
seed=0, tiled=True,
|
||||||
|
)
|
||||||
|
if dist.get_rank() == 0:
|
||||||
|
save_video(video, "video1.mp4", fps=15, quality=5)
|
||||||
Reference in New Issue
Block a user