-
Notifications
You must be signed in to change notification settings - Fork 107
feat: Add LoRA and ControlNet support for Qwen Image models #647
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat: Add LoRA and ControlNet support for Qwen Image models #647
Conversation
- Introduced `NunchakuQwenImageLoraLoader` and `NunchakuQwenImageLoraStack` nodes for applying LoRA weights. - Wrapped transformer in `ComfyQwenImageWrapper` for enhanced integration with ComfyUI. - Implemented LoRA composition and caching strategies in the transformer model. - Added methods for updating and restoring original parameters in transformer blocks. - Enhanced model processing to handle dynamic LoRA inputs and strengths.
…der/ComfyUI-nunchaku into add-lora-support-for-qwen
- Update model implementation in models/qwenimage.py - Improve LoRA node functionality in nodes/lora/qwenimage.py - Refine model loading in nodes/models/qwenimage.py - Update wrapper implementation in wrappers/qwenimage.py
- Added support for `controlnet_block_samples` in `NunchakuQwenImageTransformer2DModel` to maintain backward compatibility. - Updated logic to convert old control format to a dictionary for improved flexibility. - Refined control parameter handling in `ComfyQwenImageWrapper` to streamline integration with the transformer model.
…r2DModel - Adjusted calculations for height and width lengths to use padded dimensions after applying padding. - Modified return statement to include both padded and original dimensions for better unpatchify functionality. - Cleaned up whitespace in ComfyQwenImageWrapper for consistency.
|
This PR seems like it might be a duplicate of #642? |
No, this is realized based on my own ideas. |
…uQwenImageTransformer2DModel - Updated the calculation of txt_start to utilize padded height and width dimensions for improved accuracy. - Ensured compatibility with previous padding adjustments for consistent model behavior.
…nImageTransformer2DModel - Introduced a VAE scale factor for image shape calculations to align with the diffusers pipeline. - Updated patch grid dimension calculations to ensure consistency with the original model behavior. - Added a RoPE-based position embedding method for improved positional encoding. - Enhanced image processing methods to support packed latents and maintain compatibility with the official diffusers pipeline. - Refined the forward method to directly implement original ComfyUI logic for better integration. - Updated ComfyQwenImageWrapper to unpack model output correctly.
… for Image Edit models - Added support for handling both Qwen Image (T2I) and Qwen Image Edit (I2I) models. - Updated input tensor documentation to reflect new shape handling for Image Edit models. - Implemented logic to squeeze the middle dimension for 5D input tensors.
|
大佬!节点已经做出来了吗?要去哪里下?等了好久了 |
|
牛逼了 66666666666 |
|
@black4der 大佬 什么时候可以用? |
最简单的方式就是git我们分支的nunchaku和comfyui-nunchaku ,从新编译nunchaku,然后git我分支的comfyui-nunchaku文件到custom_node文件夹,然后重启comfyui就可以使用了,但是现在是测试版本,稳定性等还没有办法保证,安装不懂得话你可以看看nunchaku是如何编译的,我只是简单的修改了python代码 c++代码我没有动过所以说编译应该没有问题,还有一个简单的方法,就是git我的nunchaku分支,下载后里面有一个nunchaku的python文件夹,你可以把这些python文件替换掉你环境里面的nunchaku的python文件,然后下载Comfyui-nunchaku到你的comfyui插件目录同样可以使用,不懂的可以再这里质询 |
|
@black4der 要是有解压包直接解压覆盖就好了,虽然有电脑基础,但是太复杂确实不知道怎么弄.不过还是感谢大佬,希望还是看到的. |
|
@lmxyy 大佬,麻烦先更新一下Lora的支持,拜托了 |
没有看见一个名为nunchaku的python的文件夹 |
插件免编译安装教程此安装过程分为两个核心部分:
**第一步:替换核心库python文件 **这一步的目标是将 1. 获取 nunchaku 库文件 你有两种方式获取:
2. 复制核心文件 这是最关键的一步,请仔细操作: 打开刚刚通过 Git 克隆或解压得到的 *复制这个内层 3. 粘贴到 ComfyUI 的 Python 环境中
复制的所有文件和文件夹,粘贴到这个Python环境下的的 至此,核心库替换完成。 第二步:安装 ComfyUI 插件 (ComfyUI-nunchaku)这一步是将插件的节点文件安装到 ComfyUI 的自定义节点目录中。 1. 导航到 custom_nodes 目录
2. 获取插件文件 同样,你有两种方式:
完成后,你的 第三步:重启并使用
|
|
Another thing I think may need looking at is if the lora application code in this PR uses the lora's Alpha at all, it should use the alpha setting from the lora or set the alpha to the same as the rank. I'm not 100% sure what this does at the moment as some loras seem to apply themselves differently to expected from running the non-nunchaku model. |
During my tests on FP4, I noticed that the strength of FP4 is completely insufficient when AMPLIFICATION_FACTOR is set to 1.0. I hope you can collaborate with me on further testing: use a LoRA with a strong style, paired with Lightning-8steps-V2.0, and run the test with both LoRAs set to a strength of 1. This way, we can compare the sensitivity of IN4 and FP4 models to LoRA strength. |
|
@black4der 你好!我的ComfyUI文件夹里没有看到python_embeded这个文件夹,是python文件夹吗? |
我尝试把你教程第一步的文件复制到D:\Comfy UI\python\Lib\site-packages\nunchaku |
|
@black4der 大佬 能教一下吗? |
|
@black4der Unfortunately Rank 32 and Alpha 32 lora's do not work as expected. Without nunchaku and strength 1.0 -> works fine. I would expect it to have the same/similiar effect on strength 1.0 that I get without nunchaku |
|
Looking forward to the arrival of the official version and the official wheels |
|
Seems like its working perfectly with the handful of lora I've tested so far with the int4 lightning model. Thanks for your efforts on this, it's finally made qwen usable for me. |
|
Please release… |
求教,用这个方法最后没有出来nunchaku qwen image lora stack这个节点,看诊断包有下面一段,但是不知道要怎么处理
|
|
安装搞定了,之前git clone的时候没有指定分支-b add-lora-support-for-qwen |
















