Views
No views yet
fairseq (the transformers models are obtained with a conversion script similar to this.1@inproceedings{decao2020autoregressive,
2 title={Autoregressive Entity Retrieval},
3 author={Nicola {De Cao} and Gautier Izacard and Sebastian Riedel and Fabio Petroni},
4 booktitle={International Conference on Learning Representations},
5 url={https://openreview.net/forum?id=5k8F6UU39V},
6 year={2021}
7}1from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
2
3# OPTIONAL: load the prefix tree (trie), you need to additionally download
4# https://huggingface.co/facebook/genre-linking-aidayago2/blob/main/trie.py and
5# https://huggingface.co/facebook/genre-linking-aidayago2/blob/main/kilt_titles_trie_dict.pkl
6# import pickle
7# from trie import Trie
8# with open("kilt_titles_trie_dict.pkl", "rb") as f:
9# trie = Trie.load_from_dict(pickle.load(f))
10
11tokenizer = AutoTokenizer.from_pretrained("facebook/genre-linking-aidayago2")
12model = AutoModelForSeq2SeqLM.from_pretrained("facebook/genre-linking-aidayago2").eval()
13
14sentences = ["Einstein was a [START_ENT] German [END_ENT] physicist."]
15
16outputs = model.generate(
17 **tokenizer(sentences, return_tensors="pt"),
18 num_beams=5,
19 num_return_sequences=5,
20 # OPTIONAL: use constrained beam search
21 # prefix_allowed_tokens_fn=lambda batch_id, sent: trie.get(sent.tolist()),
22)
23
24tokenizer.batch_decode(outputs, skip_special_tokens=True)['Germany',
'German Empire',
'Nazi Germany',
'German language',
'France']