fairseq2.models.hg.api

High-level API for loading HuggingFace models and tokenizers.

Functions

load_causal_lm(name, **kwargs)

Load a causal language model (GPT-style).

load_hg_model_simple(name, *[, model_type, ...])

Load a HuggingFace model with simplified configuration.

load_hg_tokenizer_simple(name, *[, ...])

Load a HuggingFace tokenizer with custom special tokens.

load_multimodal_model(name, **kwargs)

Load a multimodal model with processor.

load_seq2seq_lm(name, **kwargs)

Load a sequence-to-sequence model (T5-style).

fairseq2.models.hg.api.load_hg_model_simple(name: str, *, model_type: str = 'auto', use_processor: bool = False, device: str = 'cpu', trust_remote_code: bool = False, dtype: dtype | None = None, **kwargs: Any) Any[source]

Load a HuggingFace model with simplified configuration.

This is the main entry point for users who want to load HuggingFace models into fairseq2 with minimal configuration.

Parameters:
  • name – HuggingFace model identifier (e.g., ‘gpt2’, ‘microsoft/DialoGPT’)

  • model_type – Type of AutoModel to use (‘auto’, ‘causal_lm’, ‘seq2seq_lm’, ‘custom’)

  • use_processor – Whether to use AutoProcessor instead of AutoTokenizer

  • device – Device placement (‘cpu’, ‘cuda:0’, or ‘auto’ for HF accelerate)

  • trust_remote_code – Whether to trust remote code for custom architectures

  • dtype – PyTorch dtype to use. None means ‘auto’ (let HuggingFace decide)

  • kwargs – Additional kwargs passed to from_pretrained

Returns:

The loaded HuggingFace model

Examples:

Load a standard causal language model:

model = load_hg_model_simple("gpt2")

Load a seq2seq model:

model = load_hg_model_simple("t5-small", model_type="seq2seq_lm")

Load a multimodal model with processor:

model = load_hg_model_simple(
    "Qwen/Qwen2.5-Omni-7B",
    use_processor=True,
    trust_remote_code=True
)
fairseq2.models.hg.api.load_hg_tokenizer_simple(name: str, *, unk_token: str | None = None, bos_token: str | None = None, eos_token: str | None = None, pad_token: str | None = None, boh_token: str | None = None, eoh_token: str | None = None) HgTokenizer[source]

Load a HuggingFace tokenizer with custom special tokens.

Parameters:
  • name – HuggingFace tokenizer identifier (same as model name)

  • unk_token – Custom unknown token

  • bos_token – Custom beginning of sequence token

  • eos_token – Custom end of sequence token

  • pad_token – Custom padding token

  • boh_token – Custom beginning of human token

  • eoh_token – Custom end of human token

Returns:

The loaded tokenizer with custom tokens

Examples:

Load a tokenizer with default settings:

tokenizer = load_hg_tokenizer_simple("gpt2")

Load with custom tokens:

tokenizer = load_hg_tokenizer_simple(
    "gpt2",
    pad_token="<pad>",
    eos_token="<end>"
)
fairseq2.models.hg.api.load_causal_lm(name: str, **kwargs: Any) Any[source]

Load a causal language model (GPT-style).

Convenience function for loading causal language models like GPT-2, DialoGPT, or LLaMA.

Parameters:
  • name – HuggingFace model identifier

  • kwargs – Additional arguments passed to load_hg_model_simple

Returns:

A causal language model

Example:

Load GPT-2 for text generation:

model = load_causal_lm("gpt2")
fairseq2.models.hg.api.load_seq2seq_lm(name: str, **kwargs: Any) Any[source]

Load a sequence-to-sequence model (T5-style).

Convenience function for loading seq2seq models like T5, BART, or Pegasus for tasks like translation, summarization, and question answering.

Parameters:
  • name – HuggingFace model identifier

  • kwargs – Additional arguments passed to load_hg_model_simple

Returns:

A sequence-to-sequence model

Example:

Load T5 for translation:

model = load_seq2seq_lm("t5-small")
fairseq2.models.hg.api.load_multimodal_model(name: str, **kwargs: Any) Any[source]

Load a multimodal model with processor.

Convenience function for loading multimodal models that require processors instead of tokenizers (e.g., vision-language models).

Parameters:
  • name – HuggingFace model identifier

  • kwargs – Additional arguments passed to load_hg_model_simple

Returns:

A multimodal model

Example:

Load a multimodal model:

model = load_multimodal_model(
    "Qwen/Qwen2.5-Omni-3B",
    trust_remote_code=True
)