Ë
    ÿÍ:jéA  ã                   ó0  — d dl Z d dlmZmZmZmZ d dlmZ d dlm	Z	m
Z
mZ d dlZd dlmZ d dlmZ d dlmZmZmZmZmZmZ d dlmZ d dlmZ d d	lmZmZmZm Z  d d
l!m"Z" d dl#m$Z$ d dl%m&Z& d dl'm(Z( d dl)m*Z*m+Z+ eee,ee	df   ed   f   Z- e*«       Z.de-dee
e/   ddf   fd„Z0de-de/fd„Z1	 dde2ddde3dee   fd„Z4	 d dedeeef   de
e&   defd„Z5	 d dedeeef   de
e&   de6e6e	   e7e,e	f   f   fd„Z8	 d dedeeef   de
e&   de7e,e	f   fd„Z9de2de3fd„Z:y)!é    N)Ú	GeneratorÚIterableÚMappingÚSized)Úfields)ÚAnyÚOptionalÚUnion)Úis_dataclass_instance)ÚTensor)ÚBatchSamplerÚ
DataLoaderÚIterableDatasetÚRandomSamplerÚSamplerÚSequentialSampler)Ú	TypeGuard)Ú_reinstantiate_wrapped_clsÚ_replace_value_in_saved_argsÚhas_iterable_datasetÚ	sized_len)ÚPossibleUserWarning)Ú_IndexBatchSamplerWrapper)ÚRunningStage)ÚMisconfigurationException)ÚWarningCacheÚrank_zero_warnÚBTypeÚbatchÚreturnc              #   óÔ  K  — t        | t        «      r(| j                  dk(  rd–— y | j                  d«      –— y t        | t        t
        f«      rKt        | t        «      s;t        | t
        «      r| j                  «       } | D ]  }t        |«      E d {  –—†  Œ y t        | «      r8t        | «      D ])  }t        t        | |j                  «      «      E d {  –—†  Œ+ y d –— y 7 ŒO7 Œ­w)Nr   é   )Ú
