
    oi+                    t   S SK Jr  S SKrS SKJr  S SKJrJr  S SKr	S SK
r
S SKrS SK
Jr  \	R                  R                  \R                  5      \	R                  R                  S5      :  rSS jr\SSS jj5       rSS	 jrSSS
 jjrSS jrS rSS jr\SSS jj5       r\SSS jj5       r\S 5       rS rg)    )annotationsN)contextmanager)LiteralOptional)nnz
5.0.0.dev0c                     [         R                  R                  [        R                  5      [         R                  R                  S5      :  a  SSKJn   U " 5       $ SSKJn   U " 5       $ )Nz4.33.0r   is_deepspeed_zero3_enabled)	packagingversionparsetransformers__version__transformers.integrationsr
   transformers.deepspeedr	   s    Q/home/james-whalen/.local/lib/python3.13/site-packages/peft/utils/integrations.pycheck_deepspeed_zero3_enabledr      sP    |778I<M<M<S<ST\<]]H &'' 	F%''    c              #     #    [        5       (       d  Sv   gSSKnUR                  R                  XUS9   Sv   SSS5        g! , (       d  f       g= f7f)z`Call DeepSpeed GatheredParameters context manager if DeepSpeed is enabled, otherwise do nothing.Nr   )modifier_rank
fwd_module)r   	deepspeedzeroGatheredParameters)paramr   r   r   s       r   gather_params_ctxr   &   sI      )**		*	*5Zd	*	e 
f
 
