
    Ng4                    f   d dl mZ d dlZd dlmZ d dlmZ d dlmZm	Z	 d dl
mZ d dlZd dlmZmZmZ d dlmZ  e            rd d	lmZ  ej        e          Z G 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dS )    )annotationsN)defaultdict)Iterator)
accumulatecycle)Any)BatchSamplerConcatDatasetSubsetRandomSampler)is_datasets_available)Datasetc                  ,     e Zd ZdZd fdZd	dZ xZS )
SetEpochMixinz
    Required for a BatchSampler as the Trainer will call set_epoch on the BatchSampler at the beginning of each epoch.
    The BatchSampler can then set the generator seed accordingly.
    returnNonec                H     t                      j        |i | d| _        d S )Nr   )super__init__epoch)selfargskwargs	__class__s      Y/var/www/html/ai-engine/env/lib/python3.11/site-packages/sentence_transformers/sampler.pyr   zSetEpochMixin.__init__   s*    $)&)))


    r   intc                    || _         d S N)r   )r   r   s     r   	set_epochzSetEpochMixin.set_epoch   s    


r   )r   r   )r   r   r   r   )__name__
__module____qualname____doc__r   r   __classcell__r   s   @r   r   r      s[         
     
       r   r   c                      e Zd ZdZdS )DefaultBatchSamplera  
    This sampler is the default batch sampler used in the SentenceTransformer library.
    It is equivalent to the PyTorch BatchSampler.

    Args:
        sampler (Sampler or Iterable): The sampler used for sampling elements from the dataset,
            such as SubsetRandomSampler.
        batch_size (int): Number of samples per batch.
        drop_last (bool): If True, drop the last incomplete batch if the dataset size
            is not divisible by the batch size.
    N)r    r!   r"   r#    r   r   r'   r'   #   s        
 
 
 
r   r'   c                  L     e Zd ZdZ	 	 	 dd fdZedd            ZddZ xZS )GroupByLabelBatchSampleraK  
    This sampler groups samples by their labels and aims to create batches such that
    each batch contains samples where the labels are as homogeneous as possible.
    This sampler is meant to be used alongside the ``Batch...TripletLoss`` classes, which
    require that each batch contains at least 2 examples per label class.

    Recommended for:
        - :class:`~sentence_transformers.losses.BatchAllTripletLoss`
        - :class:`~sentence_transformers.losses.BatchHardSoftMarginTripletLoss`
        - :class:`~sentence_transformers.losses.BatchHardTripletLoss`
        - :class:`~sentence_transformers.losses.BatchSemiHardTripletLoss`

    Args:
        dataset (Dataset): The dataset to sample from.
        batch_size (int): Number of samples per batch. Must be divisible by 2.
        drop_last (bool): If True, drop the last incomplete batch if the dataset size
            is not divisible by the batch size.
        valid_label_columns (List[str]): List of column names to check for labels.
            The first column name from ``valid_label_columns`` found in the dataset will
            be used as the label column.
        generator (torch.Generator, optional): Optional random number generator for shuffling
            the indices.
        seed (int, optional): Seed for the random number generator to ensure reproducibility.
    Nr   datasetr   
batch_sizer   	drop_lastboolvalid_label_columns	list[str]	generatortorch.Generatorseedr   r   c                   t                                          |||           || _        || _        || _        || _        || _        | j        dz  dk    rt          d          |                     ||          }t          t                    }t          |          D ] \  }	}
||
                             |	           !fd|                                D             | _        d S )N      zEThe batch size for `GroupByLabelBatchSampler` must be divisible by 2.c                Z    i | ]'\  }}t          |          d z  d z  x||d         (S )r5   Nlen).0labelsample_indicesnum_sampless      r   
