o
    wZh                  	   @   sF  d dl Z d dlZd dlmZmZ d dlmZ d dlZd dlm	Z	m
Z
 d dlmZmZmZ g dZdedee fd	d
Zdedee dee fddZdedefddZe
jfdedededefddZdedefddZ	d#dedee defddZededdededefddZdedefdd Zdedejdefd!d"ZdS )$    N)AnyOptional)
deprecated)_ShardingIterDataPipeSHARDING_PRIORITIES)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   N/var/www/auris/lib/python3.10/site-packages/torch/utils/data/graph_settings.pyr      s   r   id_cachec                 C   sL   g }|   D ]\}\}}||v rq|| || |t|| q|S r   )itemsaddappendextendr   )r   r   resultsZdp_iddatapipe	sub_graphr   r   r   r      s   

r   r   c                 C   s    t | tpt| dot| jS )Nr   )
isinstancer   hasattrinspectismethodr   r   r   r   r   _is_sharding_datapipe)   s   


r#   num_of_instancesinstance_idc                    s(   t | }d fdd	  | | S )z
    Apply dynamic sharding over the ``sharding_filter`` DataPipe that has a method ``apply_sharding``.

    RuntimeError will be raised when multiple ``sharding_filter`` are presented in the same branch.
    Nc                    s   |   D ]A\}}d }t|r:|d urtd| d| t|j}t|jdk r0| n|jd |}|d u r@|} || qd S )NzoSharding twice on a single pipeline is likely unintended and will cause data loss. Sharding already applied to z while trying to apply to    )sharding_group)valuesr#   RuntimeErrorr    	signaturer   len
parameters)r   Zprev_appliedZdpr   Zappliedsig_helperr%   r$   r'   r   r   r/   =   s,   zapply_sharding.<locals>._helperr   )r	   )r   r$   r%   r'   r   r   r.   r   r   0   s   r   c                 C   s,   t | dot | dot| jot| jS )Nset_shuffleset_seed)r   r    r!   r0   r1   r"   r   r   r   _is_shuffle_datapipeX   s   


r2   shufflec                 C   sb   |du r| S t | }t|}dd |D }|s%|r%td |  } | g}|D ]}|| q'| S )aE  
    Traverse the graph of ``DataPipes`` to find and set shuffle attribute.

    Apply the method 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   )r2   ).0piper   r   r   
<listcomp>s   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warnr3   r0   )r   r3   r   	all_pipesZ	shufflersZshufflerr   r   r   r   a   s   r   z`apply_shuffle_seed` is deprecated since 1.12 and will be removed in the future releases. Please use `apply_random_seed` instead.)categoryrngc                 C   s
   t | |S r   )r
   )r   r;   r   r   r   r      s   
r   c                 C   s   t | do
t| jS )Nr1   )r   r    r!   r1   r"   r   r   r   _is_random_datapipe   s   r<   c                 C   s   t | }t|}t }g }|D ]}t||v rqt|r(|| |t| q|D ]}ttj	dtj
dj|d }|| q+| S )a6  
    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 to those ``DataPipe``.

    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_itemr1   )r   r;   r   r9   cacheZrandom_datapipesr5   Zrandom_seedr   r   r   r
      s"   

r
   r   ) r    r7   typingr   r   Ztyping_extensionsr   r@   Z(torch.utils.data.datapipes.iter.shardingr   r   Ztorch.utils.data.graphr   r   r	   __all__listr   r   r?   r   boolr#   DEFAULTr   r2   r   FutureWarningr   r<   	Generatorr
   r   r   r   r   <module>   sV   	

(

#