You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Mar 20, 2026. It is now read-only.
Repository navigation
This repository was archived by the owner on Mar 20, 2026. It is now read-only.
How to score text with trained language model #1259
I successfully trained a Transformer language model with fairseq. Now I would like to score text with this model.
This is what I am looking for:
echo"Input text to be scored by lm"| fairseq-score trained_model_path/checkpoint_best.pt
78.23 # example language model perplexity score for this sentence
Alternatively, something like
importtorchfromfairseq.models.transformer_lmimportTransformerLanguageModelcustom_lm=TransformerLanguageModel.from_pretrained('trained_model_path', 'checkpoint_best.pt')
custom_lm.score('Input text to be scored by lm')
# 78.23 # example language model perplexity score for this sentence
>>>tokens='the potentially medically important signs and symptoms'>>>num_tokens=len(tokens.split(" "))
>>>custom_lm.sample('the potentially medically important signs and symptoms', verbose=True, max_len_b=num_tokens)
SthepotentiallymedicallyimportantsignsandsymptomsH-3.916642665863037thepotentiallymedicallyimportantsignsandsymptomsP-2.5552-10.4895-4.0601-2.6499-1.0805-1.8499-0.0001-8.6480'the potentially medically important signs and symptoms'
Which is inconvenient since it just prints the scores to STDOUT directly without returning them.
Activity
bricksdont commentedon Oct 17, 2019
Found a way to do it:
Which is inconvenient since it just prints the scores to STDOUT directly without returning them.
bricksdont commentedon Oct 24, 2019
My solution, without having much insight into torch and fairseq:
Then:
myleott commentedon Dec 16, 2019
Yeah, the
sampleinterface is just a wrapper aroundencode,generateanddecode, so I recommend using those directly for more control: https://github.com/pytorch/fairseq/blob/master/fairseq/hub_utils.py#L119-L122bricksdont commentedon Dec 17, 2019
@myleott Is scoring a string with fairseq LMs not a frequent use case for you? If it is, could you perhaps post your code somewhere?
My solution above works, but is too slow, for instance because there is no batching.
myleott commentedon Dec 18, 2019
Good call, we should add better documentation around this and the proposed
scorefunction makes sense. I'll add something like this shortly.myleott commentedon Dec 18, 2019
Added a
.scorefunction in 9d77252. It should be much faster than usinggenerate.Usage:
Initial support for ZeRO optimizer state sharding (#1259)
Initial support for ZeRO optimizer state sharding (#1259)
damyana79 commentedon Oct 16, 2021
When I try to load a pretrained language model I get following error:
assert task is not None, f"Could not infer task type from {cfg}. Available tasks: {TASK_REGISTRY.keys()}"
AssertionError: Could not infer task type from {'_name': 'language_modeling', 'data': '/raid/data/daga01/fairseq_train
/lm_models/my_LM_de_2', 'sample_break_mode': 'none', 'tokens_per_sample': 512, 'output_dictionary_size': -1, 'self_tar
get': False, 'future_target': False, 'past_target': False, 'add_bos_token': False, 'max_target_positions': None, 'shor
ten_method': 'none', 'shorten_data_split_list': '', 'pad_to_fixed_length': False, 'pad_to_fixed_bsz': False, 'seed': 1
, 'batch_size': None, 'batch_size_valid': None, 'dataset_impl': None, 'data_buffer_size': 10, 'tpu': False, 'use_plasm
a_view': False, 'plasma_path': '/tmp/plasma'}. Available tasks: dict_keys(['hubert_pretraining'])