forked from facebookresearch/GENRE
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Additional preparation for mGENRE HF release
- Loading branch information
1 parent
1fa1623
commit 9a28197
Showing
4 changed files
with
106 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ kilt | |
fairseq | ||
transformers | ||
bs4 | ||
marisa_trie |
88 changes: 88 additions & 0 deletions
88
scripts_genre/convert_bart_original_pytorch_checkpoint_to_pytorch.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import os | ||
import torch | ||
from genre.fairseq_model import GENRE, mGENRE | ||
from transformers import ( | ||
BartConfig, | ||
BartTokenizer, | ||
BartForConditionalGeneration, | ||
TFBartForConditionalGeneration, | ||
MBartConfig, | ||
XLMRobertaTokenizer, | ||
MBartForConditionalGeneration, | ||
TFMBartForConditionalGeneration, | ||
load_pytorch_model_in_tf2_model, | ||
) | ||
|
||
|
||
def remove_ignore_keys_(state_dict): | ||
ignore_keys = [ | ||
"encoder.version", | ||
"decoder.version", | ||
"model.encoder.version", | ||
"model.decoder.version", | ||
"_float_tensor", | ||
"decoder.output_projection.weight", | ||
] | ||
for k in ignore_keys: | ||
state_dict.pop(k, None) | ||
|
||
|
||
def make_linear_from_emb(emb): | ||
vocab_size, emb_size = emb.weight.shape | ||
lin_layer = torch.nn.Linear(vocab_size, emb_size, bias=False) | ||
lin_layer.weight.data = emb.weight.data | ||
return lin_layer | ||
|
||
|
||
# Load GENRE | ||
|
||
# fairseq_path = "../models/fairseq_entity_disambiguation_aidayago" | ||
# hf_path = "../models/hf_entity_disambiguation_aidayago" | ||
|
||
# fairseq_model = GENRE.from_pretrained(fairseq_path).eval() | ||
# config = BartConfig(vocab_size=50264) | ||
# hf_model = BartForConditionalGeneration(config).eval() | ||
# hf_tokenizer = BartTokenizer.from_pretrained("facebook/bart-large") | ||
|
||
# Load mGENRE | ||
|
||
fairseq_path = "../models/fairseq_multilingual_entity_disambiguation" | ||
hf_path = "../models/hf_multilingual_entity_disambiguation" | ||
|
||
fairseq_model = mGENRE.from_pretrained(fairseq_path).eval() | ||
config = MBartConfig(vocab_size=256001, scale_embedding=True) | ||
hf_model = MBartForConditionalGeneration(config).eval() | ||
hf_tokenizer = XLMRobertaTokenizer(os.path.join(fairseq_path, "spm_256000.model")) | ||
|
||
# Convert model | ||
|
||
state_dict = fairseq_model.model.state_dict() | ||
remove_ignore_keys_(state_dict) | ||
state_dict["shared.weight"] = state_dict["decoder.embed_tokens.weight"] | ||
hf_model.model.load_state_dict(state_dict) | ||
hf_model.lm_head = make_linear_from_emb(hf_model.model.shared) | ||
|
||
# Save | ||
|
||
hf_tokenizer.save_pretrained(hf_path) | ||
hf_model.save_pretrained(hf_path) | ||
|
||
# Convert TF GENRE | ||
|
||
# hf_model = load_pytorch_model_in_tf2_model( | ||
# TFBartForConditionalGeneration( | ||
# config | ||
# ), | ||
# hf_model, | ||
# ) | ||
|
||
# Convert TF mGENRE | ||
|
||
hf_model = load_pytorch_model_in_tf2_model( | ||
TFMBartForConditionalGeneration(config), | ||
hf_model, | ||
) | ||
|
||
# Save | ||
|
||
hf_model.save_pretrained(hf_path) |