U
    <cp                     @   s   d dl Z d dlZd dlmZmZmZmZ d dlZd dlm	Z	m
Z
mZ dddddgZe
ee	 d	d
dZe
ee ee	 dddZe	eee	dddZe	edddZde	ee e	dddZe	ee	dddZe	edddZe	eje	dddZdS )    N)AnyListOptionalSet)DataPipeDataPipeGraphtraverse_dpsapply_random_seedapply_shardingapply_shuffle_seedapply_shuffle_settingsget_all_graph_pipes)graphreturnc                 C   s   t | t S N)_get_all_graph_pipes_helperset)r    r   C/tmp/pip-unpacked-wheel-gikjz4vx/torch/utils/data/graph_settings.pyr      s    )r   id_cacher   c                 C   sL   g }|   D ]:\}\}}||kr"q|| || |t|| q|S r   )itemsaddappendextendr   )r   r   resultsZdp_iddatapipeZ	sub_graphr   r   r   r      s    

r   )r   num_of_instancesinstance_idr   c                 C   sf   t | }t|}d }|D ]H}t|dr| rt|dr|d k	rPtdd|d|||| |}q| S )Nis_shardabler
   zVThis implementation of sharding can be only applied once per instance of DataPipeline.zAlready applied tozwhile trying to apply to)r   r   hasattrr   RuntimeErrorr
   )r   r   r   r   	all_pipesZalready_applied_topiper   r   r   r
   "   s"    

   )r   r   c                 C   s8   t | drt | dsdS t| jr0t| js4dS dS )Nset_shuffleset_seedFT)r   inspectismethodr#   r$   r   r   r   r   _is_shuffle_datapipe2   s
    r(   )r   shuffler   c                 C   sb   |dkr| S t | }t|}dd |D }|sJ|rJtd |  } | g}|D ]}|| qN| S )a.  
    Traverse the graph of ``DataPipes`` to find and set shuffle attribute
    to each `DataPipe` that has APIs of ``set_shuffle`` and ``set_seed``.

    Args:
        datapipe: DataPipe that needs to set shuffle attribute
        shuffle: Shuffle option (default: ``None`` and no-op to the graph)
    Nc                 S   s   g | ]}t |r|qS r   )r(   ).0r"   r   r   r   
<listcomp>H   s      z*apply_shuffle_settings.<locals>.<listcomp>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   warningswarnr)   r#   )r   r)   r   r!   Z	shufflersZshufflerr   r   r   r   :   s    	)r   rngr   c                 C   s   t d t| |S )Nz`apply_shuffle_seed` is deprecated since 1.12 and will be removed in the future releases.
Please use `apply_random_seed` instead.)r,   r-   r	   )r   r.   r   r   r   r   W   s    c                 C   s   t | drt| jrdS dS )Nr$   TF)r   r%   r&   r$   r'   r   r   r   _is_random_datapipe_   s    r/   c                 C   s   t | }t|}t }g }|D ]2}t||kr0qt|r|| |t| q|D ].}ttj	dtj
dj|d }|| qV| S )a  
    Traverse the graph of ``DataPipes`` to find random ``DataPipe`` with an API of
    ``set_seed`` then set the random seed based on the provided RNG.

    Args:
        datapipe: DataPipe that needs to set randomness
        rng: Random number generator to generate random seeds
    r   )Zdtype)	generator)r   r   r   idr/   r   r   inttorchemptyZint64Zrandom_itemr$   )r   r.   r   r!   cacheZrandom_datapipesr"   Zrandom_seedr   r   r   r	   e   s    	
 )N)r%   r,   typingr   r   r   r   r3   Ztorch.utils.data.graphr   r   r   __all__r   r2   r   r
   boolr(   r   r   r/   	Generatorr	   r   r   r   r   <module>   s$   	