<dictcomp>z5GroupByLabelBatchSampler.__init__.<locals>.<dictcomp>c   sU     
 
 
%~">22a7!;;
>,;,/
 
 
r   )r   r   r+   r,   r-   r1   r3   
ValueError_determine_labels_to_user   list	enumerateappenditemsgroups)r   r+   r,   r-   r/   r1   r3   labelsrE   
sample_idxr;   r=   r   s              @r   r   z!GroupByLabelBatchSampler.__init__K   s     	*i888$""	?Q!##deee..w8KLLT""!*6!2!2 	- 	-J5M  ,,,,
 
 
 
)/
 
 
r   	list[Any]c                l    |pg D ]}|| j         v r
| |         c S t          d| d| j          d          )Nz None of the valid_label_columns z3 are in the dataset, which only has these columns: .)column_namesr?   )r+   r/   column_names      r   r@   z1GroupByLabelBatchSampler._determine_labels_to_usei   sy    .4" 	, 	,Kg222{++++ 3E/B E E-4-AE E E
 
 	
r   Iterator[list[int]]c              #  :  K   | j         r.| j        r'| j                             | j        | j        z              g }t	          | j                                                  }t          j        t          | j                  | j                   D ]|}||         }| j        |         }|
                    |           t          |          | j        k    r8|d | j                 V  || j        d          }t          |          | j        k    8}| j        s|r|V  d S d S d S )Nr1   )r1   r3   manual_seedr   rA   rE   keystorchrandpermr9   extendr,   r-   )r   partial_batchunique_labels	label_idxr;   sampless         r   __iter__z!GroupByLabelBatchSampler.__iter__s   s?     > 	?di 	?N&&ty4:'=>>>T[--//00DK(8(8DNSSS 	A 	AI!),Ek%(G  )))m$$77#$5do$56666 -do.?.? @ m$$77 ~ 	 - 	 	  	  	  	 r   )NNr   r+   r   r,   r   r-   r.   r/   r0   r1   r2   r3   r   r   r   )r+   r   r/   r0   r   rH   r   rM   )	r    r!   r"   r#   r   staticmethodr@   rY   r$   r%   s   @r   r*   r*   1   s         < *.%)
 
 
 
 
 
 
< 
 
 
 \
               r   r*   c                  8     e Zd Zg ddfd fdZddZddZ xZS )NoDuplicatesBatchSamplerNr   r+   r   r,   r   r-   r.   r/   r0   r1   r2   r3   r   r   c                
   t                                          |||           t          |j                  t          |          z  x}r|                    |          }|| _        || _        || _        || _        || _	        dS )as  
        This sampler creates batches such that each batch contains samples where the values are unique,
        even across columns. This is useful when losses consider other samples in a batch to be in-batch
        negatives, and you want to ensure that the negatives are not duplicates of the anchor/positive sample.

        Recommended for:
            - :class:`~sentence_transformers.losses.MultipleNegativesRankingLoss`
            - :class:`~sentence_transformers.losses.CachedMultipleNegativesRankingLoss`
            - :class:`~sentence_transformers.losses.MultipleNegativesSymmetricRankingLoss`
            - :class:`~sentence_transformers.losses.CachedMultipleNegativesSymmetricRankingLoss`
            - :class:`~sentence_transformers.losses.MegaBatchMarginLoss`
            - :class:`~sentence_transformers.losses.GISTEmbedLoss`
            - :class:`~sentence_transformers.losses.CachedGISTEmbedLoss`

        Args:
            dataset (Dataset): The dataset to sample from.
            batch_size (int): Number of samples per batch.
            drop_last (bool): If True, drop the last incomplete batch if the dataset size
                is not divisible by the batch size.
            valid_label_columns (List[str]): List of column names to check for labels.
                The first column name from ``valid_label_columns`` found in the dataset will
                be used as the label column.
            generator (torch.Generator, optional): Optional random number generator for shuffling
                the indices.
            seed (int, optional): Seed for the random number generator to ensure reproducibility.
        N)
