o
    h(                     @   s  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 d dlmZ d dl	m
Z
 d dlmZ d dlZd dlmZ d dlmZ dd	lmZ dd
lm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  ddl!m"Z" ddl#m$Z$ ddl%m&Z& ee'Z(e ddrd dl)m*  m+Z, dd Z-d4de.fddZ/dd Z0de1fddZ2d5d e.d!e.fd"d#Z3ed$d% Z4ed&d' Z5d(d) Z6d*d+ Z7d6d,e8d-e.fd.d/Z9d0d1 Z:d2d3 Z;dS )7    N)contextmanager)partial)
MethodType)OrderedDict)Version)	save_file   )write_basic_config)
get_logger)PartialState   )FSDP_PYTORCH_VERSION)DistributedType)is_deepspeed_availableis_torch_distributed_availableis_tpu_available)id_tensor_storage)convert_model)is_torch_versionF)check_devicec                 C   s(   t dds
ttdsdS t| tjjjS )zD
    Check whether the module was compiled with torch.compile()
    <z2.0.0_dynamoF)r   hasattrtorch
isinstancer   
eval_frameOptimizedModule)module r   L/var/www/html/ai/venv/lib/python3.10/site-packages/accelerate/utils/other.pyis_compiled_module/   s   r    Tkeep_fp32_wrapperc           	      C   s  t jjjt jjf}t| }|r| }| j} t r"ddlm	} ||f7 }t
dtr5t r5ddlm} ||f7 }t| |rB| j} t| |s:|sxt| d}| jdd}|durlt|drf|j}||krant|dsYt|| | _t| d	d
rxt| d
d |r| |_|} | S )a<  
    Extract a model from its distributed containers.

    Args:
        model (`torch.nn.Module`):
            The model to extract.
        keep_fp32_wrapper (`bool`, *optional*):
            Whether to remove mixed precision hooks from the model.

    Returns:
        `torch.nn.Module`: The extracted model.
    r   )DeepSpeedEnginez>=)FullyShardedDataParallelforward_original_forwardN__wrapped__ _converted_to_transformer_engineF)to_transformer_engine)r   nnparallelDistributedDataParallelDataParallelr    	_orig_modr   	deepspeedr"   r   r   r   2torch.distributed.fsdp.fully_sharded_data_parallelr#   r   r   getattr__dict__popr   r&   r   r$   r   )	modelr!   optionsis_compiledcompiled_modelr"   FSDPr$   original_forwardr   r   r   extract_model_from_parallel8   s<   






r9   c                   C   s   t    dS )a  
    Introduces a blocking point in the script, making sure all processes have reached this point before continuing.

    <Tip warning={true}>

    Make sure all processes will reach this instruction otherwise one of your processes will hang forever.

    </Tip>
    N)r   wait_for_everyoner   r   r   r   r:   l   s   
r:   
state_dictc                    s   t t}  D ]\}}t|ts|t| | q	dd | D }t }|	 D ] } fdd|D }|
|dd  |dd D ]} |= qFq,t|dkr\td| d	 d
d   D   S )z
    Cleans the state dictionary from a model and removes tensor aliasing if present.

    Args:
        state_dict (`dict`):
            The state dictionary from a model
    c                 S   s"   i | ]\}}t |d kr||qS )r   )len).0ptrnamesr   r   r   
<dictcomp>   s   " z4clean_state_dict_for_safetensors.<locals>.<dictcomp>c                    s   g | ]}| v r|qS r   r   )r=   namer;   r   r   
<listcomp>   s    z4clean_state_dict_for_safetensors.<locals>.<listcomp>r   Nr   zRemoved shared tensor zk while saving. This should be OK, but check by verifying that you don't receive any warning while reloadingc                 S   s*   i | ]\}}|t |tjr| n|qS r   )r   r   Tensor
contiguous)r=   kvr   r   r   r@      s   * )collectionsdefaultdictlistitemsr   strr   appendsetvaluesupdater<   loggerwarning)r;   ptrsrA   tensorshared_ptrs
warn_namesr?   found_namesr   rB   r    clean_state_dict_for_safetensorsy   s&   


