§
    [zni5  ã                   óö   — d dl Zd dlZd dlmZ d dlmZ ddlmZm	Z	m
Z
  e	¦   «         rd dlZd dlZd dlmZ ddlmZ  e¦   «         rd dlmZ d dlmZ  e
j        e¦  «        Zdd	„Z	 dd„Zdd„Zd„ Zd„ Zd„ ZdS )é    N)Údeepcopy)Úversioné   )Úis_accelerate_availableÚis_bitsandbytes_availableÚlogging)ÚConv1D)Úinit_empty_weights)Úfind_tied_parametersc                 ó\  — d|v rS|                      d¦  «        }|dd…         D ]+}t          | |¦  «        }|€t          | › d|› d�¦  «        ‚|} Œ,|d         }|| j        vr|| j        vrt          | › d|› d�¦  «        ‚|| j        v }t          | |¦  «        }	|	j        t          j        d¦  «        k    r/|dt          j        d¦  «        fvr|€t          |› d|› d�¦  «        ‚d}
d}|st          ¦   «         sd}d}
nnt          t          j
        d	¦  «        o)t          | j        |         t          j
        j        ¦  «        }
t          | j        |         t          j
        j        ¦  «        }|s|
�r«| j        |         }|j        j        d
k    �r‰|€|	                     |¦  «        }n³t          |t          j        ¦  «        rƒ|                     d¦  «        }|j        t          j        k    rXt'          j        t*          j                             d¦  «        ¦  «        t'          j        d¦  «        k    }|st          d¦  «        ‚nt          j        |d¬¦  «        }t1          | j        t4          ¦  «        r	|€|j        }|	j        }|r-t          j
        j        |fddi|¤Ž                     |¦  «        }n.|
r,t          j
        j        |fddi|¤Ž                     |¦  «        }|| j        |<   |�-t;          | j        d|                     |¦  «        ¦  «         dS dS dS |€|	                     |¦  «        }nFt          |t          j        ¦  «        r|                     |¦  «        }nt          j        ||¬¦  «        }|r|| j        |<   dS t          j        ||	j         ¬¦  «        }|| j        |<   dS )aÍ  
    A helper function to set a given tensor (parameter of buffer) of a module on a specific device (note that doing
    `param.to(device)` creates a new tensor not linked to the parameter, which is why we need this function). The
    function is adapted from `set_module_tensor_to_device` function from accelerate that is adapted to support the
    class `Int8Params` from `bitsandbytes`.

    Args:
        module (`torch.nn.Module`):
            The module in which the tensor we want to move lives.
        tensor_name (`str`):
            The full name of the parameter/buffer.
        device (`int`, `str` or `torch.device`):
            The device on which to set the tensor.
        value (`torch.Tensor`, *optional*):
            The value of the tensor (useful when going from the meta device to any other device).
        fp16_statistics (`torch.HalfTensor`, *optional*):
            The list of fp16 statistics to set on the module, used for serialization.
    ú.Néÿÿÿÿz has no attribute z- does not have a parameter or a buffer named Úmetaz7 is on the meta device, we need a `value` to put in on FÚ
