U
    D‹Ñhî  ã                   @   s  U d dl Z d dlZd dlZd dlmZmZmZmZmZm	Z	 d dl
mZ d dlZd dlmZ d dlmZ g Zee ed< dZefeejdœdd	„Zefeeejef dœd
d„Zeƒ aeeef dœdd„Ze	dƒZedƒZeeef eeef dœdd„Zeeef eeef dœdd„ZdS )é    N)ÚAnyÚCallableÚDictÚListÚTupleÚTypeVar)Ú	ParamSpec)Ú_log_handlersÚ__all__Údefault)ÚdestinationÚreturnc                 C   sP   t | ƒ\}}t d|› �¡}| tj¡ t d¡}| |¡ d|_| |¡ |S )Nzc10d-za%(asctime)s %(filename)s:%(lineno)s %(levelname)s p:%(processName)s t:%(threadName)s: %(message)sF)	Ú_get_logging_handlerÚloggingÚ	getLoggerÚsetLevelÚDEBUGÚ	FormatterÚsetFormatterÚ	propagateÚ
addHandler)r   Zlogging_handlerÚlog_handler_nameÚloggerÚ	formatter© r   úK/tmp/user/2202/pip-unpacked-wheel-bf9o3bgk/torch/distributed/c10d_logger.pyÚ_get_or_create_logger   s    ÿ

r   c                 C   s   t |  }t|ƒj}||fS ©N)r	   ÚtypeÚ__name__)r   Zlog_handlerr   r   r   r   r   '   s    
r   )r   c              
   O   s¼   t  ¡ r¢| d¡p| d¡}| › |› d|› �t  | d¡¡› t  |¡› t  ¡ › t  |¡› t  ¡ › t  |¡› dœ}|d dkr¸tjj	 
¡ }d d	d
„ |D ƒ¡|d< n| › |› d|› �dœ}|S )NÚgroupZprocess_groupz, Zpg)Ú	func_nameÚargsZpg_nameÚbackendZ
world_sizeZ
group_sizeZglobal_rankZ
local_rankr#   ÚncclÚ.c                 s   s   | ]}t |ƒV  qd S r   )Ústr)Ú.0Úvr   r   r   Ú	<genexpr>@   s     z _get_msg_dict.<locals>.<genexpr>Únccl_version)r!   r"   )ÚdistZis_initializedÚgetZ_get_process_group_nameÚget_backendZget_world_sizeZget_rankÚtorchZcudar$   ÚversionÚjoin)r!   r"   Úkwargsr    Úmsg_dictr*   r   r   r   Ú_get_msg_dict1   s$    


ø
þr3   Ú_TÚ_P)Úfuncr   c                    s(   t  ˆ ¡tjtjtdœ‡ fdd„ƒ}|S )N©r"   r1   r   c               
      s\   zˆ | |ŽW S  t k
rV } z,tˆ jf| ž|Ž}|› |d< t |¡ ‚ W 5 d }~X Y nX d S )NÚerror)Ú	Exceptionr3   r   Ú_c10d_loggerÚdebug)r"   r1   r8   r2   ©r6   r   r   ÚwrapperL   s    

z"_exception_logger.<locals>.wrapper©Ú	functoolsÚwrapsr5   r"   r1   r4   ©r6   r=   r   r<   r   Ú_exception_loggerK   s    	rB   c                    s(   t  ˆ ¡tjtjtdœ‡ fdd„ƒ}|S )Nr7   c                     sL   t  ¡ }ˆ | |Ž}t  ¡ | }tˆ jf| ž|Ž}|› d�|d< t |¡ |S )NÚnsÚ
time_spent)ÚtimeÚtime_nsr3   r   r:   r;   )r"   r1   Út1Zfunc_returnrD   r2   r<   r   r   r=   Z   s    

z_time_logger.<locals>.wrapperr>   rA   r   r<   r   Ú_time_loggerY   s    rH   ) r?   r   rE   Útypingr   r   r   r   r   r   Ztyping_extensionsr   r.   Ztorch.distributedZdistributedr+   Z"torch.distributed.logging_handlersr	   r
   r&   Ú__annotations__Z_DEFAULT_DESTINATIONÚLoggerr   ÚHandlerr   r:   r3   r4   r5   rB   rH   r   r   r   r   Ú<module>
   s$      