
    hX                     `    S SK JrJrJrJr  S SKrS SKJrJrJ	r	J
r
  SSKJrJr   " S S5      rg)    )AnyCallableOptionalUnionN)GenerationConfigPreTrainedTokenizerPreTrainedTokenizerFastset_seed   )SUPPORTED_ARCHITECTURESPreTrainedModelWrapperc                       \ rS rSrSr    SS\S\\\4   S\	\
\   /\
\   4   S\S\S	\\   S
\S\\   SS4S jjr  SS\\
\   \R&                  \
\R&                     \
\
\      4   S\S\\\\R*                  4      S\
\
\      4S jjrSrg)BestOfNSampler   a  
Sampler for best-of-n generation.

Args:
    model ([`PreTrainedModelWrapper`]):
        The pretrained model to use for generation.
    tokenizer ([`~transformers.PreTrainedTokenizer`] or [`~transformers.PreTrainedTokenizerFast`]):
        Tokenizer associated with the pretrained model.
    queries_to_scores (`Callable[[list[str]], list[float]]`):
        Callable that takes a list of generated texts and returns the associated reward scores.
    length_sampler (`Any`):
        Sampler used to sample the length of the generated text.
    sample_size (`int`, *optional*, defaults to `4`):
        Number of samples to generate for each query.
    seed (`int`, *optional*):
        Random seed used to control generation.
    n_candidates (`int`, *optional*, defaults to `1`):
        Number of candidates to return for each query.
    generation_config ([`~transformers.GenerationConfig`], *optional*):
        Generation config passed to the underlying model's `generate` method. See
        [`~transformers.GenerationConfig`] for more details.
Nmodel	tokenizerqueries_to_scoreslength_samplersample_sizeseedn_candidatesgeneration_configreturnc	                 >   Ub  [        U5        [        U[        [        45      (       d  [	        S[        U5       35      e[        U[        5      (       d  [	        S[        U5       S[         35      eXl        X l        X0l	        X@l
        Xl        XPl        Xpl        g )NzHtokenizer must be a PreTrainedTokenizer or PreTrainedTokenizerFast, got z,model must be a PreTrainedModelWrapper, got z  - supported architectures are: )r
   
isinstancer   r	   
ValueErrortyper   r   r   r   r   
gen_configr   r   )	selfr   r   r   r   r   r   r   r   s	            V/home/james-whalen/.local/lib/python3.13/site-packages/trl/extras/best_of_n_sampler.py__init__BestOfNSampler.__init__/   s     TN)&9;R%STTZ[_`i[jZkl  %"9;;>tE{mKk  mD  lE  F  
"!2,+&(    tokenized_queryskip_special_tokensdevicec                 &   Sn[        U[        R                  5      (       a"  UR                  S:X  a  UR	                  S5      nO[        U[
        5      (       a  [        US   5      nU[        L a&  [        R                  " U5      R	                  S5      nOiU[        R                  L a!  U Vs/ s H  owR                  S5      PM     nnO5U Vs/ s H(  n[        R                  " U5      R                  S5      PM*     nn/ n	U H  nUR                  U R                  S45      nU R                  R                  " UR                  U5      4U R                  5       U R                   S.UD6R#                  5       n
U R$                  R'                  XS9n
[        R                  " U R)                  U
5      5      nUR+                  U R,                  5      R.                   Vs/ s H  oU   PM	     n
nU	R1                  U
5        M     U	$ s  snf s  snf s  snf )a!  
Generate the best of n samples for input queries.

Args:
    tokenized_query (`list[int]` or `torch.Tensor` or `list[torch.Tensor]` or `list[list[int]]`):
        Either a single tokenized query (a single tensor or a list of integers) or a batch of tokenized queries
        (a list of tensors or a list of lists of integers).
    skip_special_tokens (`bool`, *optional*, defaults to `True`):
        Whether to remove the special tokens from the output.
    device (`str` or `torch.device`, *optional*):
        The device on which the model will be loaded.
    **generation_kwargs:
        Additional keyword arguments passed along to the underlying model's `generate` method. This is used to
        override generation config.

Returns:
    `list[list[str]]`: A list of lists of generated texts.
N   r   )r(   )max_new_tokensr   )r%   )r   torchTensorndim	unsqueezelistr   inttensorreshaperepeatr   r   generatetor   r   squeezer   batch_decoder   topkr   indicesappend)r   r$   r%   r&   generation_kwargsquerieselement_typer1   queryresultoutputscoresis                r    r4   BestOfNSampler.generateO   s   2 ou||449M9MQR9R%//2G.. 23Ls",,7AA!D-APQv>>'2QM\]_E5<<.66w?_]EllD$4$4a#89GZZ((

6"#224"&// $	
 gi  ^^000aF\\$"8"8"@AF)/T5F5F)G)O)OP)OAQi)OFPMM&!  ' R] Qs   -H/H	H)r   r   r   r   r   r   r   )   Nr(   N)TN)__name__
__module____qualname____firstlineno____doc__r   r   r   r	   r   r/   strfloatr   r0   r   r   r!   r+   r,   boolr&   r4   __static_attributes__ r#   r    r   r      s   : "8<)%) ,.EEF) $T#YKe$<=	)
 ) ) sm) ) $$45) 
)F %)59	5tCy%,,U\\8JDQUVYQZO[\5 "5 sELL012	5 
d3i5 5r#   r   )typingr   r   r   r   r+   transformersr   r   r	   r
   modelsr   r   r   rN   r#   r    <module>rR      s%    2 1  a a Dm mr#   