
     i@<                    j   S SK Jr  S SKrS SKJrJr  S SKJr  S SKJ	r	  S SK
JrJr  S SKJr  S SKrS SKJrJrJr  S S	KJr  \" 5       (       a  S S
KJr  \R0                  " \5      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  " S S\5      r!g)    )annotationsN)ABCabstractmethod)defaultdict)Iterator)
accumulatecycle)Any)BatchSamplerConcatDatasetSubsetRandomSampler)is_datasets_available)Datasetc                  :   ^  \ rS rSrSrSU 4S jjrSS jrSrU =r$ )SetEpochMixin   z
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.
c                4   > [         TU ]  " U0 UD6  SU l        g Nr   )super__init__epoch)selfargskwargs	__class__s      m/home/dmtnaga/Documents/work/airagagent/rag_env/lib/python3.13/site-packages/sentence_transformers/sampler.pyr   SetEpochMixin.__init__   s    $)&)
    c                    Xl         g Nr   )r   r   s     r   	set_epochSetEpochMixin.set_epoch   s    
r   r!   )returnNone)r   intr$   r%   )	__name__
__module____qualname____firstlineno____doc__r   r"   __static_attributes____classcell__r   s   @r   r   r      s    
 r   r   c                  T   ^  \ rS rSrSr   S             SU 4S jjjrSrU =r$ )DefaultBatchSampler#   a\  
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.
    valid_label_columns (List[str], optional): 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): Seed for the random number generator to ensure reproducibility. Defaults to 0.
