Skip to content
fn

initialize_lora_eva_weights

→None
initialize_lora_eva_weights(model: nn.Module, dataloader: Iterable[object] | None = None, eva_state_dict: dict[str, Tensor] | None = None, forward_fn: _ForwardFn = _forward_fn_dict, prepare_model_inputs_fn: _ModelInputFn = _prepare_model_inputs_fn_language_modeling, prepare_layer_inputs_fn: _LayerInputFn = _prepare_layer_inputs_fn_language_modeling, adapter_name: str = 'default', gather_distributed_inputs: bool = True, show_progress_bar: bool = True)

Initialise LoRA layers of model in place with EVA singular vectors and redistributed ranks.

A layer whose rank changes gets new factors (lora_A the singular vectors, lora_B zero), so make the optimizer afterwards; rank_pattern and alpha_pattern of the adapter's config record the new ranks, and a saved adapter is rebuilt at them.

Parameters

A PeftModel holding an adapter made with init_lora_weights='eva'.
dataloaderiterable= None
The calibration batches; required unless eva_state_dict is given.
eva_state_dictdict of str to Tensor= None
The result of get_eva_state_dict, to skip the forward passes.
forward_fncallable= _forward_fn_dict
Runs the model on a batch; by default a mapping is passed as keyword arguments.
prepare_model_inputs_fncallable= _prepare_model_inputs_fn_language_modeling
Turns a batch into what prepare_layer_inputs_fn is given as model_input; by default the token mask of a language-modelling batch.
prepare_layer_inputs_fncallable or dict of str to callable= _prepare_layer_inputs_fn_language_modeling
Selects the activations a layer's singular vectors are taken from, one function or one per module name; by default the unmasked tokens.
adapter_namestr= "default"
The adapter to initialise.
gather_distributed_inputsbool= True
Kept for the reference's signature: there is no process group to gather across.
show_progress_barbool= True
Kept for the reference's signature: nothing is displayed.

Raises

ValueError
model is not a PeftModel of an init_lora_weights='eva' adapter, more than one adapter is active, no dataloader or eva_state_dict is given, the adapter is merged into the base weights (unmerge_adapter() first), or EVA gives every layer rank 0 (the adapter would hold no layer; the model is left as it was).

Notes

A layer EVA gives rank 0 loses this adapter and no other: its factors are dropped from the layer and the layer is left out of the adapter's target_modules, so a saved adapter is rebuilt without it. Only a layer left holding no adapter at all is replaced by the module it wrapped. The reference replaces the layer by its base module whatever else it holds, deleting the other adapters on it while their configs still list the module; this does not.

rank_pattern and alpha_pattern match a name by its dotted tail, so the entry of q_proj is also the one read for block.q_proj. A layer holding the config's r or lora_alpha is therefore listed too when such an entry would be read for it, and a saved adapter is rebuilt at the rank and scale of every layer. The reference lists only the layers that differ, and reads a name through the first key that matches it, so it can rebuild block.q_proj at the rank of q_proj; this does not.

Used by 1