� %�g���@�ddlZddlmZddlmZmZmZddlmZddl m Z m Z m Z m Z mZmZmZmZddedefd �Zd �Zd �Zd �Z ddeeeeefdeeedeeedeeeefdeedeef d�ZdS)�N)� MethodType)�Any�Optional�Union�)� PartialState)�calculate_maximum_sizes� convert_bytes�copy_tensor_to_devices�ignorant_find_batch_size�infer_auto_device_map�is_pippy_available�pad_input_tensors�send_to_device� num_processes� max_memoryc�|� �|dkrt||d���S|��t|��\}}||dz|z � t� ��� � �d��\}}t jt |����dz� � �d|��� � fd�t|��D��}t|||d� ��}|S) zH Calculates the device map for `model` with an offset for PiPPy rF)�no_split_module_classes� clean_resultNr� g�������?c���i|]}|���S�r)�.0�i�memorys ��d/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/accelerate/inference.py� <dictcomp>z'generate_device_map.<locals>.<dictcomp>0s���>�>�>�A�a��>�>�>�)rrr)r r r �split�math�ceil�float�range) �modelrrr� model_size�shared�value�ending� device_maprs @r�generate_device_mapr*s��������$�U�D[�jo�p�p�p�p���4�U�;�;�� �F��v�a�y�(�M�9���v�&�&��� � �S�)�)� ��v���5��<�<�(�(�3�.���%�%�V�%�%��>�>�>�>��}�)=�)=�>�>�>� �&� �� 7�� ���J� �rc��d}|�|D]}t|��}|�n�|�,|�*|���D]}t|��}|�n�|S�N)r �values)�args�kwargs�found_batch_size�arg�kwargs r�find_pippy_batch_sizer3:s����� ��� � �C�7��<�<� ��+���,� ��.�6��]�]�_�_� � �E�7��>�>� ��+���,� �rc��� �ddlm}m� m}t ��}� fd�|D��}|||||���} | �|j|j���} || |��} | S)aB Attaches the split points to the model based on `self.device_map` and generates a `PipelineStage`. Requires passing in needed `args` and `kwargs` as the model needs on the CPU. Users can pass in custom `num_chunks` as an optional hyper-parameter. By default will use `AcceleratorState.num_processes` r)� ScheduleGPipe� SplitPoint�pipelinec� ��i|] }|�j�� Sr)� BEGINNING)r� split_pointr6s �rrz"build_pipeline.<locals>.<dictcomp>Vs���T�T�T� �+�z�3�T�T�Tr)�mb_args� mb_kwargs� split_spec)�device)�torch.distributed.pipeliningr5r6r7r� build_stage�local_process_indexr>) r$� split_pointsr.r/� num_chunksr5r7�stater=�pipe�stage�scheduler6s @r�build_pipelinerHIs����Q�P�P�P�P�P�P�P�P�P� �N�N�E�T�T�T�T�|�T�T�T�J� �8� ����  � � �D� � � �U�6�u�|� � L� L�E��}�U�J�/�/�H� �Orc�Z�t��}d}|jdkr ||i|��}nu|jrRt||��}|�t d���||kr"t |||��}t |||��}||i|��n|jr |��}n |��|rt|��}|S)Nrz-Could not find batch size from args or kwargs)rr�is_local_main_processr3� ValueErrorr�is_last_processr )�forwardrC� gather_outputr.r/rD�outputr0s r� pippy_forwardrPcs��� �N�N�E� �F� ��a�����$�)�&�)�)��� � $� �0��v�>�>�� � #��L�M�M� M��:�-�-�(��/?��L�L��*�6�3C�Z�P�P����� �� � � � � � ���������� � � ��0�'��/�/�� �Mr�autorFrBr� example_args�example_kwargsrCrNc�Z��� � �t��std���t��}t|d��}t|d��}��|j�|dkrgt |�|���}g}t d���D]B� |�t� fd�|� ��D�������C||_ t||||���� |j |_ |j|_� |_||_ ��� fd�} t#| |��} | | _| |_ |S) a Wraps `model` for pipeline parallel inference. Args: model (`torch.nn.Module`): A model we want to split for pipeline-parallel inference split_points (`str` or `List[str]`, defaults to 'auto'): How to generate the split points and chunk the model across each GPU. 'auto' will find the best balanced split given any model. Should be a list of layer names in the model to split by otherwise. no_split_module_classes (`List[str]`): A list of class names for layers we don't want to be split. example_args (tuple of model inputs): The expected inputs for the model that uses order-based inputs for a *single process*. Recommended to use this method if possible. example_kwargs (dict of model inputs) The expected inputs for the model that uses dictionary-based inputs for a *single process*. This is a *highly* limiting structure that requires the same keys be present at *all* inference calls. Not recommended unless the prior condition is true for all cases. num_chunks (`int`, defaults to the number of available GPUs): The number of different stages the Pipeline will have. By default it will assign one chunk per GPU, but this can be tuned and played with. In general one should have num_chunks >= num_gpus. gather_output (`bool`, defaults to `False`): If `True`, the output from the last GPU (which holds the true outputs) is sent across to all GPUs. zEUsing `torch.distributed.pipelining` requires PyTorch 2.4.0 or later.�cpuNrQ)rrc3�.�K�|]\}}|�k� |V��dSr,r)r�k�vrs �r� <genexpr>z prepare_pippy.<locals>.<genexpr>�s+�����$P�$P�4�1�a��a���Q�����$P�$Prc�2��t�j��g|�Ri|��Sr,)rP�step)r.r/rNrCrFs ���rrMzprepare_pippy.<locals>.forward�s'����U�Z��]�T�T�T�T�T�V�T�T�Tr)r� ImportErrorrrrr*r#�append�next�items�hf_split_pointsrHrM�_original_forward�__call__�_original_call� pippy_stager� __wrapped__) r$rBrrRrSrCrNrDr)rM� model_forwardrrFs `` @@r� prepare_pippyrg|sl������B � � �c��a�b�b�b� �N�N�E�!�,��6�6�L�#�N�E�:�:�N����(� ��v���(�� �Tk�l�l�l� �� ��q�*�%�%� R� R�A� � � ��$P�$P�$P�$P�:�3C�3C�3E�3E�$P�$P�$P� P� P� Q� Q� Q� Q�(�E�� �5�,� �n�j� Y� Y�E�#�m�E�� �>�E���E��(�E��U�U�U�U�U�U�U� �w��.�.�M�'�G���E�M� �Lr)rNN)rQNrNNF)r �typesr�typingrrrrDr�utilsr r r r r rrr�int�dictr*r3rHrP�str�list�tuple�boolrgrrr�<module>rqs��� � � � �������'�'�'�'�'�'�'�'�'�'������� � � � � � � � � � � � � � � � � � � � ���c��ae�����6 � � ����4���65;�37�)+�/3� $�$)�<�<��5��d�3�i��0�1�<�&�d�3�i�0�<��5��:�&� <� �T�#�s�(�^�,� <� �� � <��D�>�<�<�<�<�<�<r
Memory