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

How to load a pretrained model from huggingface and use it in fairseq? #2666

Closed
ttzHome opened this issue Sep 28, 2020 · 10 comments
Closed

How to load a pretrained model from huggingface and use it in fairseq? #2666

ttzHome opened this issue Sep 28, 2020 · 10 comments

Comments

@ttzHome
Copy link

ttzHome commented Sep 28, 2020

I want to load bert-base-chinese in huggingface or google bert and use fairseq to finetune it, how to do? thanks a lot!

@jia-zhuang
Copy link

me too, hope for answers

@myleott
Copy link
Contributor

myleott commented Sep 29, 2020

It should be straightforward to wrap huggingface models in the corresponding fairseq abstractions. We've done this for the gpt2 language model implementation in huggingface: https://github.com/pytorch/fairseq/blob/master/fairseq/models/huggingface/hf_gpt2.py

It'd be great to add more wrappers for other model types (e.g., FairseqEncoderModel for BERT-like models) and also to generalize it to load arbitrary pretrained models from huggingface (e.g., using AutoModel).

PRs are welcome! 😄

@shamanez
Copy link

shamanez commented Oct 6, 2020

@myleott According to the suggested way can we use the pretrained huggingface checkpoint?

I feel like we need to specially change data preprocessing steps.

  1. Tokenization
  2. Fairseq-preprocess function. (Here I don't understand how to create a dict.txt)

@myleott
Copy link
Contributor

myleott commented Oct 10, 2020

Fairseq doesn’t really do any preprocessing. If you want to apply tokenization or BPE, that should happen outside of fairseq, then you can feed the resulting text into fairseq-preprocess/train.

Steps might be:

  1. start with raw text training data
  2. use huggingface to tokenize and apply BPE. Get back a text file with BPE tokens separated by spaces
  3. feed step 2 into fairseq-preprocess, which will tensorize and generate dict.txt

@CheungZeeCn
Copy link

CheungZeeCn commented Oct 27, 2020

@myleott Is it necessary to go through fairseq-preprocess ?
How about just use the output of the hugging face tokenizer(raw text like "您好,世界" as tokenizer's input, dict of tensors as output) as model's input ?

    from transformers import BertModel, BertTokenizer

    tokenizer = BertTokenizer.from_pretrained(model_path)
    model = BertModel.from_pretrained(model_path)
    input_texts = ["您好, 世界"]
    inputs = tokenizer(input_texts, padding=True, return_tensors='pt')
    print("inputs:{}".format(inputs))

got:


 inputs:{
'input_ids': tensor([[ 101, 2644, 1962,  117,  686, 4518,  102]]), 
'token_type_ids': tensor([[0, 0, 0, 0, 0, 0, 0]]), 
'attention_mask': tensor([[1, 1, 1, 1, 1, 1, 1]])}

Thank you!

@shamanez
Copy link

shamanez commented Oct 27, 2020 via email

@CheungZeeCn
Copy link

CheungZeeCn commented Oct 27, 2020

Is there an example of using the code in https://github.com/pytorch/fairseq/blob/master/fairseq/models/huggingface/hf_gpt2.py ?
@myleott @shamanez

It seems like that this is only a wrap, but there are more should be done if we want to load the pretrained gpt2 model from hugging face?

Thank you!

@CheungZeeCn
Copy link

CheungZeeCn commented Dec 10, 2020

Hi guys, Here is my code for this task exactly, HERE plz check whether it can help you!
@ttzHome @shamanez

cc @myleott

@stale
Copy link

stale bot commented Jul 21, 2021

This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

@stale stale bot added the stale label Jul 21, 2021
@stale
Copy link

stale bot commented May 2, 2022

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you!

@stale stale bot closed this as completed May 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants