bridge.peft.base#
Module Contents#
Classes#
Abstract base class for Parameter-Efficient Fine-Tuning (PEFT) methods. |
Functions#
Module paths a model keeps out of PEFT. |
|
Wrap a walk callback so that it returns modules on |
|
The modules a PEFT walk starts from: every chunk of a list, the wrapped module of a DDP model, or the model. |
Data#
API#
- bridge.peft.base.logger: logging.Logger#
‘getLogger(…)’
- bridge.peft.base.ModelType#
‘TypeVar(…)’
- bridge.peft.base.excluded_module_paths(model: torch.nn.Module) frozenset[str]#
Module paths a model keeps out of PEFT.
A model, or one pipeline chunk of it, may define
peft_excluded_module_paths()returning the dotted paths (asnamed_modules()reports them) of modules that no PEFT method may replace or wrap module by module. The case this exists for is parameter sharing by aliasing: ERNIE 5.5’s shared-decoder MTP body holds the verynn.Parameterobjects of the decoder’s last layer in its own modules, so wrapping either side would give the shared weights two independent adapters and move them behind wrapper attributes. Modules on the returned paths keep their frozen base parameters; everything else is transformed as usual. :meth:PEFT.__call__reads the declaration once per traversal root and reuses that snapshot for its validation and transform walks, so the method may return any iterable, including one that can be consumed only once.- Parameters:
model – A model chunk, possibly behind a wrapper that exposes it as
.module(Float16Module, DDP).- Returns:
The excluded paths relative to
model(prefixed withmodule.when the method lives on the wrapped module), or an empty set when the model does not define the method.- Raises:
TypeError – If the method returns anything but an iterable of nonempty strings.
- bridge.peft.base._skipping_excluded(func, excluded: frozenset[str])#
Wrap a walk callback so that it returns modules on
excludedpaths unchanged.
- bridge.peft.base._traversal_roots(
- model: torch.nn.Module | list[megatron.core.transformer.module.MegatronModule],
The modules a PEFT walk starts from: every chunk of a list, the wrapped module of a DDP model, or the model.
- class bridge.peft.base.PEFT#
Bases:
abc.ABCAbstract base class for Parameter-Efficient Fine-Tuning (PEFT) methods.
This class defines the interface for PEFT methods, which are used to fine-tune large language models efficiently by modifying only a small subset of the model’s parameters.
.. rubric:: Example
class MyPEFT(PEFT): def transform(self, module, name=None, prefix=None): # Implement the transform logic pass
from megatron.bridge.models import get_base_model
peft = MyPEFT() base_model = get_base_model(model_config) # Returns list[MegatronModule] adapted_model = peft(base_model, training=True)
- params_to_save: set[str]#
‘field(…)’
- abstractmethod transform(
- module: torch.nn.Module,
- name: Optional[str] = None,
- prefix: Optional[str] = None,
Transform a single module according to the PEFT method.
This method is called for each module in the model during the PEFT application process. It should be implemented by subclasses to define how individual modules are transformed for the specific PEFT technique.
- Parameters:
module (nn.Module) – The individual module to be transformed.
name (Optional[str]) – The name of the module within the model structure. Defaults to None.
prefix (Optional[str]) – A prefix to be added to the module name, typically used for nested modules. Defaults to None.
- Returns:
The transformed module. This can be the original module with modifications, a new module replacing the original, or the original module if no transformation is needed for this specific module.
- Return type:
nn.Module
.. note::
This method is automatically called for each module in the model when the PEFT instance is applied to the model using the call method.
- __call__(
- model: bridge.peft.base.ModelType,
- training: bool = True,
Apply the PEFT method to the entire model.
This method freezes the model parameters and walks through the model structure, applying the transform method to each module. Modules a model chunk names in
peft_excluded_module_paths()are left as they are (see- Func:
excluded_module_paths).- Parameters:
model – The model to be fine-tuned. Can be a single model or a list of model chunks (for pipeline parallelism).
training (bool) – Whether the model will be used for training. If False, additional freezing may be applied. Defaults to True.
- Returns:
The same type as the input model, transformed with PEFT applied.
- _walk_model(
- model: bridge.peft.base.ModelType,
- func,
- *,
- exclusions: dict[int, frozenset[str]] | None = None,
Apply
functo every module ofmodel(a module, a DDP-wrapped module or a list of chunks).exclusionsmaps theid()of a traversal root (see :func:_traversal_roots) to the module paths that root keeps out of PEFT (see :func:excluded_module_paths); those modules are returned unchanged. Each chunk’s exclusions apply to that chunk only.
- enable_adapter_layers(model: bridge.peft.base.ModelType) None#
Enable adapter layers for all PEFT-wrapped modules in the model.
- disable_adapter_layers(model: bridge.peft.base.ModelType) None#
Disable adapter layers for all PEFT-wrapped modules in the model.
- disable_adapter(model: bridge.peft.base.ModelType)#
Disables the adapter module.
- freeze_model(
- model: bridge.peft.base.ModelType,
- training: bool = True,
Apply a default freeze method to the model.
This method freezes all the model parameters. This method can be overridden by subclasses to implement custom freeze strategies (e.g. freeze only parts of the model)
- Parameters:
model – The model to be fine-tuned.
training (bool) – Whether the model is being used for training. Affects training mode handling.
- set_params_to_save(model: bridge.peft.base.ModelType) None#
Set parameters to be saved for PEFT checkpointing.
This method identifies which parameters should be saved during checkpointing to reduce storage requirements (only adapter parameters, not the full model).
- Parameters:
model – The model after PEFT has been applied.
- adapter_key_filter(key) bool#
Filter function for adapter parameters during checkpointing.
This method determines if a parameter should be included in checkpoints. Used to save only adapter weights, not the full model.
- Parameters:
key (str or tuple) – Parameter name/key to check. Can be a string for regular checkpointing or a tuple for distributed checkpointing.
- Returns:
True if the parameter should be saved.
- Return type:
bool