
    [i              	       t   d dl Z d dlZd dl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	Zded
ee         dee         fdZdedefdZe	j        fdedededefdZdedefdZddededz  defdZ ede          dededefd            ZdedefdZdedej        defdZdS )    N)Any)
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                 :    t          | t                                S N)_get_all_graph_pipes_helperset)r   s    R/var/www/icac/venv/lib/python3.11/site-packages/torch/utils/data/graph_settings.pyr   r      s    &ucee444    id_cachec                     g }|                                  D ]Z\  }\  }}||v r|                    |           |                    |           |                    t	          ||                     [|S r   )itemsaddappendextendr   )r   r   resultsdp_iddatapipe	sub_graphs         r   r   r      s     !G(- I I$$)HUx   29hGGHHHHNr   r   c                 ~    t          | t                    p(t          | d          ot          j        | j                  S )Nr   )
isinstancer   hasattrinspectismethodr   r   s    r   _is_sharding_datapiper'   )   s>    h 566 *++ 	6X455r   num_of_instancesinstance_idc                 V    t          |           }ddf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.
    Nr   c                 p   |                                  D ]\  }}d }t          |          ry|t          d| d|           t          j        |j                  }t          |j                  dk     r|                               n|                    	           |}||} ||           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   prev_applieddpr    appliedsig_helperr)   r(   r-   s
         r   r7   zapply_sharding.<locals>._helper=   s   "\\^^ 	( 	(MB	G$R(( +&d7Cd d_ad d  
 '(9::s~&&**%%&6DDDD%%(+n &    &GIw'''''	( 	(r   r   )r   N)r	   )r   r(   r)   r-   r   r7   s    ``` @r   r   r   0   s[     ""E( ( ( ( ( ( ( ( ( (, GENNNOr   c                     t          | d          oAt          | d          o1t          j        | j                  ot          j        | j                  S )Nset_shuffleset_seed)r#   r$   r%   r9   r:   r&   s    r   _is_shuffle_datapiper;   X   sT    -(( 	0Hj))	0X122	0 X.//	r   shufflec                     || S t          |           }t          |          }d |D             }|s/|r-t          j        dd           |                                 } | g}|D ]}|                    |           | 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                 0    g | ]}t          |          |S  )r;   ).0pipes     r   
<listcomp>z*apply_shuffle_settings.<locals>.<listcomp>q   s&    JJJ$/CD/I/IJJJJr   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.   )
stacklevel)r	   r   warningswarnr<   r9   )r   r<   r   	all_pipes	shufflersshufflers         r   r   r   a   s     ""E#E**IJJ)JJJI 	
 	
[	
 	
 	
 	

 ##%%
	  & &W%%%%Or   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                 "    t          | |          S r   )r
   )r   rK   s     r   r   r      s     Xs+++r   c                 T    t          | d          ot          j        | j                  S )Nr:   )r#   r$   r%   r:   r&   s    r   _is_random_datapiperN      s%    8Z((PW-=h>O-P-PPr   c                    t          |           }t          |          }t                      }g }|D ]Z}t          |          |v rt	          |          r7|                    |           |                    t          |                     [|D ]j}t          t          j	        dt          j
                                      |                                                    }|                    |           k| 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?   )dtype)	generator)r	   r   r   idrN   r   r   inttorchemptyint64random_itemr:   )r   rK   r   rG   cacherandom_datapipesrA   random_seeds           r   r
   r
      s     ""E#E**I EEE    d88ut$$ 	 ##D)))IIbhh  # #K%+...666EEJJLL
 
 	k""""Or   r   )r$   rE   typingr   typing_extensionsr   rT   (torch.utils.data.datapipes.iter.shardingr   r   torch.utils.data.graphr   r   r	   __all__listr   r   rS   r   boolr'   DEFAULTr   r;   r   FutureWarningr   rN   	Generatorr
   r?   r   r   <module>rf      sc           ( ( ( ( ( (         I H H H H H H H H H  5} 5h 5 5 5 5

$'H
	(^
 
 
 
H      '.	% %%% %
 % % % %P8      X t x    D .  
, , , , , , 
,Q( Qt Q Q Q Q u 8      r   