� %�g�;����ddlZddlZddlZddlZddlZddlZddlmZddlm Z m Z ddl m Z m Z ddlmZddlmZmZddlZddlmZeje��Zdefd �Zd#d ed eeeffd �Zd�Zd#d�Zd$d�Z de!d e"e!fd�Z#d�Z$d�Z%d�Z&d�Z'e d���Z(e Gd�d����Z)d e)fd�Z*d%dedeed dfd�Z+e d%dedeed dfd���Z,ed ���Z-ed!���Z.d"�Z/dS)&�N)�contextmanager)� dataclass�field)� lru_cache�wraps)�which)�Optional�Union)�parse� current_envc�H���gd�}g}|���D]�\��t��fd�|D����rBt���dkr/t���dkr|���d��d����ct�d��d��d�����|S)a� Verifies that all keys and values in `current_env` do not contain illegal keys or values, and returns a list of strings as the result. Example: ```python >>> from accelerate.utils.environment import verify_env >>> env = {"ACCELERATE_DEBUG_MODE": "1", "BAD_ENV_NAME": "<mything", "OTHER_ENV": "2"} >>> valid_env_items = verify_env(env) >>> print(valid_env_items) ["ACCELERATE_DEBUG_MODE=1 ", "OTHER_ENV=2 "] ``` )�;� �<�>� c3�&�K�|] }|��zvV�� dS�N�)�.0�char�key�values ���l/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/accelerate/utils/environment.py� <genexpr>z0convert_dict_to_env_variables.<locals>.<genexpr>4s,�����E�E�T�t�C�%�K�(�E�E�E�E�E�E���=rzWARNING: Skipping z7 as it contains forbidden characters or missing values.)�items�all�len�append�logger�warning)r �forbidden_chars�valid_env_itemsrrs @@r�convert_dict_to_env_variablesr'"s�����1�0�0�O��O�!�'�'�)�)�v�v� ��U� �E�E�E�E�E�_�E�E�E� E� E� v�#�c�(�(�VW�-�-�\_�`e�\f�\f�jk�\k�\k� � "� "�c�#5�#5�E�#5�#5�#5� 6� 6� 6� 6� �N�N�t��t�t�e�t�t�t� u� u� u� u� �rF�to_bool�returnc�v�|���}|dvr|sdndS|dvr|sdndStd|�����)z� Converts a string representation of truth to `True` (1) or `False` (0). True values are `y`, `yes`, `t`, `true`, `on`, and `1`; False value are `n`, `no`, `f`, `false`, `off`, and `0`; )�y�yes�t�true�on�1rT)�n�no�f�false�off�0rFzinvalid truth value )�lower� ValueError)rr(s r� str_to_boolr9;s^�� �K�K�M�M�E� �4�4�4��)�q�q�T�)� �7� 7� 7��*�q�q�U�*��7��7�7�8�8�8rc�~�|D]9}ttj�|d����}|dkr|cS�:|S)zQReturns the first positive env value found in the `env_keys` list or the default.�����r)�int�os�environ�get)�env_keys�default�e�vals r�get_int_from_envrDJsJ�� �����"�*�.�.��B�'�'�(�(�� �!�8�8��J�J�J� � �Nrc��tj�|t|����}t |��dkS)zJReturns truthy value for `key` from the env if available else the default.r)r=r>r?�strr9�rrArs r�parse_flag_from_envrHSs0�� �J�N�N�3��G� � � -� -�E� �u� � �� "�"rr2c�`�tj�|t|����}|Sr)r=r>r?rFrGs r�parse_choice_from_envrJYs"�� �J�N�N�3��G� � � -� -�E� �Lr� library_namesc��d�|D��S)zk Checks if any of `library_names` are imported in the environment. Will return any names that are. c�R�g|]$}|tj���v�"|��%Sr)�sys�modules�keys)r�lib_names r� <listcomp>z-are_libraries_initialized.<locals>.<listcomp>bs0�� U� U� U��h�#�+�BR�BR�BT�BT�6T�6T�H�6T�6T�6Trr)rKs r�are_libraries_initializedrS^s�� V� U�]� U� U� U�Urc��tj��dkr'td��}|�tjd�d�}nd}|S)zC Returns the right nvidia-smi command based on the system. �Windowsz nvidia-smiN� systemdrivez6\Program Files\NVIDIA Corporation\NVSMI\nvidia-smi.exe)�platform�systemrr=r>)�commands r� _nvidia_smirZesK������I�%�%�� �%�%�� �?���M�2�n�n�n�G���� �Nrc���tjt��ddgd���}|���}|�t j��}t|��}d�|D��}||fS)z� Gets GPU count and names using `nvidia-smi` instead of torch to not initialize CUDA. Largely based on the `gputil` library. z--query-gpu=count,name�--format=csv,noheaderT��universal_newlinesc�h�g|]/}|�d��d�����0S)�,r)�split�strip)r�gpus rrRz get_gpu_info.<locals>.<listcomp>�s3��;�;�;�s����3����"�(�(�*�*�;�;�;r)� subprocess� check_outputrZrbrar=�linesepr!)�output�gpus� gpu_count� gpu_namess r� get_gpu_inforktsy��� $� ���0�2I�J�_c����F��\�\�^�^�F� �<�<�� � #� #�D��D� � �I�;�;�d�;�;�;�I� �i� �rc��tjt��ddgd���}|���}|�t j��dS)z^ Returns the driver version In the case of multiple GPUs, will return the first. z--query-gpu=driver_versionr\Tr]r)rdrerZrbrar=rf)rgs r�get_driver_versionrm�sV�� � $� ���4�6M�N�cg����F��\�\�^�^�F� �<�<�� � #� #�A� &�&rc���� t��\}}dh�|dkrOt�fd�|D����r4d}t��}t|��t|��krdSdSn#t$rYnwxYwdS)z� Checks if the devices being used have issues with P2P and IB communications, namely any consumer GPU hardware after the 3090. Noteably uses `nvidia-smi` instead of torch to not initialize CUDA. zRTX 40rc3�*�K�|] }�D]}||vV�� �dSrr)r� device_name�unsupported_device�unsupported_devicess �rrz,check_cuda_p2p_ib_support.<locals>.<genexpr>�sS��������*=���'�#�k�1�������rz 550.40.07FT)rk�anyrmr � Exception)� device_names� device_count�acceptable_driver_version�current_driver_versionrrs @r�check_cuda_p2p_ib_supportry�s���� �%1�^�^�"� �l�'�j�� �!� � ������#/������ � -8�)�);�)=�)=�&��/�0�0�5�9R�3S�3S�S�S� �5��t��� � � � � �� ���� �4s�A%A-�- A:�9A:c�� tjt��ddgd���}|���}t t t |�tj ��d�d������}n.#t$r!tj � ��}YnwxYw|dkS)zp Checks if the current GPU available supports FP8. Notably might initialize `torch.cuda` to check. z--query-gpu=compute_capabilityr\Tr]r�.)�� )rdrerZrb�tuple�mapr<rar=rfrt�torch�cuda�get_device_capability)rg�compute_capabilitys r�check_cuda_fp8_capabilityr��s��� @��(� �]�]�<�>U� V�ko� � � ��������"�3�s�F�L�L���,D�,D�Q�,G�,M�,M�c�,R�,R�#S�#S�T�T���� �@�@�@�"�Z�=�=�?�?����@���� �� '�'s�BB�(B?�>B?c��eZdZUdZedddi���Zeed<edddi���Zeed <eddd i���Z eed <eddd i���Z eed <dS)�CPUInformationag Stores information about the CPU in a distributed environment. It contains the following attributes: - rank: The rank of the current process. - world_size: The total number of processes in the world. - local_rank: The rank of the current process on the local node. - local_world_size: The total number of processes on the local node. r�helpz The rank of the current process.)rA�metadata�rankrz+The total number of processes in the world.� world_sizez2The rank of the current process on the local node.� local_rankz0The total number of processes on the local node.�local_world_sizeN) �__name__� __module__� __qualname__�__doc__rr�r<�__annotations__r�r�r�rrrr�r��s������������a�6�3U�*V�W�W�W�D�#�W�W�W��e�A��9f�0g�h�h�h�J��h�h�h��e�A��9m�0n�o�o�o�J��o�o�o�!�E�!�v�?q�6r�s�s�s��c�s�s�s�s�srr�c���i}tgd�d��|d<tgd�d��|d<tgd�d��|d<tgd �d��|d <td i|��S) z� Returns various information about the environment in relation to CPU distributed training as a `CPUInformation` dataclass. )�RANK�PMI_RANK�OMPI_COMM_WORLD_RANK�MV2_COMM_WORLD_RANKrr�)� WORLD_SIZE�PMI_SIZE�OMPI_COMM_WORLD_SIZE�MV2_COMM_WORLD_SIZErr�)� LOCAL_RANK�MPI_LOCALRANKID�OMPI_COMM_WORLD_LOCAL_RANK�MV2_COMM_WORLD_LOCAL_RANKr�)�LOCAL_WORLD_SIZE�MPI_LOCALNRANKS�OMPI_COMM_WORLD_LOCAL_SIZE�MV2_COMM_WORLD_LOCAL_SIZEr�r)rDr�)� informations r�get_cpu_distributed_informationr��s��� �K�*�+n�+n�+n�pq�r�r�K��� 0�Q�Q�Q�ST�!�!�K� ��!1�d�d�d�fg�!�!�K� ��'7�j�j�j� �'�'�K�"�#� � (� (�K� (� (�(r�local_process_index�verbosec���|�tdd��}tj����r,ddlm}|��st d���ddl}|���tj tj ��dz ��}|� |��}d}|�||��D] }|d �|��}� d �|D��}|���d �t!|��D��} tjd| ��|rHtjd��} t&�d t+| ���d |�d| ����dSdSdS)a� Overrides whatever NUMA affinity is set for the current process. This is very taxing and requires recalculating the affinity to set, ideally you should use `utils.environment.set_numa_affinity` instead. Args: local_process_index (int): The index of the current process on the current server. verbose (bool, *optional*): Whether to log out the assignment of each CPU. If `ACCELERATE_DEBUG_MODE` is enabled, will default to True. N�ACCELERATE_DEBUG_MODEFr)�is_pynvml_availablez_To set CPU affinity on CUDA GPUs the `pynvml` package must be available. (`pip install pynvml`)�@��064bc�,�g|]}t|����Sr)r<)r�xs rrRz*override_numa_affinity.<locals>.<listcomp>s��9�9�9�A��Q���9�9�9rc�$�g|] \}}|dk� |��S)rr)r�irBs rrRz*override_numa_affinity.<locals>.<listcomp> s!��L�L�L���A�Q�!�V�V�1�V�V�Vrz Assigning z cpu cores to process z: )rHr�r�� is_available�accelerate.utilsr�� ImportError�pynvml�nvmlInit�math�ceilr=� cpu_count�nvmlDeviceGetHandleByIndex�nvmlDeviceGetCpuAffinity�reverse� enumerate�sched_setaffinity�sched_getaffinityr#�infor!) r�r�r��nvml� num_elements�handle�affinity_string�j� affinity_list�affinity_to_set� cpu_coress r�override_numa_affinityr��s�����%�&=�u�E�E�� �z��� � �o�8�8�8�8�8�8�"�"�$�$� ��q��� � ���� � � �����y�����"�!4�5�5� ��0�0�1D�E�E�����.�.�v�|�D�D� ;� ;�A�!"�:�:��:�:�O�O�9�9��9�9�9� �������L�L��=�)A�)A�L�L�L�� ��Q��0�0�0� � o��,�Q�/�/�I� �K�K�m�S��^�^�m�m�K^�m�m�bk�m�m� n� n� n� n� n�/o�o�* o� orc�(�t||���dS)a2 Assigns the current process to a specific NUMA node. Ideally most efficient when having at least 2 cpus per node. This result is cached between calls. If you want to override it, please use `accelerate.utils.environment.override_numa_afifnity`. Args: local_process_index (int): The index of the current process on the current server. verbose (bool, *optional*): Whether to print the new cpu cores assignment for each process. If `ACCELERATE_DEBUG_MODE` is enabled, will default to True. �r�r�N)r�r�s r�set_numa_affinityr�s���/B�G�T�T�T�T�T�Trc#�K�tj���}tj��� dV�tj���tj�|��dS#tj���tj�|��wxYw)a� A context manager that will temporarily clear environment variables. When this context exits, the previous environment variables will be back. Example: ```python >>> import os >>> from accelerate.utils import clear_environment >>> os.environ["FOO"] = "bar" >>> with clear_environment(): ... print(os.environ) ... os.environ["FOO"] = "new_bar" ... print(os.environ["FOO"]) {} new_bar >>> print(os.environ["FOO"]) bar ``` N)r=r>�copy�clear�update)�_old_os_environs r�clear_environmentr�"s�����2�j�o�o�'�'�O��J������+� ���� � ������ � ���/�*�*�*�*�*�� � ������ � ���/�*�*�*�*���s �B�?Cc+�HK�i}|���D]X\}}|���}|tjvrtj|||<t |��tj|<�Y dV�|D]P}|���}||vr||tj|<�0tj�|d���QdS#|D]P}|���}||vr||tj|<�0tj�|d���QwxYw)a� A context manager that will add each keyword argument passed to `os.environ` and remove them when exiting. Will convert the values in `kwargs` to strings and upper-case all the keys. Example: ```python >>> import os >>> from accelerate.utils import patch_environment >>> with patch_environment(FOO="bar"): ... print(os.environ["FOO"]) # prints "bar" >>> print(os.environ["FOO"]) # raises KeyError ``` N)r�upperr=r>rF�pop)�kwargs� existing_varsrrs r�patch_environmentr�Es)����$�M��l�l�n�n�%�%� ��U��i�i�k�k�� �"�*� � �!#��C��M�#� ��e�*�*�� �3��� *� ����� *� *�C��)�)�+�+�C��m�#�#�"/��"4�� �3���� ���s�D�)�)�)�)�  *� *��6� *� *�C��)�)�+�+�C��m�#�#�"/��"4�� �3���� ���s�D�)�)�)�)�  *���s �3C � AD!c�������d�t�fd�����fd��t|t��s �|��S�fd���|��t�fd���|_|S)azDecorator to clean up accelerate environment variables set by the decorated class or function. In some circumstances, calling certain classes or functions can result in accelerate env vars being set and not being cleaned up afterwards. As an example, when calling: TrainingArguments(fp16=True, ...) The following env var will be set: ACCELERATE_MIXED_PRECISION=fp16 This can affect subsequent code, since the env var takes precedence over TrainingArguments(fp16=False). This is especially relevant for unit testing, where we want to avoid the individual tests to have side effects on one another. Decorate the unit test function or whole class with this decorator to ensure that after each test, the env vars are cleaned up. This works for both unittest.TestCase and normal classes (pytest); it also works when decorating the parent class. � ACCELERATE_c3���K��fd�tj���D��} dV��fd�tjD��D]<}||vr||tj|<�tj�|d���=dS#�fd�tjD��D]<}||vr||tj|<�tj�|d���=wxYw)Nc�F��i|]\}}|�����||��Sr�� startswith)r�k�v�prefixs �r� <dictcomp>zIpurge_accelerate_environment.<locals>.env_var_context.<locals>.<dictcomp>�s0���U�U�U�$�!�Q�� � �V�@T�@T�U��A�U�U�Urc�>��g|]}|�����|��Srr�)rr�r�s �rrRzIpurge_accelerate_environment.<locals>.env_var_context.<locals>.<listcomp>�s*���F�F�F�a����f�1E�1E�F��F�F�Fr)r=r>rr�)r�rr�s �r�env_var_contextz5purge_accelerate_environment.<locals>.env_var_contexts �����V�U�U�U�"�*�*:�*:�*<�*<�U�U�U� � .� �E�E�E�G�F�F�F�2�:�F�F�F� .� .���-�'�'�&3�C�&8�B�J�s�O�O��J�N�N�3��-�-�-�-�  .� .��F�F�F�F�2�:�F�F�F� .� .���-�'�'�&3�C�&8�B�J�s�O�O��J�N�N�3��-�-�-�-�  .���s �B � AC!c�N���t�����fd���}d|_|S)Nc�\�����5�|i|��cddd��S#1swxYwYdSrr)�argsr�r��funcs ��r�wrapperzDpurge_accelerate_environment.<locals>.wrap_function.<locals>.wrapper�s���� ��"�"� -� -��t�T�,�V�,�,� -� -� -� -� -� -� -� -� -� -� -� -���� -� -� -� -� -� -s �!�%�%T)r�)_accelerate_is_purged_environment_wrapped)r�r�r�s` �r� wrap_functionz3purge_accelerate_environment.<locals>.wrap_function�sB���� �t��� -� -� -� -� -� �� -�=A��9��rc����t|��D]`}|�d��rIt||��}t|��r*t |d��st ||�|�����a|S)N�testr�)�dirr��getattr�callable�hasattr�setattr)�test_class_instance�name�methodr�s �r�wrap_test_methodsz7purge_accelerate_environment.<locals>.wrap_test_methods�s�����+�,�,� N� N�D����v�&�&� N� �!4�d�;�;���F�#�#�N�G�F�<g�,h�,h�N��/��}�}�V�7L�7L�M�M�M��"�"rc����|��Srr)�cls�kwr�s �r�<lambda>z.purge_accelerate_environment.<locals>.<lambda>�s���BS�BS�TW�BX�BX�r)r� isinstance�type� classmethod�__init_subclass__)� func_or_clsr�r�r�r�s @@@@r�purge_accelerate_environmentr�js�������&�F�� .� .� .� .��^� .������ �k�4� (� (�*��}�[�)�)�)�#�#�#�#�#���k�"�"�"�$/�0X�0X�0X�0X�$Y�$Y�K�!� �r)F)r2r)0�loggingr�r=rWrdrN� contextlibr� dataclassesrr� functoolsrr�shutilr�typingr r r��packaging.versionr � getLoggerr�r#�dictr'�boolr<r9rDrHrJrF�listrSrZrkrmryr�r�r�r�r�r�r�r�rrr�<module>r s ������ � � � � � � � ��������� � � � �%�%�%�%�%�%�(�(�(�(�(�(�(�(�&�&�&�&�&�&�&�&�������"�"�"�"�"�"�"�"� � � � �#�#�#�#�#�#� �� �8� $� $���t�����2 9� 9�� 9��s�D�y�1A� 9� 9� 9� 9����#�#�#�#� ���� V�c�V�d�3�i�V�V�V�V� � � � � � �$ '� '� '����8 �(�(� ��(�* � t� t� t� t� t� t� t� �� t�)��)�)�)�)�($o�$o��$o�h�t�n�$o�X\�$o�$o�$o�$o�N �U�U�3�U��$��U�SW�U�U�U� ��U�"�+�+���+�D�!*�!*���!*�H;�;�;�;�;r
Memory