![]() * rework converter * Update modular_model_converter.py * Update modular_model_converter.py * Update modular_model_converter.py * Update modular_model_converter.py * cleaning * cleaning * finalize imports * imports * Update modular_model_converter.py * Better renaming to avoid visiting same file multiple times * start converting files * style * address most comments * style * remove unused stuff in get_needed_imports * style * move class dependency functions outside class * Move main functions outside class * style * Update modular_model_converter.py * rename func * add augmented dependencies * Update modular_model_converter.py * Add types_to_file_type + tweak annotation handling * Allow assignment dependency mapping + fix regex * style + update modular examples * fix modular_roberta example (wrong redefinition of __init__) * slightly correct order in which dependencies will appear * style * review comments * Performance + better handling of dependencies when they are imported * style * Add advanced new classes capabilities * style * add forgotten check * Update modeling_llava_next_video.py * Add prority list ordering in check_conversion as well * Update check_modular_conversion.py * Update configuration_gemma.py |
||
---|---|---|
.. | ||
configuration_dummy.py | ||
configuration_my_new_model.py | ||
configuration_my_new_model2.py | ||
configuration_new_model.py | ||
configuration_super.py | ||
convert_examples.sh | ||
modeling_dummy_bert.py | ||
modeling_dummy.py | ||
modeling_my_new_model2.py | ||
modeling_new_task_model.py | ||
modeling_roberta.py | ||
modeling_super.py | ||
modular_dummy_bert.py | ||
modular_dummy.py | ||
modular_my_new_model.py | ||
modular_my_new_model2.py | ||
modular_new_model.py | ||
modular_new_task_model.py | ||
modular_roberta.py | ||
modular_super.py | ||
README.md |
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