rX   save_on_each_nodesafe_serializationc                 C   s   |rt tddid}t| trt| } ntj}t jt	j
kr&t| | dS t jr3|s3|| | dS t jr@|rB|| | dS dS dS )a  
    Save the data to disk. Use in place of `torch.save()`.

    Args:
        obj:
            The data to save
        f:
            The file (or file-like object) to use to save the data
        save_on_each_node (`bool`, *optional*, defaults to `False`):
            Whether to only save on the global main process
        safe_serialization (`bool`, *optional*, defaults to `False`):
            Whether to save `obj` using `safetensors` or the traditional PyTorch way (that uses `pickle`).
    formatpt)metadataN)r   safe_save_filer   r   rX   r   saver   distributed_typer   TPUxmis_main_processis_local_main_process)objfrY   rZ   	save_funcr   r   r   r_      s   
r_   c                  c   s     t j} t t _dV  | t _dS )a  
    A context manager that will cache origin `os.environ` and replace it with a empty dictionary in this context.

    When this context exits, the cached `os.environ` will be back.

    Example:

    ```python
    >>> import os
    >>> from accelerate.utils import clear_environment

    >>> os.environ["FOO"] = "bar"
    >>> with clear_environment():
    ...     print(os.environ)
    ...     os.environ["FOO"] = "new_bar"
    ...     print(os.environ["FOO"])
    {}
    new_bar

    >>> print(os.environ["FOO"])
    bar
    ```
    N)osenvirondict)_old_os_environr   r   r   clear_environment   s
   
rl   c                  k   s    i }|   D ]\}}| }|tjv rtj| ||< t|tj|< qdV  | D ]}| }||v r:|| tj|< q(tj|d q(dS )a  
    A context manager that will add each keyword argument passed to `os.environ` and remove them when exiting.

    Will convert the values in `kwargs` to strings and upper-case all the keys.

    Example:

    ```python
    >>> import os
    >>> from accelerate.utils import patch_environment

    >>> with patch_environment(FOO="bar"):
    ...     print(os.environ["FOO"])  # prints "bar"
    >>> print(os.environ["FOO"])  # raises KeyError
    ```
    N)rK   upperrh   ri   rL   r2   )kwargsexisting_varskeyvaluer   r   r   patch_environment   s   
rr   c                 C   sH   t | dst | dst| d| } t | dr| jS t | dr | jS t| S )z(
    Gets a pretty name from `obj`.
    __qualname____name__	__class__)r   r0   rs   rt   rL   )re   r   r   r   get_pretty_name   s   

rv   c                 C   s@   |   D ]\}}t|tr||i }t|| q|||< q|S )z
    Recursively merges two dictionaries.

    Args:
        source (`dict`): The dictionary to merge into `destination`.
        destination (`dict`): The dictionary to merge `source` into.
    )rK   r   rj   
setdefaultmerge_dicts)sourcedestinationrp   rq   noder   r   r   rx     s   

rx   portreturnc                 C   sR   | du rd} t  t jt j}|d| fdkW  d   S 1 s"w   Y  dS )z
    Checks if a port is in use on `localhost`. Useful for checking if multiple `accelerate launch` commands have been
    run and need to see if the port is already in use.
    Ni<s  	localhostr   )socketAF_INETSOCK_STREAM
connect_ex)r|   sr   r   r   is_port_in_use  s
   $r   c                 C   sB   dD ]}| dk rt | d d|   S | d } qt | d dS )z7Converts `size` from bytes to the largest possible unit)bytesKBMBGBTBg      @r    z PB)round)sizexr   r   r   convert_bytes)  s
   
r   c                  C   sj   t  } | j}|dkrdS td| j^}}}d}t|t|k r3d| d| d}tj|dd	 dS dS )
zFWarns if the kernel version is below the recommended minimum on Linux.LinuxNz(\d+\.\d+\.\d+)z5.5.0zDetected kernel version z,, which is below the recommended minimum of zo; this can cause the process to hang. It is recommended to upgrade the kernel to the minimum version or higher.T)main_process_only)	platformunamesystemresplitreleaser   rQ   rR   )infor   _versionmin_versionmsgr   r   r   check_os_kernel3  s   r   )T)FF)N)<rH   rh   r   r   r   
contextlibr   	functoolsr   typesr   typingr   r   packaging.versionr   safetensors.torchr   r^   commands.config.defaultr	   loggingr
   stater   	constantsr   dataclassesr   importsr   r   r   modelingr   transformer_enginer   versionsr   rt   rQ   torch_xla.core.xla_modelcore	xla_modelrb   r    boolr9   r:   rj   rX   r_   rl   rr   rv   rx   intr   r   r   r   r   r   r   <module>   sL   
	4#
 
#
