我已经在一台新机器上安装了 python 3.8、pytorch 1.7 和 fairseq 0.10.1,然后从具有 python 3.6、pytorch 1.4 和 fairseq 0.9.0 的机器复制了脚本和模型,它正在工作。
模型已加载并准备好:
model = TransformerModel.from_pretrained(...)
model.eval()
model.cuda()
然后用于:
inputs = [model.binarize(encode(src, str)) for str in texts]
batched_hypos = model.generate(inputs, beam)
inputs
看起来像[tensor([ 116, 1864, 181, 6, 2]), tensor([ 5, 432, 7, 2])]
它断言,调用堆栈的最后一位是:
...
batched_hypos = model.generate(inputs, beam)
File "/path/to/fairseq/hub_utils.py", line 125, in generate
sample = self._build_sample(tokens)
File "/path/to/fairseq/hub_utils.py", line 196, in _build_sample
assert torch.is_tensor(src_tokens)
相反,如果我fairseq-interactive
从命令行使用它会失败,并显示RuntimeError: result type Float can't be cast to the desired output type Long
. (下面的完整堆栈跟踪。)
由于使用 cli 也失败了,我的直觉是我用 fairseq 0.9.x 构建的模型不能与 fairseq 0.10.x 一起使用。如果是这样,有没有办法更新模型(即无需重新训练)。如果没有,问题可能是什么,我该如何解决?
顺便说一句,如果我添加到命令行参数,则会出现完全相同的错误--cpu
,因此可以消除 GPU 或 cuda 版本作为可能的原因。
$ fairseq-interactive path/to/dicts --path models/big.pt --source-lang ja --target-lang en --remove-bpe sentencepiece
File "/path/to/bin/fairseq-interactive", line 11, in <module>
sys.exit(cli_main())
File "/path/to/lib/python3.8/site-packages/fairseq_cli/interactive.py", line 190, in cli_main
main(args)
File "/path/to/lib/python3.8/site-packages/fairseq_cli/interactive.py", line 149, in main
translations = task.inference_step(generator, models, sample)
File "/path/to/lib/python3.8/site-packages/fairseq/tasks/fairseq_task.py", line 265, in inference_step
return generator.generate(models, sample, prefix_tokens=prefix_tokens)
File "/path/to/lib/python3.8/site-packages/torch/autograd/grad_mode.py", line 26, in decorate_context
return func(*args, **kwargs)
File "/path/to/lib/python3.8/site-packages/fairseq/sequence_generator.py", line 113, in generate
return self._generate(model, sample, **kwargs)
File "/path/to/lib/python3.8/site-packages/torch/autograd/grad_mode.py", line 26, in decorate_context
return func(*args, **kwargs)
File "/path/to/lib/python3.8/site-packages/fairseq/sequence_generator.py", line 376, in _generate
cand_scores, cand_indices, cand_beams = self.search.step(
File "/path/to/lib/python3.8/site-packages/fairseq/search.py", line 81, in step
torch.div(self.indices_buf, vocab_size, out=self.beams_buf)
RuntimeError: result type Float can't be cast to the desired output type Long