
    ΅i              	       x   S SK r S SKrS SKJr  S SKJr  S SKrS SKJrJ	r	  S SK
JrJrJr  / SQrS\S\\   4S	 jrS\S
\\   S\\   4S jrS\S\4S jr\	R,                  4S\S\S\S\4S jjrS\S\4S jrSS\S\S-  S\4S jjr\" S\S9S\S\S\4S j5       rS\S\4S jrS\S\R:                  S\4S jrg)    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                 *    [        U [        5       5      $ N)_get_all_graph_pipes_helperset)r   s    Y/home/james-whalen/.local/lib/python3.13/site-packages/torch/utils/data/graph_settings.pyr   r      s    &uce44    id_cachec                     / nU R                  5        HK  u  nu  pEX1;   a  M  UR                  U5        UR                  U5        UR                  [	        XQ5      5        MM     U$ r   )itemsaddappendextendr   )r   r   resultsdp_iddatapipe	sub_graphs         r   r   r      s[     !G(-$$Ux 29GH )6 Nr   r   c                     [        U [        5      =(       d3    [        U S5      =(       a     [        R                  " U R
                  5      $ )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                 N   ^^^^ [        U 5      nSSUUUU4S jjjmT" U5        U $ )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                 \  > U R                  5        H  u  p#S n[        U5      (       as  Ub  [        SU SU 35      e[        R                  " UR
                  5      n[        UR                  5      S:  a  UR                  TT5        OUR                  TTT	S9  UnUc  UnT" X45        M     g )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   apply_sharding.<locals>._helper=   s    "\\^MBG$R((+&77CnD^_a^bd 
 ''(9(9:s~~&*%%&6D%%(+n &  &I'' ,r   r   )r   N)r	   )r   r(   r)   r-   r   r7   s    ``` @r   r   r   0   s'     "E( (, ENOr   c                     [        U S5      =(       aZ    [        U S5      =(       aG    [        R                  " U R                  5      =(       a     [        R                  " U R                  5      $ )Nset_shuffleset_seed)r#   r$   r%   r:   r;   r&   s    r   _is_shuffle_datapiper<   X   sT    -( 	0Hj)	0X112	0 X../	r   shufflec                 .   Uc  U $ [        U 5      n[        U5      nU Vs/ s H  n[        U5      (       d  M  UPM     nnU(       d/  U(       a(  [        R                  " SSS9  U R                  5       n U /nU H  nUR                  U5        M     U $ s  snf )a)  
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=   r:   )r   r=   r   	all_pipespipe	shufflersshufflers          r   r   r   a   s     "E#E*I"+J)$/CD/I)IJ[	

 ##%
	 W%  O Ks
   BB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                     [        X5      $ r   )r
   )r   rH   s     r   r   r      s     X++r   c                 h    [        U S5      =(       a     [        R                  " U R                  5      $ )Nr;   )r#   r$   r%   r;   r&   s    r   _is_random_datapiperK      s%    8Z(PW-=-=h>O>O-PPr   c                    [        U 5      n[        U5      n[        5       n/ nU HQ  n[        U5      U;   a  M  [	        U5      (       d  M&  UR                  U5        UR                  [        U5      5        MS     U H[  n[        [        R                  " S[        R                  S9R                  US9R                  5       5      nUR                  U5        M]     U $ )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 to those ``DataPipe``.

Args:
    datapipe: DataPipe that needs to set randomness
    rng: Random number generator to generate random seeds
 )dtype)	generator)r	   r   r   idrK   r   r   inttorchemptyint64random_itemr;   )r   rH   r   rC   cacherandom_datapipesrD   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$   rA   typingr   typing_extensionsr   rR   (torch.utils.data.datapipes.iter.shardingr   r   torch.utils.data.graphr   r   r	   __all__listr   r   rQ   r   boolr'   DEFAULTr   r<   r   FutureWarningr   rK   	Generatorr
   rM   r   r   <module>rd      sI      (  I H5} 5h 5

$'H
	(^
H   '..	%%% %
 %P8  X t x D .
, , , ,
,Q( Qt Q u 8 r   