
    3Ki1                         S SK r S SKJrJrJrJr  S SKJrJr  S SK	r	/ SQr
\" SSS9r " S S	\\   5      r " S
 S\\   5      r " S S\\   5      r " S S\\   5      r " S S\\   5      r " S S\\\      5      rg)    N)IterableIteratorSequenceSized)GenericTypeVar)BatchSamplerRandomSamplerSamplerSequentialSamplerSubsetRandomSamplerWeightedRandomSampler_T_coT)	covariantc                   ,    \ rS rSrSrS\\   4S jrSrg)r      a  Base class for all Samplers.

Every Sampler subclass has to provide an :meth:`__iter__` method, providing a
way to iterate over indices or lists of indices (batches) of dataset elements,
and may provide a :meth:`__len__` method that returns the length of the returned iterators.

Example:
    >>> # xdoctest: +SKIP
    >>> class AccedingSequenceLengthSampler(Sampler[int]):
    >>>     def __init__(self, data: List[str]) -> None:
    >>>         self.data = data
    >>>
    >>>     def __len__(self) -> int:
    >>>         return len(self.data)
    >>>
    >>>     def __iter__(self) -> Iterator[int]:
    >>>         sizes = torch.tensor([len(x) for x in self.data])
    >>>         yield from torch.argsort(sizes).tolist()
    >>>
    >>> class AccedingSequenceLengthBatchSampler(Sampler[List[int]]):
    >>>     def __init__(self, data: List[str], batch_size: int) -> None:
    >>>         self.data = data
    >>>         self.batch_size = batch_size
    >>>
    >>>     def __len__(self) -> int:
    >>>         return (len(self.data) + self.batch_size - 1) // self.batch_size
    >>>
    >>>     def __iter__(self) -> Iterator[List[int]]:
    >>>         sizes = torch.tensor([len(x) for x in self.data])
    >>>         for batch in torch.chunk(torch.argsort(sizes), len(self)):
    >>>             yield batch.tolist()

.. note:: The :meth:`__len__` method isn't strictly required by
          :class:`~torch.utils.data.DataLoader`, but is expected in any
          calculation involving the length of a :class:`~torch.utils.data.DataLoader`.
returnc                     [         eN)NotImplementedErrorselfs    Z/var/www/html/dynamic-report/venv/lib/python3.13/site-packages/torch/utils/data/sampler.py__iter__Sampler.__iter__B   s    !!     N)	__name__
__module____qualname____firstlineno____doc__r   r   r   __static_attributes__r   r   r   r   r      s    #J"(5/ "r   r   c                   X    \ rS rSr% Sr\\S'   S\SS4S jrS\\	   4S jr
S\	4S jrS	rg)
r   a   zSamples elements sequentially, always in the same order.

Args:
    data_source (Sized): data source to sample from. Must implement __len__.
