ValueError: `state_dict` should be empty at this point but has state_dict.keys()=dict_keys([‘layers.0.adaLN_modulation.0.alpha’, ‘layers.0.adaLN_modulation.0.lokr_w1’, ‘layers.0.adaLN_modulati

这个报错通常出现在用 Diffusers 加载 Z-Image(Z Image)的 LyCORIS LoKr/混合 LoRA 权重时,转换函数没有处理 lokr_w1 / lokr_w2 这类键,最后的完整性检查直接把剩余键抛成 ValueError: `state_dict` should be

快速结论:这个报错通常出现在用 Diffusers 加载 Z-Image(Z Image)的 LyCORIS LoKr/混合 LoRA 权重时,转换函数没有处理 lokr_w1/lokr_w2 这类键,最后的完整性检查直接把剩余键抛成 ValueError: `state_dict` should be empty at this point but has state_dict.keys()=dict_keys(['layers.0.adaLN_modulation.0.alpha', 'layers.0.adaLN_modulation.0.lokr_w1', 'layers.0.adaLN_modulation.0.lokr_w2', ...])。优先排查你手里的 adapter 是否包含 LoKr 键,而不是普通 LoRA 键。

适用环境:Issue 中确认在 Diffusers 当前 main(208704a27a6f,2026-07-08)仍可复现;复现只需 ZImagePipeline.lora_state_dict(),不需要基础模型或 GPU。Issue 未提供完整的 Python、PyTorch、CUDA、显卡版本信息。

最快修复方案:暂无确认的一步修复方案。Issue 讨论中指向的修复方向是让 _convert_non_diffusers_z_image_lora_to_diffusers 支持 LoKr 键(LoKr → peft LoKrConfig),或对 LoKr 模块走 SVD 转 LoRA;但截至本 Issue 关闭时没有给出可用的已合并版本。

注意事项:如果混合权重里同时含 LoKr 和普通 LoRA,单个 peft adapter 无法用两种 config 表达,可能需要拆成两个内部 adapter,或只走 SVD 转 LoRA(有损)。在修复版本发布前,直接加载这类文件仍会失败;可转换的 LoRA 部分也会因为硬报错而一起加载不了。

问题场景

用户用 Diffusers 加载 Z-Image 的 LyCORIS LoKr adapter,或加载同时包含 LoKr 与普通 LoRA 的混合 adapter(例如用 ai-toolkit/LyCORIS 风格训练器产出、键名带 diffusion_model. 前缀的文件)。调用 ZImagePipeline.lora_state_dict(path) 时,转换流程进入 _convert_non_diffusers_z_image_lora_to_diffusers,遇到 LoKr 键后触发报错。该问题与 FLUX.2-klein 的同类 LoKr 缺口相关(讨论中提及 #13261、#13326)。

报错原文

diffusers\loaders\lora_pipeline.py", line 5475, in lora_state_dict
    state_dict = _convert_non_diffusers_z_image_lora_to_diffusers(state_dict)
  File "C:\Users\whr_u\anaconda3\envs\DBtrain\lib\site-packages\diffusers\loaders\lora_conversion_utils.py", line 2628, in _convert_non_diffusers_z_image_lora_to_diffusers
    raise ValueError(f"`state_dict` should be empty at this point but has {state_dict.keys()=}")

ValueError: `state_dict` should be empty at this point but has state_dict.keys()=dict_keys(['layers.0.adaLN_modulation.0.alpha', 'layers.0.adaLN_modulation.0.lokr_w1', 'layers.0.adaLN_modulation.0.lokr_w2', 'layers.0.attention.to_k.alpha', ...])

原因分析

最可能的原因是 Z-Image 的 LoRA 转换函数只覆盖了普通 LoRA 键,没有处理 LyCORIS LoKr 的 lokr_w1 / lokr_w2(以及配套的 alpha)。转换完成后函数预期 state_dict 已清空,但 LoKr 键没有被消费,于是最后的 sanity check 抛出 ValueError。在复现样本中,文件是混合 adapter:部分模块是 LoKr,部分是 rank-32 LoRA,且 attention.to_out.0 在不同层之间格式还会变化(0–17 层为 LoRA,18–29 层为 LoKr),普通 LoRA 部分本来可以正常转换,但 132 个 LoKr 模块(含 alpha 共 396 个张量)残留下来,直接导致硬失败。

环境排查

  • 确认 Diffusers 版本/commit:Issue 在 main(208704a27a6f,2026-07-08)上仍可复现。
  • 确认 adapter 文件的键名布局:搜索是否包含 lokr_w1、lokr_w2、alpha,以判断是否为 LoKr 或混合 adapter。
  • 确认加载入口:是否调用 ZImagePipeline.lora_state_dict(),以及是否只加载纯 LoRA 文件。
  • Issue 未确认 Python、PyTorch、CUDA 版本和显卡型号;这些不是本次报错的已知触发条件。

解决步骤

  1. 确认你的 Z-Image adapter 是否为 LoKr 或混合格式。可参考复现样本的键布局:attention.to_q/k/v 为 LoKr(lokr_w1 [4,4]、lokr_w2 [960,960]、标量 alpha),adaLN_modulation.0 为 LoKr(lokr_w1 [4,4]、lokr_w2 [3840,64]、标量 alpha),attention.to_out.0 在 0–17 层为 LoRA、18–29 层为 LoKr,feed_forward.w1/w2/w3 为 rank 32 LoRA。
  2. 如果只是要验证报错是否是 LoKr 导致,可用 Issue 提供的无 GPU 最小复现:下载 scenario-labs/z-image-lokr-repro 中的 zimage_lokr_dummy.safetensors,然后执行 ZImagePipeline.lora_state_dict(path)。若同样抛出上述 ValueError,即可确认是 LoKr 键未被处理。
  3. 可优先尝试的方案(Issue 中讨论方向,尚未给出确认可用的合并版本):在 _convert_non_diffusers_z_image_lora_to_diffusers 中增加 LoKr 键处理,将 LoKr 映射为 peft LoKrConfig;或对 LoKr 模块做 SVD 转 LoRA。
  4. 对于混合 adapter,不能用一个 peft adapter 同时表达 LoKr 和 LoRA,需要拆成两个内部 adapter(LoKr + LoRA),或走 SVD-to-LoRA 路线。
  5. 如果暂时没有修复分支,可先把文件中的 LoKr 模块排除,只加载纯 LoRA 部分验证管线其余部分;但 Issue 未确认这种做法在 Z-Image 上的可用性,需自行验证。

验证方法

用 Issue 中的最小复现脚本加载 zimage_lokr_dummy.safetensors:修复前会抛出 ValueError: `state_dict` should be empty at this point but has state_dict.keys()=dict_keys([...]);修复后 ZImagePipeline.lora_state_dict(path) 应能正常返回转换后的 state dict,不再因 LoKr 键残留而报错。若加载的是自己的混合 adapter,可检查转换后是否仍残留 lokr_w1/lokr_w2 键。

参考来源

huggingface/diffusers #13221

GamsGo AI

AI 工具推荐

想把多个 AI 模型放在一个入口?

GamsGo AI 集成 ChatGPT、DeepSeek、Gemini、Claude、Midjourney、Veo 等常用模型,适合写作、绘图、视频和日常 AI 工作流。

了解 GamsGo AI

推广链接:通过此链接购买,我可能获得佣金,不影响你的价格。

这个方案解决了吗?

celebrityanime
celebrityanime
文章: 28752

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注