
    g/                        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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!            r	d dl*m+c m,Z- d Z.d*de/de/fdZ0d Z1de2fdZ3d+de/de/fdZ4ed             Z5ed              Z6d! Z7d" Z8d,d#e9d$e/fd%Z:d& Z;d' Z<d(e=fd)Z>dS )-    N)contextmanager)partialreduce)
MethodType)OrderedDict)Version)	save_file   )write_basic_config)
get_logger)PartialState   )FSDP_PYTORCH_VERSION)DistributedType)is_deepspeed_availableis_torch_distributed_availableis_torch_xla_available)id_tensor_storage)convert_model)is_torch_versionc                     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)modules    R/var/www/html/ai-engine/env/lib/python3.11/site-packages/accelerate/utils/other.pyis_compiled_moduler!   /   sD     W%% WUI-F-F ufem6FGGG    TFkeep_fp32_wrapper	recursivec                   
 t           j        j        j        t           j        j        f}t          |           }|r	| }| j        } t                      rddlm	} ||fz  }t          dt                    rt                      rddlm} ||fz  }t          | |          r| j        } t          | |          |r
fd
 
|           } |s| j        }| j                            dd          }	|	Ct)          |d          r|j        }||	k    rnt)          |d          t-          ||           | _        t/          | d	d
          rt1          | d
           |r	| |_        |} | S )a8  
    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.
        recursive (`bool`, *optional*, defaults to `False`):
            Whether to recursively extract all cases of `module.module` from `model` as well as unwrap child sublayers
            recursively, not just the top-level distributed containers.

    Returns:
        `torch.nn.Module`: The extracted model.
    r   )DeepSpeedEnginez>=)FullyShardedDataParallelc                     t          | d          r | j                  }n| }|                                D ]\  }}t          || |                      |S )Nr   )r   r   named_childrensetattr)r   unwrapped_modulenamechild_recursive_unwraps       r    r.   z6extract_model_from_parallel.<locals>._recursive_unwrap^   s|     vx(( *#4#4V]#C#C  #) />>@@ J Je($0A0A%0H0HIIII##r"   _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   forward__dict__popr   r0   r   getattrr   )modelr#   r$   optionsis_compiledcompiled_modelr&   FSDPr:   original_forwardr.   s             @r    extract_model_from_parallelrD   8   s     x 8%(:OPG$U++K   &------O%%233 8V8X8X ggggggD7
UG
$
$  UG
$
$   )
	$ 
	$ 
	$ 
	$ 
	$ "!%(( 
>- >--.A4HH''=11 !-... '=11  'w66EM5<eDD 	>%u==== #( Lr"   c                  F    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_everyone r"   r    rF   rF      s      NN$$&&&&&r"   
state_dictc                     t          j        t                    }                                 D ]B\  }}t	          |t
                    s(|t          |                                       |           Cd |                                D             }t                      }|	                                D ]=} fd|D             }|
                    |dd                    |dd         D ]} |= >t          |          dk    rt                              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                 @    i | ]\  }}t          |          d k    ||S )r   )len).0ptrnamess      r    
<dictcomp>z4clean_state_dict_for_safetensors.<locals>.<dictcomp>   s)    OOO*#uE

Q3r"   c                     g | ]}|v |	S rG   rG   )rL   r,   rH   s     r    
<listcomp>z4clean_state_dict_for_safetensors.<locals>.<listcomp>   s#    DDD1C1Ct1C1C1Cr"   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                 v    i | ]6\  }}|t          |t          j                  r|                                n|7S rG   )r   r   Tensor
contiguous)rL   kvs      r    rO   z4clean_state_dict_for_safetensors.<locals>.<dictcomp>   s<    iiidaQR!z!U\'B'BIQ\\^^^iiir"   )collectionsdefaultdictlistitemsr   strr   appendsetvaluesupdaterK   loggerwarning)rH   ptrsr,   tensorshared_ptrs
warn_namesrN   found_namess   `       r     clean_state_dict_for_safetensorsrg      st    "4((D"((** 9 9f&#&& 	9"6**+224888 PO

OOOKJ##%% 	! 	! EDDDDDD+abb/***O 	! 	!D4  	!
: ]Z  ]  ]  ]	
 	
 	
 jiV`VfVfVhVhiiiJr"   save_on_each_nodesafe_serializationc                    t                      j        t          j        k    rt	          j        |           } |r=t          t          ddi          }t          | t                    rt          |           } nt          j        }t                      j        r|s || |           dS t                      j        r|r || |           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   distributed_typer   XLAxm_maybe_convert_to_cpur   safe_save_filer   r   rg   r   saveis_main_processis_local_main_process)objfrh   ri   	save_funcs        r    rs   rs      s    $ ~~&/*===&s++ Nh5EFFF	c;'' 	82377CJ	~~% .? 	#q		- 2C 	#q   r"   c               #     K   t           j                                        } t           j                                         	 dV  t           j                                         t           j                            |            dS # t           j                                         t           j                            |            w xY w)a  
    A context manager that will temporarily clear environment variables.

    When this context exits, the previous environment variables 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copyclearr_   )_old_os_environs    r    clear_environmentr      s      2 joo''OJ+