data_sourcer   Nc                     Xl         g r   r&   )r   r&   s     r   __init__SequentialSampler.__init__j   s    &r   c                 P    [        [        [        U R                  5      5      5      $ r   )iterrangelenr&   r   s    r   r   SequentialSampler.__iter__m   s    E#d../011r   c                 ,    [        U R                  5      $ r   )r.   r&   r   s    r   __len__SequentialSampler.__len__p   s    4##$$r   r(   )r   r   r    r!   r"   r   __annotations__r)   r   intr   r1   r#   r   r   r   r   r   a   s>     'E 'd '2(3- 2% %r   r   c            	           \ rS rSr% Sr\\S'   \\S'      SS\S\S\S-  SS4S jjr	\
S\4S	 j5       rS\\   4S
 jrS\4S jrSrg)r
   t   a  Samples elements randomly. If without replacement, then sample from a shuffled dataset.

If with replacement, then user can specify :attr:`num_samples` to draw.

Args:
    data_source (Sized): data source to sample from. Must implement __len__.
    replacement (bool): samples are drawn on-demand with replacement if ``True``, default=``False``
    num_samples (int): number of samples to draw, default=`len(dataset)`.
    generator (Generator): Generator used in sampling.
r&   replacementNnum_samplesr   c                 0   Xl         X l        X0l        X@l        [	        U R                  [
        5      (       d  [        SU R                   35      e[	        U R                  [        5      (       a  U R                  S::  a  [        SU R                   35      eg )N;replacement should be a boolean value, but got replacement=r   Dnum_samples should be a positive integer value, but got num_samples=)
r&   r7   _num_samples	generator
isinstancebool	TypeErrorr8   r4   
ValueError)r   r&   r7   r8   r=   s        r   r)   RandomSampler.__init__   s     '&'"$**D11MdN^N^M_`  $**C00D4D4D4IVW[WgWgVhi  5Jr   c                 ^    U R                   c  [        U R                  5      $ U R                   $ r   )r<   r.   r&   r   s    r   r8   RandomSampler.num_samples   s-     $t''((   r   c              #     #    [        U R                  5      nU R                  co  [        [        R
                  " S[        R                  S9R                  5       R                  5       5      n[        R                  " 5       nUR                  U5        OU R                  nU R                  (       a  [        U R                  S-  5       H>  n[        R                  " US[        R                  US9R                  5        S h  vN   M@     [        R                  " UU R                  S-  4[        R                  US9R                  5        S h  vN   g [        U R                  U-  5       H-  n[        R                   " XS9R                  5        S h  vN   M/     [        R                   " XS9R                  5       S U R                  U-    S h  vN   g  N N NG N7f)Nr   dtype    )rH   )highsizerG   r=   r=   )r.   r&   r=   r4   torchemptyint64random_item	Generatormanual_seedr7   r-   r8   randinttolistrandperm)r   nseedr=   _s        r   r   RandomSampler.__iter__   sy      !>>!u{{2U[[9AACHHJKD)I!!$'I4++r12 ==ekkY&(  3 }}&&+-kk#	
 fh  4++q01 >>!AHHJJJ 2~~a=DDF&$""Q&   KsJ   D G!GAG!GAG!G=G!GG!G!G!G!c                     U R                   $ r   r8   r   s    r   r1   RandomSampler.__len__       r   )r<   r&   r=   r7   )FNN)r   r   r    r!   r"   r   r3   r?   r4   r)   propertyr8   r   r   r1   r#   r   r   r   r
   r
   t   s    	 
 ""&  4Z	 
, !S ! !(3- 6   r   r
   c                   h    \ rS rSr% Sr\\   \S'   S
S\\   SS4S jjrS\	\   4S jr
S\4S jrS	rg)r      zSamples elements randomly from a given list of indices, without replacement.

Args:
    indices (sequence): a sequence of indices
    generator (Generator): Generator used in sampling.
indicesNr   c                     Xl         X l        g r   )ra   r=   )r   ra   r=   s      r   r)   SubsetRandomSampler.__init__   s    "r   c              #      #    [         R                  " [        U R                  5      U R                  S9R                  5        H  nU R                  U   v   M     g 7fNrK   )rL   rU   r.   ra   r=   rT   )r   is     r   r   SubsetRandomSampler.__iter__   s?     DLL 1T^^LSSUA,,q/! Vs   AAc                 ,    [        U R                  5      $ r   )r.   ra   r   s    r   r1   SubsetRandomSampler.__len__   s    4<<  r   )r=   ra   r   )r   r   r    r!   r"   r   r4   r3   r)   r   r   r1   r#   r   r   r   r   r      sF     c]# #$ #"(3- "! !r   r   c            	           \ rS rSr% Sr\R                  \S'   \\S'   \	\S'     SS\
\   S\S\	SS4S jjrS\\   4S	 jrS\4S
 jrSrg)r      a~  Samples elements from ``[0,..,len(weights)-1]`` with given probabilities (weights).

Args:
    weights (sequence)   : a sequence of weights, not necessary summing up to one
    num_samples (int): number of samples to draw
    replacement (bool): if ``True``, samples are drawn with replacement.
        If not, they are drawn without replacement, which means that when a
        sample index is drawn for a row, it cannot be drawn again for that row.
    generator (Generator): Generator used in sampling.

Example:
    >>> # xdoctest: +IGNORE_WANT("non-deterministic")
    >>> list(
    ...     WeightedRandomSampler(
    ...         [0.1, 0.9, 0.4, 0.7, 3.0, 0.6], 5, replacement=True
    ...     )
    ... )
    [4, 4, 1, 4, 5]
    >>> list(
    ...     WeightedRandomSampler(
    ...         [0.9, 0.4, 0.05, 0.2, 0.3, 0.1], 5, replacement=False
    ...     )
    ... )
    [0, 1, 4, 3, 2]
