Issue with transformer and Pytorch conflicts

Posted by Jingbiao on July 14, 2021, Reading time: 1 minute.
  • In one project, the transformers package has to be version:transformers==3.5.1, and Pytorch has be >= 1.8.0 - Installing the legacy version may have some conflicts, but anaconda should solve the conflicts

  • Using later pytorch would give: ImportError: cannot import name 'SAVE_STATE_WARNING' from 'torch.optim.lr_scheduler' as this is removed in pytorch recently, but the older transformers package calls it.

  • The torch module does check if a lower version of Pytorch is installed to avoid conflicts, but higher version is not checked by the module. i.e:

1
2
3
4
    if version.parse(torch.__version__) <= version.parse("1.4.1"):
        SAVE_STATE_WARNING = ""
    else:
        from torch.optim.lr_scheduler import SAVE_STATE_WARNING 
  • Fix by checking if higher version of pytorch is used in transformers.trainer_pt_utils.py:
1
2
3
4
5
    if version.parse(torch.__version__) <= version.parse("1.4.1") or version.parse(torch.__version__) >= version.parse("1.8.0") :
        SAVE_STATE_WARNING = ""
    else:
        from torch.optim.lr_scheduler import SAVE_STATE_WARNING