
    oi3-                        S SK Jr  S SKrS SKrS SKJr  S SK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  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 jr " S S\5      rg)    )annotationsN)Union)_calculate_correct_fan)Conv1D)is_bnb_4bit_availableis_bnb_available)	BaseTunerBaseTunerLayer)2TRANSFORMERS_MODELS_TO_VERA_TARGET_MODULES_MAPPING   )
BufferDict) _maybe_include_all_linear_layers   )
VeraConfig)Linear	VeraLayerc                   [        U [        5      (       a  [        R                  " U 5      nOU n[	        US5      n[
        R                  " S5      nU[
        R                  " U5      -  n[
        R                  " S5      U-  n[        R                  " 5          UR                  U* XaS9sSSS5        $ ! , (       d  f       g= f)a  
Kaiming Uniform Initialisation adapted to accept a `torch.Generator` object for PRNG.

Args:
    tensor_or_shape (`Union[torch.Tensor, tuple[int, ...]]`):
        Tensor to initialise, or shape of new tensor to create and then initialise.
    generator: (`torch.Generator`):
        Generator object that manages the state of the PRNG algorithm in use.

Returns:
    `torch.Tensor`: The initialised tensor.
fan_inr   g      @	generatorN)	
isinstancetupletorchemptyr   mathsqrtno_graduniform_)tensor_or_shaper   tensorfangainstdbounds          P/home/james-whalen/.local/lib/python3.13/site-packages/peft/tuners/vera/model.py_kaiming_initr&   &   s      /5))_- 
 
2C99Q<D
3
CIIcNS E	vuB 
s   B33
Cc                  |   ^  \ rS rSr% SrSrS\S'   \r\	r
SS jrSS jrSS jrSU 4S	 jjrS
 r\S 5       rSrU =r$ )	VeraModelC   a  
Creates Vector-based Random Matrix Adaptation (Vera) model from a pretrained transformers model.

Args:
    model ([`~transformers.PreTrainedModel`]): The model to be adapted.
    config ([`VeraConfig`]): The configuration of the Vera model.
    adapter_name (`str`): The name of the adapter, defaults to `"default"`.
    low_cpu_mem_usage (`bool`, `optional`, defaults to `False`):
        Create empty adapter weights on meta device. Useful to speed up the loading process.

Returns:
    `torch.nn.Module`: The Vera model.

Example:

    ```py
    >>> from transformers import AutoModelForCausalLM
    >>> from peft import VeraConfig, get_peft_model

    >>> base_model = AutoModelForCausalLM.from_pretrained("facebook/opt-125m")
    >>> config = VeraConfig(r=128)
    >>> model = get_peft_model(base_model, config)
    ```

**Attributes**:
    - **model** ([`~transformers.PreTrainedModel`]) -- The model to be adapted.
    - **peft_config** ([`VeraConfig`]): The configuration of the Vera model.
