TypeError: ‘NoneType’ object is not subscriptable

用户按照官方示例使用 pipeline 进行文本生成,加载 NVIDIA Nemotron 模型并设置 trust_remote_code=True 。模型加载权重成功,但在执行生成 pipe(messages) 时抛出 TypeError: 'NoneType' object is not sub

快速结论:该报错在使用 transformers==5.10.2 加载 NVIDIA Nemotron 模型(如 nvidia/NVIDIA-Nemotron-3-Nano-30B-A3B-BF16)并启用 trust_remote_code=True 时触发。优先排查远程代码文件 modeling_nemotron_h.py 中的处理逻辑,以及 transformers 版本是否为 5.10.2。

问题场景

用户按照官方示例使用 pipeline 进行文本生成,加载 NVIDIA Nemotron 模型并设置 trust_remote_code=True。模型加载权重成功,但在执行生成 pipe(messages) 时抛出 TypeError: 'NoneType' object is not subscriptable 错误。

报错原文

Traceback (most recent call last):
  File "/ephemeral/projects/nemotron-explorations/main.py", line 7, in <module>
    pipe(messages)
  ...
  File ".../transformers/generation/utils.py", line 2782, in _sample
    outputs = self._prefill(
...
TypeError: 'NoneType' object is not subscriptable

原因分析

根据 Issue #46468 的讨论,该 TypeError: 'NoneType' object is not subscriptable 错误并非 Transformers 核心库的问题,而是来自模型仓库中远程代码文件 modeling_nemotron_h.py(通过 trust_remote_code=True 加载)。该文件位于模型仓库(Hugging Face Hub)中,而非 transformers 代码库,因此导致 _prefill 阶段出现 NoneType 访问错误。

注意:Issue 中还有一个 NameError: name 'CompletionCreateParamsStreaming' is not defined 是由于 transformers env CLI 的回归问题,已在 #46473 中修复,但该问题与主报错无关。

环境排查

  • Python: 3.12+(用户环境中为 3.12)
  • transformers: 5.10.2(用户依赖中指定)
  • torch: 2.12.0+(CUDA 12.6 版本,用户使用 pytorch-cu126 索引)
  • 模型: nvidia/NVIDIA-Nemotron-3-Nano-30B-A3B-BF16(需 trust_remote_code=True

解决步骤

  1. 确认错误来源:检查完整堆栈跟踪,确认 TypeError 是否来自 modeling_nemotron_h.py 等远程代码文件。如果是,则问题出在模型仓库代码。
  2. 使用原生端口(推荐):该模型已有原生 Transformers 端口,无需 trust_remote_code=True。尝试修改代码移除 trust_remote_code=True 参数:
pipe = pipeline("text-generation", model="nvidia/NVIDIA-Nemotron-3-Nano-30B-A3B-BF16")
  1. 如果必须使用远程代码:考虑向模型仓库(Hugging Face Hub)的 nvidia/NVIDIA-Nemotron-3-Nano-30B-A3B-BF16 仓库提交 PR,修复 modeling_nemotron_h.py 中导致 NoneType 错误的逻辑。这是社区贡献解决的问题。

验证方法

执行 pipe(messages) 后不再报错,正常返回生成结果。若使用原生端口,确保输出符合预期。

参考来源

huggingface/transformers #46468

GamsGo AI

AI 工具推荐

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

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

了解 GamsGo AI

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

这个方案解决了吗?

celebrityanime
celebrityanime
文章: 15281

发表回复

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