� %�g����N�ddlmZddlZddlZddlZddlZddlZddlmZddl m Z ddl m Z m Z ddlZddlmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*ddl+m,Z,e%��r ddl-m.cm/Z0e d � ��rddl1Z1e$d � ��rddl2Z2e"d � ��rddl3Z3e#d � ��rddl4Z4ej5e6��Z7dd �Z8d�Z9Gd�dej:��Z;e%��se<ne;Z=Gd�d��Z>Gd�d��Z?Gd�d��Z@dS)�)� annotationsN)�contextmanager)�partial)�Any�Callable�)�DistributedType� DynamoBackend�GradientAccumulationPlugin�check_cuda_fp8_capability�check_cuda_p2p_ib_support�deepspeed_required�get_ccl_version�get_cpu_distributed_information�get_int_from_env�is_ccl_available�is_datasets_available�is_deepspeed_available�is_fp8_available�is_habana_gaudi1�is_hpu_available�is_ipex_available�is_mlu_available�is_mps_available�is_musa_available�is_npu_available�is_sdaa_available�is_torch_xla_available�is_xccl_available�is_xpu_available�parse_choice_from_env�parse_flag_from_env�set_numa_affinity)�SageMakerDistributedTypeF)� check_device�return�boolc�"�tjikS)z� Checks if the `AcceleratorState` has been initialized from `Accelerator`. Same as `AcceleratorState.initialized`, but works as a module method. )�AcceleratorState� _shared_state���`/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/accelerate/state.py�is_initializedr.Os�� � )�R� /�/r,c��dS�Nr+)�args�kwargss r-� do_nothingr3Xs�� �4r,c�*�eZdZdZd d d�Zd d�Zd�ZdS) �ThreadLocalSharedDicta� Descriptor that holds a dict shared between instances of a class in the same thread. Note: Descriptors have slightly different semantics than just a dict field on its own. `PartialState(...)._shared_state` and `PartialState._shared_state` (instance vs class) give the same value: the underlying _storage dict. Likewise, `PartialState(...)._shared_state = {...}` overrides the _storage dict inside the descriptor as you would expect. However, `PartialState._shared_state = {}` actually replaces the descriptor object with a dict instead Thus, you should modify the _storage dict in-place (e.g. `_shared_state.clear()`). See Python documentation for an explanation of descriptors: https://docs.python.org/3/howto/descriptor.html This is required for using PyTorch/XLA with PJRT in multithreaded mode (required for TPU v2 and v3). See https://github.com/pytorch/xla/blob/r2.0/docs/pjrt.md#multithreading-on-tpu-v2v3 F� thread_localr'c��i|_dSr0��_storage)�selfr6s r-�__init__zThreadLocalSharedDict.__init__ms ���� � � r,Nc��|jSr0r8)r:�obj�objtypes r-�__get__zThreadLocalSharedDict.__get__ps ���}�r,c��||_dSr0r8)r:r=�values r-�__set__zThreadLocalSharedDict.__set__ss ���� � � r,�F)r6r'r0)�__name__� __module__� __qualname__�__doc__r;r?rBr+r,r-r5r5\sZ�������� �������������r,r5c��eZdZdZe��Zgd�Zd.d/d�Zd0d �Ze d ���Z e d1d ���Z e d ���Z e d1d ���Ze d1d���Ze d1d���Zd�Zd2d�Zed.d3d���Zed���Zed���Zd4d5d�Zd4d5d�Zd5d�Zd6d7d!�Zd6d8d#�Zd$�Ze d9d&���Z d:d;d)�Zd*�Zd4d+�Z d<d-�Z!dS)=� PartialStateaQ Singleton class that has information about the current training environment and functions to help with process control. Designed to be used when only process control and device execution states are needed. Does *not* need to be initialized from `Accelerator`. Args: cpu (`bool`, *optional*): Whether or not to force the script to execute on CPU. Will ignore any accelerators available if set to `True` and force the execution on the CPU. kwargs (additional keyword arguments, *optional*): Additional keyword arguments to pass to the relevent `init_process_group` function. Valid `kwargs` can be found in [`utils.InitProcessGroupKwargs`]. See the example section for detailed usage. **Available attributes:** - **device** (`torch.device`) -- The device to use. - **distributed_type** ([`~accelerate.state.DistributedType`]) -- The type of distributed environment currently in use. - **local_process_index** (`int`) -- The index of the current process on the current server. - **mixed_precision** (`str`) -- Whether or not the current script will use mixed precision, and if so the type of mixed precision being performed. (Choose from 'no','fp16','bf16 or 'fp8'). - **num_processes** (`int`) -- The number of processes currently launched in parallel. - **process_index** (`int`) -- The index of the current process. - **is_last_process** (`bool`) -- Whether or not the current process is the last one. - **is_main_process** (`bool`) -- Whether or not the current process is the main one. - **is_local_main_process** (`bool`) -- Whether or not the current process is the main one on the local node. - **debug** (`bool`) -- Whether or not the current script is being run in debug mode. Example: ```python from accelerate.utils import InitProcessGroupKwargs # To include `InitProcessGroupKwargs`, init then call `.to_kwargs()` kwargs = InitProcessGroupKwargs(...).to_kwargs() state = PartialState(**kwargs) ``` ) �_cpu�_mixed_precisionr*�backend�debug�device�distributed_type� fork_launched�local_process_index� num_processes� process_indexF�cpur'c �8�|j|_|j�s�||_d|_t j�dd��}|�tj |��nd|_ td��|_ |� dd��}d}|�Qt j�dd��dko,t j�d��tjk}|� dd��}|�|||��\}}|�||krt!d |�d |�����||_||_d } |�s�|jd k�r�t%t j�d d����dk�rgt j�dd��dkr�t'��st)d���ddlm} | ���sb|jdkrBt j�d d��} tj�d| ����| jd-|jd d�|��d} n�|jt6jt6jfvr�tj���sj|jdkrBt j�d d��} tj�d| ����tjjd-d|ji|��|jt6jt6jfv�r�tA��}tC|j"��t jd<tC|j#��t jd<tC|j$��t jd <tC|j%��t jd<t j�dd��sdt jd<t j�dd��s*|j%|j#kr|jdkrt!d���|j"|d<|j#|d <|jt6jkrxtMd!gd��dkrcddl'} t%| �(d �"��|j%z ��} | dkrd#} tj)| ��tUj+d$| �d%���tj���stjjd-d|ji|��|j�(t6j|_d#|_,d|_-d|_.�nW|jd kr�|���t_j0|j t_j1����t_j2��|_,t_j3��|_-tid�&��rt_j5��|_.n�t%t j�d d����|_.n�tj�6��|_,tj�7��|_-|�-t%t j�d d����n|j$|_.|���| rt6j8|_td'd ��rts|j.��|j j:d(kr9tw��s+d)t jvsd*t jvrtyd+���td,d��|_=dS).N�ACCELERATE_TORCH_DEVICE�ACCELERATE_DEBUG_MODE�_use_sagemaker_dp�ACCELERATE_USE_SAGEMAKER�false�true�%ACCELERATE_SAGEMAKER_DISTRIBUTED_TYPErLzYour assigned backend z is not avaliable, please use F�xla� LOCAL_RANK������ACCELERATE_USE_DEEPSPEEDz_DeepSpeed is not available => install it using `pip3 install deepspeed` or build it from sourcer)�comm�tcclzsdaa:)� dist_backend�auto_mpi_discoveryT�RANK� WORLD_SIZE�LOCAL_WORLD_SIZE� MASTER_PORT�29500� MASTER_ADDR�mpiz�Tried to launch on distributed with multinode, but `MASTER_ADDR` env was not set, please try exporting rank 0's hostname as `MASTER_ADDR`�rank� world_size�OMP_NUM_THREADS)�logicalrz4OMP_NUM_THREADS/MKL_NUM_THREADS unset, we set it at z to improve oob performance.�� check_is_tpu�ACCELERATE_CPU_AFFINITY�cuda�NCCL_P2P_DISABLE�NCCL_IB_DISABLEz�Using RTX 4000 series doesn't support faster communication broadband via P2P or IB. Please set `NCCL_P2P_DISABLE="1"` and `NCCL_IB_DISABLE="1" or use `accelerate launch` which will do this automatically.� FORK_LAUNCHEDr+)>r*�__dict__� initializedrJrL�os�environ�get�torchrNr"rM�popr$�NO�_prepare_backend� ValueErrorrO�intr� ImportError� deepspeedrar.�sdaa� set_device�init_distributedr � MULTI_XPU� MULTI_CPU� distributed�init_process_groupr�strrlrm� local_rank�local_world_sizer�psutil� cpu_count�set_num_threads�warnings�warnrRrSrQ�xm�set_replication�get_xla_supported_devices�xrt_world_size� get_ordinalr�get_local_ordinal�get_world_size�get_rank� DEEPSPEEDr#�typer �NotImplementedErrorrP)r:rTr2� env_device�use_sagemaker_dp�dist_information�original_backendrLrO� use_deepspeed�distr�r��num_cpu_threads_per_processs r-r;zPartialState.__init__�s����*�� ���@ ��D�I��D�L�����(A�4�H�H�J�6@�6L�%�,�z�2�2�2�RV�D�K�,�-D�E�E�D�J�%�z�z�*=�t�D�D� �#� ��'��J�N�N�#=�w�G�G�6�Q�o�� ���'N�O�O�Sk�Sn�n�!� &�z�z�)�T�:�:� �(,�(=�(=�c�CS�Ue�(f�(f� %�G�%��+��;K�0K�0K� �!s�:J�!s�!s�jq�!s�!s�t�t�t�"�D�L�$4�D� !�!�M�� ]�4�<�5�0�0��r�z�~�~�l�B�7�7�8�8�B�>�>��z�~�~�&@�'�J�J�f�T�T�5�7�7��"-�!B�#�#��;�:�:�:�:�:�#�2�2�4�4�q�#�|�v�5�5�-/�Z�^�^�L�"�-M�-M� � %� � 5� 5�6J�j�6J�6J� K� K� K�1�D�1�p�t�|�`e�p�p�io�p�p�p�(,� � ��-�o�6O�Q`�Qj�5k�k�k� %� 1� @� @� B� B�l� �<�6�1�1�)+���� �b�)I�)I�J�!�J�1�1�2F�*�2F�2F�G�G�G��)�<�\�\�T�\�\�U[�\�\�\��$��)B�O�D]�(^�^�^�#B�#D�#D� �%(�)9�)>�%?�%?�� �6�"�+.�/?�/J�+K�+K�� �<�(�+.�/?�/J�+K�+K�� �<�(�14�5E�5V�1W�1W�� �-�.��z�~�~�m�T�:�:�8�07�B�J�}�-�� ���}�d�;�;��(�9�=M�=X�X�X�� ��-�-�$�R����"2�!6��v��'7�'B��|�$��)�_�-F�F�F�(�*;�)<�a�@�@�A�E�E�!�M�M�M�25��(�(��(�7�7�:J�:[�[�3�3�/�3�a�7�7�67�3��)�*E�F�F�F��M�(�Oj�(�(�(���� �(�7�7�9�9�Y��%�8�X�X���X�QW�X�X�X��|�#�(7�(:��%�%&��"�%&��"�+,��(�(����&�&����!�!�!��"�4�;��0L�0N�0N�O�O�O�%'�%6�%8�%8��"�%'�^�%5�%5��"�)�t�<�<�<�U�/1�/C�/E�/E�D�,�,�/2�2�:�>�>�,�PR�3S�3S�/T�/T�D�,�,�%*�%6�%E�%E�%G�%G��"�%*�%6�%?�%?�%A�%A��"�=M�=U�C�� ���|�R�8�8�9�9�9�[k�[v��(� �O�O� � � �� B�(7�(A��%�#�#<�e�D�D� <�!�$�":�;�;�;��{��6�)�)�2K�2M�2M�)�%�R�Z�7�7�;L�TV�T^�;^�;^�-�6����1��!�D�D����r,r&r�c �~�d|j�|jr d|jznd�d|j�d|j�d|j�d|j�d� S) NzDistributed environment: z Backend: �z Num processes: z Process index: z Local process index: z Device: � )rOrLrRrSrQrN�r:s r-�__repr__zPartialState.__repr__9s��� '��(=� '�ae�am�?u� �PT�P\�@\�@\�su� '� '�"�0� '� '�"�0� '� '�%)�$<� '� '��{�  '� '� '� r,c�B�tj���dS�zCResets `_shared_state`, is used internally and should not be calledN)rIr*�clearr+r,r-� _reset_statezPartialState._reset_stateBs�� �"�(�(�*�*�*�*�*r,c��|jikS)z7Returns whether the `PartialState` has been initialized)r*r�s r-rxzPartialState.initializedGs���!�R�'�'r,c�B�|jtjko |jdkS)�P Whether the Accelerator is configured for distributed training r)rOr r~rRr�s r-�use_distributedzPartialState.use_distributedLs#�� �$��(:�:�U�t�?Q�TU�?U�Ur,c�(�|j|jdz kS)�3Returns whether the current process is the last oner)rSrRr�s r-�is_last_processzPartialState.is_last_processSs���!�T�%7�!�%;�;�;r,c�P�|jtjkr |jdkn|jS)�7Returns whether the current process is the main processr)rOr � MEGATRON_LMrSr�r�s r-�is_main_processzPartialState.is_main_processXs-��(,�'<��@[�'[�'[�D� �!� #� #�ae�au� r,c�P�|jtjkr |jdkn|jS)�IReturns whether the current process is the main process on the local noder)rOr r�rQr�r�s r-�is_local_main_processz"PartialState.is_local_main_process_s0�� �$��(C�C�C� � $�� )� )��%� r,c ��|jtjtjtjtjtjtjtjtj tj tj f vr tj ���dS|jtjkrt!jd��dSdS)a� Will stop the execution of the current process until every other process has reached that point (so this does nothing when the script is only run in one process). Useful to do before saving a model. Example: ```python >>> # Assuming two GPU processes >>> import time >>> from accelerate.state import PartialState >>> state = PartialState() >>> if state.is_main_process: ... time.sleep(2) >>> else: ... print("I'm waiting for the main process to finish its sleep...") >>> state.wait_for_everyone() >>> # Should print on every process at the same time >>> print("Everyone is here") ``` z"accelerate.utils.wait_for_everyoneN)rOr � MULTI_GPU� MULTI_MLU� MULTI_SDAA� MULTI_MUSA� MULTI_NPUr�r�� MULTI_HPUr��FSDPr|r��barrier�XLAr�� rendezvousr�s r-�wait_for_everyonezPartialState.wait_for_everyonehs���, � � � %� � %� � &� � &� � %� � %� � %� � %� � %� � � % � � � � � %� %� '� '� '� '� '� � "�o�&9� 9� 9� �M�>� ?� ?� ?� ?� ?�:� 9r,�is_mainc#�nK�|s|���dV�|r|���dSdSr0)r�)r:r�s r-� _goes_firstzPartialState._goes_first�sS����� %� � "� "� $� $� $� ���� � %� � "� "� $� $� $� $� $� %� %r,�inputs�"list | tuple | dict | torch.Tensor� apply_paddingc#�2�����K��jdkr|V�dSt|���t|t��rvt|t |�����d���t �fd�|���D����std���t��j��\�}�j �zt�j |��z}|�z�j |krdndz}����fd���|||��V�dS)a� Splits `input` between `self.num_processes` quickly and can be then used on that process. Useful when doing distributed inference, such as with different prompts. Note that when using a `dict`, all keys need to have the same number of elements. Args: inputs (`list`, `tuple`, `torch.Tensor`, `dict` of `list`/`tuple`/`torch.Tensor`, or `datasets.Dataset`): The input to split between processes. apply_padding (`bool`, `optional`, defaults to `False`): Whether to apply padding by repeating the last element of the input so that all processes have the same number of elements. Useful when trying to perform actions such as `gather()` on the outputs or passing in less inputs than there are processes. If so, just remember to drop the padded elements afterwards. Example: ```python # Assume there are two processes from accelerate import PartialState state = PartialState() with state.split_between_processes(["A", "B", "C"]) as inputs: print(inputs) # Process 0 ["A", "B"] # Process 1 ["C"] with state.split_between_processes(["A", "B", "C"], apply_padding=True) as inputs: print(inputs) # Process 0 ["A", "B"] # Process 1 ["C", "C"] ``` rNrc3�>�K�|]}t|���kV��dSr0)�len)�.0�v�lengths �r-� <genexpr>z7PartialState.split_between_processes.<locals>.<genexpr>�s.�����A�A�A�s�1�v�v��'�A�A�A�A�A�Ar,z6All values in the dictionary must have the same lengthc���t|tttjf��r�|t |��kr |dd�}n |||�}� rit|tj��r-ddlm}m}||� j ��}|||d���}n"||dg� dzt |��z zz }|St|t��r/|� ��D]}� ||||��||<�|St��r�ddl m}t||��r�|t |��krt |��dz }|t |��krt |��}tt||����} � r| |dz g� dzt | ��z zz } |�| ��S|S)Nr_r)�pad_across_processes�send_to_device)� pad_indexr)�Dataset)� isinstance�list�tupler|�Tensorr��accelerate.utilsr�r�rN�dict�keysr�datasetsr��range�select)r�� start_index� end_index�resultr�r��tensorized_result�keyr�� result_idcs� _split_valuesr��num_samples_per_processr:s ����r-r�z;PartialState.split_between_processes.<locals>._split_values�s����&�4��� �"=�>�>� ��#�f�+�+�-�-�#�B�C�C�[�F�F�#�K� �$9�:�F� �]�!�&�%�,�7�7�]�Y�Y�Y�Y�Y�Y�Y�Y�-;�N�6�4�;�,O�,O�)�!5�!5�6G�SY�Z\�S]�!^�!^�!^����6�"�:�,�2I�A�2M�PS�TZ�P[�P[�2[�"\�\��� ��F�D�)�)� �!�;�;�=�=�U�U�C�"/�-��s� �[�)�"T�"T�F�3�K�K�� �(�*�*� :�0�0�0�0�0�0�!�&�'�2�2�:�&�#�f�+�+�5�5�*-�f�+�+��/�K�$�s�6�{�{�2�2�(+�F� � �I�&*�5��i�+H�+H�&I�&I� �(�n�'�I��M�?�>U�XY�>Y�\_�`k�\l�\l�>l�+m�m�K�%�}�}�[�9�9�9�� r,) rRr�r�r�r�r��all�valuesr��divmodrS�min) r:r�r�� num_extrasr�r�r�r�r�s ` ` @@@r-�split_between_processesz$PartialState.split_between_processes�sS���������N � �� "� "��L�L�L� �F��V���� �f�d� #� #� [����V�[�[�]�]� 3� 3�A� 6�7�8�8�F��A�A�A�A������A�A�A�A�A� [� �!Y�Z�Z�Z�.4�V�T�=O�.P�.P�+����(�+B�B�S��I[�]g�Eh�Eh�h� ��"9�9�$�BT�Wa�Ba�Ba�Q�Q�gh�i� �! �! �! �! �! �! �! �! �F�m�F�K��;�;�;�;�;�;�;r,c#�JK�|�|j��Ed{V��dS)a1 Lets the main process go first inside a with block. The other processes will enter the with block after the main process exits. Example: ```python >>> from accelerate import Accelerator >>> accelerator = Accelerator() >>> with accelerator.main_process_first(): ... # This will be printed first by process 0 then in a seemingly ... # random order by the other processes. ... print(f"This will be printed by process {accelerator.process_index}") ``` N)r�r�r�s r-�main_process_firstzPartialState.main_process_first�s7����&�#�#�D�$8�9�9�9�9�9�9�9�9�9�9�9r,c#�JK�|�|j��Ed{V��dS)a9 Lets the local main process go inside a with block. The other processes will enter the with block after the main process exits. Example: ```python >>> from accelerate.state import PartialState >>> state = PartialState() >>> with state.local_main_process_first(): ... # This will be printed first by local process 0 then in a seemingly ... # random order by the other processes. ... print(f"This will be printed by process {state.local_process_index}") ``` N)r�r�r�s r-�local_main_process_firstz%PartialState.local_main_process_firsts7����&�#�#�D�$>�?�?�?�?�?�?�?�?�?�?�?r,N�function�Callable[..., Any]c�\�|jstd���|js|js|StS)a Decorator that only runs the decorated function on the main process. Args: function (`Callable`): The function to decorate. Example: ```python >>> from accelerate.state import PartialState >>> state = PartialState() >>> @state.on_main_process ... def print_something(): ... print("This will be printed by process 0 only.") >>> print_something() "This will be printed by process 0 only" ``` zUThe `PartialState` or `Accelerator` must be initialized before calling this function.)rxr�r�r�r3�r:r�s r-�on_main_processzPartialState.on_main_processs?��0�� v��t�u�u� u� � � �t�';� ��O��r,c�0�|js|js|StS)a� Decorator that only runs the decorated function on the local main process. Args: function (`Callable`): The function to decorate. Example: ```python # Assume we have 2 servers with 4 processes each. from accelerate.state import PartialState state = PartialState() @state.on_local_main_process def print_something(): print("This will be printed by process 0 only on each server.") print_something() # On server 1: "This will be printed by process 0 only" # On server 2: "This will be printed by process 0 only" ``` )r�r�r3r�s r-�on_local_main_processz"PartialState.on_local_main_process8s$��6 � %� �T�-A� ��O��r,c�0�|js|js|StS)a Decorator that only runs the decorated function on the last process. Args: function (`Callable`): The function to decorate. Example: ```python # Assume we have 4 processes. from accelerate.state import PartialState state = PartialState() @state.on_last_process def print_something(): print(f"Printed on process {state.process_index}") print_something() "Printed on process 3" ``` )r�r�r3r�s r-�on_last_processzPartialState.on_last_processWs$��0 � � �t�';� ��O��r,rSr�c�h�|�t|j|���S|j|ks|js|StS)a� Decorator that only runs the decorated function on the process with the given index. Args: function (`Callable`, `optional`): The function to decorate. process_index (`int`, `optional`): The index of the process on which to run the function. Example: ```python # Assume we have 4 processes. from accelerate.state import PartialState state = PartialState() @state.on_process(process_index=2) def print_something(): print(f"Printed on process {state.process_index}") print_something() "Printed on process 2" ``` N)rS)r� on_processrSr�r3)r:r�rSs r-rzPartialState.on_processss@��6 � ��4�?�-�H�H�H� H� � �-� /� /��9M� /��O��r,rQc�h�|�t|j|���S|j|ks|js|StS)aO Decorator that only runs the decorated function on the process with the given index on the current node. Args: function (`Callable`, *optional*): The function to decorate. local_process_index (`int`, *optional*): The index of the local process on which to run the function. Example: ```python # Assume we have 2 servers with 4 processes each. from accelerate import Accelerator accelerator = Accelerator() @accelerator.on_local_process(local_process_index=2) def print_something(): print(f"Printed on process {accelerator.local_process_index}") print_something() # On server 1: "Printed on process 2" # On server 2: "Printed on process 2" ``` N)rQ)r�on_local_processrQr�r3)r:r�rQs r-rzPartialState.on_local_process�sC��< � ��4�0�FY�Z�Z�Z� Z� � $�(;� ;� ;�T�EY� ;��O��r,c�0�|jrt|i|��dSdSr0)r��print�r:r1r2s r-rzPartialState.print�s0�� � %� #� �4� "�6� "� "� "� "� "� #� #r,� torch.devicec��t��r#dtjd<tjd��St ��rtjd��St ��rtjd��St��rtjd��St��rtjd��St��rtjd��Stj � ��rtjd ��St��rtjd ��Stjd ��S) a� Returns the default device which is: - MPS if `torch.backends.mps.is_available()` and `torch.backends.mps.is_built()` both return True. - CUDA if `torch.cuda.is_available()` - MLU if `is_mlu_available()` - SDAA if `is_sdaa_available()` - MUSA if `is_musa_available()` - NPU if `is_npu_available()` - HPU if `is_hpu_available()` - CPU otherwise �1�PYTORCH_ENABLE_MPS_FALLBACK�mps�mlur��musa�npu�hpurs�xpurT) rryrzr|rNrrrrrrs� is_availabler r�s r-�default_devicezPartialState.default_device�s�� � � � '�8;�B�J�4� 5��<��&�&� &� � � � '��<��&�&� &� � � � '��<��'�'� '� � � � '��<��'�'� '�� � � '��<��&�&� &� � � � '��<��&�&� &� �Z� $� $� &� &� '��<��'�'� '� � � � '��<��&�&� &��<��&�&� &r,rL�tuple[str, DistributedType]c��d}|rddl}d}tj}�n@t��rd}tj}�n"t t j�dd����dkr�|s�t��rd}tj }t��rd}tj }n�t��rd }tj}n�t��rd }tj}n|t#d � ��r|�d }tj}n[t&j���r|�d }tj}n,t-��rt/��r|�d}tj}|��t t j�dd����dkst3gd�d��dkr�|st-��r tj}n tj}|dvrQt7��rCt3dgd��dks|tjkrt9��dkrddl}nddl}d}n'|dvr!t&j� ��rd}nd}|� tj!}||fS)zhPrepares any imports needed before initializing the distributed backend and sets `self.backend` properlyNr�smddpr]r^r_�cnclrb�mccl�hcclT)� init_hccl�nccl�xccl)�PMI_SIZE�OMPI_COMM_WORLD_SIZE�MV2_COMM_WORLD_SIZErfr)N�ccl�CCL_WORKER_COUNTz1.12r)Nrkrk�gloo)"�,smdistributed.dataparallel.torch.torch_smddpr r�rr�r�ryrzr{rr�rr�rr�rr�rr�r|rsrr rr�rr�rr�oneccl_bindings_for_pytorch� torch_cclr��is_mpi_availabler~)r:rT� sagemaker_dprLrO� smdistributedr"r#s r-rzPartialState._prepare_backend�s��� �� �# =� ?� ?� ?� ?��G�.�8� � � #� %� %� =��G�.�2� � � ����� �b�1�1� 2� 2�b� 8� 8�� 8��!�!� =� ��#2�#<� � �"�"� =� ��#2�#=� � �"�$�$� =� ��#2�#=� � �"�#�#� =� ��#2�#<� � �!�D�1�1�1� =��?�$�G�#2�#<� � ���(�(�*�*� =��?�$�G�#2�#<� � �!�#�#� =�(9�(;�(;� =��?�$�G�#2�#<� � � #� �� ���|�R�0�0� 1� 1�R� 7� 7�� i� i� i�kl�m�m�pq�q�q�� =�+�-�-� =�#2�#<� � �#2�#<� ��=�(�(�$�&�&�)�%�'9�&:�A�>�>��B�B�FV�Zi�Zs�Fs�Fs�"�$�$��.�.�6�6�6�6�6�$�$�$�$�����M�)�)�e�.?�.P�.P�.R�.R�)���� �� � #�.�1� ��(�(�(r,c��|j�dS|jtjkr)|jrt jd��n|j|_dSt|j���d��d� dd��� ��}|dvrtd|j�d |�d ����|d krtj ��|_dS|d kr8t jd t j�����|_dS|d krd}t!t |��}|j|���z}t j||��|_|�|j��dS)zZ Sets the device in `self.device` to the current distributed environment. NrT�.r_�MULTI_r�) rT�gpur r r rr]rr�zCan't set device for z (z6), verify we should be calling `_set_device()` for it!r]rr*rs)rNrOr r~rJr|rr��split�replace�lowerr�r�� xla_devicer�current_device�getattrrQ� device_countr�)r:rN� device_module� device_indexs r-r�zPartialState.set_device&sl�� �;� "� �F� � �O�$6� 6� 6�15��S�%�,�u�-�-�-��@S�D�K� �F��T�*�+�+�1�1�#�6�6�r�:�B�B�8�R�P�P�V�V�X�X�� �Z� Z� Z����(=��������� � �U�?�?��-�/�/�D�K�K�K� �u�_�_��,�u�e�i�.F�.F�.H�.H�I�I�D�K�K�K�������#�E�6�2�2�M��3�m�6P�6P�6R�6R�R�L��,�v�|�<�<�D�K� � $� $�T�[� 1� 1� 1� 1� 1r,c��|jr|�dStj���r!tj�|��dSdS)zn Destroys the process group. If one is not specified, the default process group is destroyed. N)rPr|r�r.�destroy_process_group�r:�groups r-r5z"PartialState.destroy_process_group@sV�� � � �%�-� �F� � � +� +� -� -� ;� � � 3� 3�E� :� :� :� :� :� ;� ;r,�namec�`�||jvrtd|�d����td|�d����)Nz(`PartialState` object has no attribute `zy`. This happens if `PartialState._reset_state()` was called and an `Accelerator` or `PartialState` was not reinitialized.z('PartialState' object has no attribute '�'�� _known_attrs�AttributeError�r:r8s r-� __getattr__zPartialState.__getattr__JsZ�� �4�$� $� $� �L�4�L�L�L��� � �O��O�O�O�P�P�Pr,rC)rTr')r&r��r&r')r�r'�r�r�r�r'r0)r�r��NN)r�r�rSr�)r�r�rQr�)r&r)FFN)rTr'rLr�r&r�r8r�)"rDrErFrG� SharedDictr*r<r;r�� staticmethodr��propertyrxr�r�r�r�r�r�rr�r�r�r�r�r�rrrrrr�r5r?r+r,r-rIrI|s�������$�$�L�J�L�L�M� � � �L�EE�EE�EE�EE�EE�N � � � ��+�+��\�+��(�(�(��X�(��V�V��X�V� �<�<�<��X�<�� � � ��X� � � � � ��X� �$@�$@�$@�L%�%�%�%��V<�V<�V<�V<��^�V<�p�:�:��^�:�(�@�@��^�@�(�����<�����>����8�����B"�"�"�"�"�H#�#�#�� '� '� '��X� '�FEI�E)�E)�E)�E)�E)�N2�2�2�4;�;�;�;� Q� Q� Q� Q� Q� Qr,rIc���eZdZdZe��Zejgd�zZ d&d'd �Ze d(d ���Z d �Z d)d�Z e d���Z ed*d+d���Zd,d�Ze d���Ze d���Ze d(d���Ze d(d���Ze d(d���Ze d(d���Zd�Zed*d-d���Zed���Zed���Ze d ���Zed.d"���Zed,d.d#���Zd$�Z d.d%�Z!dS)/r)a Singleton class that has information about the current training environment. **Available attributes:** - **device** (`torch.device`) -- The device to use. - **distributed_type** ([`~accelerate.state.DistributedType`]) -- The type of distributed environment currently in use. - **initialized** (`bool`) -- Whether or not the `AcceleratorState` has been initialized from `Accelerator`. - **local_process_index** (`int`) -- The index of the current process on the current server. - **mixed_precision** (`str`) -- Whether or not the current script will use mixed precision, and if so the type of mixed precision being performed. (Choose from 'no','fp16','bf16 or 'fp8'). - **num_processes** (`int`) -- The number of processes currently launched in parallel. - **process_index** (`int`) -- The index of the current process. - **is_last_process** (`bool`) -- Whether or not the current process is the last one. - **is_main_process** (`bool`) -- Whether or not the current process is the main one. - **is_local_main_process** (`bool`) -- Whether or not the current process is the main one on the local node. - **debug** (`bool`) -- Whether or not the current script is being run in debug mode. )�deepspeed_plugin�use_ipex� fsdp_plugin�megatron_lm_plugin� dynamo_pluginNF�mixed_precisionr�rTr'�_from_acceleratorc  �> �|j|_td��rd}tjikr t|fi| ��|j�tj��|�||��|j�s�d|_d|_||_ |�tdd��n|� ��}|dkr�t��std���tj���rKt#��s=t$�dtj����d���d }n*t+��rt$�d ��d }||_|std ���|jt0jkrdn||_|jt0jkr�t9d� ��r�|d kr�t:j�d��rAtAd��t:jd<tAd��t:jd<d|_!�n8tAd��t:jd<tAd��t:jd<d|_!�n�t:j�dd��dkr|s||_t0j|_�n�|jt0j"t0j#t0j$t0j%t0j&t0j't0j(fv�r t:j�dd��dks|�=t0j)|_|jdkr|�*|j��||_+t:j�dd��dkrF|jt0j'fvr2t0j,|_|�*|j��||_-t:j�dd��dks|j �t0j.|_nV|jt0j/t0j't0j0fvr,tc��rtdd���|_nd|_|jj2tfj0kr6|jdkr+|j4j5dkrdtj6jj7_8|jj2tfj0kr6|jdkr+|j4j5dkrdtj6j9j7_8|jtjd<dSdS)N�ACCELERATE_USE_CPUT�ACCELERATE_MIXED_PRECISION�no�fp8zPUsing `fp8` precision requires `transformer_engine` or `MS-AMP` to be installed.z-The current device has compute capability of z� which is insufficient for FP8 mixed precision training (requires a GPU Hopper/Ada Lovelace or higher, compute capability of 8.9 or higher). Will use FP16 instead.�fp16z�The current HPU device is Gaudi1 which does not support FP8 mixed precision training (requires Gaudi2 or higher). Will use BF16 instead.�bf16z�Please make sure to properly initialize your accelerator via `accelerator = Accelerator()` before using any functionality from the `accelerate` library.rp�ACCELERATE_DOWNCAST_BF16r� XLA_USE_BF16r�XLA_DOWNCAST_BF16Fr`rZr[�ACCELERATE_USE_FSDP�ACCELERATE_USE_MEGATRON_LM�ACCELERATE_USE_TP�ACCELERATE_USE_IPEX��defaultrsr rO):r*rwr"rI�update�_check_initializedrx�deepspeed_pluginsrI�torch_tp_pluginr!r-rr�r|rsrr �logger�warning�get_device_capabilityrrLrOr r�rKr�rryrzr{r��downcast_bfloatr�r�r�r�r�r�r�r��set_mixed_precisionrJr�rK�TPr�r~rrLr rNr��backends�matmul� allow_tf32r ) r:rMrTrLrHrJrbrKrNr2s r-r;zAcceleratorState.__init__us����*�� � �3� 4� 4� ��C� � %�� +� +� �� '� '�� '� '� '� � ���\�7�8�8�8� �����5�5�5���Z S�%)�D� "� �D�M�#2�D� �#�*�&�&B�D�I�I�I�$�*�*�,�,� � �%�'�'�'�)�)�-�$�j�����Z�,�,�.�.� -�7P�7R�7R� -��N�N�b�� �Hh�Hh�Hj�Hj�b�b�b���� '-�O�O�%�'�'�-��N�N�D����'-�O�!.�D� �$� � �T���� -1�,A�_�E^�,^�,^�D�D�ds�D� !��$��(;�;�;�@V�dh�@i�@i�@i�;�"�f�,�,��z�~�~�&@�A�A�5�58��V�V�� �>�2�:=�a�&�&�� �#6�7�/3��,�,�58��V�V�� �>�2�:=�a�&�&�� �#6�7�/4��,������ :�G�D�D��N�N�WZ�N�)9��&�(7�(A��%�%��&��)��)��*��*��)��)��)�+����:�>�>�"7��A�A�V�K�K�{�Of�,;�,@�D�)��,��4�4�#�7�7��8M�N�N�N�'2�D�$��:�>�>�">��H�H�F�R�R�W[�Wl�#�-�u�X�X�-<�,G�D�)�&�:�:�4�;P�Q�Q�Q�.@�D�+��:�>�>�"5�w�?�?�6�I�I�T�Ma�Mm�,;�,>�D�)���&�?�+D�o�F_�ap�as�*t�t�t�$�&�&�*�$7�8M�W[�$\�$\�$\�D�M�M�$)�D�M��"�*�m�.>�>�>��)�T�1�1��K�$��.�.�8<���#�*�5��"�*�m�.>�>�>��)�T�1�1��K�$��.�.�8<���#�*�5�=A�=R�L� &�'9� :� :� :�uZ S�Z Sr,r&c�,�|jtjkSr0)r*rIr�s r-rxzAcceleratorState.initialized�s���!�\�%?�?�?r,c��t�����d|j�d�z}|jtjkr|d|jj�d�z }|S)Nz Mixed precision type: r�z ds_config: )rIr�rMrOr r�rH�deepspeed_config)r:�reprs r-r�zAcceleratorState.__repr__�s]���~�~�&�&�(�(�+^�d�FZ�+^�+^�+^�^�� � �O�$=� =� =� �L�$�"7�"H�L�L�L� L�D�� r,c� �|jr�d}|r3|jjdkr#t|�d������|�I||jkr@|jtjkr-t|�d|�d�������dSdSdSdS)zeChecks if a modification is trying to be made and the `AcceleratorState` has already been initializedz�AcceleratorState has already been initialized and cannot be changed, restart your runtime completely and pass `{flag}` to `Accelerator()`.rTzcpu=True)�flagNzmixed_precision='r:) rxrNr�r��formatrKrOr r�)r:rMrT�errs r-r`z#AcceleratorState._check_initialized�s��� � � Z�_�C�� >�t�{�'�5�0�0� ������!<�!<�=�=�=��+�#�t�'<�<�<��)�_�-F�F�F� ����1W�_�1W�1W�1W��!X�!X�Y�Y�Y� Z� Z� ,�+�<�<�F�Fr,c��|jtjkri|jj}|�di���dd��rd}n7|�di���dd��rd}n d}n|j}|S)NrT�enabledFrUrR)rOr r�rHrnr{rK)r:�configrMs r-rMz AcceleratorState.mixed_precision�s��� � �O�$=� =� =��*�;�F��z�z�&�"�%�%�)�)�)�U�;�;� '�"(������F�B�'�'�+�+�I�u�=�=� '�"(���"&���"�3�O��r,�reset_partial_statec�|�tj���|rt���dSdSr�)r)r*r�rIr�)rws r-r�zAcceleratorState._reset_state sC�� �&�,�,�.�.�.� � (� � %� %� '� '� '� '� '� (� (r,c�H�t���|��dS)z� Destroys the process group. If one is not specified, the default process group is destroyed. If `self.fork_lauched` is `True` and `group` is `None`, nothing happens. N)rIr5r6s r-r5z&AcceleratorState.destroy_process_groups"�� ���,�,�U�3�3�3�3�3r,c�(�t��jSr0)rIrPr�s r-rPzAcceleratorState.fork_launcheds���~�~�+�+r,c�(�t��jS)r�)rIr�r�s r-r�z AcceleratorState.use_distributeds�� �~�~�-�-r,c�L�|jtjko|jjdkS)N�)rOr r�rJ� fsdp_versionr�s r-�is_fsdp2zAcceleratorState.is_fsdp2#s$���$��(<�<�c��AQ�A^�bc�Ac�cr,c�(�t��jS)r�)rIr�r�s r-r�z AcceleratorState.is_last_process'����~�~�-�-r,c�(�t��jS)r�)rIr�r�s r-r�z AcceleratorState.is_main_process,r�r,c�(�t��jS)r�)rIr�r�s r-r�z&AcceleratorState.is_local_main_process1s���~�~�3�3r,c�F�t�����dSr0)rIr�r�s r-r�z"AcceleratorState.wait_for_everyone6s�����(�(�*�*�*�*�*r,r�r�r�c#�K�t���||���5}|V�ddd��dS#1swxYwYdS)a� Splits `input` between `self.num_processes` quickly and can be then used on that process. Useful when doing distributed inference, such as with different prompts. Note that when using a `dict`, all keys need to have the same number of elements. Args: inputs (`list`, `tuple`, `torch.Tensor`, or `dict` of `list`/`tuple`/`torch.Tensor`): The input to split between processes. apply_padding (`bool`, `optional`, defaults to `False`): Whether to apply padding by repeating the last element of the input so that all processes have the same number of elements. Useful when trying to perform actions such as `gather()` on the outputs or passing in less inputs than there are processes. If so, just remember to drop the padded elements afterwards. Example: ```python # Assume there are two processes from accelerate.state import AcceleratorState state = AcceleratorState() with state.split_between_processes(["A", "B", "C"]) as inputs: print(inputs) # Process 0 ["A", "B"] # Process 1 ["C"] with state.split_between_processes(["A", "B", "C"], apply_padding=True) as inputs: print(inputs) # Process 0 ["A", "B"] # Process 1 ["C", "C"] ``` )r�N)rIr�)r:r�r�s r-r�z(AcceleratorState.split_between_processes9s�����N�^�^� 3� 3�F�-� 3� X� X� �\b��L�L�L� � � � � � � � � � � � ���� � � � � � s �8�<�<c#�K�t�����5dV�ddd��dS#1swxYwYdS)z� Lets the main process go first inside a with block. The other processes will enter the with block after the main process exits. N)rIr�r�s r-r�z#AcceleratorState.main_process_firstcs������^�^� .� .� 0� 0� � � �E�E�E� � � � � � � � � � � � ���� � � � � � � �5�9�9c#�K�t�����5dV�ddd��dS#1swxYwYdS)z� Lets the local main process go inside a with block. The other processes will enter the with block after the main process exits. N)rIr�r�s r-r�z)AcceleratorState.local_main_process_firstms������^�^� 4� 4� 6� 6� � � �E�E�E� � � � � � � � � � � � ���� � � � � � r�c�R�|jtjkrdSddlm}||��S)zp Returns the currently active DeepSpeedPlugin. If not using deepspeed, returns `None`. Nr)�get_active_deepspeed_plugin)rOr r��accelerate.utils.deepspeedr�)r:r�s r-rHz!AcceleratorState.deepspeed_pluginws=�� � �O�$=� =� =��4�J�J�J�J�J�J�*�*�4�0�0�0r,r8c��|j|S)zH Returns the DeepSpeedPlugin with the given plugin_key. )rar>s r-�get_deepspeed_pluginz%AcceleratorState.get_deepspeed_plugin�s�� �%�d�+�+r,c��|j���D]\}}||kr|���� |j|�d���dS)zj Activates the DeepSpeedPlugin with the given `name`, and will disable all other plugins. T)�_from_accelerator_stateN)ra�items� _unselectr�)r:r8r��plugins r-�select_deepspeed_pluginz(AcceleratorState.select_deepspeed_plugin�sg��  �1�7�7�9�9� #� #�K�C���d�{�{�� � �"�"�"�� ��t�$�+�+�D�+�I�I�I�I�Ir,c�8�t��j|i|��dSr0)rIrrs r-rzAcceleratorState.print�s$��� ����d�-�f�-�-�-�-�-r,c�`�||jvrtd|�d����td|�d����)Nz,`AcceleratorState` object has no attribute `z}`. This happens if `AcceleratorState._reset_state()` was called and an `Accelerator` or `PartialState` was not reinitialized.z,'AcceleratorState' object has no attribute 'r:r;r>s r-r?zAcceleratorState.__getattr__�sZ�� �4�$� $� $� �L�t�L�L�L��� � �S�D�S�S�S�T�T�Tr,)NFNNNNNF)rMr�rTr'rNr'r@rBrC)rwr'r0rArC)"rDrErFrGrDr*rIr<r;rFrxr�r`rMrEr�r5rPr�rr�r�r�r�rr�r�r�rHrr�r�rr?r+r,r-r)r)Ws���������(�J�L�L�M��,�0�0�0��L� $�������"'�mS�mS�mS�mS�mS�^�@�@�@��X�@���� Z� Z� Z� Z�� � ��X� ��(�(�(�(��\�(� 4�4�4�4��,�,��X�,��.�.��X�.� �d�d�d��X�d��.�.�.��X�.��.�.�.��X�.��4�4�4��X�4�+�+�+��'�'�'�'��^�'�R����^������^��� 1� 1��X� 1��,�,�,���,� �J�J�J�J���J�.�.�.� U� U� U� U� U� Ur,r)c��eZdZdZe��Zddd�Zedd���Zedd ���Z edd ���Z edd ���Z edd ���Z edd���Z d�Zed���Zejd���Zd�Zd�Zd�Zed���Zed���Zejd���Zedd���Zed���ZdS)� GradientStatea� Singleton class that has information related to gradient synchronization for gradient accumulation **Available attributes:** - **end_of_dataloader** (`bool`) -- Whether we have reached the end the current dataloader - **remainder** (`int`) -- The number of extra samples that were added from padding the dataloader - **sync_gradients** (`bool`) -- Whether the gradients should be synced across all devices - **active_dataloader** (`Optional[DataLoader]`) -- The dataloader that is currently being iterated over - **dataloader_references** (`List[Optional[DataLoader]]`) -- A list of references to the dataloaders that are being iterated over - **num_steps** (`int`) -- The number of steps to accumulate over - **adjust_scheduler** (`bool`) -- Whether the scheduler should be adjusted to account for the gradient accumulation - **sync_with_dataloader** (`bool`) -- Whether the gradients should be synced at the end of the dataloader iteration and the number of total steps reset - **is_xla_gradients_synced** (`bool`) -- Whether the XLA gradients have been synchronized. It is initialized as false. Once gradients have been reduced before the optimizer step, this flag is set to true. Subsequently, after each step, the flag is reset to false. FSDP will always synchronize the gradients, hence is_xla_gradients_synced is always true. N�gradient_accumulation_plugin�!GradientAccumulationPlugin | Nonec� �|j|_|js3d|_dg|_|�|���ni|_d|_|�8|j|���kr|���|_dSdSdS)NTF)r*rwrx�sync_gradients�_dataloader_references_ref� to_kwargs� plugin_kwargs�_is_xla_gradients_synced)r:r�s r-r;zGradientState.__init__�s����*�� ��� 2�"&�D� �/3�f�D� +�<X�<d�,�6�6�8�8�8�jl� � �-2�D� )� (� 3��8J�Nj�Nt�Nt�Nv�Nv�8v�8v�!=�!G�!G�!I�!I�D� � � � 4� 3�8v�8vr,r&r�c�8�|j�dd��S)z.Returns the number of steps to accumulate over� num_stepsr�r�r{r�s r-r�zGradientState.num_steps�s���!�%�%�k�1�5�5�5r,r'c�8�|j�dd��S)z0Returns whether the scheduler should be adjusted�adjust_schedulerFr�r�s r-r�zGradientState.adjust_scheduler�s���!�%�%�&8�%�@�@�@r,c�8�|j�dd��S)zyReturns whether the gradients should be synced at the end of the dataloader iteration and the number of total steps reset�sync_with_dataloaderTr�r�s r-r�z"GradientState.sync_with_dataloader�s���!�%�%�&<�d�C�C�Cr,c�"�tjikS)z8Returns whether the `GradientState` has been initialized)r�r*r�s r-rxzGradientState.initialized�s���*�b�0�0r,c�,�|jsdS|jjS)zAReturns whether we have reached the end of the current dataloaderF)� in_dataloader�active_dataloader�end_of_dataloaderr�s r-r�zGradientState.end_of_dataloader�s ���!� ��5��%�7�7r,c�,�|jsdS|jjS)zOReturns the number of extra samples that were added from padding the dataloaderr_)r�r�� remainderr�s r-r�zGradientState.remainder�s ���!� ��2��%�/�/r,c �H�d|j�d|j�d|j�d|j�d� S)NzSync Gradients: z At end of current dataloader: z Extra samples added: z Gradient accumulation plugin: r�)r�r�r�r�r�s r-r�zGradientState.__repr__�s[�� D�t�2� D� D�-1�-C� D� D�$(�N� D� D�.2�-?� D� D� D� r,c�6�tdd���rdS|jS)z�Returns the value of is_xla_gradients_synced. FSDP will always synchronize the gradients, hence is_xla_gradients_synced is always true.rYFr]T)r"r�r�s r-�is_xla_gradients_syncedz%GradientState.is_xla_gradients_synced�s(�� �4�e� D� D� D� ��4��,�,r,c��||_dS)z+Set the _is_xla_gradients_synced attribute.N)r�)r:� is_synceds r-r�z%GradientState.is_xla_gradients_synced�s��)2��%�%�%r,c��||_|jrFtd���r8t��jtjkrt j��dSdSdSdS)zhPrivate function that sets whether gradients should be synchronized. Users should not have to call this.TrpN)r�rrIrOr r�r�� mark_step)r:r�s r-�_set_sync_gradientsz!GradientState._set_sync_gradientssm��,��� � � �&�D�9�9�9� ����/�?�3F�F�F� �L�N�N�N�N�N�  � � � �G�Fr,c�(�|xj|gz c_dS)z�Private function that adds a dataloader to `self.dataloader_references` and sets `in_dataloader` to `True`. Users should not have to call this.N��dataloader_references�r:� dataloaders r-�_add_dataloaderzGradientState._add_dataloaders �� �"�"�z�l�2�"�"�"�"r,c�8���fd�|jD��|_dS)z�Private function that removes a dataloader from `self.dataloader_references` and sets `in_dataloader` to `False` if there are no more dataloaders. Users should not have to call this.c� ��g|] }|�k�|�� Sr+r+)r��dataloader_refr�s �r-� <listcomp>z4GradientState._remove_dataloader.<locals>.<listcomp>s+���& �& �& �-�~�ak�Ok�Ok�N�Ok�Ok�Okr,Nr�r�s `r-�_remove_dataloaderz GradientState._remove_dataloaders7���& �& �& �& �15�1K�& �& �& ��"�"�"r,c��|jdS)Nr_r�r�s r-r�zGradientState.active_dataloaders���)�"�-�-r,c�$�d�|jD��S)Nc�*�g|]}|� |��n|��Sr0r+)r�� references r-r�z7GradientState.dataloader_references.<locals>.<listcomp>"s'��u�u�u� �y�4� � � � � �)�u�u�ur,�r�r�s r-r�z#GradientState.dataloader_referencess��v�u�UY�Ut�u�u�u�ur,c�(�d�|D��|_dS)Nc�>�g|]}|�tj|��n|��Sr0)�weakref�ref)r�r�s r-r�z7GradientState.dataloader_references.<locals>.<listcomp>&s9��+ �+ �+ �R\�z�'=�G�K� � #� #� #�:�+ �+ �+ r,r�)r:� referencess r-r�z#GradientState.dataloader_references$s)��+ �+ �`j�+ �+ �+ ��'�'�'r,c��|jduS)z6Returns whether the current process is in a dataloaderN)r�r�s r-r�zGradientState.in_dataloader*s���%�T�1�1r,c�B�tj���dSr�)r�r*r�r+r,r-r�zGradientState._reset_state/s�� �#�)�)�+�+�+�+�+r,r0)r�r�)r&r�r@)rDrErFrGrDr*r;rFr�r�r�rxr�r�r�r��setterr�r�r�r�r�r�rEr�r+r,r-r�r��s��������,�J�L�L�M� J� J� J� J� J��6�6�6��X�6��A�A�A��X�A��D�D�D��X�D��1�1�1��X�1��8�8�8��X�8� �0�0�0��X�0�  � � ��-�-��X�-� �#�2�2�$�#�2� � � �3�3�3�  � � ��.�.��X�.��v�v��X�v��!� � �"�!� � �2�2�2��X�2��,�,��\�,�,�,r,r�r@)A� __future__r�loggingry� threadingr�r�� contextlibr� functoolsr�typingrrr|�utilsr r r r r rrrrrrrrrrrrrrrrrrr r!r"r#�utils.dataclassesr$�torch_xla.core.xla_model�core� xla_modelr�� torch_mlu� torch_sdaa� torch_musa� torch_npu� getLoggerrDrcr.r3�localr5r�rDrIr)r�r+r,r-�<module>r�s���#�"�"�"�"�"����� � � � �������������%�%�%�%�%�%������� � � � � � � � � � � � �����������������������������������������������������������:8�7�7�7�7�7�����*�)�)�)�)�)�)�)�)�)����'�'�'��������%�(�(�(��������%�(�(�(���������'�'�'������ �� �8� $� $��0�0�0�0���������I�O����80�/�1�1� L�T�T�7L� �X Q�X Q�X Q�X Q�X Q�X Q�X Q�X Q�vLU�LU�LU�LU�LU�LU�LU�LU�^ L,�L,�L,�L,�L,�L,�L,�L,�L,�L,r,
Memory