
    g                     b    d dl Z d dlZd dlZddlmZ  G d dej                  Zd	dedefdZdS )
    N   )PartialStatec                   `    e Zd ZdZed             Zd Z ej        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                 6    t                      }|  p| o|j        S )z Check if log should be performed)r   is_main_process)main_process_onlystates     N/var/www/html/ai-engine/env/lib/python3.11/site-packages/accelerate/logging.py_should_logzMultiProcessAdapter._should_log    s$     $$U):)Tu?TU    c                 t   t           j        i k    rt          d          |                    dd          }|                    dd          }|                    dd           |                     |          r|                     |          r2|                     ||          \  }} | j        j	        ||g|R i | dS |rvt                      }t          |j                  D ]U}||j        k    r0|                     ||          \  }} | j        j	        ||g|R i | |                                 RdS dS d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.
        zYou must initialize the accelerate state by calling either `PartialState()` or `Accelerator()` before using the logging utility.r	   Tin_orderF
stacklevel   N)r   _shared_stateRuntimeErrorpop
setdefaultisEnabledForr   processloggerlograngenum_processesprocess_indexwait_for_everyone)	selflevelmsgargskwargsr	   r   r
   is	            r   r   zMultiProcessAdapter.log&   s    %++ S   #JJ':DAA::j%00,***U## 	. 122 
."ll377Vs<T<<<V<<<<< .$u233 . .AE///&*ll3&?&?V'sDTDDDVDDD++----	. 	.
. .. .r   Nc                       | j         |i | dS )a  
        This method is identical to `logger.warning()`, but will emit the warning with the same message only once

        Note: The cache is for the function arguments, so 2 different callers using the same arguments will hit the
        cache. The assumption here is that all warning messages are unique across the code. If they aren't then need to
        switch to another type of cache that includes the caller frame information in the hashing function.
        N)warning)r   r!   r"   s      r   warning_oncez MultiProcessAdapter.warning_onceI   s!     	d%f%%%%%r   )
__name__
__module____qualname____doc__staticmethodr   r   	functools	lru_cacher&    r   r   r   r      su          V V \V
!. !. !.F Y& & & & &r   r   name	log_levelc                 8   | t           j                            dd          }t          j        |           }|S|                    |                                           |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
    >>> from accelerate import Accelerator

    >>> logger = get_logger(__name__)

    >>> accelerator = Accelerator()
    >>> 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")

    >>> array = ["a", "b", "c", "d"]
    >>> letter_at_rank = array[accelerator.process_index]
    >>> logger.info(letter_at_rank, in_order=True)
    ```
    NACCELERATE_LOG_LEVEL)	osenvirongetlogging	getLoggersetLevelupperrootr   )r/   r0   r   s      r   
get_loggerr;   U   s    D JNN#94@@	t$$F	))***Y__..///vr***r   )N)	r,   r6   r3   r
   r   LoggerAdapterr   strr;   r.   r   r   <module>r>      s         				      <& <& <& <& <&'/ <& <& <&~(+ (+S (+S (+ (+ (+ (+ (+ (+r   