f	e
s   3AA	A
AAc                   [        U S5      (       a  U R                  5       nU$ [        U R                  5      R                  R                  S5      (       a  U R                  R                  5       nU$ U R                  n[        U[        R                  R                  5      (       d9  [        U[        R                  5      (       a  U$ [        S[        U5       S35      eUR                  R                  nUS;  a  U$ [        U SS5      nUR                  nUR                  [        R                  " S5      R                  :H  n[!        XS	9nU(       a   U R                  R#                  U5      U l        U$ )
z
Helper function to dequantize a quantized weight.

This function should be extended if more quantization schemes are added to the library.

If the weight is not quantized, it will be returned as is.
W_qztorchao.z1Input weight should be of type nn.Parameter, got z instead)
Params4bit
Int8ParamsstateNcpu)r!   )hasattr
dequantizetypeweight
__module__
startswith
isinstancetorchr   	ParameterTensor	TypeError	__class____name__getattrdevicedequantize_bnb_weightto)moduler&   cls_namequant_stater1   is_cpus         r   dequantize_module_weightr8   5   s'    vu""$	fmm		'	'	2	2:	>	>))+]]Ffehh0011fell++MKDQWL>Yabcc((H33&'40K]]F[[ELL/444F"6=F((0Mr   c                   SSK nU R                  nU R                  R                  nUS:X  a2  UR                  R                  U R                  U R                  5      nU$ Uc  [        S5      eUR                  c  U R                  Ul	        [        UR                  S5      (       a2  UR                  R                  U R                  UR                  5      nU$ U R                  UR                  R                  SS5      -  S-  nU$ )	z7Helper function to dequantize 4bit or 8bit bnb weights.r   Nr   zNo `state` was passed for bnb 8bit quantized weights. Please open an issue on the PEFT repository and report the error: https://github.com/huggingface/peft/issuesint8_vectorwise_dequant   g   @ ?)bitsandbytesr1   r.   r/   
functionaldequantize_4bitdatar6   
ValueErrorSCBr#   r:   view)r&   r!   bnbr1   r5   dequantizeds         r   r2   r2   Z   s    ]]F((H<nn44V[[&BTBTU }K
 	

 yyJJ	s~~899nn<<V[[%))T
  kkEIINN2q$99<PPr   c                p    U R                   R                  S:X  a  gU R                   R                  S:X  a  gg)z>Returns '4bit' or '8bit' if bitsandbytes parameter, else Falser   4bitr    8bitF)r.   r/   )r   s    r   get_bnb_param_typerI   y   s/    </</r   c                   U R                   R                  5        Vs/ s H  oS;  d  M
  UPM     snS   nU R                   R                  5        VVs0 s H  u  p4X4S;   a  UOU_M     nnnUc  g[        U5      S:X  a9  SU;   a3  [	        U R
                  R                  5       Vs0 s H  ofUS   _M
     sn$ 0 nU H?  n[	        U R
                  R                  5       H  nSU S3U S3;   d  M  XX   Xv'     M=     MA     [	        U R
                  R                  5       H  nXg;  d  M
  [        SU S35      e   U$ s  snf s  snnf s  snf )	z4
Derive the device map for the layers of the model.
)r"   diskr   Nr<    .zlayer z! has not been mapped to a device.)hf_device_mapvaluesitemslenrangeconfignum_hidden_layersRuntimeError)	modeldmain_devicenamer1   execution_device_mapidxlayer_device_maplayers	            r   get_layer_device_mapr^      s_    $1188:W:>V1:WXYZK X]WjWjWpWpWrWr|t6kFBWr   #
 A%"0D*D9>u||?]?]9^_9^#)"--9^__%778C3%qzwa[((<(C % 9 &
 U\\334&u,MNOO 5 + X `s   	D;D;E &Ec                   [        U[        R                  5      (       a  [        U S5      (       d  g[        U[        R                  5      (       a  [        XR                  5        g[        U 5      n[        U R                  R                  5       H  nX#   n[        US5      (       aQ  UR                  U   nUR                  R                  U5      Ul        UR                  R                  U5      Ul        Mi  UR                  U   R                  U5      UR                  U'   UR                   U   R                  U5      UR                   U'   M     g)zh
Ensure that the key and value cache of the model are on the same device as their corresponding layers.
rN   Nlayers)r)   r   Cacher#   EncoderDecoderCachemap_cache_to_layer_device_mapself_attention_cacher^   rR   rS   rT   r`   keysr3   rO   	key_cachevalue_cache)rV   cacher\   r[   layer_devicer]   s         r   rc   rc      s    ul0011ge_6U6U%99::%e-G-GH+E2U\\334',5(##LL%E|4EJ <<??<8EL $)??3#7#:#:<#HEOOC %*%6%6s%;%>%>|%LEc" 5r   c              #     #    [        [        R                  " S5      U S9 nUv   S S S 5        g ! , (       d  f       g = f7f)Nmeta)include_buffers)_init_on_devicer*   r1   )rl   fs     r   init_empty_weightsro      s.      
f-	OST 
P	O	Os   A /	A 
=A c              #  h  ^ ^^	#    [         R                  R                  m	U(       a  [         R                  R                  mU U	4S jnSU U4S jjnU(       a"  S Vs0 s H  nU[	        [
        U5      _M     nnO0 nU 4S jn U[         R                  l        U(       a  U[         R                  l        UR                  5        H'  n[        [
        XF" [	        [
        U5      5      5        M)     S v   T	[         R                  l        U(       a  T[         R                  l        UR                  5        H  u  pG[        [
        XG5        M     g s  snf ! T	[         R                  l        U(       a  T[         R                  l        UR                  5        H  u  pG[        [
        XG5        M     f = f7f)Nc                .  > T" XU5        Ub  [        [        SS5      SLas  [        U R                  U   5      nU R                  U   R                  nUR
                  US'   U" U R                  U   R                  T5      40 UD6U R                  U'   g g g )N_skipFTrequires_grad)r0   rm   r%   _parameters__dict__rs   r3   )r4   rY   r   	param_clskwargsr1   old_register_parameters        r   register_empty_parameter1_init_on_device.<locals>.register_empty_parameter   s     	vU3GOWe$LTX$XV//56I''-66F&+&9&9F?#'01C1CD1I1L1LV1T'_X^'_Ft$	 %Yr   c                r   > T" XX#S9  Ub,  U R                   U   R                  T5      U R                   U'   g g )N)
persistent)_buffersr3   )r4   rY   bufferr|   r1   old_register_buffers       r   register_empty_buffer._init_on_device.<locals>.register_empty_buffer   s:    F&H$*OOD$9$<$<V$DFOOD! r   )emptyzerosonesfullc                   >^  UU 4S jnU$ )Nc                    > TUS'   T" U 0 UD6$ )Nr1    )argsrw   r1   fns     r   wrapperB_init_on_device.<locals>.patch_tensor_constructor.<locals>.wrapper   s    %F8t&v&&r   r   )r   r   r1   s   ` r   patch_tensor_constructor1_init_on_device.<locals>.patch_tensor_constructor   s    	' r   )T)	r   Moduleregister_parameterregister_bufferr0   r*   re   setattrrP   )
r1   rl   ry   r   torch_function_nametensor_constructors_to_patchr   old_torch_functionr   rx   s
   `       @@r   rm   rm      s\      YY99 ii77	`E E  (J(
'I#  0C!DD'I 	% (
$
 (*$D'?		$(=BII%#?#D#D#FE.0HQVXkIl0mn $G'=		$(;BII%7S7Y7Y7[3E.C 8\3(
, (>		$(;BII%7S7Y7Y7[3E.C 8\s-   AF2E7F2A0E 3A F2AF//F2c               #     #    [        [        SS5      n  S[        l        S v   U [        l        g ! U [        l        f = f7f)Nrr   FT)r0   rm   rr   )old_vals    r   _skip_init_on_devicer     s5      ow6G( $ 's   A 0 A =A c                F   ^  [         R                  " T 5      U 4S j5       nU$ )z
Ignore the init_on_device context manager when calling the decorated function.

This is a narrow use decorator that allows us to avoid initializing on meta device even when we're inside the
init_empty_weights context.

c                 ^   > [        5          T" U 0 UD6sS S S 5        $ ! , (       d  f       g = fN)r   )r   rw   funcs     r   r   $skip_init_on_device.<locals>.wrapper  s"    !#(( $##s   
,)	functoolswraps)r   r   s   ` r   skip_init_on_devicer     s'     __T) ) Nr   )returnbool)r   N)r   zOptional[int]r   torch.nn.Module)r4   r   r   torch.nn.Parameterr   )r&   r   )r   r   r   zLiteral[False, '4bit', '8bit'])r   None)rl   r   )r1   ztorch.devicerl   r   )
__future__r   r   
contextlibr   typingr   r   packaging.versionr   r*   r   r   r   r   r   is_transformers_ge_v5r   r   r8   r2   rI   r^   rc   ro   rm   r   r   r   r   r   <module>r      s    #  % $     "))//0H0HIYM^M^MdMdeqMrr (  "J><MD   2D 2Dj ( (r   