
    Χg              	          d dl Z d dlZd dlmZmZ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	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e         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ListOptionalSet)
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    [/var/www/html/ai-engine/env/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                 T    t          |           }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.
    Nc                 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+   r0   s
         r   r:   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   )r"   r+   r,   r0   r   r:   s    ``` @r   r   r   0   sV     ""E( ( ( ( ( ( ( ( (, GENNNOr   c                     t          | d          oAt          | d          o1t          j        | j                  ot          j        | j                  S )Nset_shuffleset_seed)r&   r'   r(   r<   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           |                                 } | 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>s   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.)r   r   warningswarnr?   r<   )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"   rL   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_datapiperO      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
    rB   )dtype)	generator)r   r   r   idrO   r   r   inttorchemptyint64random_itemr=   )r"   rL   r   rH   cacherandom_datapipesrD   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'   rF   typingr   r   r   r   typing_extensionsr   rU   (torch.utils.data.datapipes.iter.shardingr   r	   torch.utils.data.graphr
   r   r   __all__r   rT   r   boolr*   DEFAULTr   r>   r   FutureWarningr   rO   	Generatorr   rB   r   r   <module>rf      s}     + + + + + + + + + + + + ( ( ( ( ( (         I H H H H H H H H H  5} 5h 5 5 5 5

$'H
	(^
 
 
 
H      '.	% %%% %
 % % % %P8      37    !)$        F .  
, , , , , , 
,Q( Qt Q Q Q Q u 8      r   