Nougat OCR中cached_property导入错误与BARTDecoder参数异常问题
Nougat OCR在Python 3.12环境下的两类错误解决方案
错误1:cached_property导入错误(ImportError)
错误信息
ImportError: cannot import name 'cached_property' from 'nougat.utils' (/lfs/skampere1/0/emilyhyf/miniconda/lib/python3.12/site-packages/nougat/utils/__init__.py) OCRing with base model failed on /lfs/skampere1/0/emilyhyf/massive-evaporation-4-math/data/debug_pdfs/dummy_folder1/The_IMO_Compendium.pdf... trying small model Traceback (most recent call last): File "/lfs/skampere1/0/emilyhyf/miniconda/bin/nougat", line 5, in <module> from predict import main File "/lfs/skampere1/0/emilyhyf/miniconda/lib/python3.12/site-packages/predict.py", line 18, in <module> from nougat import NougatModel File "/lfs/skampere1/0/emilyhyf/miniconda/lib/python3.12/site-packages/nougat/__init__.py", line 1, in <module> from nougat.app import Nougat File "/lfs/skampere1/0/emilyhyf/miniconda/lib/python3.12/site-packages/nougat/app.py", line 5, in <module> from nougat.asgi import serve File "/lfs/skampere1/0/emilyhyf/miniconda/lib/python3.12/site-packages/nougat/asgi.py", line 6, in <module> from nougat.context.request import Request File "/lfs/skampere1/0/emilyhyf/miniconda/lib/python3.12/site-packages/nougat/context/__init__.py", line 1, in <module> from nougat.context.request import Request File "/lfs/skampere1/0/emilyhyf/miniconda/lib/python3.12/site-packages/nougat/context/request.py", line 8, in <module> from nougat.utils import cached_property, File ImportError: cannot import name 'cached_property' from 'nougat.utils' (/lfs/skampere1/0/emilyhyf/miniconda/lib/python3.12/site-packages/nougat/utils/__init__.py) OCRing with small model failed on /lfs/skampere1/0/emilyhyf/massive-evaporation-4-math/data/debug_pdfs/dummy_folder1/The_IMO_Compendium.pdf The following files failed OCRing and need manual review: /lfs/skampere1/0/emilyhyf/massive-evaporation-4-math/data/debug_pdfs/putnam_and_beyond.pdf /lfs/skampere1/0/emilyhyf/massive-evaporation-4-math/data/debug_pdfs/dummy_folder1/The_IMO_Compendium.pdf Time taken: 8.497612476348877 seconds, 0.1416268746058146 minutes, 0.00236044791009691 hours
成因
Nougat版本与Python 3.12不兼容:Python 3.8+标准库已内置functools.cached_property,部分旧版Nougat可能移除了内部实现的cached_property,但代码中仍保留从nougat.utils导入的逻辑;或者该模块未在nougat.utils.__init__.py中导出。
解决方法
- 升级Nougat到兼容版本:执行命令
pip install --upgrade nougat-ocr
- 临时修改导入逻辑:
- 打开
nougat/context/request.py文件,将from nougat.utils import cached_property替换为from functools import cached_property; - 或在
nougat/utils/__init__.py中添加一行:from functools import cached_property。
- 打开
错误2:BARTDecoder的cache_position参数错误(TypeError)
错误信息
TypeError: BARTDecoder.prepare_inputs_for_inference() got an unexpected keyword argument 'cache_position' -> Cannot close object, library is destroyed. This may cause a memory leak! -> Cannot close object, library is destroyed. This may cause a memory leak! -> Cannot close object, library is destroyed. This may cause a memory leak! OCRing with base model failed on /lfs/skampere1/0/naveenkc/contest_scraper/test_nougat/731991.pdf... trying small model /lfs/skampere1/0/naveenkc/miniconda/lib/python3.12/site-packages/torch/functional.py:512: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at ../aten/src/ATen/native/TensorShape.cpp:3587.) return _VF.meshgrid(tensors, **kwargs) # type: ignore[attr-defined] 0%| | 0/288 [00:24<?, ?it/s] Traceback (most recent call last): File "/lfs/skampere1/0/naveenkc/miniconda/bin/nougat", line 8, in <module> sys.exit(main()) ^^^^^^ File "/lfs/skampere1/0/naveenkc/miniconda/lib/python3.12/site-packages/predict.py", line 167, in main model_output = model.inference( ^^^^^^^^^^^^^^^^ File "/lfs/skampere1/0/naveenkc/miniconda/lib/python3.12/site-packages/nougat/model.py", line 592, in inference decoder_output = self.decoder.model.generate( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/lfs/skampere1/0/naveenkc/miniconda/lib/python3.12/site-packages/torch/utils/_contextlib.py", line 115, in decorate_context return func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/lfs/skampere1/0/naveenkc/miniconda/lib/python3.12/site-packages/transformers/generation/utils.py", line 1758, in generate result = self._sample( ^^^^^^^^^^^^^ File "/lfs/skampere1/0/naveenkc/miniconda/lib/python3.12/site-packages/transformers/generation/utils.py", line 2394, in _sample model_inputs = self.prepare_inputs_for_generation(input_ids, **model_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: BARTDecoder.prepare_inputs_for_inference() got an unexpected keyword argument 'cache_position' -> Cannot close object, library is destroyed. This may cause a memory leak! -> Cannot close object, library is destroyed. This may cause a memory leak! -> Cannot close object, library is destroyed. This may cause a memory leak! -> Cannot close object, library is destroyed. This may cause a memory leak! -> Cannot close object, library is destroyed. This may cause a memory leak! OCRing with small model failed on /lfs/skampere1/0/naveenkc/contest_scraper/test_nougat/731991.pdf The following files failed OCRing and need manual review: /lfs/skampere1/0/naveenkc/contest_scraper/test_nougat/731991.pdf Time taken: 65.70022702217102 seconds, 1.0950037837028503 minutes, 0.018250063061714172 hours
成因
Transformers版本与Nougat不兼容:新版本Transformers在文本生成逻辑中新增了cache_position参数,但旧版Nougat的BARTDecoder.prepare_inputs_for_inference方法未处理该参数,导致传参时报错。同时Python 3.12环境下,部分依赖版本的兼容性问题会放大这类参数不匹配的错误。
解决方法
- 安装Nougat指定的Transformers版本:查看Nougat官方requirements文件,安装对应版本,例如:
pip install transformers==4.33.2
- 修改Nougat代码适配新版本Transformers:
- 打开
nougat/model.py,找到调用generate方法的位置,移除cache_position相关参数; - 或修改
BARTDecoder的prepare_inputs_for_inference方法,添加**kwargs参数以接收多余的关键字参数。
- 打开
- 确保Torch与Transformers版本兼容:根据Transformers官方文档,安装与Python 3.12匹配的Torch版本,避免跨版本兼容性问题。
内容的提问来源于stack exchange,提问作者Charlie Parker
相关产品推荐
相关产品推荐

