|
| 1 | +<!--Copyright 2026 The HuggingFace Team. All rights reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 4 | +the License. You may obtain a copy of the License at |
| 5 | +
|
| 6 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +
|
| 8 | +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 9 | +an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 10 | +specific language governing permissions and limitations under the License. |
| 11 | +
|
| 12 | +⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be |
| 13 | +rendered properly in your Markdown viewer. |
| 14 | +
|
| 15 | +--> |
| 16 | + |
| 17 | +# Fusion mapping (experimental feature) |
| 18 | + |
| 19 | +Fusion mapping provides an opt-in way to replace model submodules at load time while preserving the original checkpoint format. |
| 20 | + |
| 21 | +It builds on: |
| 22 | + |
| 23 | +- [Monkey patching](./monkey_patching) to swap module classes before model instantiation. |
| 24 | +- [Dynamic weight loading](./weightconverter) to map weights between the original and fused runtime layouts. |
| 25 | + |
| 26 | +> [!WARNING] |
| 27 | +> 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. |
| 28 | +
|
| 29 | +## Quick start |
| 30 | + |
| 31 | +Fusion is enabled through [`~PreTrainedModel.from_pretrained`] with `fusion_config`: |
| 32 | + |
| 33 | +```python |
| 34 | +from transformers import AutoModelForImageTextToText |
| 35 | + |
| 36 | + |
| 37 | +model = AutoModelForImageTextToText.from_pretrained( |
| 38 | + "Qwen/Qwen2-VL-2B-Instruct", |
| 39 | + fusion_config={"patch_embeddings": True}, |
| 40 | +) |
| 41 | +``` |
| 42 | + |
| 43 | +By default, no fusion is applied. |
| 44 | +If `fusion_config` is stored in the model config, `from_pretrained()` will reuse it automatically. |
| 45 | + |
| 46 | +## How it works |
| 47 | + |
| 48 | +Fusion registration happens before the model is instantiated: |
| 49 | + |
| 50 | +1. [`~PreTrainedModel.from_pretrained`] uses the explicit `fusion_config` argument or falls back to `config.fusion_config`. |
| 51 | +2. The fusion registry validates the requested fusion names. |
| 52 | +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. |
| 53 | +4. Fused replacement classes are registered through [`~transformers.monkey_patching.register_patch_mapping`]. |
| 54 | +5. Matching [`~WeightTransform`] rules are generated from the config so checkpoint loading can map weights into the fused runtime layout. |
| 55 | +6. By default, [`~PreTrainedModel.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. |
| 56 | + |
| 57 | +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. |
| 58 | + |
| 59 | +Note: With the current monkey-patching mechanism, fusion registration is class-level: one compatible module class maps to one fused replacement class. |
| 60 | + |
| 61 | +## Current fusion families |
| 62 | + |
| 63 | +Currently, `fusion_config` supports one fusion family: |
| 64 | + |
| 65 | +- `patch_embeddings` |
| 66 | + Enable with: |
| 67 | + |
| 68 | + ```python |
| 69 | + fusion_config = {"patch_embeddings": True} |
| 70 | + ``` |
| 71 | + |
| 72 | + Effect: |
| 73 | + Replaces compatible `nn.Conv3d` patch embedding projections with equivalent flattened `nn.Linear` projections at runtime. |
| 74 | + |
| 75 | +## Extending fusion mapping |
| 76 | + |
| 77 | +To add a new fusion family: |
| 78 | + |
| 79 | +1. Add an `is_fusable` predicate. |
| 80 | + This decides whether a discovered module is compatible with the fusion. |
| 81 | +2. Optionally add `target_modules_patterns`. |
| 82 | + This makes the discovery step more explicit by pre-filtering candidate module names before `is_fusable(...)`. |
| 83 | +3. Add a `make_fused_class` factory. |
| 84 | + This returns the runtime replacement class for a compatible module class. |
| 85 | +4. Add a `make_transforms` factory if the fused layout needs checkpoint conversion. |
| 86 | + This returns the [`~WeightTransform`] rules that map weights between the original and fused layouts for a given config. |
| 87 | +5. Register the new `ModuleFusionSpec` in [`fusion_mapping.py`](https://github.com/huggingface/transformers/blob/main/src/transformers/fusion_mapping.py). |
| 88 | + |
| 89 | +Once registered, the new fusion becomes available through `fusion_config`. |
| 90 | + |
| 91 | +## Internal API |
| 92 | + |
| 93 | +[[autodoc]] fusion_mapping.ModuleFusionSpec |
| 94 | + |
| 95 | +[[autodoc]] fusion_mapping.PatchEmbeddingsFusionSpec |
| 96 | + |
| 97 | +[[autodoc]] fusion_mapping._register_module_fusion |
| 98 | + |
| 99 | +[[autodoc]] fusion_mapping.register_fusion_patches |
0 commit comments