facebookresearch / fairseq

Facebook AI Research Sequence-to-Sequence Toolkit written in Python.
MIT License
30.42k stars 6.4k forks source link

loading xmlr from local without using torch.hub.load #1430

Closed ljj7975 closed 4 years ago

ljj7975 commented 4 years ago

Is there any way to load XLMRModel class from fairseq with Pytorch 1.3?

I cannot use torch.hub.load because I do not have any internet connection In order to bypass the downloading, I have the model on local but I cannot figure out how I can initialize the model and load the model.pt.

For PyTorch 1.0, the example in the repo has

from fairseq.models.roberta import XLMRModel
xlmr = XLMRModel.from_pretrained('/path/to/xlmr.large.v0', checkpoint_file='model.pt')
xlmr.eval()  # disable dropout (or leave in train mode to finetune)

but it seems like it is no longer availble for PyTorch 1.3

Can anyone help me? Thanks

myleott commented 4 years ago

The XLMRModel.from_pretrained version should work in PyTorch 1.3 as well. Did you try it?

ljj7975 commented 4 years ago

yes, I was getting following error so I thought it was no longer supported

>>> from fairseq.models.roberta import XLMRModel
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'XLMRModel' from 'fairseq.models.roberta' (/home/xxx/anaconda3/envs/xxx/lib/python3.7/site-packages/fairseq/models/roberta/__init__.py)
myleott commented 4 years ago

Ah, did you install fairseq via pip? The pip package is quite outdated so doesn't have XLMRModel in it yet.

You need to instead clone fairseq and pip install --editable .. See "Installing from source" here: https://github.com/pytorch/fairseq#requirements-and-installation

ljj7975 commented 4 years ago

confirmed it works. Thanks!