transformers/examples/modular-transformers
Quentin Gallouédec de24fb63ed
Use HF papers (#38184)
* Use hf papers

* Hugging Face papers

* doi to hf papers

* style
2025-06-13 11:07:09 +00:00
..
configuration_dummy.py Modular transformers: modularity and inheritance for new model additions (#33248) 2024-09-24 15:54:07 +02:00
configuration_my_new_model.py Use HF papers (#38184) 2025-06-13 11:07:09 +00:00
configuration_my_new_model2.py [modular] Do not track imports in functions (#36279) 2025-02-25 10:29:47 +01:00
configuration_new_model.py Use HF papers (#38184) 2025-06-13 11:07:09 +00:00
configuration_super.py Modular transformers: modularity and inheritance for new model additions (#33248) 2024-09-24 15:54:07 +02:00
convert_examples.sh [modular] fixes! (#33820) 2024-09-30 16:43:55 +02:00
image_processing_new_imgproc_model.py [modular] Fix the prefix-based renaming if the old and new model share a common name suffix (#37829) 2025-04-29 10:43:23 +02:00
modeling_add_function.py Use HF papers (#38184) 2025-06-13 11:07:09 +00:00
modeling_dummy_bert.py Use HF papers (#38184) 2025-06-13 11:07:09 +00:00
modeling_dummy.py Use HF papers (#38184) 2025-06-13 11:07:09 +00:00
modeling_from_uppercase_model.py [modular] Fix the prefix-based renaming if the old and new model share a common name suffix (#37829) 2025-04-29 10:43:23 +02:00
modeling_multimodal1.py Use HF papers (#38184) 2025-06-13 11:07:09 +00:00
modeling_multimodal2.py [modular] Fix the prefix-based renaming if the old and new model share a common name suffix (#37829) 2025-04-29 10:43:23 +02:00
modeling_my_new_model2.py Use HF papers (#38184) 2025-06-13 11:07:09 +00:00
modeling_new_task_model.py [tests] expand flex-attn test for vision models (#38434) 2025-06-03 07:40:44 +00:00
modeling_roberta.py Use HF papers (#38184) 2025-06-13 11:07:09 +00:00
modeling_super.py Use HF papers (#38184) 2025-06-13 11:07:09 +00:00
modeling_switch_function.py [modular] Fix the prefix-based renaming if the old and new model share a common name suffix (#37829) 2025-04-29 10:43:23 +02:00
modeling_test_detr.py [modular] Fix the prefix-based renaming if the old and new model share a common name suffix (#37829) 2025-04-29 10:43:23 +02:00
modular_add_function.py Modular: support for importing functions from any file (#35692) 2025-01-16 16:37:53 +00:00
modular_dummy_bert.py Use Python 3.9 syntax in examples (#37279) 2025-04-07 12:52:21 +01:00
modular_dummy.py [modular] fixes! (#33820) 2024-09-30 16:43:55 +02:00
modular_from_uppercase_model.py Support for easier multimodal use of modular (#35056) 2024-12-04 15:13:11 +01:00
modular_multimodal1.py Support for easier multimodal use of modular (#35056) 2024-12-04 15:13:11 +01:00
modular_multimodal2.py [modular] Fix the prefix-based renaming if the old and new model share a common name suffix (#37829) 2025-04-29 10:43:23 +02:00
modular_my_new_model.py Modular transformers: modularity and inheritance for new model additions (#33248) 2024-09-24 15:54:07 +02:00
modular_my_new_model2.py Modular transformers: modularity and inheritance for new model additions (#33248) 2024-09-24 15:54:07 +02:00
modular_new_imgproc_model.py Fix support for image processors modifications in modular (#34866) 2024-11-22 18:14:24 -05:00
modular_new_model.py Modular transformers: modularity and inheritance for new model additions (#33248) 2024-09-24 15:54:07 +02:00
modular_new_task_model.py [tests] expand flex-attn test for vision models (#38434) 2025-06-03 07:40:44 +00:00
modular_roberta.py Large modular logic refactoring (#34487) 2024-11-01 10:13:51 +01:00
modular_super.py Use Python 3.9 syntax in examples (#37279) 2025-04-07 12:52:21 +01:00
modular_switch_function.py Modular: support for importing functions from any file (#35692) 2025-01-16 16:37:53 +00:00
modular_test_detr.py [modular] Fix the prefix-based renaming if the old and new model share a common name suffix (#37829) 2025-04-29 10:43:23 +02:00
README.md Modular transformers: modularity and inheritance for new model additions (#33248) 2024-09-24 15:54:07 +02:00

Using the modular_converter linter

pip install libcst is a must!

sh examples/modular-transformers/convert_examples.sh to get the converted outputs

The modular converter is a new linter specific to transformers. It allows us to unpack inheritance in python to convert a modular file like modular_gemma.py into a single model single file.

Examples of possible usage are available in the examples/modular-transformers, or modular_gemma for a full model usage.

python utils/modular_model_converter.py --files_to_parse "/Users/arthurzucker/Work/transformers/examples/modular-transformers/modular_my_new_model2.py"

How it works

We use the libcst parser to produce an AST representation of the modular_xxx.py file. For any imports that are made from transformers.models.modeling_xxxx we parse the source code of that module, and build a class dependency mapping, which allows us to unpack the modularerence dependencies.

The code from the modular file and the class dependency mapping are "merged" to produce the single model single file. We use ruff to automatically remove the potential duplicate imports.

Why we use libcst instead of the native AST?

AST is super powerful, but it does not keep the docstring, comment or code formatting. Thus we decided to go with libcst