/***** 	


/****s    B ?Cc               +   H  K   i }|                                  D ]X\  }}|                                }|t          j        v rt          j        |         ||<   t	          |          t          j        |<   Y	 dV  | D ]P}|                                }||v r||         t          j        |<   0t          j                            |d           QdS # | D ]P}|                                }||v r||         t          j        |<   0t          j                            |d           Qw xY w)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)rZ   upperrz   r{   r[   r<   )kwargsexisting_varskeyvalues       r    patch_environmentr      s)     $ Mllnn % %
Uiikk"*!#CM#e**
3	* 	* 	*C))++Cm##"/"4
3
sD))))	* 	*6 	* 	*C))++Cm##"/"4
3
sD))))	*s   3C AD!c                     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   r=   r   r   r[   )rv   s    r    get_pretty_namer     sx     3'' -Z0H0H -c;,,sN##  sJ |s88Or"   c                     |                                  D ]F\  }}t          |t                    r'|                    |i           }t	          ||           A|||<   G|S )z
    Recursively merges two dictionaries.

    Args:
        source (`dict`): The dictionary to merge into `destination`.
        destination (`dict`): The dictionary to merge `source` into.
    )rZ   r   dict
setdefaultmerge_dicts)sourcedestinationr   r   nodes        r    r   r   '  sl     llnn % %
UeT"" 	%))#r22Dt$$$$$Kr"   portreturnc                     | d} t          j         t           j        t           j                  5 }|                    d| f          dk    cddd           S # 1 swxY 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   ss     r    is_port_in_user   9  s    
 |	v~v'9	:	: 6a||[$/00A56 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6s   AAAc                 v    dD ]$}| dk     rt          | d           d| c S | dz  } %t          | d           dS )z7Converts `size` from bytes to the largest possible unit)bytesKBMBGBTBg      @r
    z PB)round)sizexs     r    convert_bytesr   D  s^    .  &==D!nn**q*****D!nn!!!!r"   c                     t          j                    } | j        }|dk    rdS t          j        d| j                  ^}}}d}t          |          t          |          k     r'd| d| d}t                              |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   r`   ra   )infor   _versionmin_versionmsgs         r    check_os_kernelr   N  s     >D[FX0$,??NAwKw'+....sw s s\g s s s 	 	sd33333 /.r"   attrc                 V    d }t          || g|                    d          z             S )z
    Recursive `getattr`.

    Args:
        obj:
            A class instance holding the attribute.
        attr (`str`):
            The attribute that is to be retrieved, e.g. 'attribute1.attribute2'.
    c                 "    t          | |          S N)r=   )rv   r   s     r    _getattrz#recursive_getattr.<locals>._getattrk  s    sD!!!r"   .)r   r   )rv   r   r   s      r    recursive_getattrr   `  s4    " " " (SEDJJsOO3444r"   )TF)FFr   )?rW   rz   r   r   r   
contextlibr   	functoolsr   r   typesr   typingr   r   packaging.versionr   safetensors.torchr	   rr   commands.config.defaultr   loggingr   stater   	constantsr   dataclassesr   importsr   r   r   modelingr   transformer_enginer   versionsr   r   r`   torch_xla.core.xla_modelcore	xla_modelrp   r!   boolrD   rF   r   rg   rs   r   r   r   r   intr   r   r   r[   r   rG   r"   r    <module>r      s       				  				  % % % % % % % % % % % % % %              % % % % % % 9 9 9 9 9 9 8 8 8 8 8 8                         + + + + + + ( ( ( ( ( ( c c c c c c c c c c ' ' ' ' ' ' - - - - - - & & & & & & 
H		  *)))))))))H H HE E$ ERV E E E EP
' 
' 
'         F D d    D + + +D !* !* !*H
 
 
  $6 6 6 6 6 6 6" " "4 4 4$5 5 5 5 5 5 5r"   