weightsr8   r7   Nr   c                    [        U[        5      (       a  [        U[        5      (       d  US::  a  [        SU 35      e[        U[        5      (       d  [        SU 35      e[        R
                  " U[        R                  S9n[        UR                  5      S:w  a!  [        S[        UR                  5       35      eXPl
        X l        X0l        X@l        g )Nr   r;   r:   rF      z=weights should be a 1d sequence but given weights have shape )r>   r4   r?   rA   rL   	as_tensordoubler.   shapetuplerl   r8   r7   r=   )r   rl   r8   r7   r=   weights_tensors         r   r)   WeightedRandomSampler.__init__   s     ;,,+t,,aVWbVcd  +t,,Mk][  E~##$)&&+N,@,@&A%BD 
 &&&"r   c              #      #    [         R                  " U R                  U R                  U R                  U R
                  S9n[        UR                  5       5       S h  vN   g  N7fre   )rL   multinomialrl   r8   r7   r=   r,   rT   )r   rand_tensors     r   r   WeightedRandomSampler.__iter__  sK     ''LL$**D,<,<
 **,---s   AA&A$A&c                     U R                   $ r   r[   r   s    r   r1   WeightedRandomSampler.__len__  r]   r   )r=   r8   r7   rl   )TN)r   r   r    r!   r"   rL   Tensorr3   r4   r?   r   floatr)   r   r   r1   r#   r   r   r   r   r      ss    4 \\ !#%# # 	# 
#@.(3- .   r   r   c                   l    \ rS rSrSrS\\   \\   -  S\S\SS4S jr	S\
\\      4S	 jrS\4S
 jrSrg)r	   i  a  Wraps another sampler to yield a mini-batch of indices.

Args:
    sampler (Sampler or Iterable): Base sampler. Can be any iterable object
    batch_size (int): Size of mini-batch.
    drop_last (bool): If ``True``, the sampler will drop the last batch if
        its size would be less than ``batch_size``

Example:
    >>> list(
    ...     BatchSampler(
    ...         SequentialSampler(range(10)), batch_size=3, drop_last=False
    ...     )
    ... )
    [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]
    >>> list(
    ...     BatchSampler(SequentialSampler(range(10)), batch_size=3, drop_last=True)
    ... )
    [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
sampler
batch_size	drop_lastr   Nc                     [        U[        5      (       a  [        U[        5      (       d  US::  a  [        SU 35      e[        U[        5      (       d  [        SU 35      eXl        X l        X0l        g )Nr   zBbatch_size should be a positive integer value, but got batch_size=z7drop_last should be a boolean value, but got drop_last=)r>   r4   r?   rA   r~   r   r   )r   r~   r   r   s       r   r)   BatchSampler.__init__4  su     :s++*d++QTU_T`a  )T**I)U  $"r   c              #   \  #    [        U R                  5      nU R                  (       a)  U/U R                  -  n[	        USS06 H	  n/ UQv   M     g / [
        R                  " XR                  5      QnU(       a0  Uv   / [
        R                  " XR                  5      QnU(       a  M/  g g 7f)NstrictF)r,   r~   r   r   zip	itertoolsislice)r   sampler_iterargsbatch_droplastbatchs        r   r   BatchSampler.__iter__M  s     DLL)>> >DOO3D"%t":E":''' #; Gi&&|__EFEJ)**<IJ %s   B&B,*B,c                     U R                   (       a"  [        U R                  5      U R                  -  $ [        U R                  5      U R                  -   S-
  U R                  -  $ )Nrn   )r   r.   r~   r   r   s    r   r1   BatchSampler.__len__Z  sI    
 >>t||$77%7!;OOr   )r   r   r~   )r   r   r    r!   r"   r   r4   r   r?   r)   r   listr   r1   r#   r   r   r   r	   r	     sd    *#-# # 	#
 
#2K(49- KP Pr   r	   )r   collections.abcr   r   r   r   typingr   r   rL   __all__r   r   r4   r   r
   r   r   r   r	   r   r   r   <module>r      s     ? ? #  	4('"gen '"J% %&H GCL H V!'#, !,F GCL F RDP749% DPr   