Transformers documentation

Fusion mapping (experimental feature)

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

Fusion mapping (experimental feature)

Fusion mapping provides an opt-in way to replace model submodules at load time while preserving the original checkpoint format.

It builds on:

Fusion mapping is an experimental loading feature. It changes the runtime module structure and may affect model behavior. Use it only when you explicitly want a fused runtime layout.

Quick start

Fusion is enabled through from_pretrained() with fusion_config:

from transformers import AutoModelForImageTextToText


model = AutoModelForImageTextToText.from_pretrained(
    "Qwen/Qwen2-VL-2B-Instruct",
    fusion_config={"patch_embeddings": True},
)

By default, no fusion is applied. If fusion_config is stored in the model config, from_pretrained() will reuse it automatically.

How it works

Fusion is set up separately for each from_pretrained() call that requests it:

  1. from_pretrained() uses the explicit fusion_config argument or falls back to config.fusion_config.
  2. The fusion registry validates the requested fusion names.
  3. Each enabled fusion meta-initializes the target model class, optionally filters candidate modules by name, and uses is_fusable(...) to discover compatible module classes.
  4. The model is initialized inside apply_patches() with the fused replacement classes. Only this model gets fused modules: other models created in the same process keep the original classes.
  5. Each fused module provides the ~WeightTransform rules that map its weights from the original checkpoint layout, built from its own shapes, so checkpoint loading can fill the fused runtime layout.
  6. By default, save_pretrained() uses the reverse conversion path to restore the original checkpoint layout. Pass save_original_format=False to keep the converted runtime layout instead.

This lets a fusion use a different runtime module structure while still loading from the original checkpoint format, and by default saving back to it as well.

Note: fusion replaces whole classes, so every module of a fusable class gets fused.

Current fusion families

Currently, fusion_config supports one fusion family:

  • patch_embeddings Enable with:

    fusion_config = {"patch_embeddings": True}

    Effect: Replaces compatible nn.Conv3d patch embedding projections with equivalent flattened nn.Linear projections at runtime.

Extending fusion mapping

To add a new fusion family:

  1. Add an is_fusable predicate. This decides whether a discovered module is compatible with the fusion.
  2. Optionally add target_modules_patterns. This makes the discovery step more explicit by pre-filtering candidate module names before is_fusable(...).
  3. Add a make_fused_class factory. This returns the runtime replacement class for a compatible module class.
  4. Add an is_fused predicate and a make_transforms factory if the fused layout needs checkpoint conversion. is_fused recognizes the modules built by make_fused_class, and make_transforms returns the ~WeightTransform rules that map the weights of one fused module between the original and fused layouts.
  5. Register the new ModuleFusionSpec in fusion_mapping.py.

Once registered, the new fusion becomes available through fusion_config.

Internal API

class transformers.fusion_mapping.ModuleFusionSpec

< >

( )

Base recipe for a fusion family.

A fusion spec decides which modules are eligible for a fusion, how to build the runtime replacement class, and which weight transforms are needed to map checkpoints between the original and fused layouts.

get_empty_log

< >

( model_name: str )

Return the log message emitted when no compatible modules are found.

is_fusable

< >

( module: Module )

Return whether module is compatible with this fusion family.

is_fused

< >

( module: Module )

Return whether module is an instance of a class built by make_fused_class.

make_fused_class

< >

( original_cls: type[torch.nn.Module] )

Build the runtime replacement class for a compatible module class.

make_transforms

< >

( module: Module )

Build the weight transforms that load and save the fused module in the original checkpoint layout.

class transformers.fusion_mapping.PatchEmbeddingsFusionSpec

< >

( )

Fuse compatible Conv3d patch embeddings into flattened Linear projections.

transformers.fusion_mapping.get_fusion_patch_mapping

< >

( config: PretrainedConfigfusion_config: collections.abc.Mapping[str, bool | collections.abc.Mapping[str, typing.Any]] )

Return the module classes to replace while one model of class cls is initialized with fusion_config.

transformers.fusion_mapping.get_fusion_weight_transforms

< >

( model: Module )

Return the weight transforms that load and save the fused modules of model in the original checkpoint layout.

Update on GitHub