
    ΅i                         S SK Jr  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)NoReturn)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/home/james-whalen/.local/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\S\4S jrSrg)	_ShardingIterDataPipe   num_of_instancesinstance_idsharding_groupreturnc                     [         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   r      s-    "" " ,	"
 
"r   r   sharding_filterc                   |   ^  \ rS rSrSr SS\SS4U 4S jjjr\R                  4S jr	SS jr
S	 rS\4S
 jrSrU =r$ )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_datapiper    c                    > [         TU ]  5         Xl        X l        0 U l        SU l        SU l        U R                  5         g Nr   r   )super__init__r,   sharding_group_filtergroupsr   r   _update_num_of_instances)r%   r,   r1   	__class__s      r   r0   #ShardingFilterIterDataPipe.__init__,   s?     	.%:"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   lenr2   RuntimeErrorr3   r$   s       r   r&   )ShardingFilterIterDataPipe.apply_sharding7   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.   )sortedr2   keysr1   reverser   r   )r%   keysorted_sharding_groupsgroup_num_of_instancesgroup_instance_ids        r   r3   3ShardingFilterIterDataPipe._update_num_of_instancesK   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__[   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   r9   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   )r2   r   r   r1   r,   r"   )r    N)r   r   r   r   __doc__r   r0   r   r   r&   r3   rI   r(   rO   r   __classcell__)r4   s   @r   r	   r	       sX     DH	(+	(		( 	( =P<W<W((< 
	U 	U 	Ur   N)collections.abcr   enumr   typingr   %torch.utils.data.datapipes._decoratorr   #torch.utils.data.datapipes.datapiper   __all__r   r   r	   r   r   r   <module>rY      sc    !   E <  ' "L " &'HU!6 HU (HUr   