o
    5ti                     @  s   U d dl mZ d dlZd dlmZ d dlmZ er-d dlmZm	Z	 d dlm
Z
mZ edZeeZG dd	 d	ZG d
d deZG dd deZG dd deZeedZded< dddZdS )    )annotationsN)Random)TYPE_CHECKING)IterableSequence)AnyTypeVar_Tc                   @  s^   e Zd Z	d!dddd"ddZ		d#d$ddZd%ddZd&ddZdd Zed!d'dd Z	dS )(ContextSamplerN)rndfewshot_indicesdfSequence[dict[str, Any]] | Noner   
int | Noner   list[int] | NonereturnNonec                K  s$   t || _|pg | _|| _d| _d S NF)r   r   r   r   _loaded)selfr   r   r   kwargs r   H/home/ubuntu/.local/lib/python3.10/site-packages/lm_eval/api/samplers.py__init__   s   


zContextSampler.__init__ninteval_docdict[str, Any] | NoneSequence[dict[str, Any]]c                 K  s   |dksJ d|dkrg S |r|| _ | j sJ d|s%| j|  |n| || j|  |d |}t||ksHJ dt| d| d|S )a"  
        Sample n documents from the pool.

        Args:
            n: Number of documents to sample
            eval_doc: Optional document to exclude from sampling
            df: Optional list of documents to sample from

        Returns:
            List of sampled documents
        r   z.Error: number of samples requested must be >=0z+Error: no documents available for sampling.   z+Error: number of fewshot samples returned (z!) not equal to number requested (z).)r   r   samplefewshot_docsrm_eval_doclen)r   r   r   r   r   resr   r   r   r       s    zContextSampler.samplec                 C  s   t || _| S N)r   r   )r   r   r   r   r   set_rndE   s   
zContextSampler.set_rndc                 C  s   || _ d| _| S r   )r   r   )r   r   r   r   r   
replace_dfI   s   zContextSampler.replace_dfc                   sD    j r jS  jr jr j s fdd jD  _d _ t jS )z'Return cached fewshot docs if availablec                   s   g | ]} j | qS r   )r   ).0ir   r   r   
<listcomp>S   s    z/ContextSampler.fewshot_docs.<locals>.<listcomp>T)r   r   r   listr*   r   r*   r   r!   N   s   
zContextSampler.fewshot_docsdocr	   _iterIterable[_T]Sequence[_T]c                   s4   |d u r fdd|D S  fdd|D d | S )Nc                      g | ]}| kr|qS r   r   r(   xr-   r   r   r+   Z       z.ContextSampler.rm_eval_doc.<locals>.<listcomp>c                   r1   r   r   r2   r4   r   r   r+   \   r5   r   )r-   r.   r   r   r4   r   r"   W   s
   zContextSampler.rm_eval_docr%   )r   r   r   r   r   r   r   r   NN)r   r   r   r   r   r   r   r   )r   r   )r   r   )r-   r	   r.   r/   r   r0   )
__name__
__module____qualname__r   r    r&   r'   r!   staticmethodr"   r   r   r   r   r
      s    
&
	r
   c                   @     e Zd ZddddZdS )FirstNSamplerNr   r   c                 K  s2   |t | jksJ dt | j d| jd| S )z
        Draw the first `n` samples in order from the specified split.
        Used for tasks with "canonical" ordered fewshot examples, such as MMLU and CMMLU.
        z7Error: number of fewshot samples requested exceeds the z that are available.N)r#   r   r   r   r   r   r   r   r   r   r    a   s   zFirstNSampler.sampler6   r   r   r7   r8   r9   r    r   r   r   r   r<   `       r<   c                   @  r;   )BalancedSamplerNr   r   c                 K     t )z
        TODO: this should return approximately class-balanced samples from our fewshot examples.
        TODO: what order should they be in? maybe random?
        NotImplementedErrorr=   r   r   r   r    m   s   zBalancedSampler.sampler6   r>   r?   r   r   r   r   rA   l   r@   rA   c                   @  r;   )ManualSamplerNr   r   c                 K  rB   ) rC   r=   r   r   r   r    w   s   zManualSampler.sampler6   r>   r?   r   r   r   r   rE   v   r@   rE   )defaultfirst_nzdict[str, type[ContextSampler]]SAMPLER_REGISTRYnamestrc                 C  sD   zt |  W S  ty! } ztd|  ddt   |d }~ww )Nz!Attempted to use contextsampler 'zH', but no sampling strategy for this name found! Supported model names: z, )rI   KeyErrorjoinkeys)rJ   er   r   r   get_sampler   s   
rP   )rJ   rK   )
__future__r   loggingrandomr   typingr   collections.abcr   r   r   r   r	   	getLoggerr7   eval_loggerr
   r<   rA   rE   rI   __annotations__rP   r   r   r   r   <module>   s"    
O
