
    Ph'              	       N   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 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dededefdZdedefdZdedej6                  defdZy)    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                 *    t        | t                     S N)_get_all_graph_pipes_helperset)r   s    jC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\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      s_     G(-$$)HUx 29hGH )6 Nr   r!   c                 ~    t        | t              ryt        | d      r t        j                  | j
                        ryy)NTr   F)
isinstancer   hasattrinspectismethodr   r!   s    r   _is_sharding_datapiper)   &   s4    (12x)*w/?/?@W@W/Xr   num_of_instancesinstance_idc                 F    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.
    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   r9   zapply_sharding.<locals>._helper9   s    $||~ORG$R(+& (FFR^Smnpmq(s t t ''(9(9:s~~&*%%&6D%%&6Tb%c&Iw'  .r   r   )r   )r!   r*   r+   r/   r   r9   s    ``` @r   r   r   .   s'     "E( ($ ENOr   c                     t        | d      rt        | d      syt        j                  | j                        rt        j                  | j                        syy)Nset_shuffleset_seedFT)r%   r&   r'   r;   r<   r(   s    r   _is_shuffle_datapiper=   P   sE    8]+78Z3PH0019I9I(J[J[9\r   shufflec                    || S t        |       }t        |      }|D cg c]  }t        |      s| }}|s*|r(t        j                  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.)r   r   r=   warningswarnr>   r;   )r!   r>   r   	all_pipespipe	shufflersshufflers          r   r   r   X   s     "E#E*I"+J)$/CD/I)IJ[	
 ##%L	W%  O Ks
   A<A<rngc                 D    t        j                  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.)r@   rA   r   )r!   rF   s     r   r   r   w   s"    MM	4 Xs++r   c                 \    t        | d      r t        j                  | j                        ryy)Nr<   TF)r%   r&   r'   r<   r(   s    r   _is_random_datapiperI      s%    x$)9)9(:K:K)Lr   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   idrI   r   r   inttorchemptyint64random_itemr<   )r!   rF   r   rB   cacherandom_datapipesrC   random_seeds           r   r   r      s     "E#E*I EEd8ut$##D)IIbh  !%++b<DDsDSXXZ[k" ! Or   r   )r&   r@   typingr   r   r   r   rP   (torch.utils.data.datapipes.iter.shardingr   r   torch.utils.data.graphr	   r
   r   __all__r   rO   r   boolr)   DEFAULTr   r=   r   r   rI   	Generatorr   rK   r   r   <module>r_      s'     + +  I H5} 5h 5} C TRZ^ H   #6"="=X %( # CKD8  X  RZ >, , , ,( t  u 8 r   