
    Χg 3                     X   d dl mZmZmZmZmZmZmZmZm	Z	 d dl
Z
g dZ edd          Z G d dee                   Z G d	 d
ee                   Z G d dee                   Z G d dee                   Z G d dee                   Z G d deee                            ZdS )    )	GenericIterableIteratorListOptionalSequenceSizedTypeVarUnionN)BatchSamplerRandomSamplerSamplerSequentialSamplerSubsetRandomSamplerWeightedRandomSampler_T_coT)	covariantc                   H    e Zd ZdZddee         ddfdZdee         fdZ	dS )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.

    Args:
        data_source (Dataset): This argument is not used and will be removed in 2.2.0.
            You may still have custom implementation that utilizes it.

    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`.
    Ndata_sourcereturnc                 @    |dd l }|                    d           d S d S )Nr   zz`data_source` argument is not used and will be removed in 2.2.0.You may still have custom implementation that utilizes it.)warningswarn)selfr   r   s      T/var/www/html/ai-engine/env/lib/python3.11/site-packages/torch/utils/data/sampler.py__init__zSampler.__init__H   s?    "OOOMMM     #"    c                     t           N)NotImplementedErrorr   s    r   __iter__zSampler.__iter__Q   s    !!r   r   )
__name__
__module____qualname____doc__r   r	   r   r   r   r"    r   r   r   r      si        ' 'R HUO t    "(5/ " " " " " "r   r   c                   R    e Zd ZU dZeed<   deddfdZdee         fdZ	defdZ
dS )r   z~Samples elements sequentially, always in the same order.

    Args:
        data_source (Dataset): dataset to sample from
    r   r   Nc                     || _         d S r   )r   )r   r   s     r   r   zSequentialSampler.__init__y   s    &r   c                 ^    t          t          t          | j                                      S r   )iterrangelenr   r!   s    r   r"   zSequentialSampler.__iter__|   s#    E#d.//00111r   c                 *    t          | j                  S r   )r-   r   r!   s    r   __len__zSequentialSampler.__len__   s    4#$$$r   )r#   r$   r%   r&   r	   __annotations__r   r   intr"   r/   r'   r   r   r   r   p   s           'E 'd ' ' ' '2(3- 2 2 2 2% % % % % % %r   r   c            	           e Zd ZU dZeed<   eed<   	 	 	 ddededee         ddfdZ	e
defd	            Zdee         fd
ZdefdZdS )r   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 (Dataset): dataset to sample from
        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   replacementFNnum_samplesr   c                    || _         || _        || _        || _        t	          | j        t
                    st          d| j                   t	          | j        t                    r| j        dk    rt          d| j                   d S )N;replacement should be a boolean value, but got replacement=r   Dnum_samples should be a positive integer value, but got num_samples=)
r   r3   _num_samples	generator
isinstancebool	TypeErrorr4   r1   
ValueError)r   r   r3   r4   r9   s        r   r   zRandomSampler.__init__   s     '&'"$*D11 	`dN^``   $*C00 	D4D4I4IiW[Wgii   5J4Ir   c                 F    | j         t          | j                  S | j         S r   )r8   r-   r   r!   s    r   r4   zRandomSampler.num_samples   s'     $t'(((  r   c              #     K   t          | j                  }| j        zt          t	          j        dt          j                                                                                            }t	          j	                    }|
                    |           n| j        }| j        rt          | j        dz            D ]<}t	          j        |dt          j        |                                          E d {V  =t	          j        || j        dz  ft          j        |                                          E d {V  d S t          | j        |z            D ]0}t	          j        ||                                          E d {V  1t	          j        ||                                          d | j        |z           E d {V  d S )Nr'   dtype    )rB   )highsizerA   r9   r9   )r-   r   r9   r1   torchemptyint64random_item	Generatormanual_seedr3   r,   r4   randinttolistrandperm)r   nseedr9   _s        r   r"   zRandomSampler.__iter__   s      !!>!u{2U[999AACCHHJJKKD))I!!$''''I 	4+r122   =ekY  &((        }&+-k#	  
 fhh         4+q011 K K >!yAAAHHJJJJJJJJJJ~a9===DDFF&$"Q&&         r   c                     | j         S r   r4   r!   s    r   r/   zRandomSampler.__len__       r   )FNN)r#   r$   r%   r&   r	   r0   r;   r   r1   r   propertyr4   r   r"   r/   r'   r   r   r   r      s         	 	 
 "%)   c]	 
   , !S ! ! ! X!(3-    6             r   r   c                   l    e Zd ZU dZee         ed<   ddee         ddfdZdee         fdZ	defdZ
dS )	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                 "    || _         || _        d S r   )rX   r9   )r   rX   r9   s      r   r   zSubsetRandomSampler.__init__   s    "r   c              #      K   t          j        t          | j                  | j                  D ]}| j        |         V  d S NrE   )rF   rO   r-   rX   r9   )r   is     r   r"   zSubsetRandomSampler.__iter__   sO      DL 1 1T^LLL 	" 	"A,q/!!!!	" 	"r   c                 *    t          | j                  S r   )r-   rX   r!   s    r   r/   zSubsetRandomSampler.__len__   s    4<   r   r   )r#   r$   r%   r&   r   r1   r0   r   r   r"   r/   r'   r   r   r   r      s           c]# # #$ # # # #"(3- " " " "! ! ! ! ! ! !r   r   c            	           e Zd ZU dZej        ed<   eed<   eed<   	 	 dde	e
         dededdfdZdee         fd	Zdefd
ZdS )r   aN  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]
    weightsr4   r3   TNr   c                    t          |t                    rt          |t                    s|dk    rt          d|           t          |t                    st          d|           t	          j        |t          j                  }t          |j                  dk    r$t          dt          |j                             || _
        || _        || _        || _        d S )Nr   r7   r6   r@      z=weights should be a 1d sequence but given weights have shape )r:   r1   r;   r=   rF   	as_tensordoubler-   shapetupler_   r4   r3   r9   )r   r_   r4   r3   r9   weights_tensors         r   r   zWeightedRandomSampler.__init__   s    ;,,	+t,,	 adWbdd   +t,, 	[k[[   EEE~#$$))D&+N,@&A&AD D  
 &&&"r   c              #      K   t          j        | j        | j        | j        | j                  }t          |                                          E d {V  d S r[   )rF   multinomialr_   r4   r3   r9   r+   rN   )r   rand_tensors     r   r"   zWeightedRandomSampler.__iter__  sd      'L$*D,<
 
 
 **,,-----------r   c                     | j         S r   rT   r!   s    r   r/   zWeightedRandomSampler.__len__!  rU   r   )TN)r#   r$   r%   r&   rF   Tensorr0   r1   r;   r   floatr   r   r"   r/   r'   r   r   r   r      s          $ \ !# #%# # 	# 
# # # #@.(3- . . . .             r   r   c                       e Zd ZdZdeee         ee         f         dededdfdZ	de
ee                  fdZdefd	ZdS )
r   ai  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                    t          |t                    rt          |t                    s|dk    rt          d|           t          |t                    st          d|           || _        || _        || _        d S )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:   r1   r;   r=   rn   ro   rp   )r   rn   ro   rp   s       r   r   zBatchSampler.__init__5  s     :s++	*d++	 QaU_aa   )T** 	U)UU   $"r   c              #   V  K   | j         rMt          | j                  	 	 fdt          | j                  D             }|V  n# t
          $ r Y d S w xY w8dg| j        z  }d}| j        D ](}|||<   |dz  }|| j        k    r|V  d}dg| j        z  })|dk    r|d |         V  d S d S )NTc                 .    g | ]}t                    S r'   )next).0rR   sampler_iters     r   
<listcomp>z)BatchSampler.__iter__.<locals>.<listcomp>T  s!    PPPAT,//PPPr   r   ra   )rp   r+   rn   r,   ro   StopIteration)r   batchidx_in_batchidxrv   s       @r   r"   zBatchSampler.__iter__N  s     > 	+--LPPPPt9O9OPPPEKKKK$   EE	 C$/)EL| 2 2&)l#!4?22KKK#$LC$/1EaM\M******  s   $A 
AAc                     | j         rt          | j                  | j        z  S t          | j                  | j        z   dz
  | j        z  S )Nra   )rp   r-   rn   ro   r!   s    r   r/   zBatchSampler.__len__e  sI    
 > 	Pt|$$77%%7!;OOr   )r#   r$   r%   r&   r   r   r1   r   r;   r   r   r   r"   r/   r'   r   r   r   r   %  s         #ws|Xc]23# # 	#
 
# # # #2+(49- + + + +.P P P P P P Pr   r   )typingr   r   r   r   r   r   r	   r
   r   rF   __all__r   r   r1   r   r   r   r   r   r'   r   r   <module>r      s  
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
    	4(((4" 4" 4" 4" 4"gen 4" 4" 4"d% % % % % % % %&H  H  H  H  H GCL H  H  H V! ! ! ! !'#, ! ! !,>  >  >  >  > GCL >  >  > BHP HP HP HP HP749% HP HP HP HP HPr   