fairseq2 Documentation

repository·main·Indexed 22 days ago

https://github.com/facebookresearch/fairseq2

A modular, high-performance sequence modeling toolkit for training and deploying large-scale models (70B+ parameters) for speech recognition, translation, and language modeling. It features multi-GPU/multi-node training via DDP and FSDP, native vLLM support, a streaming-based C++ data pipeline, and a setuptools extension mechanism for custom models and optimizers. The project consists of a Python interface (fairseq2) and a native C++/CUDA library (fairseq2n).

Tokens
68.7K
Snippets
180
Records
275
Agent score
78%

What's inside fairseq2

  1. Overview of fairseq2.data module

    main

    The fairseq2.data module provides high-performance data processing pipelines and utilities for machine learning workflows. It is designed for high-throughput workloads and utilizes both Python and C++ implementations for performance-critical operations.

    Key capabilities include:

    • High-Performance Data Pipelines: Optimized C++-based loading and processing.
    • Text Processing: Tokenization and preprocessing utilities.
    • Audio Processing: Tools for loading audio data and extracting features.
    • Structured Data: Support for formats like Parquet, JSON, and CSV.
    • Memory Efficiency: Support for streaming and batched processing of large datasets.
  2. Overview of the fairseq2.nn module

    main

    The fairseq2.nn module provides the fundamental building blocks and utilities for constructing transformer-based models and other neural network architectures. It is designed to support efficient neural network operations through a unified batch layout system and core architectural components.

    Key functional areas include:

    • Batch Layout: A unified system for managing tensor layouts across different dimensions.
    • Core Layers: Implementations of embeddings, position encoders, normalization, projections, and residual connections.
    • State Management: Utilities for handling incremental states (useful for autoregressive decoding).
    • Utilities: General-purpose neural network operation helpers.
  3. Overview of fairseq2 features

    main

    fairseq2 is a modular sequence modeling toolkit designed for research and production. Key features include:

    • Training: Multi-GPU, multi-node training using DDP, FSDP, and tensor parallelism (supports 70B+ models).
    • Inference: Native support for vLLM, with built-in sampling and beam search sequence generators.
    • Extensibility: Uses a setuptools extension mechanism to register new models, optimizers, LR schedulers, and trainer units without forking the library.
    • Data Pipeline: A high-throughput, streaming-based C++ API supporting speech and video decoding.
    • Configuration: Flexible, deterministic configuration via a built-in structured API.
    • Asset Management: Programmatic asset cards for version-controlled access to models, datasets, and tokenizers.
    • Modern Tooling: Built on modern PyTorch features like torch.compile and PyTorch FSDP.
  4. Integrate HuggingFace Transformers with fairseq2

    main

    The fairseq2.models.hg module provides seamless integration with HuggingFace Transformers models. It allows you to load and use any HuggingFace model within fairseq2's training and inference pipelines.

    Prerequisites This module requires the transformers library. Install it via:

    pip install transformers

    Note on Security Some models require trust_remote_code=True for custom architectures. Only use this with trusted model sources.

  5. Use fairseq2.assets for model and asset management

    main

    The fairseq2.assets module provides a model hub system designed for loading pre-trained models, tokenizers, and other essential assets.

    While the documentation is currently under development, the module is architected to handle:

    • Asset store and registry: Managing available assets.
    • Model downloading and caching: Automatically fetching and storing models locally.
    • Asset metadata management: Handling information associated with specific assets.
    • Pre-trained model catalogs: Accessing collections of ready-to-use models.

    For specific implementation details, refer to the source code or available usage examples in the repository.

  6. What is a Gang in fairseq2?

    main

    A Gang is an abstraction representing a set of processes capable of performing collective communication operations (e.g., all_reduce, broadcast). It encapsulates device management, process ranks, and total process count into a single object.

    There are two primary implementations:

    1. ProcessGroupGang: Used for real distributed environments, wrapping PyTorch's ProcessGroup (using NCCL or Gloo backends).
    2. FakeGang: Used for testing or single-process scenarios, simulating the distributed API without requiring a multi-process setup.
    import torch
    from fairseq2.gang import ProcessGroupGang
    
    device = torch.get_default_device()
    # Creates a ProcessGroup with NCCL or Gloo backend depending on device
    gang = ProcessGroupGang.create_default_process_group(device)
    
    tensor = torch.ones((8, 8), device=gang.device)
    gang.all_reduce(tensor, ReduceOperation.SUM)
  7. Match fairseq2 variant to PyTorch version

    main

    CRITICAL: Version Compatibility

    fairseq2 relies on the PyTorch C++ API, which lacks API/ABI compatibility between releases. You must install the fairseq2 variant that exactly matches your installed PyTorch version.

    Failure to match versions will result in immediate process crashes or spurious segmentation faults. If you upgrade PyTorch, you must also upgrade fairseq2 to the corresponding variant.

    Supported Variants

    Replace the suffix in the --extra-index-url (e.g., pt2.8.0/cu128) with the appropriate combination for your system.

    Linux

    PyTorch VersionVariants
    2.8.0pt2.8.0/cpu, pt2.8.0/cu126, pt2.8.0/cu128
    2.7.1pt2.7.1/cpu, pt2.7.1/cu126, pt2.7.1/cu128
    2.6.0pt2.6.0/cpu, pt2.6.0/cu124

    macOS (Apple Silicon)

    PyTorch VersionVariants
    2.8.0pt2.8.0/cpu
    2.7.1pt2.7.1/cpu

    Windows Support

    fairseq2 does not support Windows natively. Use Windows Subsystem for Linux (WSL) and follow the Linux installation instructions.

  8. Manage checkpoints with the new v0.5 format

    main

    fairseq2 v0.5 introduces a new checkpoint format that serves as a lightweight alternative to PyTorch DCP.

    Key features include:

    • Dynamic Resharding: Supports 3-D model sharding APIs for both offline (checkpoint) and online (training) settings.
    • No Process Groups: Unlike DCP, you do not need to set up process groups for saving or loading checkpoints.
    • Inspectability: Checkpoints are generated as regular, user-inspectable PyTorch tensor files (.pt).
    • Asynchronous Management: A new asynchronous checkpoint manager is included, which is fully deterministic and handles NFS lookup caches to prevent race conditions during async evaluation jobs.
    • Model-Only Saving: You can choose to save only models instead of entire checkpoints to reduce disk overhead during short-running post-training jobs.
  9. Use the fairseq2 model hub to manage model families

    main

    The fairseq2.models.hub module provides a unified interface for interacting with different model families (e.g., Qwen, LLaMA, Mistral). Each family has its own hub that allows you to list available architectures, create new uninitialized models, load models from asset cards, or load custom checkpoints.

    To use a specific family, use its dedicated accessor function (e.g., get_qwen_model_hub()) to obtain a ModelHub instance.

    from fairseq2.models.qwen import get_qwen_model_hub
    
    # Get the model hub for Qwen family
    hub = get_qwen_model_hub()
    
    # List available architectures
    archs = hub.get_archs()
    print(f"Available architectures: {archs}")
    
    # Create a new uninitialized model
    config = hub.get_arch_config("qwen25_7b")
    model = hub.create_new_model(config)
    
    # Load a model from asset card
    model = hub.load_model("qwen25_7b")
    
    # Load a model from custom checkpoint
    from pathlib import Path
    model = hub.load_custom_model(Path("/path/to/checkpoint.pt"), config)
  10. Compare Packed vs Padded Batching performance

    main

    Packed Batches are more memory efficient and better for highly variable sequence lengths because they only allocate space for actual elements. However, they require more careful indexing.

    Padded Batches are simpler to implement and better for uniform attention operations but may waste memory by allocating max_seq_len for every sequence in the batch.

    # Memory comparison
    seq_lens = [100, 50, 75, 25]  # Variable length sequences
    
    # Padded: allocates max_len for all sequences
    padded_layout = BatchLayout((4, 100), seq_lens=seq_lens, packed=False)
    padded_memory = 4 * 100  # 400 positions
    
    # Packed: only allocates needed positions
    packed_layout = BatchLayout((250,), seq_lens=seq_lens, packed=True)
    packed_memory = sum(seq_lens)  # 250 positions (37.5% savings)