Transformers documentation
Fusion mapping (experimental feature)
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:
- Monkey patching to swap module classes before model instantiation.
- Dynamic weight loading to map weights between the original and fused runtime layouts.
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:
- from_pretrained() uses the explicit
fusion_configargument or falls back toconfig.fusion_config. - The fusion registry validates the requested fusion names.
- Each enabled fusion meta-initializes the target model class, optionally filters candidate modules by name, and uses
is_fusable(...)to discover compatible module classes. - 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.
- Each fused module provides the
~WeightTransformrules that map its weights from the original checkpoint layout, built from its own shapes, so checkpoint loading can fill the fused runtime layout. - By default, save_pretrained() uses the reverse conversion path to restore the original checkpoint layout. Pass
save_original_format=Falseto 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_embeddingsEnable with:fusion_config = {"patch_embeddings": True}Effect: Replaces compatible
nn.Conv3dpatch embedding projections with equivalent flattenednn.Linearprojections at runtime.
Extending fusion mapping
To add a new fusion family:
- Add an
is_fusablepredicate. This decides whether a discovered module is compatible with the fusion. - Optionally add
target_modules_patterns. This makes the discovery step more explicit by pre-filtering candidate module names beforeis_fusable(...). - Add a
make_fused_classfactory. This returns the runtime replacement class for a compatible module class. - Add an
is_fusedpredicate and amake_transformsfactory if the fused layout needs checkpoint conversion.is_fusedrecognizes the modules built bymake_fused_class, andmake_transformsreturns the~WeightTransformrules that map the weights of one fused module between the original and fused layouts. - Register the new
ModuleFusionSpecinfusion_mapping.py.
Once registered, the new fusion becomes available through fusion_config.
Internal API
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.
Return the log message emitted when no compatible modules are found.
Return whether module is compatible with this fusion family.
Return whether module is an instance of a class built by make_fused_class.
Build the runtime replacement class for a compatible module class.
Build the weight transforms that load and save the fused module in the original checkpoint layout.
Fuse compatible Conv3d patch embeddings into flattened Linear projections.
transformers.fusion_mapping.get_fusion_patch_mapping
< source >( 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.
Return the weight transforms that load and save the fused modules of model in the original checkpoint layout.