U
    dh	                     @   sL   d dl Zd dlmZ d dlZddddgZdd Zdd Zd	d Zd
d Z	dS )    N)Shufflerapply_shardingapply_shuffle_seedapply_shuffle_settingsget_all_graph_pipesc                 C   sB   t  }|  D ].\}}|| t|}|D ]}|| q,q|S )N)setitemsaddr   )graphresultsdatapipeZ	sub_graphZ	sub_itemsitem r   C/tmp/pip-unpacked-wheel-ua33x9lu/torch/utils/data/graph_settings.pyr      s    
c                 C   sr   t jjjj| dd}t|}d }|D ]H}t|dr$| r$t|dr$|d k	r\tdd|d||	|| |}q$d S )NTZonly_datapipeis_shardabler   zVThis implementation of sharding can be only applied once per instance of DataPipeline.zAlready applied tozwhile trying to apply to)
torchutilsdatar
   traverser   hasattrr   RuntimeErrorr   )r   Znum_of_instancesZinstance_idr
   	all_pipesZalready_applied_topiper   r   r   r      s     

   c                 C   sn   |d kr| S t jjjj| dd}t|}dd |D }|sV|rVtd |  } | h}|D ]}|	| qZ| S )NTr   c                 S   s   h | ]}t |tr|qS r   
isinstancer   .0r   r   r   r   	<setcomp>,   s     
 z)apply_shuffle_settings.<locals>.<setcomp>z`shuffle=True` was set, but the datapipe does not contain a `Shuffler`. Adding one at the end. Be aware that the default buffer size might not be sufficient for your task.)
r   r   r   r
   r   r   warningswarnshuffleZset_shuffle)r   r!   r
   r   	shufflersshufflerr   r   r   r   &   s    c                 C   sb   t jjjj| dd}t|}dd |D }|D ].}tt jdt jdj	|d
 }|| q.| S )NTr   c                 S   s   h | ]}t |tr|qS r   r   r   r   r   r   r   >   s     
 z%apply_shuffle_seed.<locals>.<setcomp>r   )Zdtype)	generator)r   r   r   r
   r   r   intemptyZint64Zrandom_r   Zset_seed)r   rngr
   r   r"   r#   Zshuffle_seedr   r   r   r   ;   s     )
Ztorch.utils.data.graphr   Ztorch.utils.data.datapipes.iterr   r   __all__r   r   r   r   r   r   r   r   <module>   s   
