Ë
    Gêñi•8  ã                   ó–  — d dl Z ddlmZ ddlmZmZ ddlmZmZm	Z	m
Z
mZ  e	«       rd dlZ e
«       rd dlZd dlmZ ddlmZ  e«       rd dlZd dlmZmZ  ej.                  e«      Z G d„ d	e«      Z G d
„ de«      Z G d„ de«      Z G d„ de«      Z	 	 	 ddej                   j<                  dee    dz  fd„Z!ddd„Z"d„ Z#dd„Z$dd„Z%y)é    Né   )ÚConversionOps)Úget_module_from_nameÚshould_convert_module)Úget_available_devicesÚis_accelerate_availableÚis_bitsandbytes_availableÚis_torch_availableÚlogging)ÚConv1D)Úadd_hook_to_moduleÚremove_hook_from_modulec                   ó¦   — e Zd Zd„ Z	 	 ddeeeej                     f   dedz  dej                  j                  dz  deeej                  f   fd„Zy)	ÚBnb4bitQuantizec                 ó   — || _         y ©N©Úhf_quantizer©Úselfr   s     úh/var/www/pod-logistic/pod-ai/venv/lib/python3.12/site-packages/transformers/integrations/bitsandbytes.pyÚ__init__zBnb4bitQuantize.__init__   ó
   € Ø(ˆÕó    NÚ
input_dictÚfull_layer_nameÚmodelÚreturnc                 óp  — t        |j                  «       «      d   }|d   }t        ||«      \  }}t        |j                  t
        «      r|j                  }|j                  |«      }t        j                  j                  |fddi|j                  ¤Žj                  |j                  «      }	d|_        ||	iS )a:  
        we need to store some parameters to create the quantized weight. For example, bnb requires 6 values that are stored in the checkpoint to recover the quantized weight. So we store them in a dict that it stored in hf_quantizer for now as we can't save it in the op since we create an op per tensor.
        r   Úrequires_gradFT)ÚlistÚvaluesr   Ú
issubclassÚ
source_clsr   ÚTÚget_parameter_or_bufferÚbnbÚnnÚ
Params4bitÚ__dict__ÚtoÚdeviceÚ_is_hf_initialized)
r   r   r   r   ÚkwargsÚvalueÚmoduleÚ_Ú	old_valueÚ	new_values
             r   ÚconvertzBnb4bitQuantize.convert"   s«   € ô �Z×&Ñ&Ó(Ó)¨!Ñ,ˆØ�a‘ˆô )¨°Ó@‰	ˆ�ô �f×'Ñ'¬Ô0Ø—G‘GˆEà×1Ñ1°/ÓBˆ	Ü—F‘F×%Ñ% eÑW¸5ÐWÀI×DVÑDVÑW×ZÑZÐ[`×[gÑ[gÓhˆ	Ø$(ˆÔ!Ø Ð+Ð+r   ©NN©Ú__name__Ú
__module__Ú__qualname__r   ÚdictÚstrr!   ÚtorchÚTensorr(   ÚModuler4   © r   r   r   r      sl   „ ò)ð '+Ø(,ñ	,à˜˜d 5§<¡<Ñ0Ð0Ñ1ð,ð ˜t™ð,ð �x‰x�‰ Ñ%ð	,ð 
ˆc�5—<‘<ÐÑ	 ô,r   r   c                   ó¦   — e Zd Zd„ Z	 	 ddeeeej                     f   dej                  j                  dz  dedz  deeej                  f   fd„Zy)	ÚBnb4bitDeserializec                 ó   — || _         y r   r   r   s     r   r   zBnb4bitDeserialize.__init__>   r   r   Nr   r   r   r   c                 óN  — t        |«      dk(  r|S |j                  «       D ]  \  }}t        |t        «      sŒ|d   ||<   Œ  d}|j	                  |«      }t        ||«      \  }	}
t        j                  j                  j                  ||d|j                  |	¬«      }d|	_        ||iS )z_
        Deserialization of bnb keys. We need 6 keys to recreate the quantized weights
        é   r   ÚweightF)ÚdataÚquantized_statsr    r,   r0   T)ÚlenÚitemsÚ
