Refactor: PEFT method registration function #2282
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a refactor of how new PEFT methods are registered. It got bigger than I initially expected.
Goal
The goal of this refactor is the following: Right now, when a new PEFT method is added, a new directory is created in
src/peft/tuners/<name>
with a config, model, etc. This is fine and self-contained.However, in addition to that, a couple of other places in the PEFT code base need to be touched for this new PEFT method to become usable.
As an example, take the recently added Bone method (#2172). Ignoring tests, docs, and examples, we have the additions to
src/peft/tuners/bone
, but also need to:PEFT_TYPE_TO_CONFIG_MAPPING
inmapping.py
.PEFT_TYPE_TO_TUNER_MAPPING
inmapping.py
.PEFT_TYPE_TO_MODEL_MAPPING
inpeft_model.py
PEFT_TYPE_TO_PREFIX_MAPPING
inutils/constants.py
get_peft_model_state_dict
inutils.save_and_load.py
With the changes in this PR, all these steps can be omitted.
On top of that, we also have the re-imports to
peft/__init__.py
andpeft/tuners/__init__.py
but those are still required (I'm hesitant to mess with the import system). Furthermore, it's still required to add an entry toPeftType
inutils.peft_types.py
. Since this is anenum
, it can't be easily generated automatically. Therefore, adding a new PEFT method is still not 100% self-contained.Changes in this PR
With this PR, less book-keeping is required. Instead of the 5 steps described above, contributors now only need to call
in the
__init__.py
of their PEFT method. In addition to registering the method, this also performs a couple of sanity checks (e.g. no duplicate names, method name and method prefix being identical).Moreover, since so much book keeping is removed, this PR reduces the number of lines of code overall (at the moment +317, - 343).
Implementation
The real difficulty of this task is that the module structure in PEFT is really messy, easily resulting in circular imports. This has been an issue in the past but has been especially painful here. For this reason, some stuff had to be moved around:
MODEL_TYPE_TO_PEFT_MODEL_MAPPING
is now inauto.py
instead ofmapping.py
PEFT_TYPE_TO_PREFIX_MAPPING
has been moved tomapping.py
fromconstants.py
get_peft_model
had to be moved out ofmapping.py
and is now in its own module,func.py
(better name suggestions welcome). This should be safe, as the function is re-imported to the main PEFT namespace, which all examples use.The
PEFT_TYPE_TO_MODEL_MAPPING
dict could be completely removed, as it was basically redundant withPEFT_TYPE_TO_TUNER_MAPPING
. Theget_peft_model_state_dict
could be simplified, as a lot of code was almost duplicated.There were a few instances in
peft_model.py
like:Now, instead of hard-coding the model, I just do
model_cls = PEFT_TYPE_TO_TUNER_MAPPING[config.peft_type]
.Overall, I think this is a cleaner module structure, but still not very clean overall.
Open questions
I'm not 100% sure if this should be merged. AFAICT it should be a safe refactor that does not affect user code. There could be other packages out there that use some PEFT internals that could break with this refactor. If we decide to merge this, we should consider alerting potentially affected packages to test it.
I'm also open to the argument that the benefits are not outweighing the cost of the refactor.