c                F   > [         TU ]  XUS9  X@l        XPl        X`l        g )N
batch_size	drop_last)r   r   valid_label_columns	generatorseed)r   datasetr4   r5   r6   r7   r8   r   s          r   r   DefaultBatchSampler.__init__6   s'     	9M#6 "	r   )r7   r8   r6   NNr   r9   r   r4   r&   r5   boolr6   list[str] | Noner7   torch.Generator | Noner8   r&   r$   r%   )r'   r(   r)   r*   r+   r   r,   r-   r.   s   @r   r0   r0   #   sa    . 15,0  	
 . *  
 r   r0   c                  r   ^  \ rS rSrSr   S             SU 4S jjjr\S	S j5       rS
S jrSr	U =r
$ )GroupByLabelBatchSamplerE   a  
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], optional): 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): Seed for the random number generator to ensure reproducibility. Defaults to 0.
c           	       > [         TU ]  UUUUUUS9  Xl        U R                  S-  S:X  a  [	        S5      eU R                  XR                  5      n[        [        5      n[        U5       H  u  pX   R                  U	5        M     UR                  5        V
Vs0 s H$  u  p[        U5      S-  S-  =n(       d  M  XS U _M&     snn
U l        g s  snn
f )Nr4   r5   r6   r7   r8         zEThe batch size for `GroupByLabelBatchSampler` must be divisible by 2.)r   r   r9   r4   
ValueError_determine_labels_to_user6   r   list	enumerateappenditemslengroups)r   r9   r4   r5   r6   r7   r8   labelsrN   
sample_idxlabelsample_indicesnum_samplesr   s                r   r   !GroupByLabelBatchSampler.__init___   s     	! 3 	 	
 ??Q!#dee..w8P8PQT"!*6!2JM  , "3
 *0
)7%">2a7!;;; 0E,;//)7
 
s   C>	Cc                    U=(       d    /  H  nX R                   ;   d  M  X   s  $    [        SU SU R                    S35      e)Nz None of the valid_label_columns z3 are in the dataset, which only has these columns: .)column_namesrG   )r9   r6   column_names      r   rH   1GroupByLabelBatchSampler._determine_labels_to_use   s[    .4"4K222++ 5 ./B.C D--4-A-A,B!E
 	
r   c              #    #    U R                   (       a?  U R                  b2  U R                   R                  U R                  U R                  -   5        / n[	        U R
                  R                  5       5      n[        R                  " [        U R
                  5      U R                   S9 H|  nX#   nU R
                  U   nUR                  U5        [        U5      U R                  :  d  MB  US U R                   v   XR                  S  n[        U5      U R                  :  a  M:  M~     U R                  (       d  U(       a  Uv   g g g 7f)Nr7   )r7   r8   manual_seedr   rI   rN   keystorchrandpermrM   extendr4   r5   )r   partial_batchunique_labels	label_idxrQ   sampless         r   __iter__!GroupByLabelBatchSampler.__iter__   s     >>dii3NN&&tyy4::'=>T[[--/0DKK(8DNNSI!,Ekk%(G  )m$7#$5doo66 -oo.? @ m$7	 T ~~- #0~s   C&E	,8E	&#E	)r9   rN   r;   r<   )r9   r   r6   r>   r$   z	list[Any]r$   zIterator[list[int]])r'   r(   r)   r*   r+   r   staticmethodrH   re   r,   r-   r.   s   @r   rA   rA   E   s    < 15,0

 
 	

 .
 *
 
 

 
B 
 
   r   rA   c                  d   ^  \ rS rSr   S             SU 4S jjjrSS jrS	S jrSrU =r$ )
NoDuplicatesBatchSampler   c           	        > [         TU ]  UUUUUUS9  [        UR                  5      [        U R                  =(       d    / 5      -  =n(       a  UR                  [        U5      5      nXl        g)a  
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], optional): 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): Seed for the random number generator to ensure reproducibility. Defaults to 0.
rD   N)r   r   setrW   r6   remove_columnsrI   r9   )	r   r9   r4   r5   r6   r7   r8   label_columnsr   s	           r   r   !NoDuplicatesBatchSampler.__init__   sr    F 	! 3 	 	
   4 45D<T<T<ZXZ8[[[=[,,T--@AGr   c              #    #    U R                   (       a?  U R                  b2  U R                   R                  U R                  U R                  -   5        [        R                  [        R                  " [        U R                  5      U R                   S9R                  5       5      nU(       a  [        5       n/ nU H  nU R                  U   R                  5        VVs1 s H  u  pVUS:w  d  M  [        U5      iM     nnnXr-  (       a  MP  UR                  U5        [        U5      U R                  :X  a  Uv     O*UR!                  U5        M     U R"                  (       d  Uv   U H  nX	 M     U(       a  M  ggs  snnf 7f)a  
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.
Nr[   dataset_name)r7   r8   r\   r   dictfromkeysr^   r_   rM   r9   tolistrm   rL   strrK   r4   updater5   )r   remaining_indicesbatch_valuesbatch_indicesindexkeyvaluesample_valuess           r   re   !NoDuplicatesBatchSampler.__iter__   s)     >>dii3NN&&tyy4::'=>
 !MM%..T\\9JVZVdVd*e*l*l*no5LM*>Bll5>Q>W>W>Y s>Y
]`dr]rU>Y s /$$U+}%8''##M2 + ~~''&%, ')   !ts   CFE;.E;=A:F9Fc                    U R                   (       a"  [        U R                  5      U R                  -  $ [        U R                  5      U R                  -   S-
  U R                  -  $ )NrF   )r5   rM   r9   r4   r   s    r   __len__ NoDuplicatesBatchSampler.__len__   sG    >>t||$77%7!;OOr   )r9   r;   r<   rg   r$   r&   )	r'   r(   r)   r*   r   re   r   r,   r-   r.   s   @r   rj   rj      so     15,0-- - 	-
 .- *- - 