isinstancer!   Úpopr   r'   r(   r)   Úfrom_prequantizedr,   r-   )r   r   r   r   r.   Úkeyr/   Ú
key_weightrE   r0   r1   r3   s               r   r4   zBnb4bitDeserialize.convertA   sµ   € ô ˆz‹?˜aÒØÐà$×*Ñ*Ó,ò 	+‰JˆC�Ü˜%¤Õ&Ø"'¨¡(�
˜3’ð	+ð ˆ
Ø—‘ 
Ó+ˆÜ(¨°Ó@‰	ˆ�Ü—F‘F×%Ñ%×7Ñ7ØØ&ØØ—=‘=Øð 8ó 
ˆ	ð %)ˆÔ!Ø˜IÐ&Ð&r   r5   r6   r?   r   r   rA   rA   =   sl   „ ò)ð )-Ø&*ñ	'à˜˜d 5§<¡<Ñ0Ð0Ñ1ð'ð �x‰x�‰ Ñ%ð'ð ˜t™ð	'ð 
ˆc�5—<‘<ÐÑ	 ô'r   rA   c                   ó¦   — e Zd Zd„ Z	 	 ddeeeej                     f   dej                  j                  dz  dedz  deeej                  f   fd„Zy)	ÚBnb8bitQuantizec                 ó   — || _         y r   r   r   s     r   r   zBnb8bitQuantize.__init__a   r   r   Nr   r   r   r   c                 óÌ  — t        |j                  «       «      d   }t        |t         «      r|d   n|}t        ||«      \  }}t	        |j
                  t        «      r|j                  }|j                  }|j                  |«      j                  }|j                  dd «       t        j                  j                  |j                  d«      fddi|¤Žj                  |«      }	||	iS )Nr   ÚSCBÚcpur    F)r!   r"   rJ   r   r#   r$   r   r%   r,   r&   r*   rK   r'   r(   Ú
