ValueError: Could not find a sliding_window argument in the config, or it is not set`. |

这个报错通常出现在直接用默认配置(例如 XConfig() )构造模型、做 tiny-model 测试或 from_config 局部覆盖的场景:默认配置里存在必要的字段没有被填上(为 None ),或者两个子配置的维度不一致。优先检查默认配置是否与 Hub checkpoint 配置一致,并确认相

快速结论:这个报错通常出现在直接用默认配置(例如 XConfig())构造模型、做 tiny-model 测试或 from_config 局部覆盖的场景:默认配置里存在必要的字段没有被填上(为 None),或者两个子配置的维度不一致。优先检查默认配置是否与 Hub checkpoint 配置一致,并确认相关字段是否有非 None 的默认值。

适用环境:Issue 已确认的环境:transformers 5.23.0.dev0(main @ 863d2620210d72fc0a9a03fdfe200bc9699154f6)、torch 2.12.1+cu130、Python 3.13、Linux x86_64。

最快修复方案:暂无确认的一步修复方案。Issue 中讨论的方向是为默认配置增加测试(包括把“用默认配置初始化后尝试 forward”纳入额外 slow test),并从模型侧显式传入缺失字段,但这些尚未被验证为可落地的统一修复。

注意事项:修改默认值可能影响现有 checkpoint 的加载行为,因此不能简单地为所有默认配置填入固定值;跨子配置维度不一致的问题,静态检查很难覆盖,需要实际运行或人工审查。Issue 中的测试方案也因部分模型参数量可达十亿级而被标记为额外 slow test,不能作为日常快速验证。

问题场景

用户在 transformers 中仅使用默认配置构造模型时遇到问题,典型触发方式包括测试代码、tiny-model 构造,以及 from_config 配合部分参数覆盖。从 Hub 加载 checkpoint 不受影响,因为 checkpoint 配置里已经设置了这些字段。Issue 中涉及的模型包括 CohereCompass、DiffusionGemma、DeepseekOcr2、Granite4Vision、HunYuanVL、Moshi、PPOCRV6MediumDet、Qwen4Exp、Qwen3ASR、Molmo2、Step3p7 等 11 个模型家族。

报错原文

[build] CohereCompassModel: KeyError: 'full_attention'
[build] DiffusionGemmaModel: TypeError: empty() received an invalid combination of arguments - got (tuple, dtype=NoneType, device=NoneType), but expec
[build] DeepseekOcr2Model: TypeError: 'NoneType' object is not subscriptable
[build] Granite4VisionModel: TypeError: object of type 'NoneType' has no len()
[build] HunYuanVLModel: TypeError: unsupported operand type(s) for ** or pow(): 'NoneType' and 'float'
[build] MoshiForConditionalGeneration: TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
[build] PPOCRV6MediumDetModel: TypeError: 'NoneType' object is not subscriptable
[build] Qwen4ExpModel: TypeError: unsupported operand type(s) for //: 'NoneType' and 'NoneType'
[dims] Qwen3ASR audio_config.output_dim=3584 text_config.hidden_size=2048
[dims] Molmo2 adapter_config.text_hidden_size=3584 text_config.hidden_size=4096
[fwd] Step3p7 sliding_window=None layer_types=['full_attention']
ValueError: Could not find a sliding_window argument in the config, or it is not set

原因分析

最可能的原因有两类,均由默认配置本身导致:

第一类,默认配置把某个模型必需的字段留为空值。例如 CohereCompass 的 text_config.rope_parameters 默认为 {},而 layer_types 全为 full_attention,旋转嵌入初始化按 rope_parameters[layer_type] 取值,因此触发 KeyError: 'full_attention'。其他模型的 NoneType 相关报错,可能同样来自某个必需维度或参数没有默认值。Step3p7 的 sliding_window 为 None,在使用需要滑动窗口的 forward 路径时即可能产生上文的 ValueError: Could not find a sliding_window argument in the config, or it is not set。

第二类,子配置之间的维度约定不一致。Qwen3ASR 的 audio_config.output_dim 与 text_config.hidden_size 不相等,Molmo2 的 adapter_config.text_hidden_size 与 text_config.hidden_size 不相等,构建后无法完成前后向计算。Hub 上的 checkpoint 配置通常已正确设置这些值,所以问题只在默认配置构造时暴露。

环境排查

  • 确认 transformers 版本与提交是否与 Issue 一致:5.23.0.dev0,main @ 863d2620210d72fc0a9a03fdfe200bc9699154f6。
  • 确认 torch 版本:2.12.1+cu130。
  • 确认 Python 版本:3.13,平台 Linux x86_64。
  • 确认是否采用默认配置构造:getattr(t, cfg_cls)(),且没有传入 Hub checkpoint 或 from_config 覆盖相关字段。
  • 检查目标模型默认配置中被报错涉及的字段,例如 CohereCompass 的 text_config.rope_parameters 与 layer_types、Step3p7 的 sliding_window 与 layer_types,以及 Qwen3ASR、Molmo2 的跨子配置维度字段,是否仍为 None、{} 或与其他子配置不一致。

解决步骤

  1. 先按 Issue 的复现代码构造默认配置并观察报错,定位是“构建立即失败”还是“构建成功但无法运行 forward”,两类问题排查方向不同。
  2. 若报错为 KeyError 或 NoneType 相关,逐个检查该模型默认配置中对应字段是否为空;可优先尝试在构造配置时显式传入 Hub checkpoint 中使用的值,确认是否为默认值缺失导致。
  3. 若报错涉及跨子配置维度,例如 Qwen3ASR 的 audio_config.output_dim 与 text_config.hidden_size、Molmo2 的 adapter_config.text_hidden_size 与 text_config.hidden_size,可优先尝试将二者设为一致或按目标 checkpoint 配置覆盖。
  4. 若使用 from_config 覆盖部分字段,同时覆盖所有相关的子配置字段,避免只改一侧造成不一致。
  5. Issue 讨论中建议的长期修复方向包括:在 config-tests 中增加“默认配置初始化 + 尝试 forward”的测试,并可能通过仓库一致性检查或 AST 解析来发现模型侧访问了但配置侧没有默认值的属性。这些属于维护方方案,不作为用户侧已验证修复步骤。

验证方法

在目标环境按 Issue 复现代码重新执行:若默认配置构造不再抛 KeyError、TypeError 或 ValueError,且 [dims] 相关子配置维度一致,再尝试实际运行一次 forward。对于大模型,可用 tiny-model 或 meta device 构造验证,但 Issue 指出“默认配置初始化后 forward”的测试可能因模型体量而属于 slow test,实际验证时需要注意耗时与资源。

参考来源

huggingface/transformers #50601

GamsGo AI

AI 工具推荐

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

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

了解 GamsGo AI

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

这个方案解决了吗?

celebrityanime
celebrityanime
文章: 39188

发表回复

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