Params4bitÚcudaÚcpuÚbitsandbytesz0.37.2z¿Detected int8 weights but the version of bitsandbytes is not compatible with int8 serialization. Make sure to download the latest `bitsandbytes` version. `pip install --upgrade bitsandbytes`.)ÚdeviceÚrequires_gradÚSCB)r   )!ÚsplitÚgetattrÚ
ValueErrorÚ_parametersÚ_buffersr   Útorchr   ÚhasattrÚbnbÚnnÚ
isinstancer   Ú
Int8ParamsÚtypeÚtoÚTensorÚdtypeÚint8r   ÚparseÚ	importlibÚmetadataÚtensorÚ
issubclassÚ
source_clsr	   ÚTÚ__dict__ÚsetattrÚweightÚ	Parameterr   )ÚmoduleÚtensor_namer   ÚvalueÚfp16_statisticsÚsplitsr   Ú
new_moduleÚ	is_bufferÚ	old_valueÚis_4bitÚis_8bitÚparamÚ	new_valueÚis_8bit_serializableÚkwargss                   ú†/var/www/html/cool-property-finder/image-detection-backend/venv/lib/python3.11/site-packages/transformers/integrations/bitsandbytes.pyÚ%set_module_quantized_tensor_to_devicerA      s  € ð( ˆkÐÐØ×"Ò" 3Ñ'Ô'ˆØ˜C˜R˜C”[ð 	 ð 	 ˆEÝ  ¨Ñ/Ô/ˆJØÐ!Ý  FÐ!FÐ!F¸eÐ!FÐ!FÐ!FÑGÔGÐGØˆFˆFØ˜R”jˆà˜&Ô,Ð,Ð,°ÀFÄOÐ1SÐ1SÝ˜FÐ_Ð_ÐQ\Ð_Ð_Ð_Ñ`Ô`Ð`Ø˜vœÐ.€IÝ˜ Ñ,Ô,€IàÔ�5œ<¨Ñ/Ô/Ò/Ð/°FÀ6Í5Ì<ÐX^ÑK_ÔK_ÐB`Ð4`Ð4`ÐejÐerÝ˜KÐiÐiÐ`fÐiÐiÐiÑjÔjÐjà€GØ€GØð QÕ1Ñ3Ô3ð QØˆØˆˆå�#œ& ,Ñ/Ô/ÐrµJ¸vÔ?QÐR]Ô?^Õ`cÔ`fÔ`qÑ4rÔ4rˆÝ˜VÔ/°Ô<½c¼fÔ>OÑPÔPˆàð .8�'ñ .8ØÔ" ;Ô/ˆØŒ<Ô Ò&Ñ&Øˆ}Ø%ŸLšL¨Ñ0Ô0�	�	Ý˜E¥5¤<Ñ0Ô0ð >Ø!ŸHšH U™OœO�	Ø”;¥%¤*Ò,Ð,Ý+2¬=½Ô9K×9SÒ9SÐTbÑ9cÔ9cÑ+dÔ+dÕgnÔgtØ ñhô hò ,Ð(ð 0ð Ý(ð}ñô ð øõ
 "œL¨°uÐ=Ñ=Ô=�	õ ˜&Ô+­VÑ4Ô4ð (¸Ð9PØ%œK�	àÔ'ˆFØð cÝœFÔ-¨iÐWÐWÀuÐWÐPVÐWÐW×ZÒZÐ[aÑbÔb�	�	Øð cÝœFÔ-¨iÐWÐWÀuÐWÐPVÐWÐW×ZÒZÐ[aÑbÔb�	à.7ˆFÔ˜{Ñ+ØÐ*Ý˜œ u¨o×.@Ò.@ÀÑ.HÔ.HÑIÔIÐIÐIÐIð= 'Ð&ð: +Ð*ð ˆ=Ø!Ÿš VÑ,Ô,ˆIˆIÝ˜�uœ|Ñ,Ô,ð 	;ØŸš Ñ(Ô(ˆIˆIåœ U°6Ð:Ñ:Ô:ˆIàð 	8Ø+4ˆFŒO˜KÑ(Ð(Ð(åœ Y¸iÔ>UÐVÑVÔVˆIØ.7ˆFÔ˜{Ñ+Ð+Ð+ó    Fc           
      ó`  ‡— |                       ¦   «         D �]\  }}‰€g Š‰                     |¦  «         t          |t          j        ¦  «        st          |t
          ¦  «        �rg||v�rbt          ˆfd„|D ¦   «         ¦  «        �sFt          ¦   «         5  t          |t
          ¦  «        r|j        j	        \  }}n|j
        }|j        }|                     ¦   «         dk    r@t          j                             |||j        du|j        |j        ¬¦  «        | j        |<   d}nV|j        �
||j        v rnEt          j                             |||j        du|j        |j        |j        ¬¦  «        | j        |<   d}t1          |¦  «        | j        |         _        | j        |                              d¦  «         ddd¦  «         n# 1 swxY w Y   t7          t9          |                     ¦   «         ¦  «        ¦  «        dk    rt=          ||‰||¬	¦  «        \  }	}‰                     d
¦  «         �Œ| |fS )z¶
    Private method that wraps the recursion for module replacement.

    Returns the converted model and a boolean that indicates if the conversion has been successfull or not.
    Nc              3   óF   •K  — | ]}|d                       ‰¦  «        v V — ŒdS )r   N)Újoin)Ú.0ÚkeyÚcurrent_key_names     €r@   ú	<genexpr>z+_replace_with_bnb_linear.<locals>.<genexpr>†   s5   øè è € Ð[Ð[¸S�s˜cŸhšhÐ'7Ñ8Ô8Ð8Ð[Ð[Ð[Ð[Ð[Ð[rB   Úllm_int8)Úhas_fp16_weightsÚ	thresholdT)Úcompress_statisticsÚ
quant_typeFr   )Úhas_been_replacedr   ) Únamed_childrenÚappendr    r   ÚLinearr	   Úanyr
   r0   ÚshapeÚin_featuresÚout_featuresÚquantization_methodr   ÚLinear8bitLtÚbiasÚllm_int8_has_fp16_weightÚllm_int8_thresholdÚ_modulesÚllm_int8_skip_modulesÚ
Linear4bitÚbnb_4bit_compute_dtypeÚbnb_4bit_use_double_quantÚbnb_4bit_quant_typer"   r,   Úrequires_grad_ÚlenÚlistÚchildrenÚ_replace_with_bnb_linearÚpop)
ÚmodelÚmodules_to_not_convertrH   Úquantization_configrO   Únamer2   rU   rV   Ú_s
     `       r@   rf   rf   w   s¢  ø€ ð ×,Ò,Ñ.Ô.ð 5!ñ 5!‰ˆˆfØÐ#Ø!ÐØ×Ò Ñ%Ô%Ð%å�v�rœyÑ)Ô)ð &	?­Z¸ÅÑ-GÔ-Gñ &	?ÈTÐYoÐMoÑMoåÐ[Ð[Ð[Ð[ÐDZÐ[Ñ[Ô[Ñ[Ô[ñ $?Ý'Ñ)Ô)ð #?ð #?Ý! &­&Ñ1Ô1ð ;Ø4:´MÔ4GÑ1˜ \ \à&,Ô&8˜Ø'-Ô':˜à*×>Ò>Ñ@Ô@ÀJÒNÐNÝ/2¬v×/BÒ/BØ'Ø(Ø"œK¨tÐ3Ø-@Ô-YØ&9Ô&Lð 0Cñ 0ô 0˜œ tÑ,ð -1Ð)Ð)ð 0ÔEÐQØ $Ð(;Ô(QÐ QÐ Qà å36´6×3DÒ3DØ +Ø ,Ø &¤°4Ð 7Ø 3Ô JØ4GÔ4aØ+>Ô+Rð 4Eñ 4ô 4˜EœN¨4Ñ0ð 15Ð-å6:¸6±l´l�E”N 4Ô(Ô3à”N 4Ô(×7Ò7¸Ñ>Ô>Ð>ðG#?ð #?ð #?ñ #?ô #?ð #?ð #?ð #?ð #?ð #?ð #?øøøð #?ð #?ð #?ð #?õH �t�F—O’OÑ%Ô%Ñ&Ô&Ñ'Ô'¨!Ò+Ð+Ý#;ØØ&Ø Ø#Ø"3ð$ñ $ô $Ñ ˆAÐ ð 	×Ò˜RÑ Ô Ð Ñ ØÐ#Ð#Ð#s   ÂD!GÇG	ÇG	c                 óv   — |€dgn|}t          | |||¦  «        \  } }|st                               d¦  «         | S )a6  
    A helper function to replace all `torch.nn.Linear` modules by `bnb.nn.Linear8bit` modules from the `bitsandbytes`
    library. This will enable running your models using mixed int8 precision as described by the paper `LLM.int8():
    8-bit Matrix Multiplication for Transformers at Scale`. Make sure `bitsandbytes` compiled with the correct CUDA
    version of your hardware is installed before running this function. `pip install -i https://test.pypi.org/simple/
    bitsandbytes`

    The function will be run recursively and replace all `torch.nn.Linear` modules except for the `lm_head` that should
    be kept as a `torch.nn.Linear` module. The replacement is done under `init_empty_weights` context manager so no
    CPU/GPU memory is required to run this function. Int8 mixed-precision matrix decomposition works by separating a
    matrix multiplication into two streams: (1) and systematic feature outlier stream matrix multiplied in fp16
    (0.01%), (2) a regular stream of int8 matrix multiplication (99.9%). With this method, int8 inference with no
    predictive degradation is possible for very large models (>=176B parameters).

    Parameters:
        model (`torch.nn.Module`):
            Input model or `torch.nn.Module` as the function is run recursively.
        modules_to_not_convert (`List[`str`]`, *optional*, defaults to `["lm_head"]`):
            Names of the modules to not convert in `Linear8bitLt`. In practice we keep the `lm_head` in full precision
            for numerical stability reasons.
        current_key_name (`List[`str`]`, *optional*):
            An array to track the current key of the recursion. This is used to check whether the current key (part of
            it) is not in the list of modules to not convert (for instances modules that are offloaded to `cpu` or
            `disk`).
    NÚlm_headzÀYou are loading your model in 8bit or 4bit 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.)rf   ÚloggerÚwarning)rh   ri   rH   rj   rO   s        r@   Úreplace_with_bnb_linearrq   ¸   sd   € ð4 -CÐ,J˜i˜[˜[ÐPfÐÝ7ØÐ%Ð'7Ð9Lñ ô  Ñ€EÐð ð 
Ý�Šðñ	
ô 	
ð 	
ð €LrB   c                  óN   — t          j        dt          ¦  «         t          | i |¤ŽS )Nzj`replace_8bit_linear` will be deprecated in a future version, please use `replace_with_bnb_linear` instead)ÚwarningsÚwarnÚFutureWarningrq   ©Úargsr?   s     r@   Úreplace_8bit_linearrx   â   s1   € Ý„MØtÝñô ð õ # DÐ3¨FÐ3Ð3Ð3rB   c                  óN   — t          j        dt          ¦  «         t          | i |¤ŽS )Nz…`set_module_8bit_tensor_to_device` will be deprecated in a future version, please use `set_module_quantized_tensor_to_device` instead)rs   rt   ru   rA   rv   s     r@   Ú set_module_8bit_tensor_to_devicerz   ë   s4   € Ý„Mð 	PÝñô ð õ 1°$ÐA¸&ÐAÐAÐArB   c                 óh  ‡— t          | ¦  «        }|                     ¦   «          t          |¦  «        }t          |t          ¦  «        rRt          t          |                     ¦   «         ¦  «        g ¦  «        t          |                     ¦   «         ¦  «        z   }nt          |g ¦  «        }t          |¦  «        dk    }|s8|  
                    ¦   «         Š‰�"ˆfd„|                      ¦   «         D ¦   «         }|S t          |                      ¦   «         ¦  «        }|d         d         g}t          |¦  «        t          |¦  «        z
  }t          t          |¦  «        ¦  «        t          |¦  «        z   }ddg}	g }