中文版
功能概述
为 Nunchaku Qwen Image 和 Qwen Image Edit 模型添加 LoRA 支持,使用户能够在 ComfyUI 中灵活地应用和组合多个 LoRA 权重,支持文本到图像(T2I)和图像编辑(I2I)工作流。
主要改动
🎯 新增节点
NunchakuQwenImageLoraLoaderNunchakuQwenImageLoraStack🔧 核心实现
ComfyQwenImageWrapper(新增)NunchakuQwenImageTransformer2DModel以支持 ComfyUI 集成📦 模型增强
NunchakuQwenImageTransformer2DModel中添加 LoRA 相关方法:update_lora_params()- 更新 LoRA 参数reset_lora()- 重置到原始权重forward()- 适配 ComfyUI 参数格式process_img()- 处理 4D/5D 输入张量(支持 T2I 和 I2I 模型)📂 修改文件列表
新增:
wrappers/qwenimage.py- ComfyQwenImageWrapper 实现nodes/lora/qwenimage.py- LoRA 加载器节点修改:
models/qwenimage.py- 添加 LoRA 支持方法nodes/models/qwenimage.py- 包装 transformer 以支持 LoRA技术特性
.alpha参数处理:自动识别和应用 LoRA 的.alpha缩放参数使用方法
单个 LoRA:
多个 LoRA(链式):
多个 LoRA(堆栈):
(在堆栈节点中配置多个 LoRA 和强度)
ControlNet:
💡 推荐参数
LoRA 强度:
ControlNet 强度:
测试
CPU Offload 兼容性
部分 LoRA 文件(如 Qwen-Image-Lightning)对不同 transformer block 训练了不同的层,导致不同 block 之间的内部结构(rank)不一致。由于 QwenImage 使用 Python 层的
CPUOffloadManager实现 CPU offload,而该管理器要求所有 block 具有完全相同的结构,因此这类 LoRA 无法与 CPU offload 同时使用。症状:
解决方案:
技术原因:
CPUOffloadManager,通过固定的 buffer blocks 进行参数复制,要求所有 block 结构一致English Version
Overview
Add LoRA support for Nunchaku Qwen Image models, enabling users to flexibly apply and compose multiple LoRA weights in ComfyUI.
Key Changes
🎯 New Nodes
NunchakuQwenImageLoraLoaderNunchakuQwenImageLoraStack🔧 Core Implementation
ComfyQwenImageWrapper(New)NunchakuQwenImageTransformer2DModelfor ComfyUI integration📦 Model Enhancements
NunchakuQwenImageTransformer2DModel:update_lora_params()- Update LoRA parametersreset_lora()- Reset to original weightsforward()- Adapt ComfyUI parameter formatprocess_img()- Handle 4D input tensors📂 Modified Files
New:
wrappers/qwenimage.py- ComfyQwenImageWrapper implementationnodes/lora/qwenimage.py- LoRA loader nodesModified:
models/qwenimage.py- Added LoRA support methodsnodes/models/qwenimage.py- Wrap transformer for LoRA supportTechnical Features
.alphaParameter Handling: Automatically recognizes and applies LoRA.alphascaling parametersUsage
Single LoRA:
Multiple LoRAs (Chaining):
Multiple LoRAs (Stack):
(Configure multiple LoRAs and strengths in the stack node)
Qwen Image Edit (I2I) with LoRA:
Note: Image Edit models use the same LoRA nodes, automatically handling ref_latents
ControlNet:
💡 Recommended Parameters
LoRA Strength:
ControlNet Strength:
Note: Based on user testing, LoRAs for INT4 quantized models do not require additional amplification factor. 1.0 strength achieves optimal results
Testing
CPU Offload Compatibility
Some LoRA files (e.g., Qwen-Image-Lightning) train different layers for different transformer blocks, resulting in inconsistent internal structures (ranks) across blocks. Since QwenImage uses a Python-based
CPUOffloadManagerfor CPU offload, which requires all blocks to have identical structure, these LoRAs cannot be used with CPU offload simultaneously.Symptom:
Solutions:
Technical Reason:
CPUOffloadManager, which copies parameters through fixed buffer blocks, requiring all blocks to have identical structure✅ Checklist