r   r   setrK   remove_columnsr+   r,   r-   r1   r3   )	r   r+   r,   r-   r/   r1   r3   label_columnsr   s	           r   r   z!NoDuplicatesBatchSampler.__init__   s    F 	*i888 455<O8P8PPP= 	<,,];;G$""			r   rM   c              #  j  K   | j         r.| j        r'| j                             | j        | j        z              t	          t          j        t          | j                  | j                   	                                          }|rt	                      }g }|D ]y}d | j        |         
                                D             }||z  r1|                    |           t          |          | j        k    r|V   n!|                    |           z| j        s|V  |t	          |          z  }|dS dS )a5  
        Iterate over the remaining non-yielded indices. For each index, check if the sample values are already in the
        batch. If not, add the sample values to the batch keep going until the batch is full. If the batch is full, yield
        the batch indices and continue with the next batch.
        rO   c                N    h | ]"\  }}|                     d           s|dk     |#S )_prompt_lengthdataset_name)endswith)r:   keyvalues      r   	<setcomp>z4NoDuplicatesBatchSampler.__iter__.<locals>.<setcomp>   sI     ! ! !"U<<(899! ?B^>S>S >S>S>Sr   N)r1   r3   rP   r   r`   rR   rS   r9   r+   tolistrD   rC   r,   updater-   )r   remaining_indicesbatch_valuesbatch_indicesindexsample_valuess         r   rY   z!NoDuplicatesBatchSampler.__iter__   sq      > 	?di 	?N&&ty4:'=>>>s4</@/@DN [ [ [ b b d dee 	455LM* ( (! !&*l5&9&?&?&A&A! ! !
 !</ $$U+++}%%88''''E##M2222 ~ ('''']!3!331   	4 	4 	4 	4 	4r   c                    | j         rt          | j                  | j        z  S t          | j                  | j        z   dz
  | j        z  S )Nr6   )r-   r9   r+   r,   r   s    r   __len__z NoDuplicatesBatchSampler.__len__   sG    > 	Pt|$$77%%7!;OOr   rZ   r[   r   r   r    r!   r"   r   rY   rt   r$   r%   s   @r   r^   r^      s         *,%)* * * * * * *X"4 "4 "4 "4HP P P P P P P Pr   r^   c                  :     e Zd ZdZ	 	 dd fdZddZddZ xZS )RoundRobinBatchSamplera  
    Batch sampler that yields batches in a round-robin fashion from multiple batch samplers, until one is exhausted.
    With this sampler, it's unlikely that all samples from each dataset are used, but we do ensure that each dataset
    is sampled from equally.

    Args:
        dataset (ConcatDataset): A concatenation of multiple datasets.
        batch_samplers (List[BatchSampler]): A list of batch samplers, one for each dataset in the ConcatDataset.
        generator (torch.Generator, optional): A generator for reproducible sampling. Defaults to None.
        seed (int, optional): A seed for the generator. Defaults to None.
    Nr+   r
   batch_samplerslist[BatchSampler]r1   r2   r3   r   r   r   c                   t          |j                  t          |          k    rt          d          t                                          ||d         j        |d         j                   || _        || _        || _	        || _
        d S )NzTThe number of batch samplers must match the number of datasets in the ConcatDataset.r   )r9   datasetsr?   r   r   r,   r-   r+   ry   r1   r3   r   r+   ry   r1   r3   r   s        r   r   zRoundRobinBatchSampler.__init__   s     w  C$7$777sttt."3">q@Q@[\\\,"			r   rM   c              #    K   | j         r.| j        r'| j                             | j        | j        z              d | j        j        D             }dgt          t          |                    z   }d | j        D             }t          t          t          |                              D ]@}||         	 fdt          ||                   D             V  /# t          $ r Y  d S w xY wd S )Nc                ,    g | ]}t          |          S r(   r8   r:   r+   s     r   
<listcomp>z3RoundRobinBatchSampler.__iter__.<locals>.<listcomp>       IIIs7||IIIr   r   c                ,    g | ]}t          |          S r(   iterr:   samplers     r   r   z3RoundRobinBatchSampler.__iter__.<locals>.<listcomp>       KKKG$w--KKKr   c                    g | ]}|z   S r(   r(   r:   idxsample_offsets     r   r   z3RoundRobinBatchSampler.__iter__.<locals>.<listcomp>      XXXss]*XXXr   )r1   r3   rP   r   r+   r|   rA   r   ry   r   ranger9   nextStopIteration)r   r=   sample_offsetsry   dataset_idxr   s        @r   rY   zRoundRobinBatchSampler.__iter__   s     > 	?di 	?N&&ty4:'=>>>II4<3HIIItJ{$;$;<<<KKt7JKKK s>':':!;!;<< 	 	K*;7MXXXXd>+;V6W6WXXXXXXX    		 	s   4#C
C'&C'c                h    t          d | j        D                       t          | j                  z  S )Nc              3  4   K   | ]}t          |          V  d S r   r8   r   s     r   	<genexpr>z1RoundRobinBatchSampler.__len__.<locals>.<genexpr>
  s(      CCG3w<<CCCCCCr   )minry   r9   rs   s    r   rt   zRoundRobinBatchSampler.__len__	  s2    CCt/BCCCCCc$J]F^F^^^r   )NN
r+   r
   ry   rz   r1   r2   r3   r   r   r   r[   ru   )r    r!   r"   r#   r   rY   rt   r$   r%   s   @r   rx   rx      s        
 
  &*          _ _ _ _ _ _ _ _r   rx   c                  0     e Zd Zd fdZddZddZ xZS )ProportionalBatchSamplerr+   r
   ry   rz   r1   r2   r3   r   r   r   c                    t                                          ||d         j        |d         j                   || _        || _        || _        || _        dS )a  
        Batch sampler that samples from each dataset in proportion to its size, until all are exhausted simultaneously.
        With this sampler, all samples from each dataset are used and larger datasets are sampled from more frequently.

        Args:
            dataset (ConcatDataset): A concatenation of multiple datasets.
            batch_samplers (List[BatchSampler]): A list of batch samplers, one for each dataset in the ConcatDataset.
            generator (torch.Generator, optional): A generator for reproducible sampling. Defaults to None.
            seed (int, optional): A seed for the generator. Defaults to None.
        r   N)r   r   r,   r-   r+   ry   r1   r3   r}   s        r   r   z!ProportionalBatchSampler.__init__  sR    " 	."3">q@Q@[\\\,"			r   rM   c              #    K   | j                             | j        | j        z              d | j        j        D             }dgt          t          |                    z   }d | j        D             }d t          |          D             }t          || j                   }d | j        D             }|D ]>}||         	 fdt          ||                   D             V  /# t          $ r Y ;w xY wd S )Nc                ,    g | ]}t          |          S r(   r8   r   s     r   r   z5ProportionalBatchSampler.__iter__.<locals>.<listcomp>(  r   r   r   c                ,    g | ]}t          |          S r(   r8   r   s     r   r   z5ProportionalBatchSampler.__iter__.<locals>.<listcomp>+  s    GGGs7||GGGr   c                <    g | ]\  }}t          |          D ]}|S r(   )r   )r:   r   length_s       r   r   z5ProportionalBatchSampler.__iter__.<locals>.<listcomp>,  s3    ```;3RWX^R_R_``Q3````r   rO   c                ,    g | ]}t          |          S r(   r   r   s     r   r   z5ProportionalBatchSampler.__iter__.<locals>.<listcomp>/  r   r   c                    g | ]}|z   S r(   r(   r   s     r   r   z5ProportionalBatchSampler.__iter__.<locals>.<listcomp>3  r   r   )r1   rP   r3   r   r+   r|   rA   r   ry   rB   r   r   r   )	r   r=   r   num_batchesdataset_indicesdataset_idx_samplerry   r   r   s	           @r   rY   z!ProportionalBatchSampler.__iter__%  s.     ""49tz#9:::II4<3HIIItJ{$;$;<<<GG43FGGG``)K2H2H```1/T^\\\KKt7JKKK. 	 	K*;7MXXXXd>+;V6W6WXXXXXXX    		 	s   ?#C##
C0/C0c                >    t          d | j        D                       S )Nc                ,    g | ]}t          |          S r(   r8   r   s     r   r   z4ProportionalBatchSampler.__len__.<locals>.<listcomp>8  s    DDDWCLLDDDr   )sumry   rs   s    r   rt   z ProportionalBatchSampler.__len__7  s"    DD0CDDDEEEr   r   r[   ru   rv   r%   s   @r   r   r     sm             .   $F F F F F F F Fr   r   )
__future__r   loggingcollectionsr   collections.abcr   	itertoolsr   r   typingr   rR   torch.utils.datar	   r
   r   sentence_transformers.utilr   r|   r   	getLoggerr    loggerr   r'   r*   r^   rx   r   r(   r   r   <module>r      s   " " " " " "  # # # # # # $ $ $ $ $ $ ' ' ' ' ' ' ' '        M M M M M M M M M M < < < < < < !      		8	$	$           -   Q  Q  Q  Q  Q }l Q  Q  Q hUP UP UP UP UP}l UP UP UPp-_ -_ -_ -_ -_]L -_ -_ -_`+F +F +F +F +F}l +F +F +F +F +Fr   