
    oi                        S SK Jr  S SKrS SKJrJr  S SKrS SKJr  SSK	J
r
  \\R                  \R                  \R                  4   r " S S\R                  5      r " S S	\R                  5      rg)
    )annotationsN)OptionalUnion   )XLoraConfigc                  2   ^  \ rS rSrSU 4S jjrS rSrU =r$ )TemperatureScaledSoftmax   c                `   > [         TU ]  5         Xl        [        R                  " SS9U l        g )N)dim)super__init__temperaturennSoftmaxsoftmax)selfr   	__class__s     V/home/james-whalen/.local/lib/python3.13/site-packages/peft/tuners/xlora/classifier.pyr   !TemperatureScaledSoftmax.__init__   s$    &zzb)    c                @    XR                   -  nU R                  U5      $ )N)r   r   )r   logitsscaled_logitss      r   forward TemperatureScaledSoftmax.forward"   s    !1!11||M**r   )r   r   )g      ?)__name__
__module____qualname____firstlineno__r   r   __static_attributes____classcell__r   s   @r   r	   r	      s    *
+ +r   r	   c                     ^  \ rS rSrSr          S	U 4S jjr  S
     SS jjr  S
     SS jjrSS jrSS jr	Sr
U =r$ )XLoraClassifier)   z/
A classifier to select LoRA layers for XLora.
c           	       > [         T
U ]  5         X0l        X@l        X l        / U l        [        U R                  R                  S9U l        UR                  U l
        SU l        [        UR                  5       5      R                  U l        UR                  S:  n/ nU R                  R                   S:X  a  UR"                  (       aL  [$        R&                  " UR(                  X4-  SS9R+                  U5      R+                  U R                  5      nGO[$        R&                  " UR(                  USS9R+                  U5      R+                  U R                  5      nGO\U R                  R                   S::  a  [-        S5      eUR/                  [$        R&                  " UR(                  UR0                  SS9R+                  U5      R+                  U R                  5      5        UR/                  [$        R2                  " 5       5        U(       a-  UR/                  [$        R4                  " UR                  S	95        [7        UR                   S
-
  5       H  n	UR/                  [$        R&                  " UR0                  UR0                  SS9R+                  U5      R+                  U R                  5      5        UR/                  [$        R2                  " 5       5        U(       d  M  UR/                  [$        R4                  " UR                  S	95        M     UR"                  (       aK  [$        R&                  " UR0                  X4-  SS9R+                  U5      R+                  U R                  5      nOH[$        R&                  " UR0                  USS9R+                  U5      R+                  U R                  5      n[$        R8                  " / UQUP76 U l        g)z
Construct an X-LoRA classifier from a model, config and some metadata. Note that n_layers is the number of LoRA
adapter layers, not the number of model layers.
)r   Fg        r   T)biasr   z'X-LoRA depth must be strictly positive.)p   N)r   r   	n_classesn_layersconfiglog_scalingsr	   softmax_temperaturer   scaling_pass_valueoverride_scaling_pass_valuescalings_loggingnext
parametersdtypexlora_dropout_pxlora_depthlayerwise_scalingsr   Linearhidden_sizeto
ValueErrorappend
xlora_sizeReLUDropoutrange
Sequentiallayers)r   modelr.   r,   r-   deviceadd_dropoutrD   last_r   s             r   r   XLoraClassifier.__init__.   s    	" /DKK<[<[\393L3L( %%**,-33
,,s2;;""a'((yy!3!3Y5IPTUXXY_`ccdhdndnoyy!3!3YTJMMfUXXY]YcYcd{{&&!+ !JKKMM"))F$6$68I8IPTUXXY_`ccdhdndnopMM"'')$bjj6+A+ABC6--12bii(9(96;L;LSWX[[\bcffgkgqgqrsbggi(;MM"**v/E/E"FG 3 ((yy!2!2I4HtTWWX^_bbcgcmcmnyy!2!2IDILLVTWWX\XbXbcmm2V2T2r   c                P   Ub+  UR                   S   nUR                  nUR                   S   nO*UR                   S   nUR                  nUR                   S   n[        R                  " XWU R                  U R
                  4U R                  5      R                  X`R                  S9$ )a0  
Make some dummy scalings for the scalings pass (the one to get the logits for the X-LoRA classifier). These are
of shape (batch_size, seq_len, n_layers, n_classes) and filled with the override scalings pass value. Note that
n_layers is the number of LoRA adapter layers, not the number of model layers.
r   r   )rF   r6   )	shaperF   torchfullr-   r,   r2   r<   r6   )r   	input_idsinputs_embedsargskwargs
batch_sizerF   seq_lens           r   make_dummy_scalings#XLoraClassifier.make_dummy_scalingse   s      "+J%%Fooa(G&,,Q/J"))F#))!,Gzz$--@,,
 "F**"
-	.r   c                p   Ub  UR                   S   nUR                   S   nOUR                   S   nUR                   S   nUR                  nUS   n	U R                  R                  U	5      n
U R                  R
                  (       d/  U
R                  S5      n
U
R                  SSU R                  S5      n
U
R                  XgU R                  U R                  5      nU R                  R                  (       a  U R                  U5      nU R                  (       a  U R                  R                  U5        U$ )zd
Using the hidden states of the model, predict `n_classes` LoRA alpha values. Returns the scalings.
r   r   r   r+   )rL   hidden_statesrD   r   r.   r9   	unsqueezeexpandr-   reshaper,   enable_softmaxr   r3   r/   r>   )r   resultrO   rP   rQ   rR   rS   rT   rX   hidden_stater   scalingss               r   r   XLoraClassifier.forward   s     "+Jooa(G&,,Q/J#))!,G,,$R( $$\2
 {{--%%a(F]]2r4=="=F >>*t}}dnnU ;;%%||H-H  $$X.r   c                    0 n[        U R                  5       HO  u  p#UR                  S   nXA;  a
  U/U/4X'   M#  X   S   R                  U5        X   S   R                  U5        MQ     U$ )a,  
Returns bucketed scalings, bucketed by seq_len. Each value consists of the positions (the first) and the
associated tensors. The positions are paired with the associated tensors and give the position in the scaling
log. Each scaling is a tensor of shape (batch_size, seq_len, n_layers, n_classes)).
r   r   )	enumerater/   rL   r>   )r   seqlens_mapiscalingrT   s        r   _get_bucketed_scalings&XLoraClassifier._get_bucketed_scalings   s{     HJ#D$5$56JAmmA&G))*gY'7$$Q'..q1$Q'..w7 7 r   c                v    Uc  SU R                   -  U l        OXl        U R                  U R                  l        g )Nr   )r,   r2   r.   r1   )r   values     r    _set_override_scaling_pass_value0XLoraClassifier._set_override_scaling_pass_value   s0    =/04>>/AD,/4,)-)I)I&r   )	r.   r6   rD   r/   r,   r-   r2   r3   r   )
rE   z	nn.Moduler.   r   r,   intr-   rl   rF   ztorch.device)NN)rO   zOptional[torch.LongTensor]rP   zOptional[torch.FloatTensor]returnztorch.Tensor)rm   z/dict[int, tuple[list[int], list[torch.Tensor]]])ri   zUnion[Number, None])r   r   r    r!   __doc__r   rU   r   rf   rj   r"   r#   r$   s   @r   r&   r&   )   s    5353 53 	53
 53 53r 1559.-. 3. 
.: 1559	, ., 3	, 
,\"J Jr   r&   )
__future__r   builtinstypingr   r   rM   torch.nnr   r.   r   rl   floatboolNumberModuler	   r&    r   r   <module>rx      s\    #  "    
x||X^^X]]:	;
+ryy 
+ZJbii ZJr   