o
    ?߱i                     @   s  d dl Z d dlZd dlZd dlmZmZ d dlmZ d dl	m
Z
mZ dZejddZd2ded	efd
dZeddZe ej deeef d	dfddZej^ ZZZeg eegee_d3ddZded	dfddZd	efddZd	efddZ d	efddZ!ded	e"fddZ#d4dede"d	dfdd Z$d4dede"d	dfd!d"Z%d4dede"d	dfd#d$Z&d4dede"d	dfd%d&Z'd4dede"d	dfd'd(Z(d4dede"d	dfd)d*Z)d4dede"d	dfd+d,Z*d4dede"d	dfd-d.Z+d5d/eej, d	efd0d1Z-e  dS )6    N)AnyOptional)CoreLoggerTLOGURU_LEVELINFO   depthreturnc                 C   s   t t d | dddddg i d
S )NFT)
core	exceptionr	   recordlazycolorsrawcapturepatchersextra)r   r   r	    r   S/data/cameron/vidgen/cosmos-predict2.5/cosmos_predict2/_src/imaginaire/utils/log.pymake_new_logger   s   r   r   r   c                 C   sX   zt  }t j| d j|| d d< W d S  ty+   d| d j | d d< Y d S w )Nfiler   Zrelative_pathz<cwd-unavailable>:)osgetcwdpathrelpathOSError)r   startr   r   r   _add_relative_path/   s   "r   c                  C   s>   t   t } t }t }t jtjt|  | | t	d d S )N)levelformatfilter)
loggerremoveget_datetime_formatget_machine_formatget_message_formataddsysstdoutLEVEL_rank0_only_filter)datetime_formatmachine_formatmessage_formatr   r   r   init_loguru_stdout=   s   
r0   r   c              	   C   s>   t  }t }t }tj| dt| | | ddd dd d S )Nutf8z100 MBc                 S   s   t | pt S N)r,   
RANK0_ONLY)resultr   r   r   <lambda>T   s    z"init_loguru_file.<locals>.<lambda>T)encodingr    r!   rotationr"   enqueue)r%   r&   r'   r#   r(   r+   )r   r-   r.   r/   r   r   r   init_loguru_fileJ   s   
r9   c                   C   s   dS )Nz&[<green>{time:MM-DD HH:mm:ss}</green>|r   r   r   r   r   r%   Y   s   r%   c               	   C   sz   t jdd} tt jdd}d}d}t r;ts;t r;t }t	 }d| dd	|dd
|dd	|dd	d }|S )NZNGC_ARRAY_INDEX0ZNGC_ARRAY_SIZE1 r   z
<red>[Nodez<3/z][RANKz<5]z[{process.name:<8}]</red>| )
r   environgetintdistis_availabler3   is_initializedget_rankZget_world_size)Znode_idZ	num_nodesr.   rankZ
world_sizer   r   r   r&   ]   s   (r&   c                  C   s   d} | S )NzW<level>{level}</level>|<cyan>{extra[relative_path]}:{line}:{function}</cyan>] {message}r   )r/   r   r   r   r'   l   s   r'   c                 C   sF   | d  dd}t dkr|rdS |s dt  d| d  | d< | S )Nr   
rank0_onlyTr   z[RANK z] message)r@   	_get_rank)r   is_rank0r   r   r   r,   q   s   r,   rH   rG   c                 C      t jddj|d|  d S Nr   r   )rG   )r#   optbindtracerH   rG   r   r   r   rO   z      rO   c                 C   rK   rL   )r#   rM   rN   debugrP   r   r   r   rR   ~   rQ   rR   c                 C   rK   rL   )r#   rM   rN   inforP   r   r   r   rS      rQ   rS   c                 C   rK   rL   )r#   rM   rN   successrP   r   r   r   rT      rQ   rT   c                 C   rK   rL   )r#   rM   rN   warningrP   r   r   r   rU      rQ   rU   c                 C   rK   rL   )r#   rM   rN   errorrP   r   r   r   rV      rQ   rV   c                 C   rK   rL   )r#   rM   rN   criticalrP   r   r   r   rW      rQ   rW   c                 C   rK   rL   )r#   rM   rN   r   rP   r   r   r   r      rQ   r   groupc                 C   s"   d}t  rt  rt | }|S )zgGet the rank (GPU device) of the worker.

    Returns:
        rank (int): The rank of the worker.
    r   )rB   rC   rD   rE   )rX   rF   r   r   r   rI      s   
rI   )r   )r
   N)Tr2   ).atexitr   r)   typingr   r   torch.distributeddistributedrB   Zloguru._loggerr   r   r3   r?   r@   r+   rA   r   r#   registerr$   dictstrr   _optionsoptions_r   tupler0   r9   r%   r&   r'   boolr,   rO   rR   rS   rT   rU   rV   rW   r   ZProcessGrouprI   r   r   r   r   <module>   s<   


	
