U
    ‰d{  ã                   @   s:   d dl mZ d dlmZ dejee ee dœdd„ZdS )é    )Únn)ÚListN)ÚmoduleÚbalanceÚdevicesc           
      C   sz   d}d}g }|D ]^}g }t |ƒD ]}| | | ¡ |d7 }q |dkrH|n|| }	| tj|Ž  |	¡¡ |d7 }qtj|Ž S )a,  
    Given an :class:`nn.Sequential <torch.nn.Sequential>` module, partitions
    the model across multiple GPU devices according the provided ``balance``
    and ``devices``.

    Args:
        module (:class:`nn.Sequential <torch.nn.Sequential>`):
            Sequential model representing the pipe.
        balance (List[int]):
            List indicating the number of layers in each partition.
        devices (List[int], optional):
            List indicating the device to use for each partition. Defaults to
            ``range(len(balance))``
    r   é   N)ÚrangeÚappendr   Ú
SequentialÚto)
r   r   r   Z
device_idxZpipe_idxZbalanced_pipeZ
num_layersZlayersÚiZdevice© r   úI/tmp/pip-unpacked-wheel-ua33x9lu/torch/distributed/pipeline/sync/utils.pyÚpartition_model   s    

r   )N)Ztorchr   Útypingr   r
   Úintr   r   r   r   r   Ú<module>   s    ýý