Int8Paramsr+   )
r   r   r   r   r.   r/   r0   r1   Úvalue_devicer3   s
             r   r4   zBnb8bitQuantize.convertd   sÅ   € ô �Z×&Ñ&Ó(Ó)¨!Ñ,ˆÜ& u¬dÔ3��a’¸ˆä(¨°Ó@‰	ˆ�ô �f×'Ñ'¬Ô0Ø—G‘GˆEØ—|‘|ˆØ×.Ñ.¨Ó?×HÑHˆØ�
‰
�5˜$ÔÜ—F‘F×%Ñ% e§h¡h¨u£oÑUÀUÐUÈfÑU×XÑXÐYeÓfˆ	Ø Ð+Ð+r   r5   r6   r?   r   r   rP   rP   `   sl   „ ò)ð )-Ø&*ñ	,à˜˜d 5§<¡<Ñ0Ð0Ñ1ð,ð �x‰x�‰ Ñ%ð,ð ˜t™ð	,ð 
ˆc�5—<‘<ÐÑ	 ô,r   rP   c                   ó¦   — e Zd Zd„ Z	 	 ddeeeej                     f   dej                  j                  dz  dedz  deeej                  f   fd„Zy)	ÚBnb8bitDeserializec                 ó   — || _         y r   r   r   s     r   r   zBnb8bitDeserialize.__init__|   r   r   Nr   r   r   r   c                 ó‚  — t        |«      dk(  r|S |j                  «       D ]  \  }}t        |t        «      sŒ|d   ||<   Œ  t	        ||«      \  }}d}	||	   }
|j                  |«      j                  }|d   |d<   t        j                  j                  |
fddi|¤Žj                  |
j                  «      }d|_        |	|iS )z.
        Deserialization of bnb keys.
        rD   r   rE   rS   r    FT)rH   rI   rJ   r!   r   r&   r*   r'   r(   rU   r+   r,   r-   )r   r   r   r   r.   rM   r/   r0   r1   rN   rE   r3   s               r   r4   zBnb8bitDeserialize.convert   sÓ   € ô ˆz‹?˜aÒð Ðà$×*Ñ*Ó,ò 	+‰JˆC�Ü˜%¤Õ&Ø"'¨¡(�
˜3’ð	+ô )¨°Ó@‰	ˆ�àˆ
Ø˜JÑ'ˆØ×.Ñ.¨Ó?×HÑHˆØ" 5Ñ)ˆˆu‰Ü—F‘F×%Ñ% fÑL¸EÐLÀVÑL×OÑOÐPV×P]ÑP]Ó^ˆ	Ø$(ˆÔ!Ø˜IÐ&Ð&r   r5   r6   r?   r   r   rX   rX   {   sl   „ ò)ð )-Ø&*ñ	'à˜˜d 5§<¡<Ñ0Ð0Ñ1ð'ð �x‰x�‰ Ñ%ð'ð ˜t™ð	'ð 
ˆc�5—<‘<ÐÑ	 ô'r   rX   r   Úmodules_to_not_convertc                 óš  — d}| j                  «       D �]  \  }}t        ||«      sŒd}t        j                  d«      5  t	        |t
        «      st        |«      t        j                  u �r±t	        |t
        «      r|j                  j                  \  }}	n|j                  }|j                  }	|j                  «       dk(  rŠt        j                  j                  ||	|j                   du|j"                  |j$                  ¬«      }|rß|j                  j&                  j)                  t        j*                  ¬«      |j                  _        n›t        j                  j-                  ||	|j                   du|j.                  |j0                  |j2                  |j4                  ¬«      }|r?|j                  j&                  j)                  |j4                  ¬«      |j                  _        |�5t        |«      |_        |j9                  d«       | j;                  ||«       d}ddd«       �Œ |st<        j?                  d	«       | S # 1 sw Y   �Œ7xY w)
aÒ  
    A helper function to replace all `torch.nn.Linear` modules by bnb modules from the `bitsandbytes` library.

    Args:
        model (`torch.nn.Module`):
            The model to convert, can be any `torch.nn.Module` instance.
        modules_to_not_convert (`list[str]`, defaults to `None`):
            A list of nn.Linear weights to not convert. If a parameter path is in the list (e.g. `lm_head.weight`), the corresponding module will not be
            converted.
        quantization_config (`BitsAndBytesConfig`):
            The quantization config object that contains the quantization parameters.
        pre_quantized (`book`, defaults to `False`):
            Whether the model is pre-quantized or not
    FNÚmetaÚllm_int8)Úhas_fp16_weightsÚ	threshold)Údtype)Úcompress_statisticsÚ
quant_typeÚquant_storageTz»You are loading your model using eetq but no linear modules were found in your model. Please double check your model architecture, or submit an issue on github if you think this is a bug.) Únamed_modulesr   r<   r,   rJ   r   Útyper(   ÚLinearrE   ÚshapeÚin_featuresÚout_featuresÚquantization_methodr'   ÚLinear8bitLtÚbiasÚllm_int8_has_fp16_weightÚllm_int8_thresholdrF   r+   Úint8Ú
Linear4bitÚbnb_4bit_compute_dtypeÚbnb_4bit_use_double_quantÚbnb_4bit_quant_typeÚbnb_4bit_quant_storager$   Úrequires_grad_Úset_submoduleÚloggerÚwarning)
r   r[   Úquantization_configÚpre_quantizedÚhas_been_replacedÚmodule_namer0   Ú
new_moduleri   rj   s
             r   Úreplace_with_bnb_linearr   �   s  € ð( Ðà$×2Ñ2Ó4ó ,-Ñˆ�VÜ$ [Ð2HÔIØØˆ
Ü�\‰\˜&Ó!ñ (	-Ü˜&¤&Ô)¬T°&«\¼R¿Y¹YÒ-FÜ˜f¤fÔ-Ø06·±×0CÑ0CÑ-�K¡à"(×"4Ñ"4�KØ#)×#6Ñ#6�LØ&×:Ñ:Ó<À
ÒJÜ!$§¡×!4Ñ!4Ø#Ø$ØŸ™¨4Ð/Ø)<×)UÑ)UØ"5×"HÑ"Hð "5ó "�Jñ %ð 2<×1BÑ1B×1GÑ1G×1JÑ1JÔQV×Q[ÑQ[Ð1JÓ1\˜
×)Ñ)Õ.ä!$§¡×!2Ñ!2Ø#Ø$ØŸ™¨4Ð/Ø+×BÑBØ,?×,YÑ,YØ#6×#JÑ#JØ&9×&PÑ&Pð "3ó "�Jñ %à1;×1BÑ1B×1GÑ1G×1JÑ1JØ"5×"LÑ"Lð 2Kó 2˜
×)Ñ)Ô.ð Ð)ä,0°«L�JÔ)à×-Ñ-¨eÔ4Ø×'Ñ'¨°ZÔ@Ø(,Ð%÷Q(	-ñ (	-ð	,-ñ\ Ü�‰ðô	
ð
 €L÷a(	-ñ (	-ús   ¾GI É I
	c                 ó^  — t        | t        j                  j                  «      st	        dt        | «      › d�«      ‚| j                  j                  }|dvr| S |dk(  r6t        j                  j                  | j                  | j                  «      }|S |j                  €| j                  |_        t        t        j                  d«      r6t        j                  j                  | j                  |j                  «      }|S | j                  |j                  j!                  dd«      z  dz  }|S )	zŽ
    Helper function to dequantize 4bit or 8bit bnb weights.

    If the weight is not a bnb quantized weight, it will be returned as is.
    z1Input weight should be of type nn.Parameter, got z instead)r)   rU   r)   Úint8_vectorwise_dequantéÿÿÿÿrD   g   €@ €?)rJ   r<   r(   Ú	ParameterÚ	TypeErrorrf   Ú	__class__r7   r'   Ú
functionalÚdequantize_4bitrF   Úquant_staterS   Úhasattrr�   Úview)rE   ÚstateÚcls_nameÚoutput_tensorÚdequantizeds        r   Údequantize_bnb_weightr�   ë   sõ   € ô �fœeŸh™h×0Ñ0Ô1ÜÐKÌDÐQWËLÈ>ÐYaÐbÓcÐcà×Ñ×(Ñ(€HØÐ3Ñ3Øˆà�<ÒÜŸ™×6Ñ6°v·{±{ÀF×DVÑDVÓWˆØÐà‡y�yÐØ—J‘JˆŒ	äŒs�~‰~Ð8Ô9ä—n‘n×<Ñ<¸V¿[¹[È%Ï)É)ÓTˆð
 Ðð —k‘k E§I¡I§N¡N°2°qÓ$9Ñ9Ð<PÑPˆàÐr   c                 ó  — t        t        j                  | j                  j                  «      }| j
                  }i }t        j                  |j                  «      }|D ]  }||j                  v sŒ||   ||<   Œ  |di |¤Ž}|S )a  
    Creates a new hook based on the old hook. Use it only if you know what you are doing !
    This method is a copy of: https://github.com/huggingface/peft/blob/748f7968f3a31ec06a1c2b0328993319ad9a150a/src/peft/utils/other.py#L245
    with some changes
    r?   )
ÚgetattrÚ
accelerateÚhooksr…   r7   r*   ÚinspectÚ	signaturer   Ú
parameters)Úold_hookÚold_hook_clsÚold_hook_attrÚfiltered_old_hook_attrÚold_hook_init_signatureÚkÚnew_hooks          r   Ú_create_accelerate_new_hookrž   	  s�   € ô œ:×+Ñ+¨X×-?Ñ-?×-HÑ-HÓI€LØ×%Ñ%€MØÐÜ%×/Ñ/°×0EÑ0EÓFÐØò 9ˆØÐ'×2Ñ2Ò2Ø(5°aÑ(8Ð" 1Ò%ð9ñ Ñ5Ð4Ñ5€HØ€Or   c                 ó°  — |j                  «       }|dk(  rt        j                  j                  nt        j                  j                  }| j                  «       D �]Ô  \  }}t        ||«      sŒt        j                  d«      5  t        |dd«      }t        j                  j                  |j                  |j                  |du¬«      }ddd«       |dk(  r|j                  nd}	t        j                  j                  t        |j                   |	«      «      _        t        |j                   |	«      }
|€$t"        j%                  d|
j&                  › d�«       n7t"        j%                  d|
j&                  › d|› d	�«       |
j)                  |«      }
t        j                  j                  |
«      |_        �||_        t-        |d
«      r.|j.                  }t1        |«      }t3        |«       t5        ||«       |j)                  |j                   j                  «       | j7                  ||«       d}�Œ× st"        j9                  d«       | S # 1 sw Y   �Œ„xY w)a&  
    Converts a quantized model into its dequantized original version. The newly converted model will have
    some performance drop compared to the original model before quantization - use it only for specific usecases
    such as QLoRA adapters merging.

    Returns the converted model.
    r^   r]   rm   N)rm   zThe modules are dequantized in zK. If you want to change the dtype, please specify `dtype` in `dequantize`. z and casted to ú.Ú_hf_hookTz_For some reason the model has not been properly dequantized. You might see unexpected behavior.)rk   r'   r(   rl   rq   re   rJ   r<   r,   r‘   rg   ri   rj   r‹   rƒ   r�   rE   rx   Úwarning_oncera   r+   rm   r‰   r¡   rž   r   r   rw   ry   )r   rz   ra   Úquant_methodÚ
target_clsr}   r0   rm   r~   r‹   rE   r—   r�   r|   s                 r   Údequantize_and_replacer¥     sü  € ð '×:Ñ:Ó<€Là(4¸
Ò(B”—‘×$Ò$ÌÏÉ×HYÑHY€JØ$×2Ñ2Ó4ó %Ñˆ�VÜ�f˜jÕ)Ü—‘˜fÓ%ñ mÜ˜v v¨tÓ4�Ü"ŸX™XŸ_™_¨V×-?Ñ-?À×ATÑATÐ[_ÐgkÐ[k˜_Ól�
÷mð %1°JÒ$>�F—L’LÀDˆEÜ %§¡× 2Ñ 2Ô3HÈÏÉÐX]Ó3^Ó _ˆJÔÜ*¨6¯=©=¸%Ó@ˆFØˆ}Ü×#Ñ#Ø5°f·l±l°^ð  DOð  Põô ×#Ñ#Ð&EÀfÇlÁlÀ^ÐSbÐchÐbiÐijÐ$kÔlØŸ™ 5Ó)�Ü %§¡× 2Ñ 2°6Ó :ˆJÔØÐØ"&�
”Ü�v˜zÔ*Ø!Ÿ?™?�Ü6°xÓ@�Ü'¨Ô/Ü" :¨xÔ8Ø�M‰M˜&Ÿ-™-×.Ñ.Ô/Ø×Ñ ¨ZÔ8Ø $Òð3%ñ6 Ü�‰Ømô	
ð €L÷;mñ mús   ÂAIÉI	c                 óÞ   — t        t        dt        «       «      }t        t        «       «      }|j	                  |«      s0| rd|xs d› d|› d�}t        |«      ‚t        j                  d«       yy)	zs
    Validates if the available devices are supported by bitsandbytes, optionally raising an exception if not.
    Úsupported_torch_devicesz3None of the available devices `available_devices = NzY` are supported by the bitsandbytes version you have installed: `bnb_supported_devices = z¢`. Please check the docs to see if the backend you intend to use is available and how to install it: https://huggingface.co/docs/bitsandbytes/main/en/installationz+No supported devices found for bitsandbytesFT)r‘   r'   Úsetr   ÚintersectionÚRuntimeErrorrx   ry   )Úraise_exceptionÚbnb_supported_devicesÚavailable_devicesÚerr_msgs       r   Ú!validate_bnb_backend_availabilityr¯   G  s�   € ô $¤CÐ)BÄCÃEÓJÐÜÔ1Ó3Ó4Ðà×)Ñ)Ð*?Ô@ÙàEÐFWÒF_Ð[_ÐE`ð  azð  {Pð  zQð Qrð rð ô ˜wÓ'Ð'ä�‰ÐDÔEØØr   )NNFr   )rE   ztorch.nn.Parameterr5   )F)&r”   Úcore_model_loadingr   Úquantizers.quantizers_utilsr   r   Úutilsr   r   r	   r
   r   Úbitsandbytesr'   r<   Útorch.nnr(   Úpytorch_utilsr   r’   Úaccelerate.hooksr   r   Ú
get_loggerr7   rx   r   rA   rP   rX   r>   r!   r;   r   r�   rž   r¥   r¯   r?   r   r   ú<module>r¸      sÌ   ðÛ å .ß U÷õ ñ ÔÛáÔÛÝå&áÔÛßLà	ˆ×	Ñ	˜HÓ	%€ô,�mô ,ô> '˜ô  'ôF,�mô ,ô6'˜ô 'ðH 04ØØñ	JØ�8‰8�?‰?ðJà  ™I¨Ñ,óJô\ò<ó"*ôZr   