� %�g�3���ddlZddlZddlZddlZddlmZddlmZddlmZm Z ddl m Z ddl Z ddlZddlmZddlmZdd lmZdd lmZdd lmZd d lmZd dlmZd dlmZm Z m!Z!m"Z"m#Z#d dl$m%Z%d dl&m'Z'd dl(m)Z)ee*��Z+e"��r ddl,m-cm.Z/d�Z0 d.de1de1de1fd�Z2d�Z3de4fd�Z5d/de1de1fd�Z6e d ��re j7ne j-Z8e8j9j:e j;ee j<gZ=e d!��re=�>e j?j@��d0d"�ZAd#�ZBd$�ZCd0d%eDd&e1fd'�ZEd(�ZFd)�ZGd*eHfd+�ZId,ejJjKd&eLejJjKfd-�ZMdS)1�N)�encode)� OrderedDict)�partial�reduce)� MethodType)�Version)� save_file�)�write_basic_config)� get_logger)� PartialState�)�FSDP_PYTORCH_VERSION)�DistributedType)�is_deepspeed_available�is_numpy_available�is_torch_distributed_available�is_torch_xla_available�is_weights_only_available)�id_tensor_storage)� convert_model)�is_torch_versionc�x�ttd��sdSt|tjjj��S)zD Check whether the module was compiled with torch.compile() �_dynamoF)�hasattr�torch� isinstancer� eval_frame�OptimizedModule)�modules �f/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/accelerate/utils/other.py�is_compiled_moduler"5s3�� �5�)� $� $���u� �f�e�m�6�F� G� G�G�TF�keep_fp32_wrapper�keep_torch_compile� recursivec�� �tjjjtjjf}t |��}|r |}|j}t��r ddlm }||fz }tdt��rt��r ddl m}||fz }t||��r|j}t||���|r� fd�� � |��}|s�|j} |j�dd��} | �Ct)| d��r| j} | | krnt)| d���t-| |��|_t/|d d ��rt1|d � ��|r |r ||_|}|S) a� Extract a model from its distributed containers. Args: model (`torch.nn.Module`): The model to extract. keep_fp32_wrapper (`bool`, *optional*): Whether to remove mixed precision hooks from the model. keep_torch_compile (`bool`, *optional*): Whether to unwrap compiled model. recursive (`bool`, *optional*, defaults to `False`): Whether to recursively extract all cases of `module.module` from `model` as well as unwrap child sublayers recursively, not just the top-level distributed containers. Returns: `torch.nn.Module`: The extracted model. r)�DeepSpeedEnginez>=)�FullyShardedDataParallelc���t|d��r�|j��}n|}|���D]\}}t||�|����� |S)Nr )rr �named_children�setattr)r �unwrapped_module�name�child�_recursive_unwraps �r!r0z6extract_model_from_parallel.<locals>._recursive_unwraphs|����v�x�(�(� *�#4�#4�V�]�#C�#C� � �#)� �/�>�>�@�@� J� J� ��e��(�$�0A�0A�%�0H�0H�I�I�I�I�#� #r#�_original_forwardN� __wrapped__� _converted_to_transformer_engineF)�to_transformer_engine)r�nn�parallel�DistributedDataParallel� DataParallelr"� _orig_modr� deepspeedr(rrr�2torch.distributed.fsdp.fully_sharded_data_parallelr)rr �forward�__dict__�poprr2r�getattrr) �modelr$r%r&�options� is_compiled�compiled_modelr(�FSDPr<�original_forwardr0s @r!�extract_model_from_parallelrF>s����(�x� �8�%�(�:O�P�G�$�U�+�+�K�� ���������&�-�-�-�-�-�-��O�%�%����2�3�3��8V�8X�8X��g�g�g�g�g�g��D�7��� �U�G� $� $��� �� �U�G� $� $���)� $� $� $� $� $�"�!�%�(�(�� � >��-�� �>�-�-�.A�4�H�H�� � '��'�=�1�1� �!�-���.�.�.���'�=�1�1� �'�w��6�6�E�M� �5�<�e� D� D� >� �%�u� =� =� =� =���k��#(�� ��� �Lr#c�F�t�����dS)a  Introduces a blocking point in the script, making sure all processes have reached this point before continuing. <Tip warning={true}> Make sure all processes will reach this instruction otherwise one of your processes will hang forever. </Tip> N)r �wait_for_everyone�r#r!rHrH�s ���N�N�$�$�&�&�&�&�&r#� state_dictc���tjt��}����D]B\}}t |t ��s(|t |���|���Cd�|���D��}t��}|� ��D]=}�fd�|D��}|� |dd���|dd�D]}�|=��>t|��dkrt� d|�d���d�����D����S) z� Cleans the state dictionary from a model and removes tensor aliasing if present. Args: state_dict (`dict`): The state dictionary from a model c�@�i|]\}}t|��dk�||��S)r)�len)�.0�ptr�namess r!� <dictcomp>z4clean_state_dict_for_safetensors.<locals>.<dictcomp>�s)��O�O�O�*�#�u��E� � �Q���3����r#c���g|]}|�v�|�� SrIrI)rNr.rJs �r!� <listcomp>z4clean_state_dict_for_safetensors.<locals>.<listcomp>�s#���D�D�D����1C�1C�t�1C�1C�1Cr#rNrzRemoved shared tensor zk while saving. This should be OK, but check by verifying that you don't receive any warning while reloadingc�v�i|]6\}}|t|tj��r|���n|��7SrI)rr�Tensor� contiguous)rN�k�vs r!rQz4clean_state_dict_for_safetensors.<locals>.<dictcomp>�s<��i�i�i�d�a�QR�!�z�!�U�\�'B�'B�I�Q�\�\�^�^�^��i�i�ir#)� collections� defaultdict�list�itemsr�strr�append�set�values�updaterM�logger�warning)rJ�ptrsr.�tensor� shared_ptrs� warn_namesrP� found_namess` r!� clean_state_dict_for_safetensorsri�st��� � "�4� (� (�D�"�(�(�*�*�9�9� ��f��&�#�&�&� 9� �"�6�*�*� +� 2� 2�4� 8� 8� 8��P�O�� � � � �O�O�O�K����J��#�#�%�%� !� !�� E�D�D�D��D�D�D� ����+�a�b�b�/�*�*�*�����O� !� !�D��4� � � !� �:��������� ]�Z� ]� ]� ]� � � �j�i�V`�Vf�Vf�Vh�Vh�i�i�i�J� �r#�save_on_each_node�safe_serializationc��t��jtjkrt j|��}|r=t tddi���}t|t��rt|��}n tj }t��j r|s|||��dSt��jr|r|||��dSdSdS)a� Save the data to disk. Use in place of `torch.save()`. Args: obj: The data to save f: The file (or file-like object) to use to save the data save_on_each_node (`bool`, *optional*, defaults to `False`): Whether to only save on the global main process safe_serialization (`bool`, *optional*, defaults to `False`): Whether to save `obj` using `safetensors` or the traditional PyTorch way (that uses `pickle`). �format�pt)�metadataN)r �distributed_typer�XLA�xm�_maybe_convert_to_cpur�safe_save_filerrrir�save�is_main_process�is_local_main_process)�obj�frjrk� save_funcs r!ruru�s���$�~�~�&�/�*=�=�=��&�s�+�+�����N�h��5E�F�F�F� � �c�;� '� '� 8�2�3�7�7�C���J� ��~�~�%��.?��� �#�q������ ��� -��2C��� �#�q���������r#z2.0.0z1.25.0c �T� t��rLtj���}d|vrd|d<tj�t ��n|�dd��tj|fd|i|��}t��r?tj���|rtj�|��nT#t��r@tj���|r!tj�|��wwwxYw|S)a Compatible drop-in replacement of `torch.load()` which allows for `weights_only` to be used if `torch` version is 2.4.0 or higher. Otherwise will ignore the kwarg. Will also add (and then remove) an exception for numpy arrays Args: f: The file (or file-like object) to use to load the data map_location: a function, `torch.device`, string or a dict specifying how to remap storage locations **kwargs: Additional keyword arguments to pass to `torch.load()`. � weights_onlyTN� map_location) rr� serialization�get_safe_globals�add_safe_globals�TORCH_SAFE_GLOBALSr>�load�clear_safe_globals)ryr}�kwargs�old_safe_globals� loaded_objs r!r�r��s1�� G� $� &� &� -�$�2�C�C�E�E� ��V�+�+�)-��~�&� � � 0� 0�1C� D� D� D� D� �J�J�~�t� ,� ,� ,��Z��G�G� �G��G�G� � $� &� &� G� � � 2� 2� 4� 4� 4�� G��#�4�4�5E�F�F�F��� %� &� &� G� � � 2� 2� 4� 4� 4�� G��#�4�4�5E�F�F�F�F� G� G���� �s �BC�AD%c���t|d��s!t|d��st|d|��}t|d��r|jSt|d��r|jSt |��S)z( Gets a pretty name from `obj`. � __qualname__�__name__� __class__)rr?r�r�r])rxs r!�get_pretty_namer� sx�� �3�� '� '�-���Z�0H�0H�-��c�;��,�,���s�N�#�#� �����s�J�����|�� �s�8�8�Or#c��|���D]F\}}t|t��r'|�|i��}t ||���A|||<�G|S)z� Recursively merges two dictionaries. Args: source (`dict`): The dictionary to merge into `destination`. destination (`dict`): The dictionary to merge `source` into. )r\r�dict� setdefault� merge_dicts)�source� destination�key�value�nodes r!r�r�sl���l�l�n�n�%�%� ��U� �e�T� "� "� %��)�)�#�r�2�2�D� ��t� $� $� $� $�$�K�� � � �r#�port�returnc���|�d}tjtjtj��5}|�d|f��dkcddd��S#1swxYwYdS)z� Checks if a port is in use on `localhost`. Useful for checking if multiple `accelerate launch` commands have been run and need to see if the port is already in use. Ni<s� localhostr)�socket�AF_INET� SOCK_STREAM� connect_ex)r��ss r!�is_port_in_user�+s���  �|��� ��v�~�v�'9� :� :�6�a��|�|�[�$�/�0�0�A�5�6�6�6�6�6�6�6�6�6�6�6�6����6�6�6�6�6�6s�A�A�Ac�v�dD]$}|dkrt|d���d|��cS|dz}�%t|d���d�S)z7Converts `size` from bytes to the largest possible unit)�bytes�KB�MB�GB�TBg�@r � z PB)�round)�size�xs r!� convert_bytesr�6s^�� .���� �&�=�=��D�!�n�n�*�*�q�*�*� *� *� *� ������D�!�n�n� !� !� !�!r#c��tj��}|j}|dkrdStjd|j��^}}}d}t |��t |��kr'd|�d|�d�}t�|d� ��dSdS) zFWarns if the kernel version is below the recommended minimum on Linux.�LinuxNz(\d+\.\d+\.\d+)z5.5.0zDetected kernel version z,, which is below the recommended minimum of zo; this can cause the process to hang. It is recommended to upgrade the kernel to the minimum version or higher.T)�main_process_only) �platform�uname�system�re�split�releaserrbrc)�infor��_�version� min_version�msgs r!�check_os_kernelr�@s��� �>� � �D� �[�F� �������X�0�$�,�?�?�N�A�w���K��w���'�+�.�.�.�.� s�w� s� s�\g� s� s� s� � ���s�d��3�3�3�3�3� /�.r#�attrc�V�d�}t||g|�d��z��S)z� Recursive `getattr`. Args: obj: A class instance holding the attribute. attr (`str`): The attribute that is to be retrieved, e.g. 'attribute1.attribute2'. c�"�t||��S�N)r?)rxr�s r!�_getattrz#recursive_getattr.<locals>._getattr]s���s�D�!�!�!r#�.)rr�)rxr�r�s r!�recursive_getattrr�Rs4��"�"�"� �(�S�E�D�J�J�s�O�O�3� 4� 4�4r#r@c��|g}g}|ry|���}|���D]9\}}t|tjj��r|�|���:|�|��|�y|ddd�S)aATraverse the model in bottom-up order and return the children modules in that order. Args: model (`torch.nn.Module`): the model to get the children of Returns: `list[torch.nn.Module]`: a list of children modules of `model` in bottom-up order. The last element is the `model` itself. N�����)r>r+rrr5�Moduler^)r@�stack�ordered_modules�current_moduler�r�s r!�get_module_children_bottom_upr�cs����G�E��O� �/�������%�4�4�6�6� #� #�G�A�t��$����0�0� #�� � �T�"�"�"�����~�.�.�.� �/� �4�4�R�4� � r#)TTF)FFr�)NrYr�r�r��codecsrr� functoolsrr�typesr�numpy�npr�packaging.versionr�safetensors.torchr rt�commands.config.defaultr �loggingr �stater � constantsr� dataclassesr�importsrrrrr�modelingr�transformer_enginer�versionsrr�rb�torch_xla.core.xla_model�core� xla_modelrrr"�boolrFrHr�riru�_core�np_core� multiarray� _reconstruct�ndarray�dtyper�r^�dtypes� UInt32DTyper�r�r��intr�r�r�r]r�r5r�r[r�rIr#r!�<module>r�s����������� � � � � � � � �������#�#�#�#�#�#�%�%�%�%�%�%�%�%����������� � � � �%�%�%�%�%�%�9�9�9�9�9�9�8�8�8�8�8�8� � � � � � � � � � � � �+�+�+�+�+�+�(�(�(�(�(�(���������������(�'�'�'�'�'�-�-�-�-�-�-�&�&�&�&�&�&� ��H� � ������*�)�)�)�)�)�)�)�)�)�H�H�H�_d�I�I�"�I�?C�I�W[�I�I�I�I�X '� '� '� �� � � � �F��D��d�����H)�(��1�1� >�"�(�(�r�w�� ��#��J� ��H� ����h���5����b�i�3�4�4�4�����@ � � ����$6�6��6��6�6�6�6�"�"�"�4�4�4�$5��5�5�5�5�"!����!�T�%�(�/�=R�!�!�!�!�!�!r#
Memory