Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeyError: "best loss", when loading checkpoint as Fairseq Model #3

Closed
chrisdoyleIE opened this issue Apr 5, 2020 · 7 comments
Closed

Comments

@chrisdoyleIE
Copy link

chrisdoyleIE commented Apr 5, 2020

Hi guys,

Thank you for the incredible work.

I tried to load this model from the larger checkpoint in the following manner:

from fairseq.models.transformer import TransformerModel

model = TransformerModel.from_pretrained(model_name_or_path=MODEL_DIR,  \
                                         checkpoint_file='prophetnet_large_pretrained_160G_14epoch_model.pt')

but was presented with a key error:

KeyError                                  Traceback (most recent call last)
<ipython-input-13-782ea15f21fd> in <module>()
      1 MODEL_DIR = '/content/drive/My Drive/src/models/'
----> 2 model = TransformerModel.from_pretrained(model_name_or_path=MODEL_DIR,                                         checkpoint_file='prophetnet_large_pretrained_160G_14epoch_model.pt')

4 frames
/usr/local/lib/python3.6/dist-packages/fairseq/checkpoint_utils.py in _upgrade_state_dict(state)
    298     if "optimizer_history" not in state:
    299         state["optimizer_history"] = [
--> 300             {"criterion_name": "CrossEntropyCriterion", "best_loss": state["best_loss"]}
    301         ]
    302         state["last_optimizer_state"] = state["optimizer"]

KeyError: 'best_loss'

Versions
fairseq==0.9.0
torch==1.4.0

Any advice on how to proceed would be greatly appreciated, I wish to load ProphetNet into a fairseq model so I can adapt the architecture to a custom task.

@chrisdoyleIE chrisdoyleIE changed the title Key Error: "best loss" when loading checkpoint as Fairseq Model KeyError: "best loss" when loading checkpoint as Fairseq Model Apr 5, 2020
@chrisdoyleIE chrisdoyleIE changed the title KeyError: "best loss" when loading checkpoint as Fairseq Model KeyError: "best loss", when loading checkpoint as Fairseq Model Apr 5, 2020
@qiweizhen
Copy link
Contributor

Hi, this happens because we remove the useless optimization history logs from the model to reduce the file size. Only the desired model weights are kept to release. As a result, if you directly load the model, error will be reported that some logs are missed. You can refer to this code with the function model.load_state_dict(states) to load our pretrained weights.

@chrisdoyleIE
Copy link
Author

@qiweizhen thank you for the reply.

In the code for build_model that you linked, model.load_state_dict(states) is called on model, such that model is instantiated with

model = NgramTransformerProphetModel(encoder, decoder)

It follows that both encoder and decoder require instantiation, for which src_dict and tgt_dict are needed which require the translation_prophetnet task. Do you have any advice on how to create these dictionaries? Issue continued below.

Alternatively, I tried the below and got a task KeyError

from fairseq import tasks

# Unpickle args, model_state_dict
state = torch.load(f'{MODEL_DIR}/{CHECKPOINT_FILE}')

# Attempt to load task
args = state["args"]
task = tasks.setup_task(args) # error here, KeyError: translation_prophetnet

# Below function creates dictionaries
model = task.build_model(args)
model.load_state_dict(state["model"], strict=strict, args=args)
KeyError                                  Traceback (most recent call last)
<ipython-input-11-947a1ef8f52c> in <module>()
      2 
      3 args = state["args"]
----> 4 task = tasks.setup_task(args)
      5 model = task.build_model(args)
      6 model.load_state_dict(state["model"], strict=strict, args=args)

/usr/local/lib/python3.6/dist-packages/fairseq/tasks/__init__.py in setup_task(args, **kwargs)
     15 
     16 def setup_task(args, **kwargs):
---> 17     return TASK_REGISTRY[args.task].setup_task(args, **kwargs)
     18 
     19 

KeyError: 'translation_prophetnet'

@qiweizhen
Copy link
Contributor

The dictionary we release in this repo is same to BERT-uncased dict. The Fairseq dict object is used in your defined task, for example in translation_prophetnet task
If you want to build your own dict, bpe algorithm should be used, which you can refer to code.

For your second KeyError problem, did you set --user-dir correctly? Actually the models and tasks under your --user-dir are added into Fairseq with the @regitster_xxx function, for example.

@chrisdoyleIE
Copy link
Author

I have it working correctly now, I had not set up the task correctly with @register_task.

Thank you !

@steve3p0
Copy link

steve3p0 commented Mar 5, 2022

That code is no longer available with the link you provided. You could please tell me where I can find it?

Thanks!

Hi, this happens because we remove the useless optimization history logs from the model to reduce the file size. Only the desired model weights are kept to release. As a result, if you directly load the model, error will be reported that some logs are missed. You can refer to [this code](https://github.com/microsoft/ProphetNet/blob/master/src/prophetnet/ngram_s2s_model.py#L146) with the function model.load_state_dict(states) to load our pretrained weights.

@c976237222
Copy link

@chrisdoyleIE How do you solve the problem that I encountered the same error thanks

@1029694141
Copy link

That code is no longer available with the link you provided. You could please tell me where I can find it?

Thanks!

Hi, this happens because we remove the useless optimization history logs from the model to reduce the file size. Only the desired model weights are kept to release. As a result, if you directly load the model, error will be reported that some logs are missed. You can refer to [this code](https://github.com/microsoft/ProphetNet/blob/master/src/prophetnet/ngram_s2s_model.py#L146) with the function model.load_state_dict(states) to load our pretrained weights.

https://github.com/microsoft/ProphetNet/blob/master/ProphetNet_Code/prophetnet/ngram_s2s_model.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants