
    gD                    R
   d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
mZmZ d dlmZmZmZmZmZmZ d dlZd dlmZ ddlmZ ddlmZmZ ddlmZmZmZ dd	l m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z' dd
l(m)Z)m*Z* ddl+m,Z,m-Z-m.Z. ddl/m0Z0m/Z/ ddl1m2Z2m3Z3  e$d          rd dl4Z4 e!d          rd dl5Z5 e#d          rd dl6Z6d dl7m8Z8 d dl9m:Z; dZ< ej=        e>          Z?d Z@d ZAdeeBeCf         fdZDdejE        fdZFdejG        deejH        eBeBf         fdZI	 	 	 	 d_dejJ        deCdeeBeCejH        f         deejG                 deeeCejE        f                  d eejK                 d!eeeBeejH        ejG        f         f                  fd"ZL	 d`dejJ        d$eMd%eMd&eMfd'ZNdadejJ        d%eMfd(ZO G d) d*eP          ZQd+ejJ        fd,ZRd- ZSd. ZT	 	 	 dbdej        jJ        d0eeej        jJ                          d1eCd2eMfd3ZUdcdej        jJ        d2eMfd4ZVd+ej        jJ        fd5ZWd6 ZXdeeCejH        f         dejE        fd7ZY	 	 	 ddd+ejJ        deeeCejH        f                  d8eeeCeeCejH        f         f                  d9eMfd:ZZ	 	 ded+ejJ        deeeCejH        f                  d8eeeCeeCejH        f         f                  fd;Z[d<eeeCej        jJ        f                  d=eeCeBf         d>eeC         fd?Z\dfd@eeeeBeCf         eeBeCf         f                  fdAZ]dgdBeeCeeBeCejH        f         f         dCeCfdDZ^dE Z_dF Z`	 	 	 	 	 dhd+ejJ        d@eeeeBeCf         eeBeCf         f                  d>eeeC                  deeeCejE        f                  d8eeeCeeCejH        f         f                  dGeMfdHZad+ej        jJ        fdIZb	 	 	 	 	 	 	 did+ejJ        d@eeeeBeCf         eeBeCf         f                  d>eeeC                  deeeCejE        f                  d8eeeCeeCejE        f         f                  dJeMdKeMdLeMfdMZcd+ejJ        dBeeCeeBeCejH        f         f         fdNZddfdOZed+ejJ        fdPZf	 djdejJ        dCeCdReeCeeCejg        f         f         dSeeBeCejH        f         fdTZh	 	 	 	 	 	 	 	 dkd+ejJ        dUeeCeji        f         dBeeeCeeBeCejH        f         f                  dVeeeCeji        f                  deeeCejE        f                  dWeMdLeMdXeeC         dYeMdZeMfd[Zjdld\eMd]efd^ZkdS )m    N)OrderedDictdefaultdict)DictListOptionalSetTupleUnion   )AcceleratorState   )SAFE_WEIGHTS_NAMEWEIGHTS_NAME)AutocastKwargsCustomDtypeDistributedType)is_mlu_availableis_mps_availableis_musa_availableis_npu_availableis_peft_availableis_torch_xla_availableis_xpu_available)clear_device_cacheget_xpu_available_memory)load_offloaded_weightoffload_weightsave_offload_index)is_tqdm_availabletqdm)compare_versionsis_torch_versionF)check_device)	safe_open)	load_filezpytorch_model.bin.index.jsonc                     ddl m} t                      rddlm} t                      ot           ||           |          S )Nr   )extract_model_from_parallelr   )	PeftModel)otherr'   r   peftr(   
isinstance)modelr'   r(   s      U/var/www/html/ai-engine/env/lib/python3.11/site-packages/accelerate/utils/modeling.pyis_peft_modelr.   C   s\    222222 #""""""\:.I.I%.P.PR[#\#\\    c                     | j         |j         k    rdS | j         dk    r| j        t          j        dd          } |j         dk    r|j        t          j        dd          }| |k    S )ar  
    Utility method to check if two `torch` devices are similar. When dealing with CUDA devices, torch throws `False`
    for `torch.device("cuda") == torch.device("cuda:0")` whereas they should be the same

    Args:
        first_device (`torch.device`):
            First device to check
        second_device (`torch.device`):
            Second device to check
    FcudaNr   index)typer3   torchdevice)first_devicesecond_devices     r-   check_device_samer9   L   s}     M...uF""|'9'A |F!444V##(;(C V1555=((r/   sizec                    d}d|  d}	 t          | t                    r| }n0|                                                     d          r)t          t	          | dd                   dz            }n|                                                     d          r)t          t	          | dd                   d	z            }n|                                                     d
          r)t          t	          | dd                   dz            }n@|                                                     d          rDt          t	          | dd                   dz            }|                     d          r|dz  n|}n|                                                     d          rDt          t	          | dd                   dz            }|                     d          r|dz  n|}nj|                                                     d          rCt          t	          | dd                   dz            }|                     d          r|dz  n|}n# t
          $ r t          |          w xY w|dk     rt          |          |S )a(  
    Converts a size expressed as a string with digits an unit (like `"5MB"`) to an integer (in bytes).

    Args:
        size (`int` or `str`): The size to convert. Will be directly returned if an `int`.

    Example:

    ```py
    >>> convert_file_size_to_int("1MiB")
    1048576
    ```
    z`size` z] is not in a valid format. Use an integer for bytes, or a string with an unit (like '5.0GB').GIBNi   @MIBi   KIBi   GBi ʚ;b   MBi@B KBi  r   )r+   intupperendswithfloat
ValueError)r:   mem_sizeerr_msgint_sizes       r-   convert_file_size_to_intrO   g   sm    Hu$uuu "dC   	IHHZZ\\""5)) 	I5crc++u566HHZZ\\""5)) 	I5crc++u566HHZZ\\""5)) 
	I5crc++u566HHZZ\\""4(( 	I5crc++u566H(,c(:(:Hx1}}HHZZ\\""4(( 	I5crc++u566H(,c(:(:Hx1}}HHZZ\\""4(( 	I5crc++u566H(,c(:(:Hx1}}H " " "!!!" !||!!!Os   I	I I.dtypec                    | t           j        k    rdS | t          j        k    rdS | t          j        k    rdS | t          j        k    rdS t          dd          r| t           j        k    rdS t          j	        dt          |                     }|t          d	|  d
          t          |                                d                   }|dz  S )z
    Returns the size (in bytes) occupied by one parameter of type `dtype`.

    Example:

    ```py
    >>> dtype_byte_size(torch.float32)
    4
    ```
    g      ?g      ?g      ?r   z>=z2.1.0z[^\d](\d+)$Nz`dtype` is not a valid dtype: .r   rD   )r5   boolr   INT2INT4FP8r"   float8_e4m3fnresearchstrrK   rG   groups)rP   
bit_searchbit_sizes      r-   dtype_byte_sizer^      s     
u	+"	"	"u	+"	"	"u	+/	!	!q	$	(	( Ue6I-I-Iq>3u::66JB%BBBCCC:$$&&q)**Hq=r/   tensorreturnc                    t           j        dt           j        dt           j        dt           j        dt           j        dt           j        dt           j        dt           j        dt           j	        dt           j
        di
}	 |                                                                 }|                                                                 }n# t          $ r 	 |                                                                 }|                                                                 || j                 z  }n4# t$          $ r' d}|                                 || j                 z  }Y nw xY wY nw xY w| j        ||fS )a  
    Unique identifier to a tensor storage. Multiple different tensors can share the same underlying storage. For
    example, "meta" tensors all share the same storage, and thus their identifier will all be equal. This identifier is
    guaranteed to be unique and constant for this tensor's storage during its lifetime. Two tensor storages with
    non-overlapping lifetimes may have the same id.
    rD      r   r   r   )r5   int64float32int32bfloat16float16int16uint8int8rS   float64untyped_storagedata_ptrnbytes	Exceptionstorager:   rP   NotImplementedErrornelementr6   )r_   _SIZEstorage_ptrstorage_sizes       r-   id_tensor_storagerv      sR    	QqQqQQ
A
AqEC,,..7799--//6688 	C 	C 	C	C ..**3355K!>>++0022U6<5HHLL" 	C 	C 	CK!??,,uV\/BBLLL		C	C =+|33s8   <AC	 	
E&AD/.E&/.E E&E  E&%E&moduletensor_namer6   valuefp16_statisticstied_params_mapc                    d|v rS|                     d          }|dd         D ]+}t          | |          }	|	t          |  d| d          |	} ,|d         }|| j        vr|| j        vrt          |  d| d          || j        v }
t          | |          }|^|\|                                |v rF|||                                         v r*||                                         |         | j        |<   dS |\|                                |v rF|||                                         v r*||                                         |         | j        |<   dS |j        t          j        d          k    r/|dt          j        d          fvr|t          | d| d          || j        v r| j        |         nd}t          |          }||j	        |j	        k    r.|j
        dk    r#t          d	|j	         d
| d|j	         d          ||                    |j                  }n<t          |j                                      d          s|                    |          }d}t          j                    5  |:|j        j        dk    r*t          j        |          j        dk    r|j
        dv r|}d}t!          |t"                    rOt%                      rd| }n;t'                      rd| }n't)                      rd| }nt+                      rd| }dt          |          v r t+                      st          | d          ||                    |          }|p|dt          j        d          fv rXt          |j                                      d          s|                    |          }|
s |||j                  | j        |<   nFt!          |t          j                  r|                    |          }nt          j        ||          }||}|
r|| j        |<   n|3t3          t          j        |          | j        |         j                  st          | j        |                   }| j        |         j        }|j
        dv r|j
        dk    r4|j        t          j        k    r|                    t          j                  }|dk    r|j
        dk    ru ||fd|j        i|                    d                              d          }|j                            d          |_        |j                            d          |_        n ||fd|j        i|                    |          }n|j
        dv r:t          j                             ||j                                      |          }n|j
        dv rft          j                              ||j!        |j"        |j	        |j#        |j$        |j%                  |j                                      |          }n% |||j                                      |          }|| j        |<   |&|                    |          | j        |         _        ~| j&        j
        dk    rt          | j'        dd          t          | j'        j                  dk    rt          j        |          j        dk    rt          j        |          j(        nd}t          | j'        dd          sP|N| j)        +| j)        j        j        dk    r| *                    |          } n| j)        | *                    |          } n| j&        j
        d k    rt          | j'        d!d          t          | j'        j                  dk    rot          j        |          j        dk    rt          j        |          j(        nd}t          | j'        d!d          s!|| j'        *                    |          | _'        ddd           n# 1 swxY w Y   |dk    rtW                       |Q|                                |v r;|||                                         vr|||                                         |<   dS |S|S|                                |v r?|||                                         vr%|||                                         |<   dS dS dS dS 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).

    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).
        dtype (`torch.dtype`, *optional*):
            If passed along the value of the parameter will be cast to this `dtype`. Otherwise, `value` will be cast to
            the dtype of the existing parameter in the model.
        fp16_statistics (`torch.HalfTensor`, *optional*):
            The list of fp16 statistics to set on the module, used for 8 bit model serialization.
        tied_params_map (Dict[int, Dict[torch.device, torch.Tensor]], *optional*, defaults to `None`):
            A map of current data pointers to dictionaries of devices to already dispatched tied weights. For a given
            execution device, this parameter is useful to reuse the first available pointer of a shared weight on the
            device for all others, instead of duplicating memory.
    rR   Nr<   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 
Params4bitz Trying to set a tensor of shape z in "z" (which has shape z), this looks incorrect.z
torch.uintz	torch.intz
torch.boolr1   )
Int8Params	FP4Paramsr~   cpuznpu:zmlu:zmusa:xpu:xpuz6 is not available, you should use device="cpu" instead)requires_gradr6   r   r   r   )QTensorQBitsTensor)AffineQuantizedTensorLinear8bitLtSCB
Linear4bitquant_state),splitgetattrrK   _parameters_buffersrm   r6   r5   r4   shape__name__torP   rZ   
startswithno_gradr+   rG   r   r   r   r   r   Tensorr_   r9   __dict__rd   rg   CBr   nn	Parameterlayout_tensor
block_size	quant_min	quant_maxzero_point_domain	__class__weightr3   biasr1   r   )rw   rx   r6   ry   rP   rz   r{   splitsr   
new_module	is_buffer	old_valueparam	param_clsdevice_quantization	new_valuekwargsdevice_indexs                     r-   set_module_tensor_to_devicer      s	   B k""3''CRC[ 	  	 E //J! F!F!Fe!F!F!FGGGFFRj&,,,FO1S1SF__Q\___```v.I,,I
 	'NN//oenn&6&6777*9%..:J:J*KF*S;'#  O33oi&8&8&:&:;;;*9):L:L:N:N*OPV*W;'5<////F65<X^K_K_B`4`4`ejerKii`fiiijjj/:f>P/P/PF{++VZEUI ?ek))i.@L.P.P O5;  O  O[  O  Oenet  O  O  O   =HHY_--EEU[!!,,-VWW 	$HHUOOE	 aE aE !V++V$$)V33"&OOO"(Ffc"" 	)!! )(!## )("$$ ))))!## )(CKK(8(:(:^^^___=!V,,I VV8L8L/M%M%M9?++667`aa 4 )U 3 3I  r6?i	YbYp6q6q6qF&{3u|,, 	;((IIU6:::I*(F <	E+4FOK((&7V8L8LfN`alNmNt&u&uV/<==I'4=F!%NNN%55)/U]:Z:Z )U] ; ;IU??y'9\'I'I )	) e e9CZ e^d e e h hij k k n not u uI#,<??5#9#9IL$-M$4$4U$;$;IMM )	) e e9CZ e^d e e h hio p pII#'AAA!H..y	H_.``ccdjkk		#'@@@!H..I!/!,!!+!+!3  #,"9 / 
 
 "V** 	 &Iiy?VWWWZZ[abb	.7F{+*6E6H6H6P6P";/3#  )^;;FM5$77?,--77 >C\&=Q=Q=VZ`=`=`u|F3399fjv}eT:: ;|?W{.6;3E3Jf3T3T!'\!:!:,!'\!:!: )\99FM=$??G,--77 >C\&=Q=Q=VZ`=`=`u|F3399fjv}mTBB E|G_$*M$6$6|$D$DFMCaE aE aE aE aE aE aE aE aE aE aE aE aE aE aEF 
 	#  O33/)*<*<*>*>???8A	**,,-f555'NN///%..*:*:;;;4=(()&111 	''//;;s   7V$`''`+.`+Tinclude_buffersrecurseremove_non_persistentc              #      K   |                      |          E d{V  |rGt                      }|rt          | |          }|                     |          D ]}|\  }}||vr|V  dS dS )a?  
    A helper function that gathers all the tensors (parameters + buffers) of a given module. If `include_buffers=True`
    it's the same as doing `module.named_parameters(recurse=recurse) + module.named_buffers(recurse=recurse)`.

    Args:
        module (`torch.nn.Module`):
            The module we want the tensors on.
        include_buffer (`bool`, *optional*, defaults to `True`):
            Whether or not to include the buffers in the result.
        recurse (`bool`, *optional`, defaults to `False`):
            Whether or not to go look in every submodule or just return the direct parameters and buffers.
        remove_non_persistent (`bool`, *optional*, defaults to `False`):
            Whether or not to remove the non persistent buffer from the buffers. Useful only when include_buffers =
            True
    r   N)named_parameterssetget_non_persistent_buffersnamed_buffers)rw   r   r   r   non_persistent_buffersnamed_buffername_s           r-   named_module_tensorsr     s      $ &&w&777777777 #!$  	Y%?PW%X%X%X""000AA 	# 	#L"GD!111""""# #	# 	#r/   c                 `    | j         }|r$|                                 D ]\  }}||j         z  }|S )aV  
    Gather all non persistent buffers of a given modules into a set

    Args:
        module (`nn.Module`):
            The module we want the non persistent buffers on.
        recurse (`bool`, *optional*, defaults to `False`):
            Whether or not to go look in every submodule or just return the direct non persistent buffers.
    )_non_persistent_buffers_setnamed_modules)rw   r   non_persistent_buffers_setr   ms        r-   r   r     sN     "(!C H((** 	H 	HDAq&!*GG&&%%r/   c                   (     e Zd ZdZ fdZd Z xZS )FindTiedParametersResultz
    This is a subclass of a list to handle backward compatibility for Transformers. Do not rely on the fact this is not
    a list or on the `values` method as in the future this will be removed.
    c                 :     t                      j        |i | d S N)super__init__)selfargsr   r   s      r-   r   z!FindTiedParametersResult.__init__  s%    $)&)))))r/   c                 j    t          j        dt                     t          d | D             g           S )NzhThe 'values' method of FindTiedParametersResult is deprecated and will be removed in Accelerate v1.3.0. c                 "    g | ]}|d d         S )r   N ).0xs     r-   
<listcomp>z3FindTiedParametersResult.values.<locals>.<listcomp>  s     (((aAabbE(((r/   )warningswarnFutureWarningsum)r   s    r-   valueszFindTiedParametersResult.values  s=    v	
 	
 	
 ((4((("---r/   )r   
__module____qualname____doc__r   r   __classcell__)r   s   @r-   r   r     sQ         
* * * * *. . . . . . .r/   r   r,   c                    d}d}d}dd t          j        | j                  D             v rt          | d          o)t	          | j        dd          o|                                 }t          | d          o+t	          | j        dd          ot	          | j        dd          }t          d |                                 D                       }t          |||g          S )	z
    Check if there is any indication in the given model that some weights should be tied.

    Args:
        model (`torch.nn.Module`): The model to inspect

    Returns:
        bool: True if the model needs to have tied weights
    FPreTrainedModelc                     g | ]	}|j         
S r   )r   )r   cs     r-   r   z3check_tied_parameters_in_config.<locals>.<listcomp>  s    QQQAQZQQQr/   configtie_word_embeddingsis_encoder_decodertie_encoder_decoderc              3   6   K   | ]}t          |d           V  dS )_tie_weightsN)hasattr)r   rw   s     r-   	<genexpr>z2check_tied_parameters_in_config.<locals>.<genexpr>  s,      \\&gfn==\\\\\\r/   )	inspectgetmror   r   r   r   get_output_embeddingsanymodules)r,   has_tied_word_embeddinghas_tied_encoder_decoderhas_tied_modules       r-   check_tied_parameters_in_configr     s     $$OQQ1P1PQQQQQE8$$ .&;UCC.++-- 	  E8$$ D&:EBBD&;UCC 	!
 \\EMMOO\\\\\')A?STTTr/   c                     | |v r||          S d                     |                     d          d d                   }|| k    rt          d|  d          t          ||          S )NrR   r<   z-The `device_map` does not contain the module )joinr   rK   _get_param_device)r   
device_mapparent_params      r-   r   r     ss    
%  88EKK,,SbS122LuQQQQRRR z:::r/   c                     | D ]l}i }|D ]}t          ||          ||<   t          t          |                                                    dk    rt                              d| d           mdS )a9  
    Check if tied parameters are on the same device

    Args:
        tied_params (`List[List[str]]`):
            A list of lists of parameter names being all tied together.

        device_map (`Dict[str, Union[int, str, torch.device]]`):
            A map that specifies where each submodule should go.

    r   z*Tied parameters are on different devices: zC. Please modify your custom device map or set `device_map='auto'`. N)r   lenr   r   loggerr   )tied_paramsr   	tie_paramtie_param_devicesr   s        r-   $check_tied_parameters_on_same_devicer     s     !  	 	L 	LE'8
'K'Ke$$s$++--..//!33KKT=N T T T   r/    memoprefixremove_duplicatec              #      K   |t                      }| |vrc|r|                    |            || fV  | j                                        D ].\  }}|||rdndz   |z   }t	          ||||          E d{V  -dS dS )a7  
    Return an iterator over all modules in the network, yielding both the name of the module as well as the module
    itself. Copied from PyTorch `torch.nn.Module.named_modules` for compatability with torch < 2.0 versions with
    `remove_duplicate` option added.

    Args:
        memo (set of `torch.nn.Module`, *optional*):
            A memo to store the set of modules already added to the result
        prefix (`str`, *optional*):
            A prefix that will be added to the name of the module
        remove_duplicate (`bool`, *optional*):
            Whether to remove the duplicated module instances in the result or not

    Yields:
        (str, Module): Tuple of name and module

    Note:
        Duplicate modules are returned only once. In the following example, ``l`` will be returned only once.
    NrR   r   )r   add_modulesitems_get_named_modules)rw   r   r   r   r   
sub_modulesubmodule_prefixs          r-   r  r  #  s      2 |uuT 	HHVfn & 5 5 7 7 	` 	`D*!%)>B?$F)*d<LN^__________ 	` 	`r/   c              #   
  K   t                      }|rt          | ||          n|| fg}|D ]V\  }} | j                                        }|D ]5\  }}	|	|	|v r|r|                    |	           ||rdndz   |z   }
|
|	fV  6WdS )z
    Help yield various names + members of modules. Copied from PyTorch `torch.nn.Module.named_modules` for
    compatability with torch < 2.0 versions with `remove_duplicate` option added.
    )r   r   NrR   r   )r   r  r   r  r   )rw   r   r   r   r   r   module_prefixmemberskvr   s              r-   _get_named_parametersr
  I  s      
 55DX_w6&CSTTTTgmoufvew  ")  v$**,, 	 	DAqyAII  =$@CCbAAED'MMMM	 r/   c                    d t          | d          D             }d t          | d          D             }t          |                                          t          |                                          z
  }i }|D ]L}||         }|                                D ]-\  }}	|	|u r$||vrg ||<   ||                             |           .Mt          d |                                D                       S )a  
    Find the tied parameters in a given model.

    <Tip warning={true}>

    The signature accepts keyword arguments, but they are for the recursive part of this function and you should ignore
    them.

    </Tip>

    Args:
        model (`torch.nn.Module`): The model to inspect.

    Returns:
        List[List[str]]: A list of lists of parameter names being all tied together.

    Example:

    ```py
    >>> from collections import OrderedDict
    >>> import torch.nn as nn

    >>> model = nn.Sequential(OrderedDict([("linear1", nn.Linear(4, 4)), ("linear2", nn.Linear(4, 4))]))
    >>> model.linear2.weight = model.linear1.weight
    >>> find_tied_parameters(model)
    [['linear1.weight', 'linear2.weight']]
    ```
    c                     i | ]\  }}||	S r   r   r   r   r   s      r-   
<dictcomp>z(find_tied_parameters.<locals>.<dictcomp>|  s    pppKD%D%pppr/   F)r   c                     i | ]\  }}||	S r   r   r  s      r-   r  z(find_tied_parameters.<locals>.<dictcomp>  s+     % % %#ee% % %r/   Tc           
      n    g | ]2\  }}t          |gt          t          |                    z             3S r   )sortedlistr   )r   r   tieds      r-   r   z(find_tied_parameters.<locals>.<listcomp>  s8    $v$v$vLFTXVVHtCII,F%G%G$v$v$vr/   )r
  r   keysr  appendr   )
r,   r   all_named_parametersno_duplicate_named_parameterstied_param_namestied_param_groupstied_param_name
tied_param
param_namer   s
             r-   find_tied_parametersr  ]  s@   > qp;PQVin;o;o;oppp% %'<UUY'Z'Z'Z% % %!
 /446677#>[>`>`>b>b:c:cc + F F)/:
!>!D!D!F!F 	F 	FJ
""%66646%j1!*-44_EEE	F $$v$v\m\s\s\u\u$v$v$vwwwr/   c                    |D ]}d}|D ]o}| }|                     d          }|dd         D ]}t          ||          }t          ||d                   }|!|j        t          j        d          k    r|} np|P|D ]M}| }|                     d          }|dd         D ]}t          ||          }t	          ||d         |           NdS )aX  
    Reties tied parameters in a given model if the link was broken (for instance when adding hooks).

    Args:
        model (`torch.nn.Module`):
            The model in which to retie parameters.
        tied_params (`List[List[str]]`):
            A mapping parameter name to tied parameter name as obtained by `find_tied_parameters`.
    NrR   r<   r}   )r   r   r6   r5   setattr)	r,   r   
tied_groupparam_to_tier  rw   r   r   r   s	            r-   retie_parametersr"    s    " : :
$ 	 	JF%%c**F 0 0 //FF2J//E#V8L8L(L(L$#( : :
#))#..#CRC[ 4 4E$VU33FFr
L9999%: :r/   c                     t          | t                    r+|                     dd          } t          t          |           } | S )z4
    Just does torch.dtype(dtype) if necessary.
    torch.r   )r+   rZ   replacer   r5   rP   s    r-   _get_proper_dtyper'    s=     % &h++u%%Lr/   special_dtypesbuffers_onlyc                    |t          |          }t          |          }|<d |                                D             }d |                                D             }t          t                    }g }|st          | d          }n|                     d          }|D ]@\  }}	|"||v r|	                                ||         z  }
n|*|	                                t          |	j                  z  }
nt          |	j                  
                    d          r*|	                                t          |	j                  z  }
n7|	                                t          |t          |	j                            z  }
|                    d          }t          t          |          dz             D ]-}|d                    |d|                   xx         |
z  cc<   .B|S )	z>
    Compute the size of each submodule of a given model.
    Nc                 4    i | ]\  }}|t          |          S r   )r'  r   keydtyps      r-   r  z(compute_module_sizes.<locals>.<dictcomp>  s'    ___93#066___r/   c                 4    i | ]\  }}|t          |          S r   )r^   r,  s      r-   r  z(compute_module_sizes.<locals>.<dictcomp>  s&    bbbic4sOD$9$9bbbr/   Tr   r   rR   r   )r'  r^   r  r   rG   r   r   numelrP   rZ   r   minr   ranger   r   )r,   rP   r(  r)  
