Search code examples
pytorchhuggingface-transformers

Error importing LayoutLMv2ForTokenClassification from HuggingFace


I am trying to run this demo notebook on colab and I am getting the following pytorch error when importing LayoutLMv2ForTokenClassification:

from transformers import LayoutLMv2ForTokenClassification

Error:

ModuleNotFoundError                       Traceback (most recent call last)
~\PycharmProjects\playground\camelot\camelotenv\lib\site-packages\transformers\utils\import_utils.py in _get_module(self, module_name)
   1029         try:
-> 1030             return importlib.import_module("." + module_name, self.__name__)
   1031         except Exception as e:

~\AppData\Local\Programs\Python\Python39\lib\importlib\__init__.py in import_module(name, package)
    126             level += 1
--> 127     return _bootstrap._gcd_import(name[level:], package, level)
    128 

~\AppData\Local\Programs\Python\Python39\lib\importlib\_bootstrap.py in _gcd_import(name, package, level)

~\AppData\Local\Programs\Python\Python39\lib\importlib\_bootstrap.py in _find_and_load(name, import_)

~\AppData\Local\Programs\Python\Python39\lib\importlib\_bootstrap.py in _find_and_load_unlocked(name, import_)

~\AppData\Local\Programs\Python\Python39\lib\importlib\_bootstrap.py in _load_unlocked(spec)

~\AppData\Local\Programs\Python\Python39\lib\importlib\_bootstrap_external.py in exec_module(self, module)

~\AppData\Local\Programs\Python\Python39\lib\importlib\_bootstrap.py in _call_with_frames_removed(f, *args, **kwds)

~\PycharmProjects\playground\camelot\camelotenv\lib\site-packages\transformers\models\layoutlmv2\modeling_layoutlmv2.py in <module>
     48     import detectron2
---> 49     from detectron2.modeling import META_ARCH_REGISTRY
     50 

~\PycharmProjects\playground\camelot\camelotenv\lib\site-packages\detectron2\modeling\__init__.py in <module>
     19 )
---> 20 from .meta_arch import (
     21     META_ARCH_REGISTRY,

~\PycharmProjects\playground\camelot\camelotenv\lib\site-packages\detectron2\modeling\meta_arch\__init__.py in <module>
      5 
----> 6 from .panoptic_fpn import PanopticFPN
      7 

~\PycharmProjects\playground\camelot\camelotenv\lib\site-packages\detectron2\modeling\meta_arch\panoptic_fpn.py in <module>
     13 from .build import META_ARCH_REGISTRY
---> 14 from .rcnn import GeneralizedRCNN
     15 from .semantic_seg import build_sem_seg_head

~\PycharmProjects\playground\camelot\camelotenv\lib\site-packages\detectron2\modeling\meta_arch\rcnn.py in <module>
     17 from ..proposal_generator import build_proposal_generator
---> 18 from ..roi_heads import build_roi_heads
     19 from .build import META_ARCH_REGISTRY

~\PycharmProjects\playground\camelot\camelotenv\lib\site-packages\detectron2\modeling\roi_heads\__init__.py in <module>
     14 )
---> 15 from .roi_heads import (
     16     ROI_HEADS_REGISTRY,

~\PycharmProjects\playground\camelot\camelotenv\lib\site-packages\detectron2\modeling\roi_heads\roi_heads.py in <module>
     16 from ..matcher import Matcher
---> 17 from ..poolers import ROIPooler
     18 from ..proposal_generator.proposal_utils import add_ground_truth_to_proposals

~\PycharmProjects\playground\camelot\camelotenv\lib\site-packages\detectron2\modeling\poolers.py in <module>
      9 from detectron2.structures import Boxes
---> 10 from detectron2.utils.tracing import assert_fx_safe
     11 

~\PycharmProjects\playground\camelot\camelotenv\lib\site-packages\detectron2\utils\tracing.py in <module>
      3 import torch
----> 4 from torch.fx._symbolic_trace import _orig_module_call
      5 from torch.fx._symbolic_trace import is_fx_tracing as is_fx_tracing_current

ModuleNotFoundError: No module named 'torch.fx._symbolic_trace'

The above exception was the direct cause of the following exception:

RuntimeError                              Traceback (most recent call last)
<ipython-input-36-b5a73c6d310a> in <module>
      4 device_ids = [0,1]
      5 
----> 6 from transformers import LayoutLMv2ForTokenClassification, TrainingArguments, Trainer
      7 from datasets import load_metric
      8 import numpy as np

~\AppData\Local\Programs\Python\Python39\lib\importlib\_bootstrap.py in _handle_fromlist(module, fromlist, import_, recursive)

~\PycharmProjects\playground\camelot\camelotenv\lib\site-packages\transformers\utils\import_utils.py in __getattr__(self, name)
   1019         elif name in self._class_to_module.keys():
   1020             module = self._get_module(self._class_to_module[name])
-> 1021             value = getattr(module, name)
   1022         else:
   1023             raise AttributeError(f"module {self.__name__} has no attribute {name}")

~\PycharmProjects\playground\camelot\camelotenv\lib\site-packages\transformers\utils\import_utils.py in __getattr__(self, name)
   1018             value = self._get_module(name)
   1019         elif name in self._class_to_module.keys():
-> 1020             module = self._get_module(self._class_to_module[name])
   1021             value = getattr(module, name)
   1022         else:

~\PycharmProjects\playground\camelot\camelotenv\lib\site-packages\transformers\utils\import_utils.py in _get_module(self, module_name)
   1030             return importlib.import_module("." + module_name, self.__name__)
   1031         except Exception as e:
-> 1032             raise RuntimeError(
   1033                 f"Failed to import {self.__name__}.{module_name} because of the following error (look up to see its"
   1034                 f" traceback):\n{e}"

RuntimeError: Failed to import transformers.models.layoutlmv2.modeling_layoutlmv2 because of the following error (look up to see its traceback):
No module named 'torch.fx._symbolic_trace'

Can anyone please help?

Thanks!


Solution

  • This is issue from importing torch.fix flag check for symbolic trace and new commit error of detectron (use 5aeb252b194b93dc2879b4ac34bc51a31b5aee13 this checkout and install )

    for temporary work

    or clone pytorch with new commit