vera_lambda_strprefixc                   U R                  U R                  5      nU R                  X5      n[        X0R                  5      nSnU R                  R	                  5        H  u  pVU R                  X55      (       d  M  [        U[        R                  5      (       a  UR                  UR                  4nOg[        U[        5      (       aP  [        UR                  S5      (       a  UR                  R                  OUR                  R                  nUSSS2   nOM  Uc  UnM  Xt:w  d  M  [!        S [#        XG5       5       5      nM     Uc  Sn[%        U5      eU$ )z
Finds the largest input and output dimensions across linear layers that have been wrapped with VeRA.

This will be used for determining the size of the shared vera_A and vera_B matrices.
Nds_shapec              3  <   #    U  H  u  p[        X5      v   M     g 7fN)max).0abs      r%   	<genexpr>&VeraModel._find_dim.<locals>.<genexpr>   s     %]<\DAc!ii<\s   z[No layers types compatible with VeRA were found. Please check `peft_config.target_modules`.)get_model_configmodel_prepare_adapter_configr   named_modules_check_target_module_existsr   nnr   out_featuresin_featuresr   hasattrweightr.   shaper   zip
ValueError)	selfconfigmodel_configpeft_configlargest_shapekeymodulemodule_shapemsgs	            r%   	_find_dimVeraModel._find_dime   s"    ,,TZZ8226H6{JJO::335KC33KEE&")),,%22F4F4FFFF++9@PZ9[9[v}}55agananatat+DbD1$ ,, %%]C<\%] ]# 6&  oCS/!    c                v   U R                  U5      u  p4[        0 UR                  S9U l        [        0 UR                  S9U l        [
        R                  " SS9R                  UR                  5      n[        UR                  U4US9n[        X1R                  4US9nX`R                  U'   XpR                  U'   g )N)
persistentcpu)devicer   )rN   r   save_projectionvera_Avera_Br   	Generatormanual_seedprojection_prng_keyr&   r)rE   rF   adapter_namelinear_out_dimlinear_in_dimr   rV   rW   s           r%   _init_vera_A_vera_BVeraModel._init_vera_A_vera_B   s    (,v(>% !0F0FG 0F0FG OO51==f>X>XY	-8IN9YO$*L!$*L!rP   c                &    U R                  X#5        g r1   )r_   )rE   r9   rF   r\   s       r%   _pre_injection_hookVeraModel._pre_injection_hook   s      6rP   c                  > [         TU ]  U5        U R                  R                  5        HJ  nX!L a  M	  UR                  UR                  :w  d  M%  [        SUR                  < SUR                   S35      e   [        U R                  R                  5        Vs1 s H  oR                  iM     sn5      n[        U5      S:  a  [        SU 35      egs  snf )z
A helper method to check the config when a new adapter is being added.

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

z_Vera PRNG initialisation key must be the same for all adapters. Got config.projection_prng_key=z but previous config had .r   zcVeRA projection weights must be saved for all adapters or none, but got multiple different values: N)	super_check_new_adapter_configrH   valuesrZ   rD   sortedrU   len)rE   rF   existing_configsave_project_unique_values	__class__s       r%   rg   #VeraModel._check_new_adapter_config   s     	)&1#//668O(22f6P6PP v[a[u[uZw x++:+N+N*OqR   9 &,RVRbRbRiRiRk,lRk-C-CRk,l%m")*Q.u-.0  / -ms   Cc           
        Uc  [        S5      eUR                  n[        US5      =(       a    UR                  S Ln	UUR                  UR
                  UR                  [        U R                  SS5      [        U R                  SS5      S.n
XS'   [        U[        5      (       aH  UR                  UU R                  U R                  UUR                  UR                  UR                  S9  g U R                  " XR                  U R                  X#40 U
D6nX R                   ;  a  UR#                  S5        U R%                  XTX5        g )NzCurrent Key shouldn't be `None`biasis_loaded_in_8bitFis_loaded_in_4bit)r[   vera_dropoutfan_in_fan_outinit_weightsloaded_in_8bitloaded_in_4bit)	d_initial)rD   r[   r@   rp   rs   rt   ru   getattrr9   r   r   update_layerrV   rW   rx   _create_new_moduleactive_adapterrequires_grad__replace_module)rE   vera_configr\   targettarget_nameparentcurrent_keyoptional_kwargsr[   rp   kwargs
new_modules               r%   _create_and_replaceVeraModel._create_and_replace   s'    >??MMvv&B6;;d+B'44)88'44%djj2EuM%djj2EuM
 vff%%((((%//    00kk4;;XdwpvwJ#6#66))%0  jIrP   c                   [        5       (       a
  SS KnSSKJn  [	        5       (       a  SSKJn  UR                  SS5      n	UR                  SS5      n
UR                  SS5      n[        U[        5      (       a  UR                  5       nOUnU
(       a  [        UWR                  R                  5      (       aa  UR                  5       nUR                  UR                  R                  UR                  R                   UR"                  S	.5        W" XCX40 UD6$ U(       a  [        UWR                  R
                  5      (       aa  UR                  5       nUR                  UR$                  UR&                  R(                  UR&                  R*                  S
.5        W" XCX40 UD6$ [        U[,        R                  R.                  5      (       a-  US   (       a"  [0        R2                  " S5        S=US'   U l        OV[        U[6        5      (       a2  SUS'   US   (       d"  [0        R2                  " S5        S=US'   U l        O[9        SU S35      e[/        UUUU4U	U R:                  S.UD6nU$ )Nr   r   )Linear8bitLt)
Linear4bitrp   Frv   rw   )has_fp16_weights	thresholdindex)compute_dtypecompress_statistics
quant_typert   zjfan_in_fan_out is set to True but the target module is `torch.nn.Linear`. Setting fan_in_fan_out to False.Tis_target_conv_1d_layerzafan_in_fan_out is set to False but the target module is `Conv1D`. Setting fan_in_fan_out to True.zTarget module z is not supported. Currently, only the following modules are supported: `torch.nn.Linear`, `transformers.pytorch_utils.Conv1D`.)rp   rx   )r   bitsandbytesbnbr   r   r   popgetr   r
   get_base_layerr=   copyupdatestater   r   r   r   rA   r   r   r   r   warningswarnrt   r   rD   rx   )r   rV   rW   r\   r   r   r   r   r   rp   rv   rw   target_base_layereightbit_kwargsfourbit_kwargsr   s                   r%   r{   VeraModel._create_new_module   sG    &) ""'zz&%($4e<$4e<fn-- & 5 5 7 &j):CFF<O<OPP$kkmO""(9(?(?(P(P!2!8!8!B!B.44  fXXX
+<cff>O>O P P#[[]N!!%6%D%D+<+C+C+W+W"3":":"E"E fFUnUU)588??;;&'7 INM'(;+E)62204F,-*+w IML'(;+E  )J J  	

 !++
 

 rP   )rV   rW   )returnztuple[int, int])rF   r   r\   r+   r   None)r9   z	nn.ModulerF   r   r\   r+   r   r   )rF   r   r   r   )__name__
__module____qualname____firstlineno____doc__r,   __annotations__r   tuner_layer_clsr   target_module_mappingrN   r_   rb   rg   r   staticmethodr{   __static_attributes____classcell__)rm   s   @r%   r(   r(   C   sQ    : !FC ON#J+76(JT D DrP   r(   )r   z$Union[torch.Tensor, tuple[int, ...]]r   ztorch.Generatorr   ztorch.Tensor) 
__future__r   r   r   typingr   r   torch.nnr=   torch.nn.initr   transformers.pytorch_utilsr   peft.import_utilsr   r   peft.tuners.tuners_utilsr	   r
   
peft.utilsr   _buffer_dictr   tuners_utilsr   rF   r   layerr   r   r&   r(    rP   r%   <module>r      si    #      0 - E > & ;  $C9CC C:c	 crP   