|D ]6}|	D ]}||v r|                     |d¦  «        }Œ|
                     |¦  «         Œ7|
S )aº  
    An utility function to get the key of the module to keep in full precision if any For example for CausalLM modules
    we may want to keep the lm_head in full precision for numerical stability reasons. For other architectures, we want
    to keep the tied weights of the model. The function will return a list of the keys of the modules to not convert in
    int8.

    Parameters:
    model (`torch.nn.Module`):
        Input model
    r   Nc                 óZ   •— g | ]'\  }}t          |¦  «        t          ‰¦  «        k    ¯%|‘Œ(S © )Úid)rF   rk   r2   Ú
output_embs      €r@   ú
<listcomp>z+get_keys_to_not_convert.<locals>.<listcomp>  s;   ø€ ÐoÐoÐo©¨¨vÕRTÐU[ÑR\ÔR\Õ`bÐcmÑ`nÔ`nÒRnÐRn ÐRnÐRnÐRnrB   r   z.weightz.biasÚ )r   Útie_weightsr   r    ÚdictÚsumrd   ÚvaluesÚkeysrc   Úget_output_embeddingsÚnamed_modulesÚnamed_parametersÚsetÚreplacerQ   )rh   Ú
tied_modelÚtied_paramsÚ	tied_keysÚhas_tied_paramsÚlist_last_moduleÚlist_modulesÚintersectionÚlist_untouchedÚnames_to_removeÚfiltered_module_namesrk   Úname_to_remover   s                @r@   Úget_keys_to_not_convertr—   ó   s¾  ø€ õ ˜%‘”€JØ×ÒÑÔÐå& zÑ2Ô2€Kå�+�tÑ$Ô$ð )Ý�˜[×/Ò/Ñ1Ô1Ñ2Ô2°BÑ7Ô7½$¸{×?OÒ?OÑ?QÔ?QÑ:RÔ:RÑRˆ	ˆ	å˜ RÑ(Ô(ˆ	Ý˜)‘n”n qÒ(€Oð ð $Ø×0Ò0Ñ2Ô2ˆ
ØÐ!ØoÐoÐoÐo¸×9LÒ9LÑ9NÔ9NÐoÑoÔoÐØ#Ð#õ ˜×.Ò.Ñ0Ô0Ñ1Ô1€LØ$ RÔ(¨Ô+Ð,ÐåÐ'Ñ(Ô(­3¨y©>¬>Ñ9€LÝ�#˜i™.œ.Ñ)Ô)­D°Ñ,>Ô,>Ñ>€Nð ! 'Ð*€OØÐØð +ð +ˆØ-ð 	8ð 	8ˆNØ Ð%Ð%Ø—|’| N°BÑ7Ô7�øØ×$Ò$ TÑ*Ô*Ð*Ð*à Ð rB   )NN)NNNF)NNN)Úimportlib.metadatar(   rs   Úcopyr   Ú	packagingr   Úutilsr   r   r   r   r   r   Útorch.nnr   Úpytorch_utilsr	   Ú
accelerater
   Úaccelerate.utilsr   Ú
get_loggerÚ__name__ro   rA   rf   rq   rx   rz   r—   r}   rB   r@   ú<module>r¢      ss  ðØ Ð Ð Ð Ø €€€Ø Ð Ð Ð Ð Ð à Ð Ð Ð Ð Ð à OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ Oð ÐÑÔð 'ØÐÐÐØ€L€L€LØÐÐÐÐÐà&Ð&Ð&Ð&Ð&Ð&àÐÑÔð 6Ø-Ð-Ð-Ð-Ð-Ð-Ø5Ð5Ð5Ð5Ð5Ð5à	ˆÔ	˜HÑ	%Ô	%€ð\8ð \8ð \8ð \8ð@ lqð>$ð >$ð >$ð >$ðB&ð &ð &ð &ðT4ð 4ð 4ðBð Bð Bð/!ð /!ð /!ð /!ð /!rB   