
    Th                         S SK Jr  S SKJr  S SKJr  S SKJr  SS/r " S S\5      r	 " S S	\5      r
\" S
5       " S S\
5      5       rg)    )Sized)IntEnum)functional_datapipe)IterDataPipeSHARDING_PRIORITIESShardingFilterIterDataPipec                        \ rS rSrSrSrSrSrg)r                N)__name__
__module____qualname____firstlineno__DEFAULTDISTRIBUTEDMULTIPROCESSING__static_attributes__r       b/var/www/fran/franai/venv/lib/python3.13/site-packages/torch/utils/data/datapipes/iter/sharding.pyr   r      s    GKOr   c                   *    \ rS rSrS\S\S\4S jrSrg)_ShardingIterDataPipe   num_of_instancesinstance_idsharding_groupc                     [         eN)NotImplementedErrorselfr   r   r   s       r   apply_sharding$_ShardingIterDataPipe.apply_sharding   s
     "!r   r   N)r   r   r   r   intr   r$   r   r   r   r   r   r      s#    "" " ,	"r   r   sharding_filterc                   \    \ rS rSrSrSS\4S jjr\R                  4S jr	S r
S rS	 rS
rg)r      aW  
Wrapper that allows DataPipe to be sharded (functional name: ``sharding_filter``).

After ``apply_sharding`` is called, each instance of the DataPipe (on different workers) will have every `n`-th element of the
original DataPipe, where `n` equals to the number of instances.

Args:
    source_datapipe: Iterable DataPipe that will be sharded
Nsource_datapipec                 f    Xl         X l        0 U l        SU l        SU l        U R                  5         g Nr   r   )r*   sharding_group_filtergroupsr   r   _update_num_of_instances)r#   r*   r-   s      r   __init__#ShardingFilterIterDataPipe.__init__+   s1    .%:"24 !%%'r   c                 t   X!:  a  [        SU SU S35      eU[        R                  :X  aD  [        U R                  5      (       a)  [        R                  U R                  ;  a  [        S5      eO)[        R                  U R                  ;   a  [        S5      eX4U R                  U'   U R                  5         g )Nzinstance_id(z*) should be smaller than num_of_instances()z8ShardingFilter cannot mix DEFAULT and non DEFAULT groups)
ValueErrorr   r   lenr.   RuntimeErrorr/   r"   s       r   r$   )ShardingFilterIterDataPipe.apply_sharding3   s     *{m+UVfUgghi  08884;;$7$?$?t{{$R"N  #**dkk9"N  (8&EN#%%'r   c                    [        U R                  R                  5       5       Vs/ s H0  nU R                  b  XR                  :X  d  M!  U R                  U   PM2     nnUR	                  5         SU l        SU l        U H<  u  p4U =R                  U R
                  U-  -  sl        U =R
                  U-  sl        M>     g s  snf r,   )sortedr.   keysr-   reverser   r   )r#   keysorted_sharding_groupsgroup_num_of_instancesgroup_instance_ids        r   r/   3ShardingFilterIterDataPipe._update_num_of_instancesG   s     dkk..01"
1))1S<V<V5V DKK1 	 "
 	&&( !9O5" 5 58I II!!%;;! :P"
s    C 
C c              #      #    [        U R                  5       H'  u  pXR                  -  U R                  :X  d  M#  Uv   M)     g 7fr    )	enumerater*   r   r   )r#   iitems      r   __iter__#ShardingFilterIterDataPipe.__iter__W   s8      !5!56GA(((D,<,<<
 7s
   5A	Ac                 4   [        U R                  [        5      (       aY  [        U R                  5      U R                  -  U R
                  [        U R                  5      U R                  -  :  a  S-   $ S-   $ [        [        U 5      R                   S35      e)Nr   r   z# instance doesn't have valid length)	
isinstancer*   r   r5   r   r   	TypeErrortyper   )r#   s    r   __len__"ShardingFilterIterDataPipe.__len__\   s    d**E22t++,0E0EE $$s4+?+?'@4CXCX'XX  
   4:..//RSTTr   )r.   r   r   r-   r*   r    )r   r   r   r   __doc__r   r0   r   r   r$   r/   rE   rK   r   r   r   r   r   r      s4    ( ( =P<W<W((< 
	Ur   N)collections.abcr   enumr   %torch.utils.data.datapipes._decoratorr   #torch.utils.data.datapipes.datapiper   __all__r   r   r   r   r   r   <module>rS      s`    !  E <  ' "L " &'EU!6 EU (EUr   