isinstancer   ÚndimÚsizer   r   ÚstrÚvaluesÚ_extract_batch_sizer   r   ÚgetattrÚname)r   ÚsampleÚfields      úu/home/mcse/projects/srt_converter/srt-converter-venv/lib/python3.12/site-packages/lightning/pytorch/utilities/data.pyr(   r(   +   sÁ   è ø€ Ü�%œÔ Ø�:‰:˜Š?Ø‹Gà—*‘*˜Q“-ÓÜ	�EœH¤gÐ.Ô	/¼
À5Ì#Ô8NÜ�eœWÔ%Ø—L‘L“NˆEàò 	3ˆFÜ*¨6Ó2×2Ñ2ñ	3ä	˜uÔ	%Ü˜E“]ò 	GˆEÜ*¬7°5¸%¿*¹*Ó+EÓF×FÑFñ	Gð ‹
ð 3øð Gús%   ‚BC(ÂC$ÂAC(ÃC&ÃC(Ã&C(c                 óÄ   — d}d}	 t        | «      D ]'  }|€|}Œ||k7  sŒt        j                  d|› d�«        n |€t	        |«      ‚|S # t        $ r t        |«      ‚w xY w)z’Unpack a batch to find a ``torch.Tensor``.

    Returns:
        ``len(tensor)`` when found, or ``1`` when it hits an empty or non iterable.

    z”We could not infer the batch_size from the batch. Either simplify its structure or provide the batch_size as `self.log(..., batch_size=batch_size)`.NzZTrying to infer the `batch_size` from an ambiguous collection. The batch size we found is zK. To avoid any miscalculations, use `self.log(..., batch_size=batch_size)`.)r(   Úwarning_cacheÚwarnÚRecursionErrorr   )r   Ú	error_msgÚ
batch_sizeÚbss       r-   Úextract_batch_sizer5   >   s›   € ð	Pð ð €Jð(Ü% eÓ,ò 	ˆBØÐ!Ø‘
Ø˜rÓ!Ü×"Ñ"ð!Ø!+ Ð,wðyôñ ð	ð ÐÜ'¨	Ó2Ð2àÐøô ò (Ü˜YÓ'Ð'ð(ús   †A
 ŸA
 Á
AÚ
dataloaderÚstrategyzpl.strategies.StrategyÚ2allow_zero_length_dataloader_with_multiple_devicesc                 ó|  — t        | «      }|€y|j                  t        j                  ||j                  ¬«      d¬«      }|dk(  r"t        dt        | «      j                  › d�«       |dkD  r:|dk(  r5t        | «      j                  }|st        d|› d	�«      ‚t        d|› d
�«       t        | «      rt        d«       y)zIChecks if a given object has ``__len__`` method implemented on all ranks.F)ÚdeviceÚsum)Ú	reduce_opr   zTotal length of `zA` across ranks is zero. Please make sure this was your intention.ú`zW` within local rank has zero length. Please make sure that it returns at least 1 batch.zb` across ranks is zero, but local rank has zero length. Please be cautious of uneven batch length.záYour `IterableDataset` has `__len__` defined. In combination with multi-process data loading (when num_workers > 1), `__len__` could be inaccurate if each worker is not configured independently to avoid having duplicate data.T)
r   ÚreduceÚtorchÚtensorÚroot_devicer   ÚtypeÚ__name__ÚRuntimeErrorr   )r6   r7   r8   Úlocal_lengthÚtotal_lengthÚdataloader_cls_names         r-   Úhas_len_all_ranksrH   ]   sê   € ô ˜ZÓ(€LØÐàà—?‘?¤5§<¡<°ÀX×EYÑEYÔ#ZÐfk�?Ól€LØ�qÒÜØ¤ ZÓ 0× 9Ñ 9Ð:ð ;9ð 9ô	
ð �aÒ˜L¨AÒ-Ü" :Ó.×7Ñ7ÐÙAÜØÐ'Ð(ð )Fð Fóð ô 	ØÐ 3Ð4ð 5Bð Bô	
ô
 ˜JÔ'Üð/ô	
ð ó    ÚsamplerÚmodec                 ó@   — t        | ||«      \  }}t        | g|¢­i |¤ŽS ©N)Ú$_get_dataloader_init_args_and_kwargsr   )r6   rJ   rK   Údl_argsÚ	dl_kwargss        r-   Ú_update_dataloaderrQ   „   s-   € ô >¸jÈ'ÐSWÓXÑ€GˆYÜ% jÐH°7ÒH¸iÑHÐHrI   c                 óÀ  — t        | t        «      st        d| › d�«      ‚t        | d«      }|r1| j                  }| j
                  }| j                  }| j                  }nQt        | «      j                  «       D ��	ci c]  \  }}	|j                  d«      rŒ||	“Œ }
}}	d }| j                  |
d<   d}t        t        j                  | j                  «      j                   «      }t#        d„ |j%                  «       D «       «      }|rÈ|rx|j'                  t        j                  t        j                  «      j                   j                  «       D ��	ci c]!  \  }}	|	j(                  |	j*                  usŒ||	“Œ# c}	}«       nN|j'                  t        j                  t        j                  «      j                   «       |j-                  dd «       |st|j                  «       D ��ch c]  \  }}|
v sŒ|j(                  |
|   usŒ|’Œ  }}}|j/                  d	«       
j                  «       D ��	ci c]  \  }}	||v sŒ||	“Œ }}}	d}j1                  d	|«      }t        |t2        «      rd |d
<   d |d<   n|j'                  t5        | ||«      «       |j%                  «       D �ch c]f  }|j6                  |j8                  |j:                  fv r@|j(                  |j*                  u r(|j<                  |vr|j<                  |vr|j<                  ’Œh }}|rQt?        |«      }| j@                  jB                  }djE                  d„ |D «       «      }tG        d|› d|› d|› d|› d�	«      ‚|s`tI        |«      tI        |«      z  |jK                  «       z
  }|r6t?        |«      }| j@                  jB                  }tG        d|› d|› d|› d�«      ‚|fS c c}	}w c c}	}w c c}}w c c}	}w c c}w )NzThe dataloader z0 needs to subclass `torch.utils.data.DataLoader`Ú__pl_saved_argsÚ_Úmultiprocessing_context© c              3   óL   K  — | ]  }|j                   |j                  u –— Œ y ­wrM   )ÚkindÚVAR_KEYWORD)Ú.0Úps     r-   ú	<genexpr>z7_get_dataloader_init_args_and_kwargs.<locals>.<genexpr>¥   s   è ø€ ÒO¸!˜aŸf™f¨¯©Ô5ÑOùs   ‚"$ÚselfÚdatasetÚbatch_samplerrJ   z, c              3   ó(   K  — | ]
  }d |› d�–— Œ y­w)z`self.r=   NrV   )rZ   Úarg_names     r-   r\   z7_get_dataloader_init_args_and_kwargs.<locals>.<genexpr>Ð   s   è ø€ Ò(cÀ(¨6°(°¸1Ô)=Ñ(cùs   ‚z,Trying to inject custom `Sampler` into the `z…` instance. This would fail as some of the `__init__` arguments are not available as instance attributes. The missing attributes are z. If you instantiate your `zZ` inside a `*_dataloader` hook of your module, we will do this for you. Otherwise, define z inside your `__init__`.z&Trying to inject parameters into the `z{` instance. This would fail as it doesn't expose all its attributes in the `__init__` signature. The missing arguments are z. HINT: If you wrote the `zA` class, add the `__init__` arguments or allow passing `**kwargs`)&r#   r   Ú
ValueErrorÚhasattrrS   Ú__pl_saved_kwargsÚ__pl_saved_arg_namesÚ	__datasetÚvarsÚitemsÚ
startswithrU   ÚdictÚinspectÚ	signatureÚ__init__Ú
parametersÚanyr'   ÚupdateÚdefaultÚemptyÚpopÚaddÚgetr   Ú'_dataloader_init_kwargs_resolve_samplerrX   ÚPOSITIONAL_ONLYÚPOSITIONAL_OR_KEYWORDr*   ÚsortedÚ	__class__rC   Újoinr   ÚsetÚkeys)r6   rJ   rK   Úwas_wrappedrO   rP   Ú	arg_namesÚoriginal_datasetÚkÚvÚattrsÚparamsÚhas_variadic_kwargsr*   r[   Únon_defaultsr^   Úrequired_argsÚsorted_required_argsrG   Úmissing_args_messageÚmissing_kwargsÚsorted_missing_kwargss                          r-   rN   rN   ‹   sÑ  € ô
 �j¤*Ô-Ü˜?¨:¨,Ð6fÐgÓhÐhä˜*Ð&7Ó8€KÙØ×,Ñ,ˆØ×0Ñ0ˆ	Ø×3Ñ3ˆ	Ø%×/Ñ/Ñô #' zÓ"2×"8Ñ"8Ó":×T™$˜!˜QÀ!Ç,Á,ÈsÕBS��A‘ÐTˆÑTð  Ðà+5×+MÑ+MˆÐ'Ñ(Øˆ	ô ”'×#Ñ# J×$7Ñ$7Ó8×CÑCÓD€FÜÑO¸v¿}¹}»ÔOÓOÐÙñ ð �M‰MÜ!(×!2Ñ!2´:×3FÑ3FÓ!G×!RÑ!R×!XÑ!XÓ!Z÷Ù˜˜AÐ^_×^gÑ^gÐop×ovÑovÒ^v��1‘óõ ð �M‰Mœ'×+Ñ+¬J×,?Ñ,?Ó@×KÑKÔLØ�J‰J�v˜tÔ$áà,2¯L©L«N×m¡  q¸dÀeºmÐPQ×PYÑPYÐafÐgkÑalÒPlšÐmˆÑmð 	×Ñ˜Ô#à&+§k¡k£m×I™d˜a °q¸LÒ7H�Q˜‘TÐIˆ	ÑIØˆà�m‰m˜IÐ'7Ó8€GÜ�'œ?Ô+Ø%)ˆ	�/Ñ"Ø#ˆ	�)Òà×ÑÔ@ÀÈWÐVZÓ[Ô\ð —‘“öàØ�6‰6�a×'Ñ'¨×)@Ñ)@ÐAÑAØ�I‰I˜Ÿ™Ñ Ø�F‰F˜)Ñ#Ø�F‰F˜)Ñ#ð 	
�‹ð€Mð ñ Ü% mÓ4ÐØ(×2Ñ2×;Ñ;ÐØ#Ÿy™yÑ(cÐNbÔ(cÓcÐÜ'Ø:Ð;NÐ:Oð P*à*>Ð)?Ð?ZÐ[nÐZoð p"à"6Ð!7Ð7Oð	Qó
ð 	
ñ ä˜i›.¬3¨y«>Ñ9¸V¿[¹[»]ÑJˆÙÜ$*¨>Ó$:Ð!Ø",×"6Ñ"6×"?Ñ"?ÐÜ+Ø8Ð9LÐ8Mð N-à-BÐ,CÐC]Ð^qÐ]rð sRðRóð ð �IÐÐùóW Uùó"ùó nùó
 Jùòs=   Á;OÂOÅ O	
Å0O	
Ç!OÇ.OÈ OÈ+OÈ8OÊA+Oc                 óR  — |t         j                  k(  }t        | d«      }t        |«      }|��6|t        us|�r+t        |d«      r |j                  }|j                  }|j                  }|j                  }	|r/t        dd||||	«      \  }
}}|
st        d|j                  › d�«       t        d|||||	«      \  }
}}|
st        d	|j                  › d
�«      ‚t        |g|¢­i |¤Ž}njt        |d«      r1t        |d«      r%	  |||j                  |rdn|j                   ¬«      }n-|r t        d|j                  › d�t(        ¬«       nt        d«      ‚|rt+        |«      }dd|dddœS |dddœS # t        $ r3}ddl}|j%                  dt'        |«      «      }|s‚ t        d«      |‚d}~ww xY w)a.  This function is used to handle the sampler, batch_sampler arguments associated within a DataLoader for its re-
    instantiation.

    If the dataloader is being used for prediction, the sampler will be wrapped into an `_IndexBatchSamplerWrapper`, so
    Lightning can keep track of its indices.

    r_   NrS   Ú	drop_lastFzlTrying to inject `drop_last=False` into batch sampler since you are predicting, however it seems the class `z�` does not support it. Your predictions might be incomplete. To mitigate this, expose `drop_last` in the `__init__` method of your custom class.rJ   zYTrying to inject a modified sampler into the batch sampler; however, it seems the class `z‹` does not have an argument called `sampler.` To mitigate this, expose an argument `sampler` in the `__init__` method of your custom class.r3   )r3   r�   r   z:.*__init__\(\) (got multiple values)|(missing \d required)aµ   Lightning can't inject a (distributed) sampler into your batch sampler, because it doesn't subclass PyTorch's `BatchSampler`. To mitigate this, either follow the API of `BatchSampler` and instantiate your custom batch sampler inside the `*_dataloader` hook of your module, or set `Trainer(use_distributed_sampler=False)`. If you choose the latter, you will be responsible for handling the distributed sampling within your batch sampler.z&You are using a custom batch sampler `zÛ` for prediction. Lightning would normally set `drop_last=False` to ensure all samples are returned, but for custom samplers it can't guarantee this. Make sure your sampler is configured correctly to return all indices.)Úcategorya\   Lightning can't inject a (distributed) sampler into your batch sampler, because it doesn't subclass PyTorch's `BatchSampler`. To mitigate this, either follow the API of `BatchSampler` or set `Trainer(use_distributed_sampler=False)`. If you choose the latter, you will be responsible for handling the distributed sampling within your batch sampler.r"   )rJ   Úshuffler_   r3   r�   )rJ   r�   r_   )r   Ú
PREDICTINGr)   rB   r   rc   rS   rd   Ú__pl_saved_default_kwargsre   r   r   Ú__qualname__Ú	TypeErrorr   r3   r�   ÚreÚmatchr&   r   r   )r6   rJ   rK   Úis_predictingr_   Úbatch_sampler_clsÚargsÚkwargsÚdefault_kwargsr   ÚsuccessÚexr”   r•   s                 r-   rv   rv   é   s'  € ð œL×3Ñ3Ñ3€MÜ˜J¨Ó8€MÜ˜]Ó+ÐàÑ Ð&7¼|Ñ&KÊ}Ü�=Ð"3Ô4à ×0Ñ0ˆDØ"×4Ñ4ˆFØ*×DÑDˆNØ%×:Ñ:ˆIáÜ(DØ ¨¨f°nÀió)Ñ%�˜˜vñ Ü"ð/Ø/@×/MÑ/MÐ.Nð OFðFôô %AØ˜7 D¨&°.À)ó%Ñ!ˆG�T˜6ñ ÜðØ)×6Ñ6Ð7ð 8hðhóð ô 7°}ÐVÀtÒVÈvÑV‰MÜ�] LÔ1´g¸mÈ[Ô6YðÙ 1ØØ,×7Ñ7Ù(5™u¸=×;RÑ;Rô!‘ñ, ÜØ8Ð9J×9WÑ9WÐ8Xð Y ð  ô -öô ð`óð ñ Ü5°mÓDˆMð ØØ*ØØñ
ð 	
ð ¨5À4ÑHÐHøô_ ò ÛàŸ™Ð!^Ô`cÐdfÓ`gÓh�Ùàô  ðdóð ðûðús   Ã>#E* Å*	F&Å3.F!Æ!F&c                 óÔ  — t        | d«      rSd| j                  v r| j                  d   S d| j                  v r(| j                  | j                  j	                  d«         S t        | d«      rt        | j                  t        «      ryt        | d«      sy| j                  }|�t        |d«      r|j                  n|}n| j                  }t        |t        «      ryt        |t        «      S )Nrd   r�   r^   FrJ   )rc   rd   re   rS   Úindexr#   r^   r   r_   rJ   r   r   )r6   r_   rJ   s      r-   Ú_is_dataloader_shuffledrŸ   S  sÖ   € ÜˆzÐ.Ô/ð ˜
×4Ñ4Ñ4Ø×/Ñ/°	Ñ:Ð:Ø˜
×7Ñ7Ñ7Ø×-Ñ-¨j×.MÑ.M×.SÑ.SÐT]Ó.^Ñ_Ð_Üˆz˜9Ô%¬*°Z×5GÑ5GÌÔ*YàÜ�:˜yÔ)àØ×,Ñ,€MØÐ ä+2°=À)Ô+L�-×'Ò'ÐR_‰à×$Ñ$ˆÜ�'Ô,Ô-ØÜ�gœ}Ó-Ð-rI   )FrM   );rk   Úcollections.abcr   r   r   r   Údataclassesr   Útypingr   r	   r
   r?   Ú#lightning_utilities.core.apply_funcr   r   Útorch.utils.datar   r   r   r   r   r   Útyping_extensionsr   Úlightning.pytorchÚpytorchÚplÚlightning.fabric.utilities.datar   r   r   r   Ú#lightning.fabric.utilities.warningsr   Ú'lightning.pytorch.overrides.distributedr   Ú lightning.pytorch.trainer.statesr   Ú&lightning.pytorch.utilities.exceptionsr   Ú%lightning.pytorch.utilities.rank_zeror   r   r&   r   r/   Úintr(   r5   ÚobjectÚboolrH   rQ   Útuplerj   rN   rv   rŸ   rV   rI   r-   ú<module>r³      sÛ  ðó ß ?Ó ?Ý ß 'Ñ 'ã Ý EÝ ß q× qÝ 'å ÷ó õ DÝ MÝ 9Ý Lß Nàˆf�c˜7 3¨ <Ñ0°(¸7Ñ2CÐCÑD€á“€ð˜uð ¨°8¸C±=À$ÈÐ3LÑ)Mó ð&˜eð ¨ó ðD @Eñ$Øð$à&ð$ð 9=ð$ð ˆuÑó	$ðP _cñIØðIØ%*¨7°HÐ+<Ñ%=ðIØEMÈlÑE[ðIàóIð $(ñ[Øð[à�7˜HÐ$Ñ%ð[ð �<Ñ
 ð[ð ˆ5�‰:�t˜C ˜H‘~Ð%Ñ&ó	[ðB $(ñgIØðgIà�7˜HÐ$Ñ%ðgIð �<Ñ
 ðgIð 
ˆ#ˆsˆ(�^ó	gIðT.¨ð .°4ô .rI   