fairseq

raw JSON →
0.12.2 verified Fri May 01 auth: no python maintenance

fairseq is a sequence-to-sequence toolkit by Facebook AI Research, written in PyTorch. It provides reference implementations of various models for machine translation, language modeling, and other sequence generation tasks. Current version: 0.12.2. Release cadence is irregular, with the last significant release in 2021.

pip install fairseq
error ImportError: cannot import name 'transformer' from 'fairseq.models'
cause Incorrect or incomplete import path. The models are submodules that must be imported after fairseq is installed.
fix
Use: from fairseq.models.transformer import TransformerModel
error KeyError: 'in_proj_weight'
cause Loading a checkpoint from an older version of fairseq that used separate k, v, q projections in MultiheadAttention, but the current version expects a single weight.
fix
Convert the checkpoint using a migration script or use the version of fairseq that matches the checkpoint.
error 'generator' object has no attribute 'generate'
cause Using fairseq versions 0.8.0+ where the method was renamed from 'generate' to 'translate'.
fix
Replace .generate() with .translate() in your code.
error ModuleNotFoundError: No module named 'fairseq'
cause fairseq is not installed or the environment is incorrect.
fix
Run: pip install fairseq
breaking Between v0.9.0 and v0.10.0, the MultiheadAttention module changed from a single 'in_proj_weight' to separate k, v, q projections. Models saved with older versions will not load directly.
fix Update model checkpoints or adjust code to match new parameter structure.
breaking In v0.8.0, the torch.hub interface changed: 'generate' was renamed to 'translate'. Code using 'generate' will break.
fix Replace .generate() with .translate() in your code.
deprecated Many components (e.g., older masked LM modules, dataset formats) have been deprecated or replaced. The project is in maintenance mode with few updates.
fix Refer to the latest documentation and avoid using deprecated APIs.
gotcha fairseq is no longer actively maintained; new projects should consider alternatives like fairseq2 or direct PyTorch implementations.
fix For new projects, consider using fairseq2 or other actively maintained toolkits.
gotcha Installing from source may require specific PyTorch versions (e.g., PyTorch 1.8 or older). Check the setup.py for exact dependencies.
fix Use pip install fairseq to get prebuilt wheels or carefully match PyTorch version when building from source.

Quickstart: load a pretrained translation model and translate a sentence.

import torch
import fairseq

# Load a pretrained model from torch.hub
model = torch.hub.load('pytorch/fairseq', 'transformer.wmt14.en-fr', tokenizer='moses', bpe='subword_nmt')

# Prepare input
input_text = 'Hello world!'

# Translate
print(model.translate(input_text))