a
    d^1                     @   s   d dl Z d dlmZ d dlmZ ddlmZ ddlmZm	Z	m
Z
 e
 rbd dlZd dlZd dlmZ e	 rd dlmZ d d	lmZ eeZdd
dZdddZdddZdd Zdd Zdd ZdS )    N)deepcopy)version   )logging   )importlib_metadatais_accelerate_availableis_bitsandbytes_available)init_empty_weights)find_tied_parametersc                 C   s  d|v rV| d}|dd D ].}t| |}|du rHt|  d| d|} q|d }|| jvr~|| jvr~t|  d| d|| jv }t| |}	|	jtdkr|dtdfvr|du rt| d| dd}
d}|st sd}d}
n6tt	j
d	ot| j| t	j
j}
t| j| t	j
j}|s.|
r4| j| }|jjd
kr|du r\|	|}n^t|tjr|d}|jtjkrttdtdk}|stdntj|dd}|	j}|rt	j
j|fddi||}n$|
r
t	j
j|fddi||}|| j|< |durt| jd|| nj|du rJ|	|}n(t|tjrd||}ntj||d}|r|| j|< nt
j||	jd}|| 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.
    .Nz 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
Params4bitZcudacpubitsandbytesz0.37.2zDetected 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_gradZSCB)r   )splitgetattr
ValueError_parameters_buffersr   torchr	   hasattrbnbnn
isinstancer   Z
Int8ParamstypetoZTensorZdtypeZint8r   parser   Ztensor__dict__setattrZweight	Parameterr   )moduleZtensor_namer   valueZfp16_statisticssplitsr   Z
new_moduleZ	is_buffer	old_valueZis_4bitZis_8bitparam	new_valueZis_8bit_serializablekwargs r+   h/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/transformers/utils/bitsandbytes.py%set_module_quantized_tensor_to_device   sl    



*
"


 


r-   Fc              
      sT  |   D ]@\}} du rg   | t|tjr||vrt fdd|D st  | dkrtjj	|j
|j|jdu|j|jd| j|< d}nH|jdur||jv rn2tjj|j
|j|jdu|j|j|jd| j|< d}| j| d W d   n1 s
0    Y  tt| d	kr@t|| ||d
\}} d q| |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   s   | ]}|d   v V  qdS )r   N)join).0keycurrent_key_namer+   r,   	<genexpr>       z+_replace_with_bnb_linear.<locals>.<genexpr>Zllm_int8)Zhas_fp16_weights	thresholdT)Zcompress_statisticsZ
quant_typeFr   )has_been_replacedr   )Znamed_childrenappendr   r   ZLinearanyr
   Zquantization_methodr   ZLinear8bitLtZin_featuresZout_featuresZbiasZllm_int8_has_fp16_weightZllm_int8_thresholdZ_modulesZllm_int8_skip_modulesZ
Linear4bitZbnb_4bit_compute_dtypeZbnb_4bit_use_double_quantZbnb_4bit_quant_typeZrequires_grad_lenlistchildren_replace_with_bnb_linearpop)modelmodules_to_not_convertr2   quantization_configr6   namer$   _r+   r1   r,   r<   p   sR    
0
r<   c                 C   s6   |du rdgn|}t | |||\} }|s2t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`).
    NZlm_heada  You are loading your model in 8bit or 4bit but no linear modules were found in your model. this can happen for some architectures such as gpt2 that uses Conv1D instead of Linear layers. Please double check your model architecture, or submit an issue on github if you think this is a bug.)r<   loggerwarning)r>   r?   r2   r@   r6   r+   r+   r,   replace_with_bnb_linear   s    rE   c                  O   s   t 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FutureWarningrE   argsr*   r+   r+   r,   replace_8bit_linear   s
    rK   c                  O   s   t 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)rF   rG   rH   r-   rI   r+   r+   r,    set_module_8bit_tensor_to_device   s
    rL   c                 C   s   t | }|  t|}t|tr0t| }ntdd |D g }t|dk}t	| | j
 }|sj|rjg S t|  }|d d g}t|t| }|t| }	ddg}
g }|	D ],}|
D ]}||v r||d}q|| q|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
    c                 S   s   g | ]}|d d qS )r   Nr+   )r/   xr+   r+   r,   
<listcomp>   r4   z+get_keys_to_not_convert.<locals>.<listcomp>r   r   z.weightz.bias )r   Ztie_weightsr   r   dictr:   valuessumr9   r   Zbase_model_prefixZnamed_parameterssetreplacer7   )r>   Z
tied_modelZtied_paramsZ	tied_keysZhas_tied_paramsZis_base_modelZlist_modulesZlist_last_moduleintersectionZlist_untouchedZnames_to_removeZfiltered_module_namesrA   Zname_to_remover+   r+   r,   get_keys_to_not_convert   s,    
rV   )NN)NNNF)NNN)rF   copyr   	packagingr   utilsr   Zimport_utilsr   r   r	   r   r   r   Ztorch.nnr   Z
accelerater
   Zaccelerate.utilsr   Z
get_logger__name__rC   r-   r<   rE   rK   rL   rV   r+   r+   r+   r,   <module>   s&   

[ 
9
+	