ó
    >:jT“ ã                  ó�  • S SK Jr  S SKrS SKrS SKrS SKrS SKrS SKrS SKJ	r	J
r
  S SKJr  S SKJrJr  S SKJrJrJrJr  S SKrS SKJr  S SKJrJr  S S	KJr  S S
KJr  S SKJr  S SKJ r   S SK!J"r"  S SK#J$r$  S SK%J&r&  S SK'J(r(J)r)  S SK*J+r+J,r,J-r-J.r.J/r/  S SK0J1r1  S SK2J3r3J4r4J5r5J6r6J7r7J8r8  S SK9J:r:J;r;  S SK<J=r=  SSK>J?r?  SSK@JArA  SSKBJCrC  SrD\RŠ                  " \RŒ                  5      \RŠ                  " S5      :¬  rG\G=(       a    \R�                  R“                  5       rJ\S 5       rKS5S jrLS6S jrM " S  S!\Rœ                  \	5      rO " S" S#\	5      rP      S7S$ jrQ " S% S&5      rRS8S' jrSS9S:S( jjrTS;S) jrUS<S=S* jjrVS>S?S, jjrWS@S- jrXSAS. jrYS+\P4       SBS/ jjrZSCS0 jr[\P4         SDS1 jjr\SESFS2 jjr]SESGS3 jjr^SHS4 jr_g)Ié    )ÚannotationsN)ÚABCÚabstractmethod)ÚSequence)ÚcontextmanagerÚnullcontext)ÚAnyÚOptionalÚUnionÚoverload)ÚAlignDevicesHook)Únamed_module_tensorsÚoffload_state_dict)Úversion)Únn)Útqdm)ÚPreTrainedModel)ÚConv1D)Úis_transformers_ge_v5)ÚPEFT_TYPE_TO_PREFIX_MAPPING)ÚINCLUDE_LINEAR_LAYERS_SHORTHANDÚUPCAST_DTYPES)ÚDUMMY_MODEL_CONFIGÚDUMMY_TARGET_MODULESÚEMBEDDING_LAYER_NAMESÚ#MIN_TARGET_MODULES_FOR_OPTIMIZATIONÚSEQ_CLS_HEAD_NAMES)Úinit_empty_weights)ÚAuxiliaryTrainingWrapperÚ%_get_module_names_tied_with_embeddingÚ_set_adapterÚ_set_layer_requires_gradÚmatch_target_against_keyÚ set_additional_trainable_modules)ÚPeftTypeÚTaskType)ÚPeftWarningé   )Ú
PeftConfig)Ú_get_submodulesé   )Ú
BufferDictáK  Model has `tie_word_embeddings=True` and a tied layer is part of the adapter, but no implementation exists to tie the adapters. This can lead to complications, for example when merging the adapter or converting your model to formats other than safetensors. Check the discussion here: https://github.com/huggingface/peft/issues/2777z2.5.0c              #  ó”  #   • / nU R                  5        HŠ  u  p#US;   a  M  [        US5      (       d  M   [        UR                  [        5      (       d  MA  UR                  R
                  (       d  M^  UR                  R                  U5        UR                  U5        MŒ     Sn[        U S5      (       Ga  [        U R                  S5      (       Gaó  [        U R                  R                  [        5      (       GaÉ  U R                  R                  R
                  (       Ga£  [        R                  " S5      U R                  R                  R                  R                  5       ;   Ga+  [        U R                  R                  R                  S5      (       aü  U R                  R                  R                  R                  R                  n[!        [#        U R                  R                  R                  R                  5      R%                  5       5      S   nXV   S   n/ n[&        R(                  R+                  U5       H-  n	S	U	;   a  UR                  U	5          OUR                  U	5        M/     [&        R(                  R,                  " U6 n
U
S
-   nU R                  R                  R                  U R                  5        SnSv •  U H3  nUR                  R/                  U[        R0                  " / 5      5        M5     U(       Ga6  [3        U R                  5       VVs0 s H  u  p,X,R5                  S5      _M     snnU R                  R                  l        [        R                  " S5      U R                  R                  R                  R                  5       ;   aY  [        U R                  R                  R                  S5      (       a*  [7        WU R                  R                  R                  5        U R                  R                  R/                  U R                  [        R0                  " / 5      5        ggs  snnf 7f)aû  
A utility for modifying a module containing one or more tuners and a base layer, any of which are offloaded to the
CPU or disk. Moves a module's sub-modules to the execution device before some action is performed, after that the
base layer state dictionary is re-assigned (if that layer was offloaded to the disk) and finally the parameters are
offloaded.

If the module has no offloaded sub-modules, this function does nothing.

Args:
    layer ('torch.nn.Module'):
        layer with tuners to be merged
)Ú Ú
base_layerÚ_hf_hookFr0   ÚmetaÚdatasetr   Úsafetensors_filez--z-mergedTNÚcpu)Únamed_modulesÚhasattrÚ
isinstancer1   r   ÚoffloadÚpre_forwardÚappendr0   ÚtorchÚdeviceÚoriginal_devicesÚvaluesÚweights_mapr3   ÚindexÚlistÚdictÚkeysÚosÚpathÚsplitÚjoinÚpost_forwardÚtensorr   Útor   )ÚlayerÚoffloaded_modulesÚnameÚmoduleÚbase_layer_offloadrA   Úmodule_nameÚ	file_nameÚbase_name_arrÚiÚ	base_nameÚsafetensors_filenameÚparams                ÚU/home/mande/repo/quber/.venv/lib/python3.13/site-packages/peft/tuners/tuners_utils.pyÚonload_layerrY   J   s7  é € ð ÐØ×+Ñ+Ö-‰ˆØÐ%Ó%ÙÜ�6˜:×&Ó&¬:°f·o±oÔGW×+XÓ+XÐ]c×]lÑ]l×]t×]tÑ]tØ�O‰O×'Ñ'¨Ô/Ø×$Ñ$ VÖ,ñ .ð ÐÜˆu�l×#Ò#Ü�× Ñ  *×-Ò-Ü�u×'Ñ'×0Ñ0Ô2B×CÒCØ×Ñ×%Ñ%×-×-Ð-ô �<Š<˜Ó 5×#3Ñ#3×#<Ñ#<×#MÑ#M×#TÑ#TÓ#VÔVÔ[bØ×Ñ×%Ñ%×1Ñ1°9÷\
ñ \
ð ×$Ñ$×-Ñ-×9Ñ9×AÑA×GÑGˆEÜœt E×$4Ñ$4×$=Ñ$=×$IÑ$I×$QÑ$QÓR×WÑWÓYÓZÐ[\Ñ]ˆKØÑ*Ð+=Ñ>ˆIØˆMä—W‘W—]‘] 9Ö-�Ø˜1“9Ø!×(Ñ(¨Ô+ÙØ×$Ñ$ QÖ'ñ	 .ô
 Ÿ™Ÿš mÐ4ˆIØ#,¨yÑ#8Ð Ø×Ñ×!Ñ!×-Ñ-¨e×.>Ñ.>Ô?Ø!Ðã	ã#ˆØ�‰×$Ñ$ V¬U¯\ª\¸"Ó-=Ö>ñ $÷ ô 6JÈ%×JZÑJZÔ5[ô1
Ú5[¡k dˆD—(‘(˜5“/Ò!Ñ5[ò1
ˆ×Ñ×!Ñ!Ô-ô �<Š<˜Ó 5×#3Ñ#3×#<Ñ#<×#MÑ#M×#TÑ#TÓ#VÓVÔ[bØ×Ñ×%Ñ%×1Ñ1°9÷\
ñ \
ô Ð3°U×5EÑ5E×5NÑ5N×5ZÑ5ZÔ[Ø×Ñ×!Ñ!×.Ñ.¨u×/?Ñ/?ÄÇÂÈbÓAQÕRð ùó1
ùs$   ‚/QµQÁQÁ3J0QÌ#QÍ DQc                ó  • 1 SknSS1n1 SknU R                   U;   aj  [        US5      (       aX  [        UR                  SS5      U;   a<  X$;   a6  [	        SU R                    S	U S
UR                  R
                   SU S3	5      egggg)úX
Prevent applying LoRA to incompatible modules in specific architectures (e.g., Mamba).
>   ÚLORAÚXLORAÚADALORAÚRANDLORAÚout_projÚconv1d>   ÚmambaÚmamba2Ú	falcon_h1Úfalcon_mambaÚconfigÚ
model_typeNz[PEFT:z
] Module 'z7' is incompatible with Mamba-based models (model_type='z'). Incompatible modules: zG. Please remove it from `target_modules` to avoid compatibility issues.)Ú	peft_typer7   Úgetattrrf   Ú
ValueErrorrg   )Úpeft_configÚmodelÚtarget_nameÚlora_like_typesÚincompatible_modulesÚmamba_model_typess         rX   Ú _check_lora_target_modules_mambarq   Ž   sµ   € ò
 ?€OØ&¨Ð1ÐÚHÐð 	×Ñ Ó0Ü�E˜8×$Ñ$Ü�E—L‘L ,°Ó5Ð9JÓJàÓ.ÜØ˜×.Ñ.Ð/¨z¸+¸ð G Ø %§¡× 7Ñ 7Ð8Ð8RÐSgÐRhð iXðXóð ð /ð Kð %ð 	1ó    c                óÒ	  • [        U [        R                  5      (       aŒ  [        (       af  [        U R                  [
        R                  R                  R                  5      (       a)  U R                  R                  5       R                  u  pX!4$ U R                  U R                  p X!4$ [        U [        R                  5      (       a  U R                  U R                  pX!4$ [        U [        R                   5      (       a  U R                  U R                  pX!4$ [        U [        R"                  5      (       a  U R                  U R                  pX!4$ [        U [        R$                  5      (       a  U R&                  U R(                  pX!4$ [        U [*        5      (       aL  [-        U R                  S5      (       a  U R                  R.                  OU R                  R                  u  p!X!4$ [        U [        R0                  5      (       a9  U R2                  (       d  [5        S5      eU R6                  SU R6                  -  pX!4$ [-        U S5      (       a+  [-        U S5      (       a  U R8                  U R:                  pX!4$ [-        U S5      (       a+  [-        U S5      (       a  U R<                  U R>                  pX!4$ U R@                  RB                  S:X  d  U R@                  RB                  S	:X  a  U R                  U R                  pX!4$ U R@                  RB                  S
:X  a   U RD                  nUR                  S   nXDpX!4$ [-        U S5      (       a4  U R@                  RB                  S:X  a  U R                  U R                  pX!4$ [-        U S5      (       a4  U R@                  RB                  S:X  a  U R                  U R                  pX!4$ U R@                  RB                  S:X  a  U R                  U R                  pX!4$ [-        U S5      (       a4  U R@                  RB                  S:X  a  U R                  U R                  pX!4$ U R@                  RB                  S:X  a  U R                  U R                  pX!4$ [-        U S5      (       a)  [-        U S5      (       a  U R                  U R                  pOSu  p![F        RH                  " S[K        U 5       S3[L        5        X!4$ )am  
Get the in_features and out_features of the layer.

Returns in_features and out_features as a tuple. If they cannot be determined, return a tuple of None and None.
This function covers a broad range of layers, some of which the caller might not support. Therefore, just because
this function returns a valid result does not imply that the layer type is supported.
Úds_shapezPOnly same dim for query/key/value is supported as of now for MultiheadAttention.é   Ú
infeaturesÚoutfeaturesÚ
input_sizeÚoutput_sizeÚLinearÚLayerNormLinearÚLayerNormMLPr   Ú	codebooksÚQuantizedLinearÚbitsÚAwqGEMMQuantLinearÚ
EetqLinearÚW_qÚ	HQQLinearÚPatchedLinearÚin_featuresÚout_features©NNzUnsupported layer type 'z(' encountered, proceed at your own risk.)'r8   r   rz   Ú_torch_supports_distributedÚweightr<   ÚdistributedrJ   ÚDTensorÚto_localÚshaper…   r†   ÚConv1dÚin_channelsÚout_channelsÚConv2dÚConv3dÚ	EmbeddingÚnum_embeddingsÚembedding_dimr   r7   rt   ÚMultiheadAttentionÚ_qkv_same_embed_dimrj   Ú	embed_dimrv   rw   rx   ry   Ú	__class__Ú__name__Úlayer_norm_weightÚwarningsÚwarnÚtypeÚUserWarning)rO   r†   r…   Ú	ln_weightÚln_sizes        rX   Ú_get_in_out_featuresr¢   ¤   sp  € ô �&œ"Ÿ)™)×$Ñ$ß&Ò&¬:°f·m±mÄU×EVÑEV×E]ÑE]×EeÑEe×+fÑ+fà(.¯©×(>Ñ(>Ó(@×(FÑ(FÑ%ˆLðn Ð$Ð$ðk )/×(:Ñ(:¸F×<OÑ<O™ðj Ð$Ð$ôi 
�FœBŸI™I×	&Ñ	&Ø$*×$6Ñ$6¸×8KÑ8K�\ðf Ð$Ð$ôe 
�FœBŸI™I×	&Ñ	&Ø$*×$6Ñ$6¸×8KÑ8K�\ðb Ð$Ð$ôa 
�FœBŸI™I×	&Ñ	&Ø$*×$6Ñ$6¸×8KÑ8K�\ð^ Ð$Ð$ô] 
�FœBŸL™L×	)Ñ	)Ø$*×$9Ñ$9¸6×;OÑ;O�\ðZ Ð$Ð$ôY 
�FœF×	#Ñ	#ä&-¨f¯m©m¸Z×&HÑ&HˆF�M‰M×"Ò"ÈfÏmÉm×NaÑNañ 	"ˆðV Ð$Ð$ôQ 
�FœB×1Ñ1×	2Ñ	2Ø×)×)ÜÐoÓpÐpØ$*×$4Ñ$4°a¸&×:JÑ:JÑ6J�\ðJ Ð$Ð$ôI 
�˜×	&Ñ	&¬7°6¸=×+IÑ+Ià$*×$5Ñ$5°v×7IÑ7I�\ðD Ð$Ð$ôC 
�˜×	&Ñ	&¬7°6¸=×+IÑ+Ià$*×$5Ñ$5°v×7IÑ7I�\ð> Ð$Ð$ð= 
×	Ñ	×	"Ñ	" hÓ	.°&×2BÑ2B×2KÑ2KÐO`Ó2`à$*×$6Ñ$6¸×8KÑ8K�\ð8 Ð$Ð$ð7 
×	Ñ	×	"Ñ	" nÓ	4à×,Ñ,ˆ	Ø—/‘/ !Ñ$ˆØ$+�\ð. Ð$Ð$ô- 
�˜×	%Ñ	%¨&×*:Ñ*:×*CÑ*CÐGXÓ*Xà$*×$6Ñ$6¸×8KÑ8K�\ð( Ð$Ð$ô' 
�˜×	 Ñ	  V×%5Ñ%5×%>Ñ%>ÐBVÓ%Và$*×$6Ñ$6¸×8KÑ8K�\ð" Ð$Ð$ð! 
×	Ñ	×	"Ñ	" lÓ	2à$*×$6Ñ$6¸×8KÑ8K�\ð Ð$Ð$ô 
�˜×	Ñ	 F×$4Ñ$4×$=Ñ$=ÀÓ$Là$*×$6Ñ$6¸×8KÑ8K�\ð Ð$Ð$ð 
×	Ñ	×	"Ñ	" oÓ	5à$*×$6Ñ$6¸×8KÑ8K�\ð Ð$Ð$ô �6˜=×)Ñ)¬g°f¸n×.MÑ.MØ(.×(:Ñ(:¸F×<OÑ<O™à(2Ñ%ˆKÜ�ŠÐ0´°f³°Ð>fÐgÔitÔuØÐ$Ð$rr   c                  óª  ^ • \ rS rSr% SrS\S'   S\S'   S\S'     S0         S1U 4S	 jjjr\S2S
 j5       rS3S jr	S4S jr
S4S jrS5S jrS6S jr\S7S j5       r\S7S j5       r\ S8               S9S jj5       rS:S jrS;S<S jjrS=S jrS=S jrS>S jrS;S?S jjrS@S jrS;SAS jjrS r    SB         SCS jjr SD       SES jjrSFS jrSGS jr  S=S  jr    SH           SIS! jjr!          SJS" jr"S=S# jr#SKSLS$ jjr$S% r%SMSNS& jjr&\SOS' j5       r'SPS( jr(S2S) jr)S* r*S+ r+SQS, jr,SRSSS- jjr-STU 4S. jjr.S/r/U =r0$ )UÚ	BaseTuneréé   a¦  
A base tuner model that provides the common methods and attributes for all tuners that are injectable into a
torch.nn.Module

For adding a new Tuner class, one needs to overwrite the following methods:

- **_prepare_adapter_config**:
    A private method to eventually prepare the adapter config, for example in case the field `target_modules` is
    missing.
- **_create_and_replace**:
    A private method to create and replace the target module with the adapter module.
- **_check_target_module_exists**:
    A private helper method to check if the passed module's key name matches any of the target modules in the
    adapter_config.

The easiest is to check what is done in the `peft.tuners.lora.LoraModel` class.

Attributes:
    model (`torch.nn.Module`):
        The model to which the adapter tuner layers will be attached.
    forward (`Callable`):
        The forward method of the model.
    peft_config (`Union[`PeftConfig`, dict[str, PeftConfig]]`):
        The adapter configuration object, it should be a dictionary of `str` to `PeftConfig` objects. One can also
        pass a PeftConfig object and a new adapter will be created with the default name `adapter` or create a new
        dictionary with a key `adapter_name` and a value of that peft config.
    config (`dict[str, Any]`):
        The model configuration object, it should be a dictionary of `str` to `Any` objects.
    targeted_module_names (`list[str]`):
        The list of module names that were actually adapted. Can be useful to inspect if you want to quickly
        double-check that the `config.target_modules` were specified correctly.
    targeted_parameter_names (`list[str]`):
        The list of parameter names that were actually adapted. Can be useful to inspect if you want to quickly
        double-check that the `config.target_parameters` were specified correctly.
    prefix (`str`)
        The PEFT-method specific unique prefix. E.g. `"lora_"` for LoRA.
ÚstrÚprefixútype[BaseTunerLayer]Útuner_layer_clszdict[str, list[str]]Útarget_module_mappingc                óÊ  >• [         TU ]  5         Xl        / U l        / U l        [        U S5      (       d   [        U[        5      (       a  X20OUU l        OU[        R                  " S5        [        U[        5      (       a  X R                  U'   OU R                  R                  U5        X0l        U R                  U R                  U R                  U   U5        U[        R                  :w  d  X#   [        R                  :w  a  U R!                  U R                  X4US9  U R#                  U R                  U R                  U   U5        U R                  U R                  l        g )Nrk   z”Already found a `peft_config` attribute in the model. This will lead to having multiple adapters in the model. Make sure to know what you are doing!)Úlow_cpu_mem_usageÚ
state_dict)ÚsuperÚ__init__rl   Útargeted_module_namesÚtargeted_parameter_namesr7   r8   r)   rk   rœ   r�   ÚupdateÚactive_adapterÚ_pre_injection_hookr%   r]   Úinject_adapterÚ_post_injection_hook)Úselfrl   rk   Úadapter_namer¬   r­   r™   s         €rX   r¯   ÚBaseTuner.__init__  s$  ø€ ô 	‰ÑÔàŒ
Ø02ˆÔ"Ø35ˆÔ%ô �t˜]×+Ñ+Ü>HÈÔV`×>aÑ>a Ñ:ÐgrˆDÕä�MŠMðGôô ˜+¤z×2Ñ2Ø1<× Ñ  Ò.ð × Ñ ×'Ñ'¨Ô4à/;ÔØ× Ñ  §¡¨T×-=Ñ-=¸lÑ-KÈ\ÔZØœ(Ÿ.™.Ó(¨KÑ,EÌÏÉÓ,WØ×Ñ §
¡
¨LÐjtÐÑuà×!Ñ! $§*¡*¨d×.>Ñ.>¸|Ñ.LÈlÔ[ð "&×!1Ñ!1ˆ�
‰
Õrr   c                ór   • [        U R                  [        5      (       a  U R                  /$ U R                  $ ©N©r8   r³   r¦   ©r·   s    rX   Úactive_adaptersÚBaseTuner.active_adaptersB  ó0   € ä�d×)Ñ)¬3×/Ñ/Ø×'Ñ'Ð(Ð(à×"Ñ"Ð"rr   c                ó:   • U R                   R                  " U0 UD6$ r»   )rl   Úforward)r·   ÚargsÚkwargss      rX   rÂ   ÚBaseTuner.forwardI  s   € Ø�z‰z×!Ò! 4Ð2¨6Ñ2Ð2rr   c                ó   • g)aH  
A hook to be called before the adapter is injected into the model. This method can be overridden by child
classes to perform any pre-injection operations.

Args:
    model (`nn.Module`):
        The model to be adapted.
    config (`PeftConfig`):
        The adapter config.
    adapter_name (`str`):
        The adapter name.
N© ©r·   rl   rf   r¸   s       rX   r´   ÚBaseTuner._pre_injection_hookL  ó   € ð 	rr   c                ó   • g)aH  
A hook to be called after the adapter is injected into the model. This method can be overridden by child
classes to perform any post-injection operations.

Args:
    model (`nn.Module`):
        The model to be adapted.
    config (`PeftConfig`):
        The adapter config.
    adapter_name (`str`):
        The adapter name.
NrÇ   rÈ   s       rX   r¶   ÚBaseTuner._post_injection_hook[  rÊ   rr   c                óÒ   • UR                   cY  U R                  R                  US   5      nUc  [        S5      e[	        U[
        5      (       a  X1l         U$ [        U5      Ul         U$ )ar  
A private method to prepare the adapter config.

For transformers based models, if `peft_config.target_modules` is None, for some model architectures, we can
automatically infer the target modules from the `TRANSFORMERS_MODELS_TO_XXX_TARGET_MODULES_MAPPING`.

Args:
    peft_config (`PeftConfig`):
        The adapter config.
    model_config (`dict`):
        The transformers model config, that config should contain the `model_type` key.

Returns:
    peft_config (`PeftConfig`):
        The PEFT config with updated `target_modules`.

Raises:
    ValueError:
        Raises an error if the model type was not recognized.
rg   z0Please specify `target_modules` in `peft_config`)Útarget_modulesrª   Úgetrj   r8   r¦   Úset)r·   rk   Úmodel_configrÎ   s       rX   Ú_prepare_adapter_configÚ!BaseTuner._prepare_adapter_configj  sm   € ð* ×%Ñ%Ñ-Ø!×7Ñ7×;Ñ;¸LÈÑ<VÓWˆNØÑ%Ü Ð!SÓTÐTÜ˜.¬#×.Ñ.Ø-;Ô*ð Ðô .1°Ó-@�Ô*ØÐrr   c                ó   • g)a  
A private method to modify the model structure before adapter is applied.

See `peft.tuner.lora.LoraModel._prepare_model` for an example.

Args:
    peft_config (`PeftConfig`):
        The prepared adapter config.
    model (`nn.Module`):
        The model that is going to be adapted.
NrÇ   )r·   rk   rl   s      rX   Ú_prepare_modelÚBaseTuner._prepare_model‰  s   € ð 	rr   c                ór   ^• [        U S/ 5      =(       d    / nTU;   =(       d    [        U4S jU 5       5      $ )aS  
A helper method to check if the passed module's key name matches any of the tied modules

Args:
    config (`PeftConfig`):
        A config to match target modules from.
    key (`str`):
        A key to search any matches in config.

Returns:
    `bool`
        True if key matches any tied modules from config, False if no match found.
Útarget_modules_to_tiec              3  óL   >#   • U  H  nTR                  S U 35      v •  M     g7f©Ú.N©Úendswith©Ú.0Ú
target_keyÚkeys     €rX   Ú	<genexpr>Ú6BaseTuner._check_tied_module_exists.<locals>.<genexpr>§  s(   øé € ð 3
Ú=R¨zˆC�L‰L˜1˜Z˜LÐ)×*Ð*Ò=Rùó   ƒ!$)ri   Úany)rk   rá   rØ   s    ` rX   Ú_check_tied_module_existsÚ#BaseTuner._check_tied_module_exists—  sB   ø€ ô !(¨Ð5LÈbÓ Q× WÐUWÐØÐ+Ñ+÷ 
¬sô 3
Ù=Ró3
ó 0
ð 	
rr   c                ó   • [        X5      $ )a¥  
A helper method to check if the passed module's key name matches any of the target modules in the
adapter_config.

Args:
    config (`PeftConfig`):
        A config to match target modules from.
    key (`str`):
        A key to search any matches in config.

Returns:
    `bool` | `re.Match[str]` | `None`:
        True or re.Match object if key matches any target modules from config, False or None if no match found.
)Úcheck_target_module_exists)rk   rá   s     rX   Ú_check_target_module_existsÚ%BaseTuner._check_target_module_exists«  s   € ô  *¨+Ó;Ð;rr   c                ó   • g)a¹  
Inplace replacement of the target module with the adapter layer. This method needs to be overridden by all the
tuner classes.

Check `peft.tuners.lora.LoraModel._create_and_replace` for an example.

Args:
    peft_config (`PeftConfig`):
        The adapter config.
    adapter_name (`str`):
        The adapter name.
    target (`nn.Module`):
        The target module.
    target_name (`str`):
        The target module's name.
    parent (`nn.Module`):
        The parent module.
    current_key (`str`):
        The key of the current target being adapted.
    parameter_name (`str`, *optional*)
        If, and only if, an `nn.Parameter` is being targeted, this is the name of the parameter.
NrÇ   )r·   rk   r¸   Útargetrm   ÚparentÚcurrent_keyÚparameter_names           rX   Ú_create_and_replaceÚBaseTuner._create_and_replace½  s   € ðB 	rr   c                óH  • UR                  5        H  u  p#U R                  U;  d  M  SUl        M      U R                   Hà  n[	        U R
                  U   SS5      nUS:X  a  M%  US:X  a*  UR                  5        H  u  p#SU;   d  M  SUl        M     MU  UR                  S5      (       ah  UR                  5        HR  n[        X`R                  5      (       d  M  [        US5      (       d  M2  UR                  c  MA  SUR                  l        MT     MÓ  [        SU S	35      e   g)
zc
A helper method to mark only the adapter layers as trainable (i.e. module.requires_grad = False).
FÚbiasÚnoneÚallTÚ_onlyNzRequested bias: z, is not implemented.)Únamed_parametersr§   Úrequires_gradr¾   ri   rk   rÝ   Úmodulesr8   r©   r7   rô   ÚNotImplementedError)r·   rl   ÚnÚpr³   rô   Úms          rX   Ú _mark_only_adapters_as_trainableÚ*BaseTuner._mark_only_adapters_as_trainableà  sù   € ð ×*Ñ*Ö,‰DˆAØ�{‰{ !Õ#Ø"'�–ñ -ð #×2Ô2ˆNÜ˜4×+Ñ+¨NÑ;¸VÀVÓLˆDØ�v‹~Ùà�u‹}Ø!×2Ñ2Ö4‘D�AØ •{Ø*.˜žó 5ð —‘˜w×'Ñ'ØŸ™ž�AÜ! !×%9Ñ%9×:Ó:¼wÀqÈ&×?QÓ?QÐVW×V\ÑV\ÓVhØ/3˜Ÿ™Ö,ó )ô *Ð,<¸T¸FÐBWÐ*XÓYÐYò 3rr   c                ó¢   • U R                   R                  5        H1  n[        U[        [        45      (       d  M   UR                  U5        M3     g r»   )rl   rú   r8   ÚBaseTunerLayerr   Úenable_adapters)r·   ÚenabledrO   s      rX   Ú_enable_adapter_layersÚ BaseTuner._enable_adapter_layersø  s:   € Ø—j‘j×(Ñ(Ö*ˆFÜ˜&¤>Ô3KÐ"L×MÓMØ×&Ñ& wÖ/ò +rr   c                óÄ   • U R                    HA  n[        U R                  U   SS5      nUS:w  d  M%  SU S3n[        R                  " U5        MC     U R                  SS9  g)z|
Disable all adapters in-place.

When disabling all adapters, the model output corresponds to the output of the base model.
rô   rõ   z>Careful, disabling adapter layers with bias configured to be 'zL' does not produce the same output as the base model would without adaption.F©r  N)r¾   ri   rk   rœ   r�   r  )r·   r³   Úbias_valÚmsgs       rX   Údisable_adapter_layersÚ BaseTuner.disable_adapter_layersý  sq   € ð #×2Ô2ˆNÜ˜t×/Ñ/°Ñ?ÀÈÓPˆHØ˜6Õ!àTÐU]ÐT^ð _Lð Lð ô —’˜cÖ"ñ 3ð 	×#Ñ#¨EÐ#Ò2rr   c                ó"   • U R                  SS9  g)z
Enable all adapters in-place
Tr  N)r  r½   s    rX   Úenable_adapter_layersÚBaseTuner.enable_adapter_layers  s   € ð
 	×#Ñ#¨DÐ#Ò1rr   c                óü   • U[        U R                  R                  5       5      ;  a  [        SU S35      eU R                  U	 [	        U R
                  XR                  U R                  S9nU=(       d    / U l        g)z`
Deletes an existing adapter.

Args:
    adapter_name (str): Name of the adapter to be deleted.
úAdapter z does not exist)rl   r¸   r§   Ú	layer_clsN)	rB   rk   rD   rj   Údelete_adapterrl   r§   r©   r³   )r·   r¸   Únew_adapters      rX   r  ÚBaseTuner.delete_adapter  sq   € ð œt D×$4Ñ$4×$9Ñ$9Ó$;Ó<Ó<Ü˜x¨ ~°_ÐEÓFÐFØ×Ñ˜\Ð*ä$Ø—*‘*¨<ÇÁÐW[×WkÑWkñ
ˆð *×/¨RˆÕrr   c                ó,   • [        U R                  XS9  g)á"  
Enable or disable gradients on the given adapter(s).

Args:
    adapter_name (`str` or `Sequence[str]`):
        The name of the adapter(s) whose gradients should be enabled/disabled.
    requires_grad (`bool`, *optional*)
        Whether to enable (`True`, default) or disable (`False`).
©Úadapter_namesrù   N)Úset_requires_gradrl   )r·   r  rù   s      rX   r  ÚBaseTuner.set_requires_grad%  s   € ô 	˜$Ÿ*™*°MÓ_rr   c                ó˜  ^• [        U R                  5      S::  a  g[        U4S jU R                  R                  5        5       5      (       d  [	        S5      eU R                  R                  5        Vs/ s H  n[        USS5      PM     nn[        S U 5       5      S:”  a"  [	        U R                  R                   S35      egs  snf )	z¯
A helper method to check the config of a new adapter being added.

Raise a ValueError if there is something wrong with the config or if it conflicts with existing adapters.

r+   Nc              3  ó*   >#   • U  H  oTL v •  M
     g 7fr»   rÇ   )rß   Úconfrf   s     €rX   râ   Ú6BaseTuner._check_new_adapter_config.<locals>.<genexpr>=  s   øé € ÐHÒ.G d˜6•>Ò.Gùs   ƒzœ_check_new_peft_config was called incorrectly, this should not happen. Please open an issue and report the error: https://github.com/huggingface/peft/issuesrô   rõ   c              3  ó*   #   • U  H	  oS :g  v •  M     g7f)rõ   NrÇ   )rß   Ú
bias_values     rX   râ   r  D  s   é € ÐB²k¨
˜VÖ#²kùs   ‚zf supports only 1 adapter with bias. When using multiple adapters, set bias to 'none' for all adapters.)	Úlenrk   rå   r?   rj   ri   Úsumr™   rš   )r·   rf   r  Úbias_valuess    `  rX   Ú_check_new_adapter_configÚ#BaseTuner._check_new_adapter_config1  sÈ   ø€ ô ˆt×ÑÓ  AÓ%Øô ÔH¨d×.>Ñ.>×.EÑ.EÔ.GÓH×HÑHÜðOóð ð
 BF×AQÑAQ×AXÑAXÔAZÓ[ÒAZ¸”w˜t V¨VÖ4ÑAZˆÐ[ÜÑB±kÓBÓBÀQÓFÜØ—>‘>×*Ñ*Ð+ð ,7ð 7óð ð Gùò \s   Á5Cc                ó,   • [        U R                  XS9  g)a2  
A helper method to cast the adapter weights to the correct dtype.

Currently, this only upcasts float16 and bfloat16 to float32.

Args:
    adapter_name (`str`):
        The adapter name.
    autocast_adapter_dtype (`bool`, *optional*):
        Whether to autocast the adapter dtype. Defaults to `True`.

)r¸   Úautocast_adapter_dtypeN)Úcast_adapter_dtyperl   )r·   r¸   r(  s      rX   Ú_cast_adapter_dtypeÚBaseTuner._cast_adapter_dtypeJ  s   € ô 	˜4Ÿ:™:°LÓprr   c                ó²   • [         R                  " S5      nU R                  U R                  5      nU(       a  [        R
                  " SU< S3U-   5        gg)z—Helper method to check whether the adapter can be merged.

Raise a ValueError if it is not possible to merge the adapter with the given configuration.
a   
            ```python
            from transformers import AutoModelForCausalLM

            # Load original tied model
            model = AutoModelForCausalLM.from_pretrained("google/gemma-2-2b-it", tie_word_embeddings=False)

            # Set the randomly initialized lm_head to the previously tied embeddings
            model.lm_head.weight.data = model.model.embed_tokens.weight.data.clone()

            # Save the untied model
            untied_model_dir = "dir/for/untied/model"
            model.save_pretrained(untied_model_dir)
            model.config.save_pretrained(untied_model_dir)

            # Now use the original model but in untied format
            model = AutoModelForCausalLM.from_pretrained(untied_model_dir)
            ```
            zBModel with `tie_word_embeddings=True` and the tied_target_modules=zð are part of the adapter. This can lead to complications. You can opt to merge the adapter after cloning the weights (to untie the embeddings). You can untie the embeddings by loading the model with `tie_word_embeddings=False`. For example:N)ÚtextwrapÚdedentÚ_get_tied_target_modulesrl   rœ   r�   )r·   Úexample_codeÚtied_target_moduless      rX   Ú_check_merge_allowedÚBaseTuner._check_merge_allowedY  sb   € ô
  —’ðó
ˆð* #×;Ñ;¸D¿J¹JÓGÐÞÜ�MŠMØUÐATÑ@Vð Wsð sð ñ	õð rr   c                ó4  • U(       a  U R                  5         U R                  R                  5        VVs/ s H  u  pVU R                  U;  d  M  UPM     nnnSU(       a  SOS-   S-   n[	        Xr(       + US9 H­  n [        U R                  U5      u  pšn[        U
5         [        U
S5      (       a#  U
R                  XUS9nU R                  X›XÊ5        OH[        U
S5      (       a7  U(       a  U
R                  X4S	9  U R                  X›U
R                  5       U
5        S S S 5        M¯     [        U R                  S
5      (       a  U R                  ?U(       aë  U R                  U R                  5      nUR                  S5      (       aº   U R                  R!                  5       nU R                  R#                  5       nUbv  Ubs  [%        USS 5      n[%        USS 5      nUbV  UbS  UR'                  5       UR'                  5       :w  a1  SU R                  R(                  l        [,        R.                  " S5        U R                  $ U R                  $ s  snnf ! [         a     GMê  f = f! , (       d  f       GMþ  = f! [0        [        4 a     U R                  $ f = f)Nz
Unloading zand merging r/   rl   )ÚdisableÚdescÚ"unload_and_optionally_merge_module)ÚmergeÚ
safe_merger  r0   )r9  r  rk   Útie_word_embeddingsr‰   FzvInput and output embeddings are no longer tied after merging. Setting `tie_word_embeddings=False` in the model config.)r2  rl   r6   r§   r   r*   ÚAttributeErrorrY   r7   r7  Ú_replace_moduler8  Úget_base_layerrk   Úget_model_configrÏ   Úget_output_embeddingsÚget_input_embeddingsri   Údata_ptrrf   r:  rœ   r�   rû   )r·   r8  Úprogressbarr9  r  rá   Ú_Úkey_listr6  rî   rí   rm   Úunloaded_modulerÑ   Úout_embÚin_embÚout_wÚin_ws                     rX   Ú_unload_and_optionally_mergeÚ&BaseTuner._unload_and_optionally_merge}  sE  € ö Ø×%Ñ%Ô'à&*§j¡j×&>Ñ&>Ô&@Ô[Ò&@™F˜CÀDÇKÁKÐWZÑDZ—CÑ&@ˆÑ[Ø¶™~¸BÑ?À'ÑIˆÜ˜¬/ÀÔEˆCðÜ.=¸d¿j¹jÈ#Ó.NÑ+� ô ˜fÕ%Ü˜6Ð#G×HÑHà&,×&OÑ&OØ#È-ð 'Pð '�Oð ×(Ñ(¨¸oÕVÜ˜V \×2Ñ2ÞØŸ™°
˜ÑXØ×(Ñ(¨¸f×>SÑ>SÓ>UÐW]Ô^÷ &Ñ%ñ Fô& �4—:‘:˜}×-Ñ-Ø—
‘
Ð&ö Ø×0Ñ0°·±Ó<ˆLØ×ÑÐ 5×6Ñ6ðØ"Ÿj™j×>Ñ>Ó@�GØ!ŸZ™Z×<Ñ<Ó>�FØÑ+°&Ñ2DÜ '¨°¸4Ó @˜Ü& v¨x¸Ó>˜Ø!Ñ-°DÑ4DÈ5Ï>É>ÓK[Ð_c×_lÑ_lÓ_nÓKnØDI˜DŸJ™J×-Ñ-ÔAÜ$ŸMšMð![ôð �z‰zÐˆt�z‰zÐùóW \øô
 "ó Ûðúç%×%ûôB ,¬^Ð<ó Øà�z‰zÐðús=   µIÁIÁ<IÂ A=I&Æ	B-I9 É
I#É"I#É&
I6	É9JÊJc                ó"   • U R                  XUS9$ )a@  
This method merges the adapter layers into the base model.

This is needed if someone wants to use the base model as a standalone model. The returned model has the same
architecture as the original base model.

It is important to assign the returned model to a variable and use it, this is not an in-place operation!

Args:
    progressbar (`bool`):
        whether to show a progressbar indicating the unload and merge process (default: False).
    safe_merge (`bool`):
        whether to activate the safe merging check to check if there is any potential Nan in the adapter
        weights.
    adapter_names (`List[str]`, *optional*):
        The list of adapter names that should be merged. If None, all active adapters will be merged. Defaults
        to `None`.

Example:

```py
>>> from transformers import AutoModelForCausalLM
>>> from peft import PeftModel

>>> model_id = ...
>>> base_model = AutoModelForCausalLM.from_pretrained(model_id)
>>> peft_model_id = ...
>>> model = PeftModel.from_pretrained(base_model, peft_model_id)
>>> merged_model = model.merge_and_unload()
```
)rB  r9  r  ©rJ  )r·   rB  r9  r  s       rX   Úmerge_and_unloadÚBaseTuner.merge_and_unload´  s"   € ðD ×0Ñ0Ø#È-ð 1ð 
ð 	
rr   c                ó    • U R                  SS9$ )z¤
Return the base model by removing all the PEFT modules.

It is important to assign the returned model to a variable and use it, this is not an in-place operation!
F)r8  rM  r½   s    rX   ÚunloadÚBaseTuner.unloadÚ  s   € ð ×0Ñ0°uÐ0Ð=Ð=rr   c                ó   • [        XU5        g)r[   N)rq   )r·   rk   rl   rm   s       rX   Ú!_check_target_module_compatiblityÚ+BaseTuner._check_target_module_compatiblityâ  s   € ô 	)¨¸[ÕIrr   c                óF   • [        U R                  R                   S35      e)Nz) does not support targeting nn.Parameter.)rû   r™   rš   )r·   rk   r¸   rí   rm   rî   rï   s          rX   Ú_create_and_replace_parameterÚ'BaseTuner._create_and_replace_parameterè  s"   € ô " T§^¡^×%<Ñ%<Ð$=Ð=fÐ"gÓhÐhrr   c                óv  • [        [        USS5      S5      n[        (       a,  U(       a%  SSKJnJn  U" U5      n	U" U R                  U   UU	S9  U R                  U   n
/ n/ n/ n/ nU R                  U
5        U R                  X5        U R                  U5      nU R                  X¯5      n
U R                  X¡5        [        U
S/ 5      (       a  U(       a  [        S5      e[        UR                  5       5      nU VVs/ s H  u  nnUPM
     nnn[        U
S	S5      [        :H  nU(       a  / n/ n[!        X¡5      n
[#        U
R$                  [        [&        45      (       aÒ  [)        U
R$                  5      [*        :¼  aµ  U
R,                  [.        R0                  :w  a—  [3        S
 U
R$                   5       5      nU Vs/ s H/  nUU
R$                  ;  d  M  UR5                  U5      (       a  M-  UPM1     nn[7        U
R$                  U5      n[)        U5      [)        U
R$                  5      :  a  UU
l        / nU H1  u  nn[#        U[8        5      (       d  M  UR;                  US-   5        M3     ['        5       nUbJ  [<        U
R,                     nU Vs1 s H*  nUR?                  SU-   S5      S   RA                  S5      iM,     nnU GH=  u  nnU(       d  M  U H,  nURC                  U5      (       d  M  UR;                  U5          O   U(       a  US   U:X  a  MT  Ucô  U RE                  U
U5      nU RG                  U
U5      (       a  UR;                  U5        M“  [#        U[H        5      (       a  UR;                  U5        M»  U(       d  UR;                  U5        MÕ  U RJ                  R;                  U5        [M        UU5      u  n n!n"U RO                  X¡U"5        U(       a  [P        O[R        n#U#" 5          U RU                  X¢U!U"U US9  SSS5        GMK  URA                  S5      nUU;  a  UR;                  U5        OžU RG                  U
U5      (       a  UR;                  U5        GMŸ  U RJ                  R;                  U5        [M        UU5      u  n n!n"U RO                  X¡U"5        U(       a  [P        O[R        n#U#" 5          U RU                  X¢U!U"U US9  SSS5        U RE                  U
U5      (       d  GM,  UR;                  U5        GM@     [        U
S/ 5      (       a  U RW                  X¡X$S9  U Hv  nU RJ                  R;                  U5        [M        UU5      u  n n!n"U RO                  X¡U"5        U(       a  [P        O[R        n#U#" 5          U RU                  X¢U!U"U US9  SSS5        Mx     Ub�  ['        U5      n$['        U RJ                  5      n%U$U%-
  n&U%U$-
  n'Sn(U&(       d  U'(       a  Sn(U&(       a  U(S[Y        U&5       S3-  n(U'(       a  U(S[Y        U'5       S3-  n(U((       a  [Z        R\                  " U([^        5        U RJ                  (       Gd  U R`                  (       Gd  U(       dý  U(       a  U(       d  [        S5      eU(       d#  U(       a  U
R$                  (       d  [        S5      eU(       dd  U(       a]  SU
R$                   S3n)[        U
SS5      b  U)SU
Rb                   S3-  n)[        U
SS5      b  U)SU
Rd                   S3-  n)[        U)5      eSn)[        U
SS5      b  U)SU
Rb                   S3-  n)[        U
SS5      b  U)SU
Rd                   S3-  n)[        U)5      e[        U
S5      (       a=  U
Rf                  (       a,  U(       d%  [Z        R\                  " S U
Rf                   S!35        OŸU(       d˜  U
R$                  (       a;  U RJ                  (       d*  [Z        R\                  " S"U
R$                   S#3[^        5        OL[        U
S/ 5      (       a:  U R`                  (       d)  [Z        R\                  " S$U
Rh                   S%3[^        5        U Rk                  U Rl                  U
Rn                  S&9  U Rq                  U5        U R                  U   Rn                  (       a)  URs                  5        H  u  n*n+UU*;   d  M  S'U+l:        M     [w        UU
[x        R                  U 5      UX Rl                  ;   S(9  gs  snnf s  snf s  snf ! , (       d  f       GMQ  = f! , (       d  f       GNQ= f! , (       d  f       GM  = f))a”  
Creates adapter layers and replaces the target modules with the adapter layers. This method is called under the
hood by `peft.mapping.get_peft_model` if a non-prompt tuning adapter class is passed.

The corresponding PEFT config is directly retrieved from the `peft_config` attribute of the BaseTuner class.

Args:
    model (`nn.Module`):
        The model to be tuned.
    adapter_name (`str`):
        The adapter name.
    autocast_adapter_dtype (`bool`, *optional*):
        Whether to autocast the adapter dtype. Defaults to `True`.
    low_cpu_mem_usage (`bool`, `optional`, defaults to `False`):
        Create empty adapter weights on meta device. Useful to speed up the loading process.
    state_dict (`dict`, *optional*, defaults to `None`)
        If a state_dict is passed here, the adapters will be injected based on the entries of the state_dict.
        This can be useful when the exact `target_modules` of the PEFT method is unknown, for instance because
        the checkpoint was created without meta data. Note that the values from the state_dict are not used,
        only the keys are used to determine the correct layers that should be adapted.

rf   Nrg   r   )Ú$convert_peft_config_for_transformersÚget_model_conversion_mapping)rl   ÚconversionsÚtarget_parameterszÊTrying to inject a PEFT adapter from a state_dict but the PEFT config uses `target_parameters`. This is not supported -- when using `target_parameters`, please inject the adapter without the state_dict.rÎ   c              3  ó,   #   • U  H
  nS U-   v •  M     g7frÚ   rÇ   )rß   Úsuffixs     rX   râ   Ú+BaseTuner.inject_adapter.<locals>.<genexpr>V  s   é € ÐSÒ8R¨f˜S 6ž\Ò8Rùs   ‚rÛ   r+   z
_orig_mod.éÿÿÿÿ)rï   )rk   rl   r¸   r¬   r/   zÄWhile injecting the PEFT adapters, an inconsistency was discovered between the PEFT config and the provided state_dict. This is not necessarily an issue and can be ignored if this was the intent. z;The PEFT config contained these additional target modules: z. z:The state_dict contained these additional target modules: z‰All modules were excluded. This is likely unintended. Check your `target_modules`, `exclude_modules` and `modules_to_save` configuration.znNo `target_modules` passed but also no `target_parameters` found. Please check the values for these arguments.zTarget modules zL not found in the base model. Please check the target modules and try again.Úlayers_to_transformz, Note: You specified 'layers_to_transform': Úlayers_patternz& You also specified 'layers_pattern': a"  No modules were targeted for adaptation. This might be caused by a combination of mismatched target modules and excluded modules. Please check your `target_modules` and `exclude_modules` configuration. You may also have only targeted modules that are marked to be saved (`modules_to_save`).Úexclude_modulesz You have passed exclude_modules=zS but no modules were excluded. Please check that exclude_modules was set correctly.ztarget_modules=z$ were set but no module was matched.ztarget_parameters=z' were set but no parameter was matched.©Úinference_modeF)rl   rk   rÑ   r¸   Úactivate_adapter)=r7   ri   r   Ú)peft.utils.transformers_weight_conversionrZ  r[  rk   r%  Ú_check_tied_modulesr>  rÒ   rÕ   rj   rB   r6   r   Ú _maybe_include_all_linear_layersr8   rÎ   rÐ   r"  r   rh   r%   ÚIA3ÚtuplerÝ   Ú_find_minimal_target_modulesr  r;   r   ÚrsplitÚremoveprefixÚ
startswithrê   ræ   Ú_ExcludedModuler°   r*   rT  r   r   rñ   Ú_inject_parametersÚsortedrœ   r�   ÚRuntimeWarningr±   rb  rc  rd  r]  Úset_adapterr¾   rf  rÿ   rø   rù   r$   r¤   ),r·   rl   r¸   r(  r¬   r­   Úis_transformers_like_modelrZ  r[  Úweight_conversionsrk   Úexcluded_modulesÚunmatched_modulesÚ!targeted_modules_from_peft_configÚtargets_to_tierÑ   r6   rá   rC  rD  Úuses_dummy_target_modulesÚsuffixesrN   Únames_no_targetÚnew_target_modulesÚexisting_adapter_prefixesrO   Úmodule_namesr§   ÚkÚadapter_keyÚresultrî   rí   rm   ÚctxÚtargeted_set_from_peft_configÚtargeted_set_from_state_dictÚdiff_peft_configÚdiff_state_dictÚwarning_msgÚ	error_msgrü   rý   s,                                               rX   rµ   ÚBaseTuner.inject_adapterí  s¹  € ôB &-¬W°U¸HÀdÓ-KÈ\Ó%ZÐ"ß Ò Ö%?÷ñ
 ">¸eÓ!DÐÙ0Ø× Ñ  Ñ.ØØ.òð ×&Ñ& |Ñ4ˆØÐØÐØ79Ð)Ø$&ˆð 	×&Ñ& {Ô3à× Ñ  Ô4à×,Ñ,¨UÓ3ˆà×2Ñ2°;ÓMˆà×Ñ˜KÔ/ä�;Ð 3°R×8Ñ8¾ZÜðxóð ô
 ˜U×0Ñ0Ó2Ó3ˆÙ&3Ô4¢m™F˜C “C¡mˆÑ4ä$+¨KÐ9IÈ4Ó$PÔThÑ$hÐ!Þ$àˆMØˆHô 7°{ÓJˆô �{×1Ñ1´D¼#°;×?Ñ?Ü�[×/Ñ/Ó0Ô4WÓWØ×&Ñ&¬(¯,©,Ó6äÑS¸×8RÒ8RÓSÓSˆHá!)óÚ!)˜¨d¸+×:TÑ:TÑ.T“Ð^b×^kÑ^kÐlt×^u—¡ð ð ô ">¸k×>XÑ>XÐZiÓ!jÐÜÐ%Ó&¬¨[×-GÑ-GÓ)HÓHØ-?�Ô*ð %'Ð!Û(‰KˆC�Ü˜&¤.×1Ó1Ø)×0Ñ0°°s±Ö;ñ )ô
 "%£ˆØÑ!Ü0°×1FÑ1FÑGˆFñ ^hÓhÒ]gÐXY˜AŸH™H S¨6¡\°1Ó5°aÑ8×EÑEÀlÖSÑ]gˆLÐhä(‰KˆC�ÞÙó  9�Ø—>‘> +×.Ó.Ø$×+Ñ+¨CÔ0Ùñ  9ö
  Ð$4°RÑ$8¸CÓ$?ÙàÑ!à×9Ñ9¸+ÀsÓK�ð
 ×1Ñ1°+¸s×CÑCØ"×)Ñ)¨#Ô.ÙÜ˜f¤o×6Ñ6Ø$×+Ñ+¨CÖ0ÞØ%×,Ñ,¨SÖ1à×.Ñ.×5Ñ5°cÔ:Ü2AÀ%ÈÓ2MÑ/�F˜F KØ×:Ñ:¸;È{Ô[Þ0AÕ,Ä{�CÙ�Ø×0Ñ0Ø'°v¸{ÈFÐ`cð 1ñ ÷ šð ×&Ñ& |Ó4�à˜lÓ*Ø%×,Ñ,¨SÕ1ð ×5Ñ5°kÀ3×GÑGØ&×-Ñ-¨cÔ2Ú Ø×.Ñ.×5Ñ5°cÔ:Ü2AÀ%ÈÓ2MÑ/�F˜F KØ×:Ñ:¸;È{Ô[Þ0AÕ,Ä{�CÙ�Ø×0Ñ0Ø'°v¸{ÈFÐ`cð 1ñ ÷ ð ×3Ñ3°KÀ×EÔEØ5×<Ñ<¸S×Añ} )ô@ �;Ð 3°R×8Ñ8à×#Ñ#Ø'À<ð $ñ ó "ˆCØ×&Ñ&×-Ñ-¨cÔ2Ü*9¸%ÀÓ*EÑ'ˆF�F˜KØ×2Ñ2°;À{ÔSÞ(9Õ$¼{ˆCÙ•Ø×(Ñ(¨ÀFÈKÐY_ÐmpÐ(Ñq÷ ‘ñ "ð Ñ!ô -0Ð0QÓ,RÐ)Ü+.¨t×/IÑ/IÓ+JÐ(Ø<Ð?[Ñ[ÐØ:Ð=ZÑZˆOØˆKÞ¦?ðð ö
  ØØQÔRXÐYiÓRjÐQkÐkmÐnñ�ö ØÐ![Ô\bÐcrÓ\sÐ[tÐtvÐwÑw�ÞÜ—’˜k¬>Ô:à×)×)Ð)°$×2O×2OÐ2OÖXqÞÖ(9ä ðjóð ö &Ö*;ÀK×D^×D^Ü ð'óð ö &Ö*;ð & k×&@Ñ&@Ð%Að BEð Fð ô ˜;Ð(=¸tÓDÑPØÐ#OÐP[×PoÑPoÐOpÐpqÐ!rÑr�IÜ˜;Ð(8¸$Ó?ÑKØÐ#IÈ+×JdÑJdÐIeÐefÐ!gÑg�IÜ  Ó+Ð+ð]ð ô ˜;Ð(=¸tÓDÑPØÐ#OÐP[×PoÑPoÐOpÐpqÐ!rÑr�IÜ˜;Ð(8¸$Ó?ÑKØÐ#IÈ+×JdÑJdÐIeÐefÐ!gÑg�IÜ  Ó+Ð+ä�[Ð"3×4Ñ4¸×9T×9TÖ]mä�MŠMØ2°;×3NÑ3NÐ2Oð PGð Gõö
 +ð
 ×)×)°$×2L×2LÜ—’Ø% k×&@Ñ&@Ð%AÐAeÐfÔhvõô ˜Ð&9¸2×>Ñ>Àt×Gd×GdÜ—’Ø(¨×)FÑ)FÐ(GÐGnÐoÜ"ôð 	×Ñ˜×-Ñ-¸k×>XÑ>XÐÑYØ×-Ñ-¨eÔ4à×Ñ˜LÑ)×8×8Ø×.Ñ.Ö0‘��1Ø 1Õ$Ø&+�A–Oñ 1ô 	)ØØ#Ü"×3Ñ3°DÓ9Ø%Ø)×-AÑ-AÑAó	
ùóU 5ùò:ùò, i÷F Ÿú÷, žú÷, —úsB   Ã=c3Æ9c9Çc9Ç)c9Ê1c>Ï.dÒ8dÖd(ä
d	ä
d%	ä(
d8	c                óR  ^ ^^^^^^• S mUUUUU U4S jn[        TR                  5      n[        U5      nTR                  5        HÞ  u  p‰[	        U	S5      (       aZ  U HR  mTR                  S5      u  p«nX¨:w  a  M  [        XœS5      c  M-  U" UTU5        T R                  R                  T5        MT     Mp  T" U5      nU	R                  SS9 HS  u  pÎU SU 3mTU;   d  [        U4S jU 5       5      (       d  M.  U" UTU5        T R                  R                  T5        MU     Mà     g)	z1Inject layers based on peft_config.target_modulesc                óR   • SnX;   a  U R                  U5      u  p#nX$-   n X;   a  M  U $ )Nz.base_layer)Ú
rpartition)rQ   rN   r§   rC  r_  s        rX   Ústrip_base_layer_from_nameÚ@BaseTuner._inject_parameters.<locals>.strip_base_layer_from_name1  s;   € ð
 !ˆDØÓ%Ø$/×$:Ñ$:¸4Ó$@Ñ!�˜6Ø$™o�ð Õ%ð Ðrr   c                óÚ  >• [        TU 5      u  p4nT" U 5      nTR                  U5      n[        U[        5      (       a?  UR                  R
                  S:w  a%  [        SU S[        U5      R
                   S35      eTR                  TTU5        T
(       a  [        O[        nU" 5          TR                  TT	UUUUUR                  S5      S   S9  S S S 5        g ! , (       d  f       g = f)NÚParamWrapperz+Trying to wrap an `nn.Parameter` of layer 'z
' of type zÅ, which is not a valid target. Make sure that this layer is not also targeted with `target_modules`. For some models, PEFT will do this automatically, try setting `target_modules=[]` to prevent it.rÛ   ra  )rï   rð   )r*   Úget_submoduler8   r  r™   rš   rj   rž   rT  r   r   rñ   r�  )rQ   rá   Ú
param_namerî   rí   rm   Úunwrapped_module_nameÚunwrapped_moduler…  r¸   r¬   rl   rk   r·   r�  s            €€€€€€rX   Úcreate_and_replace_paramÚ>BaseTuner._inject_parameters.<locals>.create_and_replace_param<  sî   ø€ ä*9¸%ÀÓ*MÑ'ˆF˜KÙ$>¸{Ó$KÐ!Ø$×2Ñ2Ð3HÓIÐäÐ*¬N×;Ñ;Ð@P×@ZÑ@Z×@cÑ@cÐguÓ@uÜ ØAÐBWÐAXÐXbÜ˜F“|×,Ñ,Ð-ð .EðEóð ð ×2Ñ2°;ÀÀ{ÔSÞ(9Õ$¼{ˆCÙ•Ø×(Ñ(ØØ ØØØØ #Ø#-×#8Ñ#8¸Ó#=¸bÑ#Að )ñ ÷ —–ús   Â+(CÃ
C*ÚparametrizationsrÛ   NF©Úrecursec              3  óL   >#   • U  H  nTR                  S U 35      v •  M     g7frÚ   rÜ   rÞ   s     €rX   râ   Ú/BaseTuner._inject_parameters.<locals>.<genexpr>q  s'   øé € Ð3rÒeqÐWa°C·L±LÀ1ÀZÀLÐAQ×4RÐ4RÒeqùrä   )rÐ   r]  rs  r6   r7   r�  ri   r±   r;   rø   rå   )r·   rk   rl   r¸   r¬   r˜  Úunsorted_target_namesÚtarget_namesrQ   rO   Útarget_module_namerC  r•  r–  rW   rá   r�  s   `````          @@rX   rr  ÚBaseTuner._inject_parameters,  s%  þ€ ò
		÷	ò 	ô8 !$ K×$AÑ$AÓ BÐô Ð3Ó4ˆØ#(×#6Ñ#6Ö#8ÑˆKÜ�vÐ1×2Ñ2ó (�CØ8;¿¹ÀsÓ8KÑ5Ð&¨:Ø)Ó8Ù Ü˜v°4Ó8Ñ@Ù Ù,¨[¸#¸zÔJØ×1Ñ1×8Ñ8¸Ö=ó (ñ )CÀ;Ó(OÐ%à)/×)@Ñ)@ÈÐ)@Ó)OÑ%�JØ2Ð3°1°Z°LÐA�CØ˜|Ó+´Ô3rÑeqÓ3r×0rÓ0rñ 1°¸cÀ:ÔNØ×5Ñ5×<Ñ<¸SÖAó *Pò' $9rr   c                óê  ^• [        XU5        [        US5      (       a  UR                  n[        US5      (       d3  UR                  Ul        [        US5      (       a  UR                  Ul        [        USS5      bc  [        US5      (       a  UR                  UR                  l        OUR                  Ul        UR                  UR                  R                  5        [        R                  " S5      mUR                  5        Hê  u  pVU R                  U;   d  M  [        US5      (       a  UR                  nOp[        US5      (       a  UR                  nOR[        US5      (       a  UR                  nO4[        US	S5      b  UR                  nO[        UR!                  5       5      n[#        U4S
 jUR!                  5        5       5      (       a  MÏ  UR                  UR                  5        Mì     g)aã  
Replace the sub-module of a given moduel with a new PEFT module.

This also deals with device placement of the new module to be in line with the child module.

Args:
    parent (`nn.Module`):
        The parent module on which the replacement should take place.
    child_name (`str`):
        The name of the child module to be replaced.
    new_module (`nn.Module`):
        The new PEFT module.
    child (`nn.Module`):
        The original child module that is being replaced.

r0   rô   ÚstateNr2   Úqweightr‚   r‰   Úin_proj_weightc              3  ó@   >#   • U  H  oR                   T:H  v •  M     g 7fr»   ©r=   ©rß   rý   r2   s     €rX   râ   Ú,BaseTuner._replace_module.<locals>.<genexpr>«  s   øé € ÐIÒ5H°Ÿ8™8 tÖ+Ò5Hùó   ƒ)Úsetattrr7   r0   r‰   rô   ri   r¤  rK   r=   r<   r6   r§   r¥  r‚   r¦  ÚnextÚ
parametersrå   )	r·   rî   Ú
child_nameÚ
new_moduleÚchildrN   rO   r‰   r2   s	           @rX   r<  ÚBaseTuner._replace_modulew  ss  ø€ ô" 	� JÔ/ô
 �5˜,×'Ñ'Ø×$Ñ$ˆEä�z <×0Ñ0Ø %§¡ˆJÔÜ�u˜f×%Ñ%Ø"'§*¡*�
”ä�5˜' 4Ó(Ñ4Ü�z <×0Ñ0Ø.3¯k©k�
×%Ñ%Õ+à#(§;¡;�
Ô Ø�M‰M˜%Ÿ,™,×-Ñ-Ô.ä�|Š|˜FÓ#ˆà&×4Ñ4Ö6‰LˆDØ�{‰{˜dÕ"Ü˜5 )×,Ñ,Ø"Ÿ]™]‘FÜ˜U E×*Ñ*Ø"ŸY™Y‘FÜ˜U H×-Ñ-Ø"Ÿ\™\‘FÜ˜UÐ$4°dÓ;ÑGØ"×1Ñ1‘Fä! %×"2Ñ"2Ó"4Ó5�FäÔI°V×5FÑ5FÔ5HÓI×IÓIØ—I‘I˜fŸm™mÖ,ò 7rr   c                óþ   • U R                  5         U R                  R                  5        H=  n[        U[        5      (       d  M  [        U5         UR                  XS9  SSS5        M?     g! , (       d  f       MQ  = f)a  
This method merges the adapter layers into the base model.

Merging adapters can lead to a speed up of the forward pass. A copy of the adapter weights is still kept in
memory, which is required to unmerge the adapters. In order to merge the adapter weights without keeping them
in memory, please call `merge_and_unload`.

Args:
    adapter_names (`list[str]`, *optional*):
        The list of adapter names that should be merged. If `None`, all active adapters will be merged.
        Defaults to `None`.
    safe_merge (`bool`, *optional*):
        If `True`, the merge operation will be performed in a copy of the original weights and check for NaNs
        before merging the weights. This is useful if you want to check if the merge operation will produce
        NaNs. Defaults to `False`.
)r  r9  N)r2  rl   rú   r8   r  rY   r8  )r·   r  r9  rO   s       rX   Úmerge_adapterÚBaseTuner.merge_adapter®  sY   € ð. 	×!Ñ!Ô#Ø—j‘j×(Ñ(Ö*ˆFÜ˜&¤.×1Ó1Ü! &Õ)Ø—L‘L¨}�LÑT÷ *Ñ)ò +ç)Ö)ús   ÁA-Á-
A<	c                óà   • U R                   R                  5        H>  n[        U[        5      (       d  M  [	        U5         UR                  5         SSS5        M@     g! , (       d  f       MR  = f)zE
This method unmerges all merged adapter layers from the base model.
N)rl   rú   r8   r  rY   Úunmerge)r·   rO   s     rX   Úunmerge_adapterÚBaseTuner.unmerge_adapterË  sI   € ð —j‘j×(Ñ(Ö*ˆFÜ˜&¤.×1Ó1Ü! &Õ)Ø—N‘NÔ$÷ *Ñ)ò +ç)Ö)ús   Á AÁ
A-	c                óN   • [        U R                  XU R                  S9  Xl        g)a	  Set the active adapter(s).

Args:
    adapter_name (str, list[str]):
        The name(s) of the adapter(s) to set as active
    inference_mode (bool, optional):
         Whether the activated adapter should be frozen (i.e. `requires_grad=False`). Default is False.
)r¸   rf  r  N)ru  rl   r©   r³   )r·   r¸   rf  s      rX   ru  ÚBaseTuner.set_adapterÔ  s&   € ô 	Ø�J‰J \Ð\`×\pÑ\pò	
ð +Õrr   c                óÐ   • [        U S[        5      n[        US5      (       a  UR                  5       nU$ [        R
                  " U5      (       a  [        R                  " U5      nU$ )a<  
This method gets the config from a model in dictionary form. If model has not attribute config, then this
method returns a default config.

Args:
    model (`nn.Module`):
        Model to get the config from.
    default (`dict|None`, *optional*)::
        What to return if model does not have a config attribute.
rf   Úto_dict)ri   r   r7   r½  ÚdataclassesÚis_dataclassÚasdict)rl   rÑ   s     rX   r>  ÚBaseTuner.get_model_configâ  s_   € ô ˜u hÔ0BÓCˆÜ�< ×+Ñ+Ø'×/Ñ/Ó1ˆLð Ðô ×%Ò% l×3Ñ3Ü&×-Ò-¨lÓ;ˆLØÐrr   c                óÜ   • / nU R                  U5      nUR                  S5      (       aB  U R                   H2  nUR                  S5      S   [        ;   d  M!  UR                  U5        M4     U$ )Nr:  rÛ   ra  )r>  rÏ   r°   rG   r   r;   )r·   rl   r1  rÑ   Útarget_modules        rX   r/  Ú"BaseTuner._get_tied_target_modulesõ  sk   € Ø ÐØ×,Ñ,¨UÓ3ˆØ×ÑÐ1×2Ñ2Ø!%×!;Ô!;�ð !×&Ñ& sÓ+¨BÑ/Ô3HÕHØ'×.Ñ.¨}Ö=ñ "<ð #Ð"rr   c                ó   • [        U 5      $ r»   )r    r½   s    rX   r    Ú/BaseTuner._get_module_names_tied_with_embedding  s   € Ü4°TÓ:Ð:rr   c                ó8   • [         R                  " [        5        g)a"  
This method adds modules to tie to `peft_config` so that those modules can be tied downstream. By default this
method raises a warning, and each tuner class extending `BaseTuner` can choose to implement this.

Check `peft.tuners.lora.LoraModel._add_modules_to_save_to_tie` for an example.
N©rœ   r�   Úwarn_msg_weight_tying©r·   rk   Útied_weight_keyss      rX   Ú_add_modules_to_save_to_tieÚ%BaseTuner._add_modules_to_save_to_tie  ó   € ô 	�ŠÔ+Õ,rr   c                ó8   • [         R                  " [        5        g)a  
This method adds targets to tie to `peft_config` so that those modules can be tied downstream. By default this
method raises a warning, and each tuner class extending `BaseTuner` can choose to implement this.

Check `peft.tuners.lora.LoraModel._add_targets_to_tie` for an example.
NrÈ  rÊ  s      rX   Ú_add_targets_to_tieÚBaseTuner._add_targets_to_tie  rÎ  rr   c                óð  ^	• [        [        US/ 5      =(       d    / 5      n[        S U 5       5      n[        USS5      m	[        T	[        5      (       a  [        U	4S j[
         5       5      nO&[        T	=(       d    / 5      n[        S U 5       5      nU R                  5       n[        USS5      (       ag  U(       aI  U(       a  U R                  X'5        gU(       a  U R                  X'5        g[        R                  " S	5        g[        R                  " S
5        gU(       d  U(       aK  U(       aC  [        US5      (       a  Sn[        R                  " U5        gSn[        R                  " U5        ggg)z­
Checks if any of the tied layers are targetted via `modules_to_save` or `target_modules`. Updates the
`peft_config` in place with any layers/adapters that needs to be tied
Úmodules_to_savec              3  óV   #   • U  H  oR                  S 5      S   [        ;   v •  M!     g7f©rÛ   ra  N©rG   r   ©rß   rþ   s     rX   râ   Ú0BaseTuner._check_tied_modules.<locals>.<genexpr>  s$   é € Ð"fÒVeÐQR§7¡7¨3£<°Ñ#3Ô7LÖ#LÒVeùó   ‚')rÎ   Nc              3  ó<   >#   • U  H  n[        TU5      v •  M     g 7fr»   )r#   )rß   rþ   Úraw_target_moduless     €rX   râ   rØ  "  s!   øé € ð )ÚI^ÀAÔ(Ð);¸Q×?Ð?ÒI^ùs   ƒc              3  óV   #   • U  H  oR                  S 5      S   [        ;   v •  M!     g7frÕ  rÖ  r×  s     rX   râ   rØ  )  s$   é € Ð(kÒ\jÐWX¯©°«°bÑ)9Ô=RÖ)RÒ\jùrÙ  Úensure_weight_tyingFzwYou have requested `ensure_weight_tying`, but no tied modules are added in either `modules_to_save` or `target_modules`zUYou have requested `ensure_weight_tying`, but no tied modules were found in the modelaG  Model has `tie_word_embeddings=True` and a tied layer is part of the adapter, but `ensure_weight_tying` is not set to True. This can lead to complications, for example when merging the adapter or converting your model to formats other than safetensors. Check the discussion here: https://github.com/huggingface/peft/issues/2777r-   )rÐ   ri   rå   r8   r¦   r   r    rÌ  rÐ  rœ   r�   r7   )
r·   rl   rk   rÓ  Úis_embedding_to_saveÚis_embedding_in_targetrÎ   rË  r
  rÛ  s
            @rX   ri  ÚBaseTuner._check_tied_modules  sF  ø€ ô
 œg kÐ3DÀbÓI×OÈRÓPˆô  #Ñ"fÑVeÓ"fÓfÐä$ [Ð2BÀDÓIÐÜÐ(¬#×.Ñ.Ü%(ô )ÝI^ó)ó &Ñ"ô !Ð!3×!9°rÓ:ˆNô &)Ñ(kÑ\jÓ(kÓ%kÐ"à×EÑEÓGÐä�;Ð 5°u×=Ñ=ÞÞ'Ø×4Ñ4°[ÕSÞ+Ø×,Ñ,¨[ÕKä—M’Mð@õô
 —’ÐuÕvæ"Ö&<ÖBRÜ�{Ð$9×:Ñ:ðað ô —’˜cÕ"ðað ô —’˜cÕ"ð% CSÐ&<rr   c                óB   • [        S U R                  5        5       5      $ )z¦
Whether it is possible for the adapter of this model to be converted to LoRA.

Normally, this works if the PEFT method is additive, i.e. W' = W_base + delta_weight.
c              3  óp   #   • U  H,  n[        U[        5      (       d  M  UR                  5       v •  M.     g 7fr»   )r8   r  Úsupports_lora_conversion)rß   rO   s     rX   râ   Ú5BaseTuner.supports_lora_conversion.<locals>.<genexpr>U  s-   é € ð 
Ú<J°&ÌjÐY_Ôao×NpÓ-ˆF×+Ñ+×-Ð-ºNùs   ‚6Ÿ6)rö   rú   ©r·   r¸   s     rX   rã  Ú"BaseTuner.supports_lora_conversionO  s$   € ô ñ 
Ø<@¿L¹L¼Nó
ó 
ð 	
rr   c                ó~   >•  [         TU ]  U5      $ ! [         a     US:X  a  e [        U R                  U5      s $ f = f)z1Forward missing attributes to the wrapped module.rl   )r®   Ú__getattr__r;  ri   rl   )r·   rN   r™   s     €rX   rè  ÚBaseTuner.__getattr__Y  sB   ø€ ð	-Ü‘7Ñ& tÓ,Ð,øÜó 	-Ø�w‹ØÜ˜4Ÿ:™: tÓ,Ò,ð	-ús   ƒ ’'<»<)r³   rl   rk   r°   r±   ©FN)
rk   z(Union[PeftConfig, dict[str, PeftConfig]]r¸   r¦   r¬   Úboolr­   ú!Optional[dict[str, torch.Tensor]]ÚreturnÚNone©rí  ú	list[str])rÃ   r	   rÄ   r	   )rl   ú	nn.Modulerf   r)   r¸   r¦   rí  rî  )rk   r)   rÑ   rC   rí  r)   )rk   r)   rl   rñ  )rk   r)   rá   r¦   rí  úbool | re.Match[str] | Noner»   )rk   r)   r¸   r¦   rí   rñ  rm   r¦   rî   rñ  rï   r¦   rð   zOptional[str]rí  rî  )rl   rñ  rí  rî  ©T©r  rë  rí  rî  ©rí  rî  ©r¸   r¦   rí  rî  ©r  zstr | Sequence[str]rù   rë  rí  rî  )rf   r)   rí  rî  )r¸   r¦   r(  rë  rí  rî  )TFFN)
r8  rë  rB  rë  r9  rë  r  úOptional[list[str]]rí  rî  )FFN)rB  rë  r9  rë  r  rø  rí  útorch.nn.Module)rí  rù  ©rk   r)   rl   rñ  rm   r¦   )TFN)rl   rñ  r¸   r¦   r(  rë  r¬   rë  r­   rì  rí  rî  )
rk   r)   rl   rñ  r¸   r¦   r¬   rë  rí  rî  ©NF)r  rø  r9  rë  rí  rî  ©F)r¸   ústr | list[str]rf  rë  rí  rî  )rl   rñ  rí  rC   )rl   rñ  rí  rð  )rl   rñ  ©Údefault©r¸   r¦   rí  rë  )rN   r¦   )1rš   Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Ú__annotations__r¯   Úpropertyr¾   rÂ   r´   r¶   rÒ   rÕ   Ústaticmethodræ   rê   r   rñ   rÿ   r  r  r  r  r  r%  r*  r2  rJ  rN  rQ  rT  rW  rµ   rr  r<  r´  r¸  ru  r>  r/  r    rÌ  rÐ  ri  rã  rè  Ú__static_attributes__Ú__classcell__)r™   s   @rX   r¤   r¤   é   s»  ø‡ ñ$ðR ƒKà)Ó)ð 0Ó/ð #(Ø8<ð%2ð >ð%2ð ð	%2ð
  ð%2ð 6ð%2ð 
÷%2ð %2ðN ó#ó ð#ô3ôôôô>ð ó
ó ð
ð& ó<ó ð<ð" ð )-ð àð ð ð ð ð	 ð
 ð ð ð ð ð ð &ð ð 
ô ó ð ôDZö00ô
3ô"2ô0ö 
`ôö2qò"ðL Ø!Ø Ø-1ð5àð5ð ð5ð ð	5ð
 +ð5ð 
õ5ðp imð$
Øð$
Ø59ð$
ØReð$
à	õ$
ôL>ôJðià	ôið (,Ø"'Ø8<ð}
àð}
ð ð}
ð !%ð	}
ð
  ð}
ð 6ð}
ð 
õ}
ð~	IBØ%ðIBØ.7ðIBØGJðIBØ_cðIBà	ôIBôV5-önUò:%ö+ð óó ðô$
#ô;ò-ò-ô7#ör
÷-õ -rr   r¤   c                  ó”  • \ rS rSr% SrSrS\S'   SrS\S'   SrS\S	'   S
r	S\S'   / r
S\S'   S'S jrS r\S(S j5       r\S(S j5       rS)S*S jjrS+S jr\S,S j5       r\S,S j5       r\S-S j5       rS.S jr\S 5       rS/S jrS0S1S jjrS2S jrS3S jrS4S5S jjrS  rS6S7S! jjr\S8S" j5       r\S9S# j5       rS:S$ jrS;S<S% jjr S&r!g)=r  ic  a/  
A tuner layer mixin that provides the common methods and attributes for all tuners.

Args:
    is_pluggable (`bool`, *optional*):
        Whether the adapter layer can be plugged to any pytorch module
    active_adapters (Union[List[`str`], `str`], *optional*):
        The name of the active adapter.
rÇ   ztuple[str, ...]Úadapter_layer_namesÚother_param_namesFrë  Ú_disable_adaptersrÿ  rý  Ú_active_adapterrð  Úmerged_adaptersc                ój   • U n[        US5      (       a  UR                  n[        US5      (       a  M  U$ )zt
(Recursively) get the base_layer.

This is necessary for the case that the tuner layer wraps another tuner layer.

r0   )r7   r0   ©r·   r0   s     rX   r=  ÚBaseTunerLayer.get_base_layer|  s6   € ð ˆ
Ü�j ,×/Ñ/Ø#×.Ñ.ˆJô �j ,×/Ó/àÐrr   c                óÎ  • U R                  5       n[        US5      (       d  gUR                  n[        U[        [
        45      (       a=  [        R                  " X!R                  R                  UR                  R                  S9$ [        U[        R                  5      (       a@  UR                  5       S:X  a  U$ [        R                  " SUR                   S3[         5        gg)aŸ  
Extract embed_scale from base layer if present and valid.

Some embedding layers (e.g., Gemma3TextScaledWordEmbedding) apply scaling to embeddings in their forward
method. This method checks for the presence of an `embed_scale` attribute. If it exists, it is assumed to be a
scalar. Its shape is validated accordingly.

Returns:
    torch.Tensor or None: The embed_scale tensor if found and valid, None otherwise.
Úembed_scaleN)r=   Údtyper+   z'Found embed_scale attribute with shape z“, expected scalar. Embedding scaling will not be applied. If this is unexpected, please open an issue at https://github.com/huggingface/peft/issues)r=  r7   r  r8   ÚintÚfloatr<   rJ   r‰   r=   r  ÚTensorÚnumelrœ   r�   r�   r'   )r·   r0   r  s      rX   Ú_get_embed_scaleÚBaseTunerLayer._get_embed_scaleˆ  sÊ   € ð ×(Ñ(Ó*ˆ
Ü�z =×1Ñ1Øà ×,Ñ,ˆô �k¤C¬ <×0Ñ0Ü—<’< ×4EÑ4E×4LÑ4LÐT^×TeÑTe×TkÑTkÑlÐlô �k¤5§<¡<×0Ñ0Ø× Ñ Ó" aÓ'Ø"Ð"ô —’Ø=¸k×>OÑ>OÐ=Pð QAð Aô  ô	ð àrr   c                ó|   • U R                  5       n[        US5      (       a  UR                  nU$ UR                  nU$ )Nr¥  )r=  r7   r¥  r‰   )r·   r0   r‰   s      rX   r‰   ÚBaseTunerLayer.weight­  sC   € ð ×(Ñ(Ó*ˆ
Ü�:˜y×)Ñ)à×'Ñ'ˆFð ˆð  ×&Ñ&ˆFØˆrr   c                ó:   • U R                  5       nUR                  $ r»   )r=  rô   r  s     rX   rô   ÚBaseTunerLayer.bias½  s   € à×(Ñ(Ó*ˆ
Ø�‰Ðrr   Nc                ó   • [         er»   ©rû   )r·   r9  r  s      rX   r8  ÚBaseTunerLayer.mergeÂ  ó   € Ü!Ð!rr   c                ó   • [         er»   r!  r½   s    rX   r·  ÚBaseTunerLayer.unmergeÅ  r#  rr   c                ó,   • [        U R                  5      $ r»   )rë  r  r½   s    rX   ÚmergedÚBaseTunerLayer.mergedÈ  s   € ä�D×(Ñ(Ó)Ð)rr   c                ó   • U R                   $ r»   )r  r½   s    rX   Údisable_adaptersÚBaseTunerLayer.disable_adaptersÌ  s   € ð ×%Ñ%Ð%rr   c                ó   • U R                   $ r»   )r  r½   s    rX   r³   ÚBaseTunerLayer.active_adapterÑ  s   € ð ×#Ñ#Ð#rr   c                ó  • [        5       nU R                   Hg  n[        X5      n[        U[        R
                  [        R                  45      (       d  M?  UR                  [        UR                  5       5      5        Mi     U$ )z:Return all adapter names that can be found on this module.)	rÐ   r  ri   r8   r   Ú
ModuleDictÚParameterDictr²   rD   )r·   ÚadaptersÚ
layer_namerO   s       rX   Ú_get_available_adaptersÚ&BaseTunerLayer._get_available_adaptersÖ  s`   € ä“5ˆØ×2Ô2ˆJÜ˜TÓ.ˆFÜ˜f¤r§}¡}´b×6FÑ6FÐ&G×HÑHÙØ�O‰OœC §¡£Ó.Ö/ñ	 3ð
 ˆrr   c                ór   • [        U R                  [        5      (       a  U R                  /$ U R                  $ r»   r¼   r½   s    rX   r¾   ÚBaseTunerLayer.active_adaptersà  rÀ   rr   c                óè   • U(       a#  U R                  U R                  5        SU l        gU R                   H1  n[	        X5      nUR                  5        H  n[        US5        M     M3     SU l        g)zÃToggle the enabling and disabling of adapters

Takes care of setting the requires_grad flag for the adapter weights.

Args:
    enabled (bool): True to enable adapters, False to disable adapters
FTN)ru  r¾   r  r  ri   r?   r"   )r·   r  r2  Úmodule_dictrL   s        rX   r  ÚBaseTunerLayer.enable_adaptersç  sf   € ö Ø×Ñ˜T×1Ñ1Ô2Ø%*ˆDÕ"ð #×6Ô6�
Ü% dÓ7�Ø(×/Ñ/Ö1�EÜ,¨U°EÖ:ó 2ñ 7ð &*ˆDÕ"rr   c                óè   • [        U[        5      (       a  U/nU R                   HD  n[        X5      nUR	                  5        H"  u  pVXQ;   =(       a    U(       + n[        Xg5        M$     MF     Xl        g)a”  Set the active adapter(s).

Additionally, this function will set the specified adapter to trainable (i.e., requires_grad=True) unless
inference_mode is True.

Args:
    adapter_name (`str` or `list[str]`):
         The name(s) of the adapter(s) to set as active.
    inference_mode (bool, optional):
         Whether the activated adapter should be frozen (i.e. `requires_grad=False`). Default is False.
N)r8   r¦   r  ri   Úitemsr"   r  )r·   r  rf  r2  r8  rá   rL   Úshould_require_grads           rX   ru  ÚBaseTunerLayer.set_adapterú  sj   € ô �m¤S×)Ñ)Ø*˜OˆMð ×2Ô2ˆJÜ! $Ó3ˆKØ)×/Ñ/Ö1‘
�Ø'*Ñ';×&UÀnÔBTÐ#Ü(¨ÖDó 2ñ 3ð  -Õrr   c                óæ   • [        5       nU R                  U R                  -    H@  n[        X5      n[	        US5      (       d  M!  UR                  UR                  5       5        MB     [        U5      $ )z3Return a sorted list of all available adapter namesrD   )rÐ   r  r  ri   r7   r²   rD   rs  )r·   r  rN   Úattrs       rX   Ú_all_available_adapter_namesÚ+BaseTunerLayer._all_available_adapter_names  s_   € ä›ˆØ×,Ñ,¨t×/EÑ/EÔEˆDô ˜4Ó&ˆDÜ�t˜V×$Ó$Ø×$Ñ$ T§Y¡Y£[Ö1ñ Fô �mÓ$Ð$rr   c                óÎ  • U R                   U R                  -    H   nU[        X5      ;   d  M  [        X5      U	 M"     XR                  ;   a™  U R                  SS nUR	                  U5        U(       a  U R                  U5        gU R                  5       nU(       d  U R                  / 5        gUS   n[        R                  " SU SU S35        U R                  US   5        gg)a–  
Delete an adapter from the layer

This should be called on all adapter layers, or else we will get an inconsistent state.

This method will also set a new active adapter if the deleted adapter was an active adapter. It is important
that the new adapter is chosen in a deterministic way, so that the same adapter is chosen on all layers.

Args:
    adapter_name (`str`): The name of the adapter to delete

Nr   r  z< was active which is now deleted. Setting active adapter to rÛ   )	r  r  ri   r¾   Úremoveru  r@  rœ   r�   )r·   r¸   r?  r¾   Úremaining_adaptersÚnew_active_adapters         rX   r  ÚBaseTunerLayer.delete_adapter  sâ   € ð ×,Ñ,¨t×/EÑ/EÔEˆDØœw tÓ2Õ2Ü˜DÓ'¨Ò5ñ Fð ×/Ñ/Ó/à"×2Ñ2±1Ð5ˆOØ×"Ñ" <Ô0ÞØ× Ñ  Õ1ð &*×%FÑ%FÓ%HÐ"Þ)Ø×$Ñ$ RÕ(à);¸AÑ)>Ð&Ü—M’MØ" < .Ð0lØ-Ð.¨að1ôð ×$Ñ$Ð%7¸Ñ%:Õ;ð% 0rr   c                óÞ   • [        U[        5      (       a  U1nO[        U5      nU R                   H9  n[	        X5      nUR                  5        H  u  pgXc;   d  M  [        Xr5        M     M;     g)r  N)r8   r¦   rÐ   r  ri   r;  r"   )r·   r  rù   Úadapter_names_setr2  r8  rá   rL   s           rX   r  Ú BaseTunerLayer.set_requires_gradB  sa   € ô �m¤S×)Ñ)Ø!. Ñä # MÓ 2Ðà×2Ô2ˆJÜ! $Ó3ˆKØ)×/Ñ/Ö1‘
�ØÕ+Ü,¨UÖBó 2ò 3rr   c                ó®   • Su  p#S H,  n[        XS5      nUc  M  UR                  nUR                  n  O   [        US5      (       a  UR                  nX#4$ )zq
Helper function to determine the device and dtype of the base layer. If not possible to determine, return None.
r‡   )r‰   r¥  NÚcompute_dtype)ri   r=   r  r7   rK  )r·   r0   r=   r  Úweight_namer‰   s         rX   Ú _get_base_layer_device_and_dtypeÚ/BaseTunerLayer._get_base_layer_device_and_dtypeW  s`   € ð #‰ˆó 1ˆKÜ˜Z°dÓ;ˆFØÓ!ØŸ™�ØŸ™�Ùñ 1ô �:˜×/Ñ/Ø×,Ñ,ˆEàˆ}Ðrr   c                ó¶  ^
• U R                  5       n[        U[        R                  5      (       a  UR                  nU R                  U5      u  pEUb  UOUnUc  gSnUb$  UR                  (       d  UR                  (       a  Un[        R                  " S5      m
U R                  U R                  -    H£  n[        XS5      n	[        U	[        R                  [        R                  [        45      (       d  ME  X;  a  ML  [!        U
4S jU	R#                  5        5       5      (       a  Mv  Ub  X‘   R%                  XgS9X‘'   MŽ  X‘   R%                  U5      X‘'   M¥     g)zZ
Move the adapter of the given name to the device, and possibly dtype, of the base layer.
Nr2   c              3  ó@   >#   • U  H  oR                   T:H  v •  M     g 7fr»   r¨  r©  s     €rX   râ   ÚGBaseTunerLayer._move_adapter_to_device_of_base_layer.<locals>.<genexpr>‰  s   øé € ÐHÒ-G¨—8‘8˜tÖ#Ò-Gùr«  ©r  )r=  r8   r   r–   r`   rM  Úis_floating_pointÚ
is_complexr<   r=   r  r  ri   r/  r0  r,   rå   r®  rK   )r·   r¸   r=   r0   Úbase_layer_deviceÚbase_layer_dtypeÚtarget_deviceÚtarget_dtypeÚadapter_layer_nameÚadapter_layerr2   s             @rX   Ú%_move_adapter_to_device_of_base_layerÚ4BaseTunerLayer._move_adapter_to_device_of_base_layerj  s3  ø€ ð ×(Ñ(Ó*ˆ
Ü�j¤"×"7Ñ"7×8Ñ8Ø#×,Ñ,ˆJØ.2×.SÑ.SÐT^Ó._Ñ+Ðà"(Ñ"4™Ð:KˆØÑ ààˆØÑ'à×1×1Ð5E×5P×5PØ/�ä�|Š|˜FÓ#ˆð
 #'×":Ñ":¸T×=SÑ=SÔ"SÐÜ# D¸dÓCˆMÜ˜m¬b¯m©m¼R×=MÑ=MÌzÐ-Z×[Ñ[ÙØÓ0ÙÜÔH¨]×-EÑ-EÔ-GÓH×HÑHÙàÑ'Ø.;Ñ.I×.LÑ.LÈ]Ð.LÐ.o�Ó+à.;Ñ.I×.LÑ.LÈ]Ó.[�Ó+ò #Trr   c                ó   • g r»   rÇ   ©r·   Úxr  s      rX   Ú_cast_input_dtypeÚ BaseTunerLayer._cast_input_dtype‘  s   € ØFIrr   c                ó   • g r»   rÇ   r^  s      rX   r`  ra  ”  s   € ØVYrr   c                ót   • Uc  g[        U SS5      nU(       a  UR                  U:X  a  U$ UR                  US9$ )aT  
Whether to cast the dtype of the input of the forward method.

Usually, we want to enable this to align the input dtype with the dtype of the weight, but by setting
layer.cast_input_dtype=False, this can be disabled if necessary.

Enabling or disabling can be managed via the peft.helpers.disable_lora_input_dtype_casting context manager.
NÚcast_input_dtype_enabledTrR  )ri   r  rK   )r·   r_  r  rd  s       rX   r`  ra  —  s@   € ð ‰9Øä#*¨4Ð1KÈTÓ#RÐ Þ(¨a¯g©g¸Ó.>ØˆHØ�t‰t˜%ˆtÐ Ð rr   c                ó   • g)zœ
Whether it is possible for this layer type to be converted to LoRA.

Normally, this works if the PEFT method is additive, i.e. W' = W_base + delta_weight.
FrÇ   rå  s     rX   rã  Ú'BaseTunerLayer.supports_lora_conversion¨  s   € ð rr   )r  r  )rí  rñ  )rí  útorch.Tensorrê  )r9  rë  r  rø  rí  rî  rõ  )rí  rë  )rí  rý  )rí  úset[str]rô  rü  )r  rý  rf  rë  rí  rî  rï  rö  ró  r÷  r»   )r¸   r¦   r=   zOptional[torch.device]rí  rî  )r_  rî  r  útorch.dtyperí  rî  )r_  rg  r  ri  rí  rg  )r  ri  rþ  r   )"rš   r  r  r  r  r  r  r  r  r  r  r=  r  r  r‰   rô   r8  r·  r'  r*  r³   r3  r¾   r  ru  r@  r  r  rM  r[  r   r`  rã  r  rÇ   rr   rX   r  r  c  s*  ‡ ñð ,.Ð˜Ó-à)+Ð�Ó+ð $Ð�tÓ#ð (1€O�_Ó0ð "$€O�YÓ#ô
ò#ðJ óó ðð óó ðö"ô"ð ó*ó ð*ð ó&ó ð&ð ó$ó ð$ôð ñ#ó ð#ô*ö&-ô0	%ô#<öJCò*ö&%\ðN ÛIó ØIàÛYó ØYô!÷"ñ rr   r  c                ó~  ^• [        U [        5      (       d  U (       d  [        S5      e[        U 5      n SU ;   a  [        S5      e[        U5      nU R	                  U5      (       d  Sn[        U5      eS nU VVs1 s H  oC" U5        H  oUiM     M     nnnU  Vs0 s H  oDU" U5      _M     nn[        5       n[        UR                  5       S S9 HI  u  mn	U	 H=  nXX;   d  XV;   a  M  [        U4S jU 5       5      (       a  M+  UR                  U5          MG     MK     U(       d  [        U 5      $ U$ s  snnf s  snf )	a¼  Find the minimal set of target modules that is sufficient to separate them from the other modules.

Sometimes, a very large list of target_modules could be passed, which can slow down loading of adapters (e.g. when
loaded from diffusers). It may be possible to condense this list from hundreds of items to just a handful of
suffixes that are sufficient to distinguish the target modules from the other modules.

Example:
    ```py
    >>> from peft.tuners.tuners_utils import _find_minimal_target_modules

    >>> target_modules = [f"model.decoder.layers.{i}.self_attn.q_proj" for i in range(100)]
    >>> target_modules += [f"model.decoder.layers.{i}.self_attn.v_proj" for i in range(100)]
    >>> other_module_names = [f"model.encoder.layers.{i}.self_attn.k_proj" for i in range(100)]
    >>> _find_minimal_target_modules(target_modules, other_module_names)
    {"q_proj", "v_proj"}
    ```

Args:
    target_modules (`list[str]` | `set[str]`):
        The list of target modules.
    other_module_names (`list[str]` | `set[str]`):
        The list of other module names. They must not overlap with the target modules.

Returns:
    `set[str]`:
        The minimal set of target modules that is sufficient to separate them from the other modules.

Raises:
    ValueError:
        If `target_modules` is not a list or set of strings or if it contains an empty string. Also raises an error
        if `target_modules` and `other_module_names` contain common elements.
z2target_modules should be a list or set of strings.r/   z2target_modules should not contain an empty string.zÅtarget_modules and other_module_names contain common elements, this should not happen, please open a GitHub issue at https://github.com/huggingface/peft/issues with the code to reproduce this issuec                ó¦   • U R                  S5      n[        [        U5      5       Vs/ s H  nSR                  XS  5      PM     snS S S2   $ s  snf )NrÛ   ra  )rG   Úranger"  rH   )ÚsÚpartsrT   s      rX   Úgenerate_suffixesÚ7_find_minimal_target_modules.<locals>.generate_suffixesä  sH   € Ø—‘˜“ˆÜ-2´3°u³:Ô->Ó?Ò->¨�—‘˜˜r˜Ö#Ñ->Ñ?ÁÀ"ÀÑEÐEùÒ?s   ¨Ac                ó   • U S   $ )Nr+   rÇ   )Útups    rX   Ú<lambda>Ú._find_minimal_target_modules.<locals>.<lambda>ó  s   € ÐTWÐXYÒTZrr   )rá   c              3  óL   >#   • U  H  nTR                  S U-   5      v •  M     g7frÚ   rÜ   )rß   Ú
req_suffixÚitems     €rX   râ   Ú/_find_minimal_target_modules.<locals>.<genexpr>ú  s$   øé € Ð[ÒIZ¸:�t—}‘} S¨:Ñ%5×6Ð6ÒIZùrä   )	r8   r¦   rj   rÐ   Ú
isdisjointrs  r;  rå   Úadd)
rÎ   Úother_module_namesr
  ro  rw  r_  Úother_module_suffixesÚtarget_modules_suffix_mapÚrequired_suffixesr}  s
       `     rX   rm  rm  ±  sP  ø€ ôF �.¤#×&Ñ&®nÜÐMÓNÐNä˜Ó(€NØ	ˆ^ÓÜÐMÓNÐNäÐ/Ó0ÐØ×$Ñ$Ð%7×8Ñ8ðvð 	ô ˜‹oÐòFñ
 1CÔiÒ0B¨ÐQbÐcg×QhÀvšVÑQh™VÑ0BÐÑiñ LZÓ ZÊ>À4Ñ'8¸Ó'>Ò!>É>ÐÐ Zô ›Ðô !Ð!:×!@Ñ!@Ó!BÑHZÔ[‰ˆˆhãˆFàÓ*¨fÓ.MÙäÔ[ÑIZÓ[×[Ó[Ø!×%Ñ% fÔ-Úó ñ \ö Ü�>Ó"Ð"ØÐùó/ jùò ![s   Á;D4ÂD:c                  ó   • \ rS rSrSrS rSrg)rq  i  zh
A private helper method used to represent excluded modules in the check_target_module_exists function.
c                ó   • grû  rÇ   r½   s    rX   Ú__bool__Ú_ExcludedModule.__bool__  s   € Ørr   rÇ   N)rš   r  r  r  r  r�  r  rÇ   rr   rX   rq  rq    s   † ñõrr   rq  c                ó  ^• [        U S5      (       a©  U R                  (       a˜  [        U R                  [        5      (       a1  [        R
                  " U R                  T5      (       a
  [        5       $ OHTU R                  ;   a
  [        5       $ [        U4S jU R                   5       5      (       a
  [        5       $ [        U SS5      nU(       a$  [        U4S jU 5       5      (       a
  [        5       $ U R                  c  U R                  b  g[        U R                  [        5      (       a  [        U R                  T5      nU$ TU R                  ;   a  SnU$ [        U4S jU R                   5       5      n[        U S	S5      n[        U S
S5      nUSL=(       a%    [        U[        5      (       a  [        U5      S:g  OSnU(       a¶  U(       a¯  SnUb  [        U5      S:X  a  [        R                  " ST5      nOC[        U[        5      (       a  U/OUnU H#  n[        R                  " SU S3T5      nUc  M#    O   Uc  SnU$ [        UR!                  S5      5      n[        U[        5      (       a  Xt:H  nU$ Xt;   nU$ )a£  A helper method to check if the passed module's key name matches any of the target modules in the adapter_config.

Args:
    config (`PeftConfig`):
        A config to match target modules from.
    key (`str`):
        A key to search any matches in config

Returns:
    `bool` | `re.Match[str]` | `None`:
        True or re.Match object if key matches any target modules from config, False or None if no match found.
rd  c              3  óL   >#   • U  H  nTR                  S U 35      v •  M     g7frÚ   rÜ   )rß   Úexclude_keyrá   s     €rX   râ   Ú-check_target_module_exists.<locals>.<genexpr>  s&   øé € Ð[ÒDZ°[�—‘  + Ð/×0Ð0ÒDZùrä   rÓ  Nc              3  óZ   >#   • U  H   n[         R                  " S U S3T5      v •  M"     g7f)z(^|.*\.)z($|\..*)N)ÚreÚmatch)rß   rþ   rá   s     €rX   râ   r†  &  s(   øé € ÐOº¸!Œr�xŠx˜8 A 3 hÐ/°×5Ð5ºùs   ƒ(+FTc              3  óL   >#   • U  H  nTR                  S U 35      v •  M     g7frÚ   rÜ   rÞ   s     €rX   râ   r†  3  s&   øé € Ð!iÒShÀZ #§,¡,°°:°,Ð/?×"@Ð"@ÒShùrä   rb  rc  r   z.*\.[^.]*\.(\d+)\.z.*\.z	\.(\d+)\.r+   )r7   rd  r8   r¦   rˆ  Ú	fullmatchrq  rå   ri   rÎ   r]  r#   rB   r"  r‰  r  Úgroup)	rf   rá   rÓ  Útarget_module_foundÚlayer_indexesrc  Úis_using_layer_indexesÚlayer_indexÚpatterns	    `       rX   ré   ré     sX  ø€ ô ˆvÐ(×)Ñ)¨f×.D×.DÜ�f×,Ñ,¬c×2Ñ2Ü�|Š|˜F×2Ñ2°C×8Ñ8Ü&Ó(Ð(ð 9à�F×*Ñ*Ó*Ü"Ó$Ð$ÜÔ[ÀF×DZÒDZÓ[×[Ñ[Ü"Ó$Ð$ô ˜fÐ&7¸Ó>€OÞÜÔO¹ÓO×OÑOÜ"Ó$Ð$à×ÑÑ%¨F×,DÑ,DÑ,Pàä�&×'Ñ'¬×-Ñ-Ü6°v×7LÑ7LÈcÓRÐðF ÐðE 
�×%Ñ%Ó	%à"Ðð@ Ðô= "Ô!iÐSY×ShÒShÓ!iÓiÐä Ð(=¸tÓDˆÜ  Ð)9¸4Ó@ˆà!.°dÐ!:÷ "
Ü'1°-Ä×'FÑ'FŒC�Ó !Ò#ÈDð 	ö "Ö&9ØˆKð Ñ%¬¨^Ó)<ÀÓ)AÜ ŸhšhÐ'<¸cÓB‘ä5?ÀÔPS×5TÑ5T .Ñ!1ÐZh�Û-�GÜ"$§(¢(¨d°7°)¸9Ð+EÀsÓ"K�KØ"Ó.Ùñ  .ð
 Ñ"Ø&+Ð#ð Ðô " +×"3Ñ"3°AÓ"6Ó7�Ü˜m¬S×1Ñ1Ø*5Ñ*FÐ'ð Ðð +6Ñ*FÐ'àÐrr   c                ó(  • U R                   U   nU R                  R                  5        VVs/ s H  u  p4UPM	     nnn/ / S.nU HC  nU R                  X#5      (       a  US   R	                  U5        M/  US   R	                  U5        ME     U$ s  snnf )zo
A helper function to inspect the set of matched and unmatched modules for a PEFT model and the given adapter.
)ÚmatchedÚ	unmatchedr“  r”  )rk   rl   r6   rê   r;   )Útunerr¸   rf   rá   rC  rD  r8  s          rX   Úinspect_matched_modulesr–  T  s‘   € ð ×Ñ˜|Ñ,€FØ"'§+¡+×";Ñ";Ô"=Ô>Ò"=™˜“Ñ"=€HÑ>Ø ¨rÑ2€KÛˆØ×,Ñ,¨V×9Ñ9Ø˜	Ñ"×)Ñ)¨#Ö.à˜Ñ$×+Ñ+¨CÖ0ñ	 ð
 Ðùó ?s   ­Bc                óè  ^• [        U S5      (       d  U $ [        U R                  [        5      (       a"  U R                  R	                  5       [
        :X  d  U $ [        R                  R                  [        4nSn[        5       nUR                  5        Hn  u  nm[        TU5      (       a  UR                  U5        M*  [        T[        5      (       d  MA  [        U4S jU 5       5      (       d  M]  UR                  U5        Mp     [        5       n[        U[        5      (       aÎ  UR!                  5       nUb@  UR                  5        VVs/ s H  u  pXX‡L d  M  UPM     snnS   n	UR                  U	5        O{U R"                  [$        R&                  :X  a]  [(         HS  n[+        XS5      n
U
c  M  UR                  5        VVs/ s H  u  pXXŠL d  M  UPM     snnS   n	UR                  U	5          O   UR                  5        HU  u  nm[        T[        5      (       d  M  TR                  5        H$  u  pÍU(       d  M  UR                  U SU 35        M&     MW     XF-  nX@l        U $ s  snnf s  snnf )z½
Helper function to update `target_modules` to all linear/Conv1D layers if provided as 'all-linear'. Adapted from
the QLoRA repository: https://github.com/artidoro/qlora/blob/main/qlora.py
rÎ   )rz   c              3  óR   >#   • U  H  o[        T5      R                  ;   v •  M     g 7fr»   )rž   rš   )rß   rü   rO   s     €rX   râ   Ú3_maybe_include_all_linear_layers.<locals>.<genexpr>y  s!   øé € Ð7iÒ\hÐWX¼TÀ&»\×=RÑ=RÖ8RÒ\hùs   ƒ$'Nr   rÛ   )r7   r8   rÎ   r¦   Úlowerr   r<   r   rz   r   rÐ   r6   rz  r  rå   r   r?  Ú	task_typer&   ÚSEQ_CLSr   ri   )rk   rl   Úlinear_classesÚlinear_namesÚlinear_module_namesrN   Úmodule_names_to_excludeÚ
output_embrO   Úlast_module_nameÚcls_headr§   r_  r±  s           `     rX   rj  rj  c  s!  ø€ ô
 �;Ð 0×1Ñ1ØÐô 	�;×-Ñ-¬s×3Ñ3Ø×&Ñ&×,Ñ,Ó.Ô2QÓQàÐä—h‘h—o‘o¤vÐ.€NØ€LÜ›%ÐØ×+Ñ+Ö-‰ˆˆfä�f˜n×-Ñ-Ø×#Ñ# DÖ)Ü˜¤×/Ó/´CÔ7iÑ\hÓ7i×4iÓ4ið  ×#Ñ# DÖ)ñ .ô "›eÐÜ�%œ×)Ñ)Ø×0Ñ0Ó2ˆ
ØÑ!à9>×9LÑ9LÔ9NÔgÒ9N©¨ÐRXÐRf§Ñ9NÒgÐhiÑjÐØ#×'Ñ'Ð(8Õ9Ø×"Ñ"¤h×&6Ñ&6Ó6÷ +�Ü" 5°Ó5�ØÓ'ØAF×ATÑATÔAVÔ'mÒAV±°ÐZ`ÐZl¯ÑAVÒ'mÐnoÑ'pÐ$Ø+×/Ñ/Ð0@ÔAÙñ +ð  ×-Ñ-Ö/‰ˆ�Ü�fœn×-Ó-Ø!'×!5Ñ!5Ö!7‘�ß�6Ø+×/Ñ/°6°(¸!¸F¸8Ð0DÖEó "8ñ 0ð Ñ2ÐØ!4ÔØÐùó-  hùó (ns   Å
I(ÅI(Ç
I.ÇI.c                ó¾  • Uc  U R                   n[        U[        5      (       a  [        SU< S35      eU R                  (       a’  [        U R                  5      nU Vs/ s H  o3U;  d  M
  UPM     nnU(       aG  [        R                  " SSR                  U R                  5       SSR                  U5       S35        U$ [        R                  " S5        U$ s  snf )z·
Helper function to check which adapters should be merged.

Only return those adapters that are not already merged. Give a warning if some or all of the adapters are already
merged.

z/adapter_names should be a list of strings, got rÛ   z'Already following adapters were merged Ú,z#. You are now additionally merging z/All adapters are already merged, nothing to do.)
r¾   r8   r¦   rj   r'  rÐ   r  rœ   r�   rH   )rO   r  r  rN   s       rX   Úcheck_adapters_to_merger¦  ¢  sÏ   € ð ÑØ×.Ñ.ˆÜ�-¤×%Ñ%ÜÐJÈ=ÑJ[Ð[\Ð]Ó^Ð^à‡}‡}Ü˜f×4Ñ4Ó5ˆÙ*7ÓWª- $ÀÑ;VŸ©-ˆÐWæÜ�MŠMØ9¸#¿(¹(À6×CYÑCYÓ:ZÐ9[ð \4Ø47·H±H¸]Ó4KÐ3LÈAðOôð Ðô �MŠMÐKÔLàÐùò Xs   Á	CÁ,CFc                óª   • [         R                  " U 5      nSS jnU(       a0  U R                  5        H  u  pEU" XRR                  U5      5        M     U$ )zÕClone a module in a pytorch model.

Clones a module of a model, optionally sharing all the parameters between the original and the clone. Simplifies
reusing a module when manipulating the architecture of a model.
c                óV   • U R                  SS9 H  u  p#UR                  X#5        M     g )NFr›  )rø   Úregister_parameter)ÚsrcÚdstrN   rW   s       rX   Ú_share_weightsÚ$clone_module.<locals>._share_weightsÆ  s*   € Ø×/Ñ/¸Ð/Ó>‰KˆDØ×"Ñ" 4Ö/ò ?rr   )rª  rñ  r«  rñ  )ÚcopyÚdeepcopyr6   r”  )rO   Úshare_weightsÚcloner¬  rN   Ú	submodules         rX   Úclone_moduler³  ¾  sK   € ô �MŠM˜&Ó!€Eô0ö Ø%×3Ñ3Ö5‰OˆDÙ˜9×&9Ñ&9¸$Ó&?Ö@ñ  6ð €Lrr   c           	     ó  • [        U S5      (       a  U R                  n [        U S5      (       a  M  [        U S5      (       a  U R                  n SnSn[        U S5      (       a  SnU R                  nOd[        U S5      (       a4  [        U R                  S5      (       a  SnU R                  R
                  nO[        U S5      (       a  S	nU R                  nU(       a  [        U[        R                  5      (       d  [        S
5      e/ nU Hp  u  pV[        XV5       H\  n[        U5      nUR                  [        X7   SS95        US   R                  5        H  n	[        U	S5      (       d  M  X‰l        M     M^     Mr     [        R                  " U5      nUS:X  a  X0l        O/US:X  a  X0R                  l        OUS	:X  a  X0l        O[        S5      e[        U R"                  S5      (       a  [        U5      U R"                  l        gg)an  Replicate layers in a transfomer model with weight sharing.

This function looks for a module list attribute at model[(.model)*].layers and replicates the layers in the module
list according to the layer map. For example the map `[[0, 4], [2, 5]]` will take the set of layers `[0, 1, 2, 3,
4]` and replace them with a module list containing `[0, 1, 2, 3, 2, 3, 4]`.
rl   ÚbertNÚlayersÚllamaÚencoderrL   ÚhÚfalconzlCould not locate the layers attribute in the model. Expected Llama, Bert or Falcon compatible architectures.T)r°  ra  Ú	layer_idxz@Unexpected model type, need to handle post-processing of layers.Únum_hidden_layers)r7   rl   rµ  r¶  r¸  rL   r¹  r8   r   Ú
ModuleListrj   rl  r"  r;   r³  rú   r»  rf   r¼  )
rl   Ú	layer_maprg   r¶  Ú
new_layersÚstartÚendrT   Úcurrent_idxr²  s
             rX   Úreplicate_layersrÃ  Ñ  s³  € ô �%˜×
!Ñ
!Ø—‘ˆô �%˜×
!Ó
!ô ˆu�f×ÑØ—
‘
ˆà€JØ €FÜˆu�h×ÑØˆ
Ø—‘‰Ü	�˜	×	"Ñ	"¤w¨u¯}©}¸g×'FÑ'FØˆ
Ø—‘×$Ñ$‰Ü	�˜×	Ñ	Øˆ
Ø—‘ˆÞœZ¨´·±×>Ñ>ÜðGó
ð 	
ð
 €JÛ‰
ˆÜ�uÖ"ˆAÜ˜j›/ˆKØ×Ñœl¨6©9ÀDÑIÔJà'¨™^×3Ñ3Ö5�	Ü˜9 k×2Ó2Ø*5Ö'ó 6ó	 #ñ  ô �]Š]˜:Ó&€FØ�WÓØ�Ø	�vÓ	Ø$�‰ÕØ	�xÓ	Ø�äÐ[Ó\Ð\Üˆu�|‰|Ð0×1Ñ1Ü),¨Z«ˆ�‰Õ&ð 2rr   c                óF   • U R                  5        H  u  p#X1L d  M  Us  $    g)zû
Find layer name from the model by matching the reference module to the model named modules

Args:
    model (nn.Module): The model with named modules
    reference_module (nn.Module): The reference module to find

Returns:
    str: Name of the layer
r/   )r6   )rl   Úreference_modulerü   rþ   s       rX   Úfind_parameter_name_by_modulerÆ    s)   € ð ×#Ñ#Ö%‰ˆØÔ ØŠHñ &ð rr   c                óö   • [        XUS9  U R                  5        H[  n[        XC5      (       d  M  UR                  (       a&  [        R
                  " S5        UR                  5         UR                  XS9  M]     g)ae  Set the active PEFT adapter(s) of the model.

Active adapters are those adapters that participate in the forward pass. Use this function if you want to switch
between multiple PEFT adapters.

Args:
    model (`nn.Module`):
        The model on which the adapter(s) should be set.
    adapter_name (str, list[str]):
        The name(s) of the adapter(s) to set as active
    inference_mode (bool, optional):
         Whether the activated adapter should be frozen (i.e. `requires_grad=False`). Default is False.
    layer_cls (type, optional):
        The class of the adapter layer. Defaults to `BaseTunerLayer`.
re  zJAdapter cannot be set when the model is merged. Unmerging the model first.N)r!   rú   r8   r'  rœ   r�   r·  ru  )rl   r¸   rf  r  rO   s        rX   ru  ru    sX   € ô* �°^ÒDØ—-‘-–/ˆÜ�f×(Ó(Ø�}�}Ü—’ÐjÔkØ—‘Ô Ø×Ñ˜|ÐÓKò "rr   c                ó~   • U R                  5        H)  n[        U[        5      (       d  M  UR                  XS9  M+     g )N)Únew_active_adapters)rú   r8   r   r  )rl   r¸   rÉ  rO   s       rX   Ú_delete_auxiliary_adapterrÊ  :  s1   € Ø—-‘-–/ˆÜ�fÔ6×7Ó7Ø×!Ñ! ,Ð!ÓXò "rr   c                ó  • U R                  5        VVs/ s H  u  pEX$;  d  M  UPM     nnnSnU HH  n[        X5      u  pXn[        Xƒ5      (       d  M#  UR                  U5        Ub  M9  UR                  SS nMJ     [        XUS9  U$ s  snnf )as  
Delete an existing PEFT adapter.

Note: This function does not delete the PEFT config on the model, if there is one. It will also not completely
purge the PEFT layers if the last PEFT adapter is deleted. For this, consider using `model.unload()` if using a
PEFT model instance, or just reloading the base model.

Args:
    model (`nn.Module`):
        The model from which the adapter should be deleted.
    adapter_name (str):
        The name of the adapter to be deleted.
    prefix (str):
        The prefix of the PEFT method, e.g. "lora_" for LoRA.
    layer_cls (type, optional):
        The class of the adapter layer. Defaults to `BaseTunerLayer`.

Returns:
    new_adapter (list[str] | None):
        The name of remaining adapter(s) after deletion, or `None` if there are no active adapters left. Use this
        to set the new active adapter of the model if necessary.
N)r¸   rÉ  )r6   r*   r8   r  r¾   rÊ  )	rl   r¸   r§   r  rá   rC  rD  r  rí   s	            rX   r  r  @  s�   € ð2 #(×"5Ñ"5Ô"7ÔMÒ"7™˜¸6Ñ;L—Ñ"7€HÑMØ€KãˆÜ& uÓ2‰ˆ�1Ü�f×(Ó(Ø×!Ñ! ,Ô/ØÓ"Ø$×4Ñ4±QÐ7’ñ ô ˜eÐT_Ò`ØÐùó Ns
   ”B£Bc                óú  • U(       d  g[         R                  [         R                  1n[         H*  n[	        [         US5      =nc  M  UR                  U5        M,     U R                  5        GHŠ  n[        U[        5      (       d  M  UR                  5        GHZ  n[        U[        R                  [        R                  [        45      (       d  M:  X;  a  MA  [        Xq   [        R                  5      (       aF  Xq   R                  U;   a2  Xq   R                  R!                  [         R"                  5      Xq   l        M¨  [        Xq   [         R$                  5      (       a8  Xq   R                  U;   a#  Xq   R!                  [         R"                  5      Xq'   GM  Xq   R'                  5        HC  nUR                  U;   d  M  UR                  R!                  [         R"                  5      Ul        ME     GM]     GM�     g)a)  
A helper method to cast the adapter weights to the correct dtype.

Currently, this only upcasts float dtypes to float32.

Args:
    adapter_name (`str`):
        The adapter name.
    autocast_adapter_dtype (`bool`, *optional*):
        Whether to autocast the adapter dtype. Defaults to `True`.
N)r<   Úfloat16Úbfloat16r   ri   rz  rú   r8   r  r   r/  r0  r,   Ú	Parameterr  ÚdatarK   Úfloat32r  r®  )	rl   r¸   r(  Údtypes_to_convert_to_fp32rN   Útorch_dtyperO   r²  rW   s	            rX   r)  r)  g  su  € ö "Øä!&§¡´·±Ð ?Ð÷ ˆÜ"¤5¨$°Ó5Ð5ˆKÓBØ%×)Ñ)¨+Ö6ñ ð —-‘-—/ˆÜ˜&¤.×1Ñ1ÙàŸ™×)ˆIÜ˜i¬"¯-©-¼×9IÑ9IÌ:Ð)V×WÑWÙàÓ,Ùä˜)Ñ1´2·<±<×@Ñ@ØÑ*×0Ñ0Ð4MÓMØ3<Ñ3J×3OÑ3O×3RÑ3RÔSX×S`ÑS`Ó3a�IÑ+Ô0Ùä˜)Ñ1´5·<±<×@Ñ@ØÑ*×0Ñ0Ð4MÓMØ.7Ñ.E×.HÑ.HÌÏÉÓ.W�IÑ+Úà"Ñ0×;Ñ;Ö=�Ø—;‘;Ð";Õ;Ø!&§¡§¡¬u¯}©}Ó!=�E–Jô >ô# *ò	 "rr   c                óŠ   • U R                  5        H/  n[        U[        [        45      (       d  M   UR	                  XS9  M1     g)aw  
Enable or disable gradients on the given adapter(s).

Args:
    model (`nn.Module`):
        The model from which the adapter should be deleted.
    adapter_name (`str` or `Sequence[str]`):
        The name of the adapter(s) whose gradients should be enabled/disabled.
    requires_grad (`bool`, *optional*)
        Whether to enable (`True`, default) or disable (`False`).
r  N)rú   r8   r  r   r  )rl   r  rù   rO   s       rX   r  r  —  s8   € ð —-‘-–/ˆÜ�fœ~Ô/GÐH×IÓIØ×$Ñ$°=Ð$Ó^ò "rr   c                óŽ   • [        U S5      (       a  U R                  nU$ S[        U R                  5       5      R                  0nU$ )NÚhf_device_mapr/   )r7   rÖ  r­  r®  r=   )rl   Ú
device_maps     rX   Úget_device_maprØ  ¨  sH   € Üˆu�o×&Ñ&à×(Ñ(ˆ
ð Ðð œ$˜u×/Ñ/Ó1Ó2×9Ñ9Ð:ˆ
ØÐrr   rú  )rO   rñ  rí  z#tuple[int, int] | tuple[None, None])rÎ   úlist[str] | set[str]r{  rÙ  rí  rh  )rá   r¦   rí  rò  rþ  )r•  r¤   r¸   r¦   rí  rC   )rk   r)   rl   rñ  rí  r)   r»   )rO   r  r  rø  rí  rð  rü  )rO   rñ  )rl   rñ  r¾  zlist[tuple[int, int]])rl   rñ  rÅ  rñ  rí  r¦   )r¸   rý  rf  rë  r  r¨   rí  rî  )r¸   r¦   rÉ  rø  rí  rî  )
rl   rñ  r¸   r¦   r§   r¦   r  r¨   rí  zlist[str] | Noneró  )rl   rñ  r¸   r¦   r(  rë  rí  rî  r÷  )rí  rC   )`Ú
__future__r   r®  r¾  rE   rˆ  r-  rœ   Úabcr   r   Úcollections.abcr   Ú
contextlibr   r   Útypingr	   r
   r   r   r<   Úaccelerate.hooksr   Úaccelerate.utilsr   r   Ú	packagingr   r   r   Útransformersr   Útransformers.pytorch_utilsr   Úpeft.import_utilsr   Úpeft.mappingr   Ú
peft.utilsr   r   Úpeft.utils.constantsr   r   r   r   r   Úpeft.utils.integrationsr   Úpeft.utils.otherr   r    r!   r"   r#   r$   Úpeft.utils.peft_typesr%   r&   Úpeft.utils.warningr'   rf   r)   Úutilsr*   Ú_buffer_dictr,   rÉ  ÚparseÚ__version__Ú_torch_supports_dtensorrŠ   Úis_availablerˆ   rY   rq   r¢   ÚModuler¤   r  rm  rq  ré   r–  rj  r¦  r³  rÃ  rÆ  ru  rÊ  r  r)  r  rØ  rÇ   rr   rX   Ú<module>ró     sà  ðõ #ã Û Û 	Û 	Û Û ß #Ý $ß 2ß 1Ó 1ã Ý -ß EÝ Ý Ý Ý (Ý -å 3Ý 4ß E÷õ õ 7÷÷ ÷ 5Ý *å Ý #Ý $ðQð ð "Ÿ-š-¨×(9Ñ(9Ó:¸g¿mºmÈGÓ>TÑTÐ Ø5×Z¸%×:KÑ:K×:XÑ:XÓ:ZÐ ð ñ@Só ð@SôFô,B%ôJw-�—	‘	˜3ô w-ôt#K�Sô Kð\
OØ(ðOØ>RðOàôO÷dñ ôEöPô<ö~ö8ô&19ôhð4 !Ø&4ð	Là!ðLð ðLð $ð	Lð
 
õLô<Yð Ygð$Øð$Ø$'ð$Ø14ð$ØAUð$àõ$öN->ö`_õ"rr   