- -^"-HP Pr   rj   c                  r   ^  \ rS rSrSr  S         SU 4S jjjr\S	S j5       r\S
S j5       rSr	U =r
$ )MultiDatasetDefaultBatchSampler   a0  
Abstract base batch sampler that yields batches from multiple batch samplers.
This class must be subclassed to implement specific sampling strategies, and
cannot be used directly.

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): Seed for the random number generator to ensure reproducibility. Defaults to 0.
c                   > [        UR                  5      [        U5      :w  a  [        S5      e[        TU ]  XS   R
                  US   R                  S9  Xl        X l        X0l	        X@l
        g )NzTThe number of batch samplers must match the number of datasets in the ConcatDataset.r   r3   )rM   datasetsrG   r   r   r4   r5   r9   batch_samplersr7   r8   )r   r9   r   r7   r8   r   s        r   r   (MultiDatasetDefaultBatchSampler.__init__  sh     w C$77sttA->-I-IUcdeUfUpUpq,"	r   c                    g)z?Yield batches from the underlying datasets in a specific order.N r   s    r   re   (MultiDatasetDefaultBatchSampler.__iter__       	r   c                    g)z,Return the number of batches in the sampler.Nr   r   s    r   r   'MultiDatasetDefaultBatchSampler.__len__  r   r   )r   r9   r7   r8   r   )
r9   r   r   zlist[BatchSampler]r7   r?   r8   r&   r$   r%   rg   r   )r'   r(   r)   r*   r+   r   r   re   r   r,   r-   r.   s   @r   r   r      sr    
  -1 + *	
  
     r   r   c                  ,    \ rS rSrSrSS jrSS jrSrg)	RoundRobinBatchSampleri  aw  
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): Seed for the random number generator to ensure reproducibility. Defaults to 0.
c              #  Z  #    U R                   (       a?  U R                  b2  U R                   R                  U R                  U R                  -   5        U R                  R
                   Vs/ s H  n[        U5      PM     nnS/[        [        U5      5      -   nU R                   Vs/ s H  n[        U5      PM     nn[        [        [        U5      5      5       H*  nX6   n [        XV   5       Vs/ s H  oU-   PM	     snv   M,     g s  snf s  snf s  snf ! [         a       g f = f7fr   )r7   r8   r\   r   r9   r   rM   rI   r   r   iterr	   rangenextStopIteration)	r   r9   rS   sample_offsetssamplerr   dataset_idxsample_offsetidxs	            r   re   RoundRobinBatchSampler.__iter__+  s     >>dii3NN&&tyy4::'=>37<<3H3HI3Hs7|3HItJ{$;<<7;7J7JK7JG$w-7JK s>':!;<K*7M6:>;V6WX6Ws]*6WXX =	 J L Y  sT   A)D++D (D+(D=&D+$D3DDD+D
D($D+'D((D+c                f    [        S U R                   5       5      [        U R                  5      -  $ )Nc              3  8   #    U  H  n[        U5      v   M     g 7fr    )rM   ).0r   s     r   	<genexpr>1RoundRobinBatchSampler.__len__.<locals>.<genexpr><  s     C/BG3w<</Bs   )minr   rM   r   s    r   r   RoundRobinBatchSampler.__len__;  s)    Ct/B/BCCc$J]J]F^^^r   r   Nrg   r   r'   r(   r)   r*   r+   re   r   r,   r   r   r   r   r     s    
 _r   r   c                  ,    \ rS rSrSrSS jrSS jrSrg)	ProportionalBatchSampleri?  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): Seed for the random number generator to ensure reproducibility. Defaults to 0.
c              #    #    U R                   R                  U R                  U R                  -   5        U R                  R
                   Vs/ s H  n[        U5      PM     nnS/[        [        U5      5      -   nU R                   Vs/ s H  n[        U5      PM     nn[        U5       VVVs/ s H  u  pg[        U5        H  oPM     M     n	nnn[        XR                   S9n
U R                   Vs/ s H  n[        U5      PM     nnU
 H*  nX<   n [        X   5       Vs/ s H  ofU-   PM	     snv   M,     g s  snf s  snf s  snnnf s  snf s  snf ! [         a     MW  f = f7f)Nr   r[   )r7   r\   r8   r   r9   r   rM   rI   r   r   rJ   r   r   r   r   r   )r   r9   rS   r   r   num_batchesr   length_dataset_indicesdataset_idx_samplerr   r   r   s                 r   re   !ProportionalBatchSampler.__iter__K  s?    ""499tzz#9:37<<3H3HI3Hs7|3HItJ{$;<<373F3FG3Fs7|3FG2;K2H`2H;3RWX^R_Q3R_32H`1/^^\7;7J7JK7JG$w-7JK.K*7M6:>;V6WX6Ws]*6WXX / J H` L Y  sl   AE)D="(E)
EE)0 E%E)5E
E)E%E3E8E)E
E&"E)%E&&E)c                j    [        U R                   Vs/ s H  n[        U5      PM     sn5      $ s  snf r    )sumr   rM   )r   r   s     r   r    ProportionalBatchSampler.__len__]  s+    0C0CD0CWCL0CDEEDs   0r   Nrg   r   r   r   r   r   r   r   ?  s    	$Fr   r   )"
__future__r   loggingabcr   r   collectionsr   collections.abcr   	itertoolsr   r	   typingr
   r^   torch.utils.datar   r   r   sentence_transformers.utilr   r   r   	getLoggerr'   loggerr   r0   rA   rj   r   r   r   r   r   r   <module>r      s    "  # # $ '   M M < 			8	$ - DT 2 T nXP2 XPv$m\3 $N_< _BF> Fr   