U
    0È-e»  ã                   @   sD   d dl Z d dlZddlmZ G dd„ de jƒZd	eedœdd„ZdS )
é    Né   )ÚPartialStatec                   @   s$   e Zd ZdZedd„ ƒZdd„ ZdS )ÚMultiProcessAdaptera@  
    An adapter to assist with logging in multiprocess.

    `log` takes in an additional `main_process_only` kwarg, which dictates whether it should be called on all processes
    or only the main executed one. Default is `main_process_only=True`.

    Does not require an `Accelerator` object to be created first.
    c                 C   s   t ƒ }|  p| o|jS )z Check if log should be performed)r   Zis_main_process)Úmain_process_onlyÚstate© r   úS/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/accelerate/logging.pyÚ_should_log   s    zMultiProcessAdapter._should_logc           	      O   s¼   t ji krtdƒ‚| dd¡}| dd¡}|  |¡r¸|  |¡rf|  ||¡\}}| jj||f|ž|Ž nR|r¸t ƒ }t	|j
ƒD ]<}||jkr®|  ||¡\}}| jj||f|ž|Ž | ¡  qzdS )a\  
        Delegates logger call after checking if we should log.

        Accepts a new kwarg of `main_process_only`, which will dictate whether it will be logged across all processes
        or only the main executed one. Default is `True` if not passed

        Also accepts "in_order", which if `True` makes the processes log one by one, in order. This is much easier to
        read, but comes at the cost of sometimes needing to wait for the other processes. Default is `False` to not
        break with the previous behavior.

        `in_order` is ignored if `main_process_only` is passed.
        z€You must initialize the accelerate state by calling either `PartialState()` or `Accelerator()` before using the logging utility.r   TÚin_orderFN)r   Z_shared_stateÚRuntimeErrorÚpopÚisEnabledForr	   ÚprocessÚloggerÚlogÚrangeZnum_processesZprocess_indexZwait_for_everyone)	ÚselfÚlevelÚmsgÚargsÚkwargsr   r
   r   Úir   r   r   r   %   s"    
ÿ


zMultiProcessAdapter.logN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústaticmethodr	   r   r   r   r   r   r      s   	
r   )ÚnameÚ	log_levelc                 C   sP   |dkrt j dd¡}t | ¡}|dk	rF| | ¡ ¡ |j | ¡ ¡ t|i ƒS )a=  
    Returns a `logging.Logger` for `name` that can handle multiprocessing.

    If a log should be called on all processes, pass `main_process_only=False` If a log should be called on all
    processes and in order, also pass `in_order=True`

    Args:
        name (`str`):
            The name for the logger, such as `__file__`
        log_level (`str`, *optional*):
            The log level to use. If not passed, will default to the `LOG_LEVEL` environment variable, or `INFO` if not

    Example:

    ```python
    >>> from accelerate.logging import get_logger

    >>> logger = get_logger(__name__)

    >>> logger.info("My log", main_process_only=False)
    >>> logger.debug("My log", main_process_only=True)

    >>> logger = get_logger(__name__, log_level="DEBUG")
    >>> logger.info("My log")
    >>> logger.debug("My second log")

    >>> from accelerate import Accelerator

    >>> accelerator = Accelerator()
    >>> array = ["a", "b", "c", "d"]
    >>> letter_at_rank = array[accelerator.process_index]
    >>> logger.info(letter_at_rank, in_order=True)
    ```
    NZACCELERATE_LOG_LEVEL)	ÚosÚenvironÚgetÚloggingÚ	getLoggerÚsetLevelÚupperÚrootr   )r   r   r   r   r   r   Ú
get_loggerG   s    #
r'   )N)r"   r   r   r   ÚLoggerAdapterr   Ústrr'   r   r   r   r   Ú<module>   s   2