Using transformers on Kaggle

In this Kaggle kernel I’m using a ModernBert base model with a custom head. The resulting model inherits from transformers.PreTrainedModel
This works fine in the draft session, but throws an error when I try to save and commit.

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
/usr/local/lib/python3.11/dist-packages/transformers/utils/import_utils.py in __getattr__(self, name)
   2044             try:
-> 2045                 module = self._get_module(self._class_to_module[name])
   2046                 value = getattr(module, name)

/usr/local/lib/python3.11/dist-packages/transformers/utils/import_utils.py in _get_module(self, module_name)
   2074         except Exception as e:
-> 2075             raise e
   2076 

/usr/local/lib/python3.11/dist-packages/transformers/utils/import_utils.py in _get_module(self, module_name)
   2072         try:
-> 2073             return importlib.import_module("." + module_name, self.__name__)
   2074         except Exception as e:

/usr/lib/python3.11/importlib/__init__.py in import_module(name, package)
    125             level += 1
--> 126     return _bootstrap._gcd_import(name[level:], package, level)
    127 

/usr/lib/python3.11/importlib/_bootstrap.py in _gcd_import(name, package, level)

/usr/lib/python3.11/importlib/_bootstrap.py in _find_and_load(name, import_)

/usr/lib/python3.11/importlib/_bootstrap.py in _find_and_load_unlocked(name, import_)

/usr/lib/python3.11/importlib/_bootstrap.py in _load_unlocked(spec)

/usr/lib/python3.11/importlib/_bootstrap_external.py in exec_module(self, module)

/usr/lib/python3.11/importlib/_bootstrap.py in _call_with_frames_removed(f, *args, **kwds)

/usr/local/lib/python3.11/dist-packages/transformers/modeling_utils.py in <module>
     72 )
---> 73 from .loss.loss_utils import LOSS_MAPPING
     74 from .pytorch_utils import (  # noqa: F401

/usr/local/lib/python3.11/dist-packages/transformers/loss/loss_utils.py in <module>
     20 
---> 21 from .loss_d_fine import DFineForObjectDetectionLoss
     22 from .loss_deformable_detr import DeformableDetrForObjectDetectionLoss, DeformableDetrForSegmentationLoss

/usr/local/lib/python3.11/dist-packages/transformers/loss/loss_d_fine.py in <module>
     20 from ..utils import is_vision_available
---> 21 from .loss_for_object_detection import (
     22     box_iou,

/usr/local/lib/python3.11/dist-packages/transformers/loss/loss_for_object_detection.py in <module>
     31 if is_vision_available():
---> 32     from transformers.image_transforms import center_to_corners_format
     33 

/usr/local/lib/python3.11/dist-packages/transformers/image_transforms.py in <module>
     20 
---> 21 from .image_utils import (
     22     ChannelDimension,

/usr/local/lib/python3.11/dist-packages/transformers/image_utils.py in <module>
     58     if is_torchvision_available():
---> 59         from torchvision.transforms import InterpolationMode
     60 

/usr/local/lib/python3.11/dist-packages/torchvision/__init__.py in <module>
      9 from .extension import _HAS_OPS  # usort:skip
---> 10 from torchvision import _meta_registrations, datasets, io, models, ops, transforms, utils  # usort:skip
     11 

/usr/local/lib/python3.11/dist-packages/torchvision/_meta_registrations.py in <module>
    162 
--> 163 @torch.library.register_fake("torchvision::nms")
    164 def meta_nms(dets, scores, iou_threshold):

/usr/local/lib/python3.11/dist-packages/torch/library.py in register(func)
   1022             use_lib = lib
-> 1023         use_lib._register_fake(op_name, func, _stacklevel=stacklevel + 1)
   1024         return func

/usr/local/lib/python3.11/dist-packages/torch/library.py in _register_fake(self, op_name, fn, _stacklevel)
    213 
--> 214         handle = entry.fake_impl.register(func_to_register, source)
    215         self._registration_handles.append(handle)

/usr/local/lib/python3.11/dist-packages/torch/_library/fake_impl.py in register(self, func, source)
     30             )
---> 31         if torch._C._dispatch_has_kernel_for_dispatch_key(self.qualname, "Meta"):
     32             raise RuntimeError(

RuntimeError: operator torchvision::nms does not exist

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

ModuleNotFoundError                       Traceback (most recent call last)
/tmp/ipykernel_26/2863755039.py in <cell line: 0>()
----> 1 class EncoderModel(transformers.PreTrainedModel):
      2 
      3     def __init__(self,path):
      4         """
      5         Creates the encoder model

/usr/local/lib/python3.11/dist-packages/transformers/utils/import_utils.py in __getattr__(self, name)
   2046                 value = getattr(module, name)
   2047             except (ModuleNotFoundError, RuntimeError) as e:
-> 2048                 raise ModuleNotFoundError(
   2049                     f"Could not import module '{name}'. Are this object's requirements defined correctly?"
   2050                 ) from e

ModuleNotFoundError: Could not import module 'PreTrainedModel'. Are this object's requirements defined correctly?

Has anyone else encountered this issue, and have any idea how to solve it?

1 Like

Perhaps the Kaggle Docker image has an outdated version of torchvision, causing incompatibility?
I think it might work if you fix the torchvision version or, if unnecessary, uninstall it altogether…