dtype_sizespecial_dtypes_sizemodule_sizesmodule_listr   r_   r:   
name_partsidxs                r-   compute_module_sizesr9    s    !%(($U++
!__H\H\H^H^___bb>K_K_KaKabbbs##LK 8*5$???))$)77# = =f%$.*@*@<<>>$7$==DD]<<>>OFL$A$AADD))*STT 	S <<>>OFL$A$AADD<<>>C
OFL4Q4Q$R$RRDZZ__
Z1,-- 	= 	=C*TcT"233444<4444	= r/   c                 T    t          | ||d          }|                    dd          S )zO
    Compute the total size of buffers in each submodule of a given model.
    T)rP   r(  r)  r   r   )r9  get)r,   rP   r(  r5  s       r-    compute_module_total_buffer_sizer<    s1     (U>hlmmmLB"""r/   r   r5  no_split_module_classesc                    	 d}g }|                                  }t          |          dk    r|                    d          \  	}t          |t          j        j                  r!t          |                                          ng }t          |          dk    s|j	        j
        |v r0|	         }||k    r|}	g}n-||k    r|                    	           n	fd|D             |z   }t          |          dk    ||fS )aO  
    Utility function that will scan a list of named modules and return the maximum size used by one full layer. The
    definition of a layer being:
    - a module with no direct children (just parameters and buffers)
    - a module whose class name is in the list `no_split_module_classes`

    Args:
        modules (`List[Tuple[str, torch.nn.Module]]`):
            The list of named modules where we want to determine the maximum layer size.
        module_sizes (`Dict[str, int]`):
            A dictionary mapping each layer name to its size (as generated by `compute_module_sizes`).
        no_split_module_classes (`List[str]`):
            A list of class names for layers we don't want to be split.

    Returns:
        `Tuple[int, List[str]]`: The maximum size of a layer with the list of layer names realizing that maximum size.
    r   c                 (    g | ]\  }} d | |fS rR   r   )r   nr	  module_names      r-   r   z&get_max_layer_size.<locals>.<listcomp>  s0    WWWdaK!5!5!!5!5q 9WWWr/   )copyr   popr+   r5   r   Moduler  named_childrenr   r   r  )
r   r5  r=  max_sizelayer_namesmodules_to_treatrw   modules_childrenr:   rB  s
            @r-   get_max_layer_sizerK    s&   ( HK||~~


!
#
#.22155V<Fvux<_<_g4 5 5 7 7888eg  A%%)9)BF])])],Dh*m!!"";///WWWWFVWWWZjj 

!
#
# [  r/   
max_memoryc                 N    ddl } i  t                      rt          t          j                                                  D ]}	 t          j        dt          j        d|                    }t          j                            |          d          |<   U# t          $ r! t                              d| d           Y w xY wnt                      rt          t          j                                                  D ]}	 t          j        dt          j        d|                    }t          j                            |          d          |<   U# t          $ r! t                              d| d           Y w xY wnt                      rt          t          j                                                  D ]}	 t          j        dt          j        d|                    }t          j                            |          d          |<   U# t          $ r! t                              d| d           Y w xY wnCt!                      rt          t          j                                                  D ]l}	 t          j        dt          j        d	|                    }t%          |           |<   ?# t          $ r! t                              d| d           Y iw xY wnt          t          j                                                  D ]p}	 t          j        dg|          }t          j                            |          d          |<   C# t          $ r! t                              d| d           Y mw xY wt)                      r|                                j         d
<   n|                                j         d<    S  D ]5}t/           |         t0                    rt3           |                    |<   6d                                  D             }|                                 t                      rt          j                                        }nt                      rt          j                                        }nxt                      rt          j                                        }nKt!                      rt          j                                        }nt          j                                        }|D ]H}||k    s|dk     r:t                              d| dt;          t          |                                I| fddD             z   }                                 D ]}	|	|vrt=          d|	 d           fd|D               S )zb
    Get the maximum memory available if nothing is passed, converts string to int otherwise.
    r   Nnpur   zDevice z; seems unavailable, Proceeding to check subsequent devices.mlumusar   mpsr   c                 <    g | ]}t          |t                    |S r   r+   rG   )r   r  s     r-   r   z"get_max_memory.<locals>.<listcomp>[  s'    FFF:a3E3EF1FFFr/   z) is not available, available devices are c                 @    g | ]}|                                 v |S r   )r  r   r  rL  s     r-   r   z"get_max_memory.<locals>.<listcomp>l  s-     ] ] ]qa:??K\K\F\F\F\F\F\r/   )rQ  r   diskzX is not recognized, available devices are integers(for GPU/XPU), 'mps', 'cpu' and 'disk'c                 "    i | ]}||         S r   r   rU  s     r-   r  z"get_max_memory.<locals>.<dictcomp>s  s    888q!Z]888r/   )psutilr   r2  r5   rN  device_countr_   r6   mem_get_inforo   r   infor   rO  r   rP  r   r   r   r1   r   virtual_memory	availabler+   rZ   rO   r  sortwarningr  rK   )
rL  rX  ir   r-  gpu_devicesnum_devicesr6   all_devicesr  s
   `         r-   get_max_memoryrd    s    MMM
 '	59113344  Qu|E1/E/EFFFA$)I$:$:1$=$=a$@JqMM    KK h! h h hiiiH	  	59113344  Qu|E1/E/EFFFA$)I$:$:1$=$=a$@JqMM    KK h! h h hiiiH	    	5:224455  Qu|FA/F/FGGGA$)J$;$;A$>$>q$AJqMM    KK h! h h hiiiH	  	59113344  Qu|E1/E/EFFFA$<Q$?$?JqMM    KK h! h h hiiiH	 5:224455  aS333A$)J$;$;A$>$>q$AJqMM    KK h! h h hiiiH  	B & 5 5 7 7 AJu & 5 5 7 7 AJu H Hjos++ 	H6z#GGJsO GFjoo//FFFK 	0i,,..			 0i,,..			 0j--//			 0i,,..j--// r r[  FQJJNNpVppVZ[`al[m[mVnVnppqqq ] ] ] ],B ] ] ]]K__  Ku!uuu     9888K888Js]   AB(CCAE(FFAH(I ?I ;J==(K('K(?M(NNr   rB  c                 "   dk    rdn dfd|                                  D             }t          t          |                    dk    r2t          |          dk    rfd| D             D ]}| |= |d         | <   fd|                                 D             }t                    dk    r%t                              d                    dz   ndt          fd|D                       }|D ]}t          | |	           | S )
z]
    Cleans a device_map by grouping all submodules that go on the same device together.
    r   rR   c                 D    g | ]\  }}|                               |S r   r   )r   r  r	  r   s      r-   r   z$clean_device_map.<locals>.<listcomp>~  s.    GGGDAq!,,v2F2FGaGGGr/   r   c                 >    g | ]}|                               |S r   rg  )r   r  r   s     r-   r   z$clean_device_map.<locals>.<listcomp>  s*    @@@1<<+?+?@!@@@r/   r   c                 ~    g | ]9}|                               t          |          t                    k    7|:S r   )r   r   )r   r  rB  r   s     r-   r   z$clean_device_map.<locals>.<listcomp>  sH    kkkaV8L8LkQTUVQWQWZ]^iZjZjQjQjQjQjQjr/   c              3   x   K   | ]4}d                      |                    d           d                   V  5dS rR   N)r   r   )r   r  r8  s     r-   r   z#clean_device_map.<locals>.<genexpr>  sB      RRA388AGGCLL#$677RRRRRRr/   )rB  )r  r   r   r  r   clean_device_map)r   rB  r   r  children_moduleschildr8  r   s    `    @@r-   rl  rl  x  sY   
 "$$RR[*;*;*;FGGGGJ,,..GGGF
3v;;1Vq@@@@Z@@@ 	 	A1"()
; lkkkk:??#4#4kkk-0-=-=-A-A#k$$
%
%
)
)qCRRRRAQRRRRR! 8 877777r/   c                    |t          |          dk    rdS |                                D ]\  }}d|v r
d}d|v rw|                    dd          |                                v rM|                    dd          }t	          t
          j                            || d          ||                   }t
          j                            || d          }t	          ||          }t          | |d||           dS )a  
    Loads the weights from the offload folder into the model.

    Args:
        model (`torch.nn.Module`):
            The model to load the weights into.
        index (`dict`):
            A dictionary containing the parameter name and its metadata for each parameter that was offloaded from the
            model.
        offload_folder (`str`):
            The folder where the offloaded weights are stored.
    Nr   r   r   z.datr   )ry   rz   )	r   r  r%  r  r   ospathr   r   )	r,   r3   offload_folderr  metadatarz   weight_nametensor_filer   s	            r-   load_offloaded_weightsrv    s    }E

a % m m
HJz!!j&8&85&I&IUZZ\\&Y&Y$,,Xu==K3^-A-A-ABBE+DV O gll>j3F3F3FGG&{H==#E:uF\klllllm mr/   c                     i | D ]E}|dk    sd|vr|                     dd          d         }                    |d          dz   |<   Ffd| D             }|S )Nr   rR   r   r   c                 T    g | ]$}                     |d           d k    |dk    "|%S )r   r   )r;  )r   rw   module_childrens     r-   r   z%get_module_leaves.<locals>.<listcomp>  s@    hhh?3F3Fvq3Q3QUV3V3V[aeg[g[gf[g[g[gr/   )rsplitr;  )r5  rw   parentleavesry  s       @r-   get_module_leavesr}    s    O E ER<<3f,,sA&&q)"1"5"5fa"@"@1"Dhhhh<hhhFMr/   low_zeroc                    du }t                    t                      rdn5t                      rdn$t                      rdnt	                      rdndt          fdD                       }|dk    rS |d	k    r`d
}|r\                                D ]G}t          |t                    r0|xx         dz  cc<   t          
                    d| d            nHt          | ||          d         |r|d	z
  n|z  }	|g }nt          |t          t          f          s|g}t          |          dk    ri }
                                D ]\  }}|dk    r| }|                    d          D ]}t!          ||          }|j        j        }||v r	||
vr||
|<   t'          |
                                          t'          |          k    r nt          |
          dk    r!t)          |
                                          nd}nd}t-                    fd                                D             t-                    t          t/          fdD                       t)          t                    d	          z            }t          dt)          ||          z            }|	|z  }	t          t1          d                                 D                                 }|dd         D ]+}t3          |r|dk    rd         n|	|                   |<   ,|rYt)          dd         t/          fdt5          d	|          D                       z
            }t3          |d                   d<   S )a  
    Compute a `max_memory` dictionary for [`infer_auto_device_map`] that will balance the use of each available GPU.

    <Tip>

    All computation is done analyzing sizes and dtypes of the model parameters. As a result, the model can be on the
    meta device (as it would if initialized within the `init_empty_weights` context manager).

    </Tip>

    Args:
        model (`torch.nn.Module`):
            The model to analyze.
        max_memory (`Dict`, *optional*):
            A dictionary device identifier to maximum memory. Will default to the maximum memory available if unset.
            Example: `max_memory={0: "1GB"}`.
        no_split_module_classes (`List[str]`, *optional*):
            A list of layer class names that should never be split across device (for instance any layer that has a
            residual connection).
        dtype (`str` or `torch.dtype`, *optional*):
            If provided, the weights will be converted to that type when loaded.
        special_dtypes (`Dict[str, Union[str, torch.device]]`, *optional*):
            If provided, special dtypes to consider for some specific weights (will override dtype used as default for
            all weights).
        low_zero (`bool`, *optional*):
            Minimizes the number of weights on GPU 0, which is convenient when it's used for other operations (like the
            Transformers generate function).
    NrN  rO  rP  r   r1   c                 f    g | ]-}t          j        |          j        k    |         d k    +|.S r   )r5   r6   r4   )r   dexpected_device_typerL  s     r-   r   z'get_balanced_memory.<locals>.<listcomp>  sA    sssQQ0DH\0\0\aklmanqrararqarararr/   r   r   Fg?z(We will use 90% of the memory on device z for storing the model, and 10% for the buffer to avoid OOM. You can set `max_memory` in to a higher value to use more memory (at your own risk).rP   r(  r   rR   c                 $    i | ]\  }}|v	||S r   r   )r   rA  r	  r|  s      r-   r  z'get_balanced_memory.<locals>.<dictcomp>  s$    MMMTQQf__Aq___r/   c                      g | ]
}|         S r   r   )r   rA  r5  s     r-   r   z'get_balanced_memory.<locals>.<listcomp>!  s    ;;;q<?;;;r/   g      ?c              3   V   K   | ]$\  }}t          |t                    r
|d k     |V  %dS )r   NrS  )r   	device_id
device_mems      r-   r   z&get_balanced_memory.<locals>.<genexpr>'  sN       
 
/)ZT]_bIcIc
hruvhvhvIhvhvhvhv
 
r/   r<   c                      g | ]
}|         S r   r   )r   r`  rL  s     r-   r   z'get_balanced_memory.<locals>.<listcomp>0  s    1_1_1_A*Q-1_1_1_r/   )rd  r   r   r   r   r   r  r+   rG   r   r[  r9  r  tupler  r   r   r   r   r   maxr   r}  r   r  r1  r2  )r,   rL  r=  rP   r(  r~  user_not_set_max_memoryrb  r-  per_gpuno_split_childrenr   r:   	submodulesubmodule_name
class_namebuffermean_leavesgpus_idx_listr8  min_zeror  r|  r5  s    `                   @@@r-   get_balanced_memoryr    sb   J )D0
++J 	&$			 &$			 &%			 &$%sssss*sssttKaa" 	!((  c3'' sOOOs*OOOKKo3 o o o   E (U>ZZZL2h#O;??KPG &"$/$?? <#:"; "##a''&,,.. 	 	JD$rzzI"&**S// ? ?#I~>>		",5J444K\9\9\04!*-$))++,,4K0L0LLL M478I4J4JQ4N4N&--//000TU |,,FMMMM\%7%7%9%9MMML|,,Fc;;;;F;;;<<s3v;;PQ?R?RRSSKFK00011FvG  
 
3=3C3C3E3E
 
 
 	
 	
 M SbS! d dxQC1HHjmm'S]^aSbcc
3 5q,r*S1_1_1_1_qR]I^I^1_1_1_-`-``aaHjm44
1r/   c                 V   t          |           }t          | dd          }|g }t          |                     d                    t          |                                           z   t          |                     d                    z   }t          |||          }|d         }||fS )z:Computes the total size of the model and its largest layer_no_split_modulesNFr   r   )r9  r   r  r   rF  r   rK  )r,   sizesno_split_modulesrI  largest_layer
total_sizes         r-   calculate_maximum_sizesr  6  s     ''Eu&94@@ 	U##E#2233
u##%%
&
&	'
u""5"11
2
2	3 
 ''7@PQQMrJ}$$r/   verboseclean_resultoffload_buffersc                   *+, t          |          }|g }nt          |t          t          f          s|g}t          |                                          }d|vr|                    d           d |D             }	d|	v rdg}
n!t          |	          dk    r|	d         dg}
ndg}
t          | ||          }t          |           }t          |           r-t          |          dk    rt                              d           t                      }d}d}i }i }t          |                     d	
                    t          |                                           z   t          |                     d	
                    z   }t!          |||          \  }}t          |          dk    r|                    d          \  *}|rt%          d* d           *fd|D             }t          |          dk    rt!          d |D             ||          \  }}|*         }*fd|D             }|r%t          |          dk    rt%          d|            t'          *fd|D             g           }|r%t          |          dk    rt%          d|            ||         }|dk    r||         nd}d}||         |
v r||z
  }|}|?||z   |k    r5t          |t(          j                  st          |t,          j                  rg n t          |                                          }|r%t%          d||          d* d||z
   d| d	           t          |          dk    s|j        j        |v r*|rt%          d           ||z   ||<   |dz  }*|fg|z   }d}n|rt%          d* d           t          |                    d	
                    |z   }*fd|D             |z   }t!          d |D             ||          \  }}nt          |          dk    rg }g }|D ]e,,fdt5          |          D             d         }|                    ||         d                    |                    ||         d                    f|r!t%          d* d||          d | d!|            |} t7          ||          D ]\  ,+| |+         |,         z
  z  } |	|| z   |k    r|rt%          d"* d#| d$||          d           || z  }||         |*<   |D ]Q++d% |D             v r6+fd&t5          |          D             d         }|                    |           ||         |+<   R|sHt          |t(          j                  r.t;          |||          }!|                    |d          |!z   ||<   n|r(t%          d||          d* d#| d||z
   d'|  d           d	}"t7          ||          D ]\  +}#t          |#                                          }$t          |$          dk    s|#j        j        |v rH|rt%          d+ d           t          |#                    d	
                    |$z   }$+fd(|$D             }$+fd)t5          |          D             d         }*|fg|d|         z   |$z   ||dz   d         z   }t!          d* |D             ||          \  }}d+}" |"s(|rt%          d,           ||z   ||<   |dz  }*|fg|z   }d}n|rG| t%          d"* d-| d.||          d           n%t%          d"* d-| d.||          d/||z
   d	           ||z  }||z   ||<   ||         |*<   |sHt          |t(          j                  r.t;          |||          }!|                    |d          |!z   ||<   t          |          dk    |rt?          |          }|                    dd          |                    dd          z   }%|%dk    r{|syd	}&|                                 D ]5\  }'}(|'dk    s|'dk    r|&s!|                    |'d          })|(|%|)z   k    rd+}&6t          |	          dk    r|&stC          j        d0|% d1           |S )2a  
    Compute a device map for a given model giving priority to GPUs, then offload on CPU and finally offload to disk,
    such that:
    - we don't exceed the memory available of any of the GPU.
    - if offload to the CPU is needed, there is always room left on GPU 0 to put back the layer offloaded on CPU that
      has the largest size.
    - if offload to the CPU is needed,we don't exceed the RAM available on the CPU.
    - if offload to the disk is needed, there is always room left on the CPU to put back the layer offloaded on disk
      that has the largest size.

    <Tip>

    All computation is done analyzing sizes and dtypes of the model parameters. As a result, the model can be on the
    meta device (as it would if initialized within the `init_empty_weights` context manager).

    </Tip>

    Args:
        model (`torch.nn.Module`):
            The model to analyze.
        max_memory (`Dict`, *optional*):
            A dictionary device identifier to maximum memory. Will default to the maximum memory available if unset.
            Example: `max_memory={0: "1GB"}`.
        no_split_module_classes (`List[str]`, *optional*):
            A list of layer class names that should never be split across device (for instance any layer that has a
            residual connection).
        dtype (`str` or `torch.dtype`, *optional*):
            If provided, the weights will be converted to that type when loaded.
        special_dtypes (`Dict[str, Union[str, torch.device]]`, *optional*):
            If provided, special dtypes to consider for some specific weights (will override dtype used as default for
            all weights).
        verbose (`bool`, *optional*, defaults to `False`):
            Whether or not to provide debugging statements as the function builds the device_map.
        clean_result (`bool`, *optional*, defaults to `True`):
            Clean the resulting device_map by grouping all submodules that go on the same device together.
        offload_buffers (`bool`, *optional*, defaults to `False`):
            In the layers that are offloaded on the CPU or the hard drive, whether or not to offload the buffers as
            well as the parameters.
    NrV  c                     g | ]}|d v|	S ))r   rV  r   r   r6   s     r-   r   z)infer_auto_device_map.<locals>.<listcomp>  s"    JJJvF/,I,IF,I,I,Ir/   rQ  r   r   r  rThe model weights are not tied. Please use the `tie_weights` method before using the `infer_auto_device` function.Fr   z
Treating module rR   c                 P    g | ]"}|k    |                     d z              |#S r@  rg  )r   rA  r   s     r-   r   z)infer_auto_device_map.<locals>.<listcomp>  s5    dddd1<<X\_bXbKcKc1r/   c                 Z    g | ](\  }}t          |t          j        j                  $||f)S r   r+   r5   r   rE  r   rA  r   s      r-   r   z)infer_auto_device_map.<locals>.<listcomp>  s3    WWWDAq
1eho8V8VW!QWWWr/   c                     g | ]:}t          fd |D                       t          fd|D                       8|;S )c              3   ,   K   | ]}d z   |d z   v V  dS rk  r   r   r  r   s     r-   r   z3infer_auto_device_map.<locals>.<listcomp>.<genexpr>  s0      ==Q4#:S(======r/   c              3   ,   K   | ]}d z   |d z   v V  dS rk  r   r  s     r-   r   z3infer_auto_device_map.<locals>.<listcomp>.<genexpr>  s5      ItItde$QT*XY\_X_J_ItItItItItItr/   )r   allr   r   r   s     r-   r   z)infer_auto_device_map.<locals>.<listcomp>  sv     
 
 
====*=====
 GJItItItItisItItItFtFt

 
 
r/   z'  Found the relevant tied param groups c                 ,    g | ]}fd |D             S )c                 (    g | ]}d z   |d z   v|S r@  r   )r   pr   s     r-   r   z4infer_auto_device_map.<locals>.<listcomp>.<listcomp>  s+    AAAAtczS'@'@a'@'@'@r/   r   r  s     r-   r   z)infer_auto_device_map.<locals>.<listcomp>  s.    eeejAAAAAAAeeer/   z4  So those parameters need to be taken into account zNot enough space on z to put z (space available z, module size z).z6This module cannot be split, going to the next device.r   z
Splitting c                 (    g | ]\  }} d | |fS r@  r   )r   rA  r	  r   s      r-   r   z)infer_auto_device_map.<locals>.<listcomp>  s,    #T#T#T41a]]q]]A$6#T#T#Tr/   c                 Z    g | ](\  }}t          |t          j        j                  $||f)S r   r  r  s      r-   r   z)infer_auto_device_map.<locals>.<listcomp>  s3    [[[1Jq%(/<Z<Z[aV[[[r/   c                 (    g | ]\  }\  }}|v |S r   r   )r   r`  rA  r   r  s       r-   r   z)infer_auto_device_map.<locals>.<listcomp>  s-    $g$g$g91fq!WX\fWfWfQWfWfWfr/   z  It looks like z is going to fit on z7 but we have tied parameters to account for.
  - Names z
  - Module names zPutting z and z on c                     g | ]
}|d          S r  r   )r   r   s     r-   r   z)infer_auto_device_map.<locals>.<listcomp>  s    +K+K+KQAaD+K+K+Kr/   c                 ,    g | ]\  }\  }}|k    |S r   r   r   r`  rA  r   tied_module_names       r-   r   z)infer_auto_device_map.<locals>.<listcomp>  s-    ,u,u,u91fq!_`dt_t_tQ_t_t_tr/   z, needed size c                 (    g | ]\  }} d | |fS r@  r   )r   rA  r	  r  s      r-   r   z)infer_auto_device_map.<locals>.<listcomp>.  s2    +l+l+ltqRS0@-F-F1-F-F,J+l+l+lr/   c                 ,    g | ]\  }\  }}|k    |S r   r   r  s       r-   r   z)infer_auto_device_map.<locals>.<listcomp>/  s-    (q(q(qyq&1a[\`p[p[p[p[p[pr/   c                 Z    g | ](\  }}t          |t          j        j                  $||f)S r   r  r  s      r-   r   z)infer_auto_device_map.<locals>.<listcomp>9  s3    ___DAq
1eho@^@^_!Q___r/   Tz?None of the tied module can be split, going to the next device.z (size=z) on z (available=zCurrent model requires z bytes of buffer for offloaded layers, which seems does not fit any GPU's remaining memory. If you are experiencing a OOM later, please consider using offload_buffers=True.)"rd  r+   r  r  r  r  r   r9  r  r   r   r   r   r   rF  r   rK  rD  printr   r   r   r5   r   r   r   	enumerateziprE  r<  r;  rl  r  r   )-r,   rL  r=  rP   r(  r  r  r  devicesgpusmain_devicesr5  tied_parametersr   current_devicecurrent_memory_useddevice_memory_useddevice_buffer_sizesrI  max_layer_sizemax_layer_namesrw   module_sizetied_param_goupsr   r6   current_max_sizecurrent_memory_reservedrJ  tied_module_namestied_modulestied_module_indexmodule_size_with_tiescurrent_buffer_sizesplit_happenedtied_moduletied_module_childrennon_gpu_buffer_sizeis_buffer_fit_any_gpu
gpu_devicegpu_max_memorygpu_memory_usedr   r  r  s-                                             @@@r-   infer_auto_device_mapr  H  s   d  
++J&"$/$?? <#:";:??$$%%GWvJJJJJD }}w	TQQ'w'U>ZZZL*511O&u-- 
#o2F2F!2K2K A	
 	
 	
 JN 	U##E#2233
u##%%
&
&	'
u""5"11
2
2	3  '99I<Yp&q&q#NO 

!
#
#'++A..f 	0.t...///ddddoddd1$$.@WW$4WWW'/ /+NO #4(
 
 
 
-
 
 
  	Ps+,,q00N<LNNOOO eeeeTdeeegi
 
  	Xs;''!++VVVWWW(1761A1A:f--t"#>"l22/.@&4#',?+,MP`,`,` fbl3337A&%,7W7W3&//1122 
  ]7>+B ] ]D ] ]'*==] ]MX] ] ]   #$$))V-=-FJa-a-a TRSSS-@CZ-Z"6*!#%)6N#36F#F &'##  0.t...///#'(?(?(?(N(N#O#ORb#b #T#T#T#TCS#T#T#TWg#g 2D[[(8[[[ +3 3/ !! "L) L L
$g$g$g$g	BR8S8S$g$g$ghi$j!!(()9:K)LQ)OPPP##$45F$G$JKKKK qt q qAX q q=Hq q]nq q   %0!03KAR0S0S c c,
,%6F)G,WaJb)bb%%'+>AV+VZj+j+j caTaa0Aaaw~G^aaabbb#'<<##*>#:
4 (9 K K$'+K+K:J+K+K+KKK,u,u,u,u	JZ@[@[,u,u,u-) ),,->???3:>3JJ/00& k:fbi+H+H k*JeN+ + +' 3F2I2I&RS2T2TWj2j'/
  uw~/F u uPT u u[l u u%58K%Ku u[pu u u   "'589JL5Y5Y  1$k+/0J0J0L0L+M+M(/00A559N9W[r9r9r  @>+;>>>???+/0L0LUZ0L0[0[+\+\_s+s(+l+l+l+lWk+l+l+l((q(q(q(qIFV<W<W(q(q(qrs(t% (*+=,=+=>?./ ++<q+@+B+BCD % 7I__,<___$/7 73NO
 &*N% , a_```1DG^1^&v."a'N)-v'7:J'J$*+'  #+^T^^+^^GND[^^^____Q4 Q Q Q Q'.BY Q Q&69L&LQ Q Q    ;.)<?V)Vv&&~6Jt" gz&")'D'D g&F%' ' '# /B.E.Efa.P.PSf.f#F+m 

!
#
#p  2%j11
-11%;;>Q>U>UV\^_>`>``Q %*4*:*:*<*< 	1 	1&JU""jF&:&:( 1"4"8"8Q"G"G!%8?%JJJ,0)t99q==!6=M)*= ) ) )   r/   c                 ^   d |                                                                  D             }|                                D ],dk    r|                                  nfd|D             }-t	          |          dk    r'd                    |          }t          d|           dS )z
    Checks a device map covers everything in a given model.

    Args:
        model (`torch.nn.Module`): The model to check the device map against.
        device_map (`Dict[str, Union[int, str, torch.device]]`): The device map to check.
    c                     g | ]\  }}|S r   r   r   r   r   s      r-   r   z$check_device_map.<locals>.<listcomp>  s    HHH'$HHHr/   r   c                 P    g | ]"}|k    |                     d z              |#S r@  rg  )r   r   rB  s     r-   r   z$check_device_map.<locals>.<listcomp>  sE     ! ! !{**4??;QTCT3U3U* ***r/   r   z, zOThe device_map provided does not give any device for the following parameters: N)
state_dictr  r  clearr   r   rK   )r,   r   all_model_tensorsnon_covered_paramsrB  s       @r-   check_device_mapr  w  s     IHU-=-=-?-?-E-E-G-GHHH!(( 	 	"##%%%E! ! ! !-! ! !
 !!!YY'899r^prr
 
 	
 "!r/   c                    |                      d          rt          | d          5 }|                                }|                                }ddd           n# 1 swxY w Y   |"t                              d|  d           ddi}|                    d          dvrt          d|  d	          |d         dk    rt          d
|d          d          |t          |           S t          t          |                                                    dk    rt          |                                          d         }|}t                      r:t          ddd          rt!          d          t#          |t$                    rd| }t          | |          S t          t          |                                          dhz
            }d|vr|                    d           d |D             |                                D ]0\  }||v r'|                             fd|D                        1d                             fd|D                        i }t-                      r.t/          dt1          fd|D                       ddd          }	nd}	|D ]}|}t                      r:t          ddd          rt!          d          t#          |t$                    rd| }t          | d|          5 }|         D ]^}
|	,|	                    |d           |	                    |
           |                    |
          ||
<   |	|	                                 _	 ddd           n# 1 swxY w Y   |	|	                                 |S t=          j        | t=          j         d                    S ) a3  
    Load a checkpoint from a given file. If the checkpoint is in the safetensors format and a device map is passed, the
    weights can be fast-loaded directly on the GPU.

    Args:
        checkpoint_file (`str`): The path to the checkpoint to load.
        device_map (`Dict[str, Union[int, str, torch.device]]`, *optional*):
            A map that specifies where each submodule should go. It doesn't need to be refined to each parameter/buffer
            name, once a given module name is inside, every submodule of it will be sent to the same device.
    z.safetensorspt)	frameworkNz"The safetensors archive passed at zx does not contain metadata. Make sure to save your model with the `save_pretrained` method. Defaulting to 'pt' metadata.format)r  tfflaxzf does not contain the valid metadata. Make sure you save your model with the `save_pretrained` method.z%The checkpoint passed was saved with z, we need a the pt format.r   r   safetensors<z0.4.2zISafetensors version must be >= 0.4.2 for XPU. Please upgrade safetensors.r   r   rV  r   c                     i | ]}|g S r   r   r  s     r-   r  z#load_state_dict.<locals>.<dictcomp>  s    ???Vfb???r/   c                 P    g | ]"}|k    s|                     d z              |#S r@  rg  )r   r  rB  s     r-   r   z#load_state_dict.<locals>.<listcomp>  s;    hhhqA4D4DU`cfUfHgHg4D4D4D4Dr/   c                 \    g | ](}|t                                          g           v&|)S r   )r   r   )r   r  device_weightss     r-   r   z#load_state_dict.<locals>.<listcomp>  s;    )o)o)oQcR`RgRgRiRikmNnNnEnEn!EnEnEnr/   Fc                 :    g | ]}t          |                   S r   )r   )r   r6   r  s     r-   r   z#load_state_dict.<locals>.<listcomp>  s&    QQQvs>&#9::QQQr/   w)main_process_onlytotalunit	smoothingleave)r  r6   )devrefresh)map_location)!rI   r$   rs  r  r   r   r;  OSErrorrK   safe_load_filer   r   r   r  r   r!   ImportErrorr+   rG   r  r  extendr   r    r   set_postfixset_description
get_tensorupdatecloser5   loadr6   )checkpoint_filer   frs  weight_namesr6   target_devicer  tensorsprogress_barr-  r  rB  s              @@r-   load_state_dictr    s    // QM$777 	$1zz||H6688L	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ KKo_ o o o   !$'H<<!!)===I_ I I I   h4''sXhEWsssttt!/222 3z((**++,,11j//112215 &#%% 8'sGDD w)*uvvv!&#.. 8(7v%omLLLL3z002233vh>??GG##u%%% @?w???N'1'7'7'9'9  #VW$$"6*11hhhhLhhh  
 5!(()o)o)o)o\)o)o)opppG "" 	$#&+QQQQQQQRR       $! 2 2 &#%% 8'sGDD w)*uvvv!&#.. 8(7v$}UUU 2YZ-f5 2 2'3(444OOO(88==='(||C'8'8'3(//11122 2 2 2 2 2 2 2 2 2 2 2 2 2 2 '""$$$Nz/U8K8KLLLLs$   )AA#&A#A(M::M>	M>	c                    ddl m} i }t                      }|                                 D ]I\  }}|dk    rt	          |d          rt          |j        |          r|j        j        r|j        j        }d|j        _        	 |j        	                    |           n# t          $ r t          d          dw xY w|                                }|j                            |t          j        g                      ||j        _        n|                                }|D ]T}||         j        t          j        d          k    r|                    |d	| z              A||         }	|	||d	| z   <   UK|                                D ]}||v r|                    |           |rt&                              d
|            |S )z
    Returns the state dictionary for an offloaded model via iterative onloading

    Args:
        model (`torch.nn.Module`):
            The offloaded model we want to save
    r   AlignDevicesHookr   _hf_hookr   z;Offloaded module must fit in CPU memory to call save_model!Nr}   rR   zMThe following tensors were not saved because they were still on meta device: )hooksr  r   r   r   r+   r  offloadexecution_devicepre_forwardMemoryErrorr  post_forwardr5   r_   r6   r   rC  remover   r_  )
r,   r  r  placeholdersr   rw   original_devicemodule_state_dictr-  paramss
             r-   get_state_dict_offloaded_modelr    s    )(((((J55L++-- 2 2f2::6:&& 	4:foGW+X+X 	4]c]l]t 	4$o>O/4FO,k++F3333 k k k!"_``fjjk & 1 1 3 3O((b1A1ABBB/>FO,, & 1 1 3 3$ 	2 	2C %,V0D0DDD  	C		!1222&s+F+1Jti#ii'((	2   "" % %*$$$ wugsuuvvvs   BB8r   r  device_to_put_offloadc                    ddl m} |d|                    d                   }d}t          | d          rUt	          | j        |          r@| j        j        r4| j        j        }|| j        _        | j                            |            d}| 	                                D ]1}| 	                                |         }	|d| z   |v r|	||d| z   <   2|r9| j        
                    | t          j        g                      || j        _        |S )aL  
    Retrieve the state dictionary (with parameters) from an offloaded module and load into a specified device (defualts
    to cpu).

    Args:
        module: (`torch.nn.Module`):
            The module we want to retrieve a state dictionary from
        module_name: (`str`):
            The name of the module of interest
        state_dict (`Dict[str, Union[int, str, torch.device]]`):
            Dictionary of {module names: parameters}
        device_to_put_offload (`Union[int, str, torch.device]`):
            Device to load offloaded parameters into, defaults to the cpu.
    r   r  NrR   Fr  T)r  r  rfindr   r+   r  r  r  r  r  r  r5   r_   )
rw   rB  r  r  r  root
preforwardr  m_keyr  s
             r-   get_state_dict_from_offloadr"     s3   ( )(((((/**3///0DJvz"" z&/CS'T'T Y_YhYp  /:+@(##F+++
""$$ 4 4""$$U+;u;;:---3Jtk%kk)* ;$$VU\"-=-=>>>+:(r/   
checkpointrr  offload_state_dictkeep_in_fp32_modulesoffload_8bit_bnbstrictc
                   # |rddl m}
 t          |           }t          |           r-t	          |          dk    rt
                              d           |t          ||           |'|%d|                                v rt          d          |.|,d|                                v rt          j        |d	           t          |t                    r+|                    d
d          }t          t           |          }d}d}t          j                            |          r+t          |                              d          r|}n|g}nt          j                            |          rQd t          j        |          D             }d t          j        |          D             }t	          |          dk    r(t          j                            ||d                   g}nt	          |          dk    r(t          j                            ||d                   g}nd t          j        |          D             }t	          |          dk    r"t          | dt.           dt0           d          t	          |          dk    r't          j                            ||d                   }n%t          | d          t          d| d          |t          j                            |          d         #t5          |          5 }t7          j        |                                          }ddd           n# 1 swxY w Y   d|v r|d         }t=          t?          tA          |                                                              }#fd|D             }i }|rtC          j"                    }i }tA                      }tA          | #                                $                                          }d | %                                D             }|D ]}tM          ||          }|P| &                    ||	           |'                    tA          |$                                          |z
             n,|(                                D ]\  }}d|v r||vr|)                    |           |	s'|}t	          |          dk    rK||vrGd                    |                    d          dd                   }t	          |          dk    r||vG|dk    rd|vrt          | d          ||         }|}|Pt!          j*        |          r<|:|t           j+        k    r*d} |D ]}!|!|v r|!dz   |v s|!|k    rd}  n| rt           j,        }d|v r\|                    dd          |$                                v r2|j-        t           j.        k    r||                    dd                   }"nd}"|dk    rL|s||vrD||j-        }|r |
| ||||||"           t_          | |d |!           ta          ||||"           |d#k    rH|rF||j-        }|r |
| ||||||"           t_          | |d |!           ta          ||||"           t_          | |||||"$           ~tc          j2                     |	sAt	          |          dk    r.t
          3                    d%| d&| j4        j5         d'| d(           tm          ||           |r%to          | ||           tq          j9        |           tu          | |           dS ))ax	  
    Loads a (potentially sharded) checkpoint inside a model, potentially sending weights to a given device as they are
    loaded.

    <Tip warning={true}>

    Once loaded across devices, you still need to call [`dispatch_model`] on your model to make it able to run. To
    group the checkpoint loading and dispatch in one single call, use [`load_checkpoint_and_dispatch`].

    </Tip>

    Args:
        model (`torch.nn.Module`):
            The model in which we want to load a checkpoint.
        checkpoint (`str` or `os.PathLike`):
            The folder checkpoint to load. It can be:
            - a path to a file containing a whole model state dict
            - a path to a `.json` file containing the index to a sharded checkpoint
            - a path to a folder containing a unique `.index.json` file and the shards of a checkpoint.
            - a path to a folder containing a unique pytorch_model.bin or a model.safetensors file.
        device_map (`Dict[str, Union[int, str, torch.device]]`, *optional*):
            A map that specifies where each submodule should go. It doesn't need to be refined to each parameter/buffer
            name, once a given module name is inside, every submodule of it will be sent to the same device.
        offload_folder (`str` or `os.PathLike`, *optional*):
            If the `device_map` contains any value `"disk"`, the folder where we will offload weights.
        dtype (`str` or `torch.dtype`, *optional*):
            If provided, the weights will be converted to that type when loaded.
        offload_state_dict (`bool`, *optional*, defaults to `False`):
            If `True`, will temporarily offload the CPU state dict on the hard drive to avoid getting out of CPU RAM if
            the weight of the CPU state dict + the biggest shard does not fit.
        offload_buffers (`bool`, *optional*, defaults to `False`):
            Whether or not to include the buffers in the weights offloaded to disk.
        keep_in_fp32_modules(`List[str]`, *optional*):
            A list of the modules that we keep in `torch.float32` dtype.
        offload_8bit_bnb (`bool`, *optional*):
            Whether or not to enable offload of 8-bit modules on cpu/disk.
        strict (`bool`, *optional*, defaults to `False`):
            Whether to strictly enforce that the keys in the checkpoint state_dict match the keys of the model's
            state_dict.

    r   )quantize_and_offload_8bitr   r  NrV  zeAt least one of the model submodule will be offloaded to disk, please pass along an `offload_folder`.T)exist_okr$  r   z.jsonc                 (    g | ]}|t           k    |S r   )r   r   r  s     r-   r   z,load_checkpoint_in_model.<locals>.<listcomp>  s"    VVVQADUDUqDUDUDUr/   c                 (    g | ]}|t           k    |S r   )r   r,  s     r-   r   z,load_checkpoint_in_model.<locals>.<listcomp>  s#    %b%b%bA1PaKaKaaKaKaKar/   c                 <    g | ]}|                     d           |S )z.index.json)rI   r,  s     r-   r   z,load_checkpoint_in_model.<locals>.<listcomp>  s)    ^^^QAJJ}D]D]^q^^^r/   z6 is not a folder containing a `.index.json` file or a z or a z filezI containing more than one `.index.json` file, delete the irrelevant ones.z`checkpoint` should be the path to a file containing a whole state dict, or the index of a sharded checkpoint, or a folder containing a sharded checkpoint or the whole state dict, but got rR   
weight_mapc                 P    g | ]"}t           j                            |          #S r   )rp  rq  r   )r   r  checkpoint_folders     r-   r   z,load_checkpoint_in_model.<locals>.<listcomp>  s*    YYY1BGLL):A>>YYYr/   c                     g | ]\  }}|S r   r   r  s      r-   r   z,load_checkpoint_in_model.<locals>.<listcomp>  s    >>>WT1D>>>r/   )r   )r'  r   r<   z doesn't have any device set.Fr   r}   r&  r2   r   )ry   rP   rz   z(Some weights of the model checkpoint at z! were not used when initializing z: z. This may or may not be an issue - make sure that the checkpoint does not have unnecessary parameters, or that the model definition correctly corresponds to the checkpoint.);bnbr)  r  r   r   r   r   r   r   rK   rp  makedirsr+   rZ   r%  r   r5   rq  isfilerI   isdirlistdirr   r   r   r   openjsonloadsreadr  r  r   tempfilemkdtempr  r  r   r  r  r  r   is_floating_pointrg   rd   rP   rj   r   r   gccollectr_  r   r   r   rv  shutilrmtreer"  )$r,   r#  r   rr  rP   r$  r  r%  r&  r'  r)  r   checkpoint_filesindex_filenamepotential_state_binpotential_state_safetensorpotential_indexr  r3   offload_indexstate_dict_folderstate_dict_indexunexpected_keys
model_keysbuffer_namesr  loaded_checkpointr  r   rB  param_device	new_dtypeproceedr-  rz   r1  s$                                      @r-   load_checkpoint_in_modelrR  K  s   j  3222222&u--K&u-- 
#k2B2Ba2G2G A	
 	
 	
 ,[*EEE*"8VzGXGXGZGZ=Z=Zs
 
 	
 
	#
(>6ZM^M^M`M`C`C`
NT2222% &h++u%%N	w~~j!! 
z??##G,, 	,'NN *|	z	"	" 
VV"*Z*@*@VVV%b%bJ1G1G%b%b%b""##q(( "Z9LQ9O P PQ+,,11 "Z9STU9V W WX _^"*Z*@*@^^^O?##q(( !  F  FYe  F  Fm~  F  F  F   _%%**!#j/!:L!M!M !lll   vhrv v v
 
 	

 !GMM.99!<.!! 	)QJqvvxx((E	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 5  ,'E!$s5<<>>':':";";<<YYYYHXYYY M $,..eeOU%%'',,..//J>>(;(;(=(=>>>L+ K K+O
SSS!!"3F!CCC""3'8'='='?'?#@#@:#MNNNN%6%<%<%>%> A A!
EJ&&Z//#''
333! ! (+&&**{*/L/L"%((;+<+<S+A+A#2#+F"G"GK +&&**{*/L/L"$$:)=)=$
%Q%Q%QRRR)+6!	$)@)G)G$+7EU]<R<R"'#7 & &C!$
!2!2sj9P9PVY]gVgVg*. % Wh # 6(-Iz))j.@.@5.Q.QUfUkUkUmUm.m.m{ej00*;J<N<NxY^<_<_*`&*O6))& 
_*L*H*H$,(-I+ d55 %uj)^Ubds   %7z6Ybcccc&uj.P]^^^^!U**/A* ($)K	' e11!5*iARTdfu    4E:vU^____&uj:KScddddd/"$#'(7     

 
c/**Q..xz x x"_5x x9Hx x x	
 	
 	

 }n555  )u&68IJJJ'(((UK(((((s   'L;;L?L?
native_ampautocast_kwargsc           
         t                      }|i }n|                                }| r|j        t          j        k    rt          d          rdn|j        j        }|j        dk    rt          j
        d	|t          j        d|S |j        dv r|j        t          j        t          j        t          j        t          j        t          j        t          j        t          j        t          j        t          j        f	v rt          j
        d	|t          j        d|S t          j
        d	d|i|S t+          j                    S )
aH  
    Return a context manager for autocasting mixed precision

    Args:
        native_amp (`bool`, *optional*, defaults to False):
            Whether mixed precision is actually enabled.
        cache_enabled (`bool`, *optional*, defaults to True):
            Whether the weight cache inside autocast should be enabled.
    NT)check_is_gpur1   fp16)device_typerP   )bf16fp8rX  r   )r   	to_kwargsdistributed_typer   XLAr   r6   r4   mixed_precisionr5   autocastrg   NO	MULTI_CPU	MULTI_GPU	MULTI_MLU
MULTI_MUSA	MULTI_NPU	MULTI_XPUFSDPrf   
contextlibnullcontext)rS  rT  staterX  s       r-   #get_mixed_precision_context_managerrk  +  sC    E)3355 ( &/*===BXfjBkBkBk= F" 	
  F**>bkbbRabbb"o55%:P%%%&%% 
U
 
;
 
;
 >ckccSbccc>MMkM_MMM%'''r/   )NNNN)TFF)F)Nr   T)r   TT)NNF)NNr   )r   )NNNNF)NNNNFTF)r   )NNNFFNFF)FN)lrh  r?  r   r9  loggingrp  rX   rA  r<  r   collectionsr   r   typingr   r   r   r   r	   r
   r5   torch.nnr   rj  r   	constantsr   r   dataclassesr   r   r   importsr   r   r   r   r   r   r   memoryr   r   r  r   r   r   r    r   versionsr!   r"   	torch_npu	torch_mlu
torch_musar  r$   safetensors.torchr%   r  WEIGHTS_INDEX_NAME	getLoggerr   r   r.   r9   rG   rZ   rO   rP   r^   r   r6   rv   rE  
HalfTensorr   rS   r   r   r  r   r   r   r   r  r
  r  r"  r'  r9  r<  rK  rd  rl  rv  r}  r  r  r  r  r  r  r_   r"  PathLikerR  rk  r   r/   r-   <module>r}     s?       				    				 				    0 0 0 0 0 0 0 0 : : : : : : : : : : : : : : : :        $ $ $ $ $ $ 6 6 6 6 6 6 6 6 E E E E E E E E E E                  A @ @ @ @ @ @ @ N N N N N N N N N N ) ) ) ) ) ) ) ) 8 8 8 8 8 8 8 8 ''' ''' %(((  ! ! ! ! ! ! 9 9 9 9 9 9 4 		8	$	$] ] ]) ) )6)5c? ) ) ) )X5;    8!4el !4uU\35K/L !4 !4 !4 !4P %)/326MQJ> J>IJ>J> #sEL()J> EL!	J>
 E#u{*+,J> e./J> d3U\5<-G(H#HIJJ> J> J> J>\ kp# #I#(,#>B#cg# # # #<& &ry &4 & & & &&. . . . .t . . ."U29 U U U U@; ; ;  2 ,0!	#` #`HO#`
3ux'
(#` #` 	#` #` #` #`L %(/ ^b    (6x 6x 6x 6x 6xr: : :>U3#45 %+     15DH	' '9'E#u|+,-' T#uS%,->'?"?@A' 	' ' ' 'X 15DH	# 	#9	#E#u|+,-	# T#uS%,->'?"?@A	# 	# 	# 	#$!%UX_,-.$!>B38n$!gklogp$! $! $! $!NX XxU38_eCHo-M(NO X X X Xv c5c5<1G+H&H!I X[    ,m m m<   DH37/3DH| |9|eCHouS#X>?@| &d3i0| E#u{*+,	|
 T#uS%,->'?"?@A| | | | |~%58? % % % %( DH37/3CG!l l9leCHouS#X>?@l &d3i0l E#u{*+,	l
 T#uS%+-='>">?@l l l l l l l^	
BI 
4U3U\CY=Z8Z3[ 
 
 
 
4\M \M \M \M~-") - - - -h <A	( (I(( S%U\ 1223( !c5<!78	( ( ( (\ FJ8</3$!&*"]) ])9])c2;&']) c5c5<)?#@@AB]) U3#345	])
 E#u{*+,]) ]) ]) s)]) ]) ]) ]) ]) ])@&( &(D &(Sa &( &( &( &( &( &(r/   