U
    •Ê¹jW  ã                   @   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
 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dœdd„Ze
jf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)Ú_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   ú^/var/www/html/bwave/python/venv/lib/python3.8/site-packages/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ÚdatapipeÚ	sub_graphr   r   r   r      s    

r   )r   r   c                 C   s,   t | tƒrdS t| dƒr(t | j¡r(dS dS )NTr   F)Ú
isinstancer   ÚhasattrÚinspectÚismethodr   ©r   r   r   r   Ú_is_sharding_datapipe&   s
    
r$   )r   Únum_of_instancesÚinstance_idr   c                    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 ]‚\}}d }t|ƒrt|d k	r8td|› d|› �ƒ‚t |j¡}t|jƒdk r`| ˆˆ¡ n|jˆˆˆd� |}|d k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   r0   8   s    zapply_sharding.<locals>._helper)N)r
   )r   r%   r&   r(   r   r   r/   r   r   .   s    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    r!   r"   r1   r2   r#   r   r   r   Ú_is_shuffle_datapipeO   s
    r3   )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   )r3   )Ú.0Úpiper   r   r   Ú
<listcomp>e   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Úwarnr4   r1   )r   r4   r   Ú	all_pipesZ	shufflersZshufflerr   r   r   r   W   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.)r8   r9   r   )r   r;   r   r   r   r   t   s    ÿc                 C   s   t | dƒrt | j¡rdS dS )Nr2   TF)r    r!   r"   r2   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_Úitemr2   )r   r;   r   r:   ÚcacheZrandom_datapipesr6   Zrandom_seedr   r   r   r   ‚   s    	
 )N)r!   r8   Útypingr   r   r   r   r@   Z(torch.utils.data.datapipes.iter.shardingr   r   Ztorch.utils.data.graphr   r	   r
   Ú__all__r   r?   r   Úboolr$   ÚDEFAULTr   r3   r   r   r<   Ú	Generatorr   r   r   r   r   Ú<module>   s2   û	ýý!