
    i              	       h   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y)    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    Y/var/www/html/engine/venv/lib/python3.12/site-packages/torch/utils/data/graph_settings.pyr   r      s    &uce44    id_cachec                     g }| j                         D ]J  \  }\  }}||v r|j                  |       |j                  |       |j                  t	        ||             L |S r   )itemsaddappendextendr   )r   r   resultsdp_iddatapipe	sub_graphs         r   r   r      sh     !G(- I$$)HUx 29hGHI Nr   r   c                     t        | t              xs- t        | d      xr t        j                  | j
                        S )Nr   )
isinstancer   hasattrinspectismethodr   r   s    r   _is_sharding_datapiper'   )   s;    h 56 *+ 	6X445r   num_of_instancesinstance_idc                 H    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.
    c                 P   | j                         D ]  \  }}d }t        |      rs|t        d| d|       t        j                  |j
                        }t        |j                        dk  r|j                         n|j                  	       |}||} ||        y )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(+&77CnD^_a^bd 
 ''(9(9:s~~&*%%&6D%%(+n &  &Iw''	(r   r   )r   N)r	   )r   r(   r)   r-   r   r7   s    ``` @r   r   r   0   s'     "E( (, ENOr   c                     t        | d      xrN t        | d      xr@ t        j                  | j                        xr 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12	0 X../	r   shufflec                    || S t        |       }t        |      }|D cg c]  }t        |      s| }}|s,|r*t        j                  dd       | j                         } | g}|D ]  }|j                  |        | S c c}w )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)
    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   r;   warningswarnr<   r9   )r   r<   r   	all_pipespipe	shufflersshufflers          r   r   r   a   s     "E#E*I"+J$/CD/IJIJ[	

 ##%
	  &W%& O Ks
   A>A>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   rG   s     r   r   r      s     Xs++r   c                 \    t        | d      xr t        j                  | j                        S )Nr:   )r#   r$   r%   r:   r&   s    r   _is_random_datapiperJ      s%    8Z(PW-=-=h>O>O-PPr   c                    t        |       }t        |      }t               }g }|D ]G  }t        |      |v rt	        |      s|j                  |       |j                  t        |             I |D ]_  }t        t        j                  dt        j                        j                  |      j                               }|j                  |       a | 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
     )dtype)	generator)r	   r   r   idrJ   r   r   inttorchemptyint64random_itemr:   )r   rG   r   rB   cacherandom_datapipesrC   random_seeds           r   r
   r
      s     "E#E*I EE  d8ut$##D)IIbh  ! #KK%++.666EJJL
 	k"	# Or   r   )r$   r@   typingr   typing_extensionsr   rQ   (torch.utils.data.datapipes.iter.shardingr   r   torch.utils.data.graphr   r   r	   __all__listr   r   rP   r   boolr'   DEFAULTr   r;   r   FutureWarningr   rJ   	Generatorr
   rL   r   r   <module>rc      sI      (  I H5} 5h 5

$'H
	(^
H   '..	%%% %
 %P8  X t x D .
, , , ,
,Q( Qt Q u 8 r   