o
    TigS                  )   @   s  d dl Z d dlZd dlZd dlZd dlZd dlmZmZmZm	Z	m
Z
mZ d dlmZmZmZmZmZmZ dd Z			dDded	ee	e  d
e
ee	e   dededee fddZ		dEded	ee	e  dee	e  dededee fddZ		dFdedee	e  dededee f
ddZ		dEdedee	e  dededee f
ddZ	dGdddddddd ddddddded	e	e d
e
e	e  d ed!ed"ed#ed$ed%ed&ed'ed(ed)e
e	e	e   d*e
eee	e e	e f  d+ed,edee f"d-d.Z	 	dHd/d dddddd ddddd0dd1ded2ee	e e	e	e  f deded3ed4ed"ed#ed$ed%ed&ed'ed(ed)e
e	e	e   d*e
eee	e e	e f  d5e
e	e  d6ed7eegef dee f&d8d9Z	 	dHd/d dddddd ddddd0dd1ded2ee	e e	e	e  f deded3ed4ed"ed#ed$ed%ed&ed'ed(ed)e
e	e	e   d*e
eee	e e	e f  d5e
e	e  d6ed7eegef dee f&d:d;ZG d<d= d=Zd>d? Z d@dA Z!dBdC Z"dS )I    N)AsyncIterableCallableIterableListOptionalUnion)GenerationResultGenerationStepResult	GeneratorScoringResultTranslationResult
Translatorc                   C   sX   t tdt t tdt t tdt t tdt t tdt t tdt t tdt	 dS )z4Registers additional attributes to compiled modules.translate_iterablescore_iterablegenerate_tokensgenerate_iterableasync_generate_tokensN)
setattrr   translator_translate_iterabletranslator_score_iterabletranslator_generate_tokensr
   generator_generate_iterablegenerator_score_iterablegenerator_generate_tokensgenerator_async_generate_tokens r   r   J/home/ubuntu/.local/lib/python3.10/site-packages/ctranslate2/extensions.pyregister_extensions   s   r       examples
translatorsourcetarget_prefixmax_batch_size
batch_typereturnc                 k   s<    |g}|dur| | t| j|||fi |E dH  dS )a6  Translates an iterable of tokenized examples.

    This method is built on top of :meth:`ctranslate2.Translator.translate_batch`
    to efficiently translate an arbitrarily large stream of data. It enables the
    following optimizations:

    * stream processing (the iterable is not fully materialized in memory)
    * parallel translations (if the translator has multiple workers)
    * asynchronous batch prefetching
    * local sorting by length

    Arguments:
      source: An iterable of tokenized source examples.
      target_prefix: An optional iterable of tokenized target prefixes.
      max_batch_size: The maximum batch size.
      batch_type: Whether :obj:`max_batch_size` is the number of "examples" or "tokens".
      **kwargs: Any translation options accepted by
        :meth:`ctranslate2.Translator.translate_batch`.

    Returns:
      A generator iterator over :class:`ctranslate2.TranslationResult` instances.

    Example:
      This method can be used to efficiently translate text files:

      .. code-block:: python

          # Replace by your own tokenization and detokenization functions.
          tokenize_fn = lambda line: line.strip().split()
          detokenize_fn = lambda tokens: " ".join(tokens)

          with open("input.txt") as input_file:
              source = map(tokenize_fn, input_file)
              results = translator.translate_iterable(source, max_batch_size=64)

              for result in results:
                  tokens = result.hypotheses[0]
                  target = detokenize_fn(tokens)
                  print(target)
    N)append_process_iterabletranslate_batch)r    r!   r"   r#   r$   kwargs	iterablesr   r   r   r      s   0
r   @   targetc                 k   s(    t | j||g||fi |E dH  dS )a}  Scores an iterable of tokenized examples.

    This method is built on top of :meth:`ctranslate2.Translator.score_batch`
    to efficiently score an arbitrarily large stream of data. It enables the
    following optimizations:

    * stream processing (the iterable is not fully materialized in memory)
    * parallel scoring (if the translator has multiple workers)
    * asynchronous batch prefetching
    * local sorting by length

    Arguments:
      source: An iterable of tokenized source examples.
      target: An iterable of tokenized target examples.
      max_batch_size: The maximum batch size.
      batch_type: Whether :obj:`max_batch_size` is the number of "examples" or "tokens".
      **kwargs: Any scoring options accepted by
        :meth:`ctranslate2.Translator.score_batch`.

    Returns:
      A generator iterator over :class:`ctranslate2.ScoringResult` instances.
    Nr'   score_batch)r    r!   r,   r#   r$   r)   r   r   r   r   [   s   r   	generatorstart_tokensc                 k   &    t | j|g||fi |E dH  dS )ac  Generates from an iterable of tokenized prompts.

    This method is built on top of :meth:`ctranslate2.Generator.generate_batch`
    to efficiently run generation on an arbitrarily large stream of data. It enables
    the following optimizations:

    * stream processing (the iterable is not fully materialized in memory)
    * parallel generations (if the generator has multiple workers)
    * asynchronous batch prefetching
    * local sorting by length

    Arguments:
      start_tokens: An iterable of tokenized prompts.
      max_batch_size: The maximum batch size.
      batch_type: Whether :obj:`max_batch_size` is the number of "examples" or "tokens".
      **kwargs: Any generation options accepted by
        :meth:`ctranslate2.Generator.generate_batch`.

    Returns:
      A generator iterator over :class:`ctranslate2.GenerationResult` instances.
    N)r'   generate_batch)r/   r0   r#   r$   r)   r   r   r   r         r   tokensc                 k   r1   )a9  Scores an iterable of tokenized examples.

    This method is built on top of :meth:`ctranslate2.Generator.score_batch`
    to efficiently score an arbitrarily large stream of data. It enables
    the following optimizations:

    * stream processing (the iterable is not fully materialized in memory)
    * parallel scoring (if the generator has multiple workers)
    * asynchronous batch prefetching
    * local sorting by length

    Arguments:
      tokens: An iterable of tokenized examples.
      max_batch_size: The maximum batch size.
      batch_type: Whether :obj:`max_batch_size` is the number of "examples" or "tokens".
      **kwargs: Any score options accepted by
        :meth:`ctranslate2.Generator.score_batch`.

    Returns:
      A generator iterator over :class:`ctranslate2.ScoringResult` instances.
    Nr-   )r/   r4   r#   r$   r)   r   r   r   r      r3   r         Fi   )max_decoding_lengthmin_decoding_lengthsampling_topksampling_toppsampling_temperaturereturn_log_probrepetition_penaltyno_repeat_ngram_sizedisable_unksuppress_sequences	end_tokenmax_input_lengthuse_vmapr7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   c                c   sF    t | j|g|dur|gnd|	|
|||||||||||dE dH  dS )a{  Yields tokens as they are generated by the model.

    Arguments:
      source: Source tokens.
      target_prefix: Optional target prefix tokens.
      max_decoding_length: Maximum prediction length.
      min_decoding_length: Minimum prediction length.
      sampling_topk: Randomly sample predictions from the top K candidates.
      sampling_topp: Keep the most probable tokens whose cumulative probability exceeds this value.
      sampling_temperature: Sampling temperature to generate more random samples.
      return_log_prob: Include the token log probability in the result.
      repetition_penalty: Penalty applied to the score of previously generated tokens
        (set > 1 to penalize).
      no_repeat_ngram_size: Prevent repetitions of ngrams with this size
        (set 0 to disable).
      disable_unk: Disable the generation of the unknown token.
      suppress_sequences: Disable the generation of some sequences of tokens.
      end_token: Stop the decoding on one of these tokens (defaults to the model EOS token).
      max_input_length: Truncate inputs after this many tokens (set 0 to disable).
      use_vmap: Use the vocabulary mapping file saved in this model

    Returns:
      A generator iterator over :class:`ctranslate2.GenerationStepResult` instances.

    Note:
      This generation method is not compatible with beam search which requires a complete decoding.
    N)r=   r>   r?   r@   rA   r7   r8   r9   r:   r;   return_scoresrB   rC   )_generate_tokensr(   )r    r!   r"   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   r   r   r   r      s&   .r   i   T)
max_length
min_lengthr9   r:   r;   r<   r=   r>   r?   r@   rA   static_promptcache_static_promptcallbackpromptrF   rG   rH   rI   rJ   c                c   s    t |dkrt|d tr|g}t| j|fi d|d|d|
d|d|d|d|d	|d
|d|d|d|d|	d|d|ddd|E dH  dS )a  Yields tokens as they are generated by the model.

    Arguments:
      prompt: Batch of start tokens. If the decoder starts from a
        special start token like <s>, this token should be added to this input.
      max_batch_size: The maximum batch size.
      batch_type: Whether :obj:`max_batch_size` is the number of "examples" or "tokens".
      max_length: Maximum generation length.
      min_length: Minimum generation length.
      sampling_topk: Randomly sample predictions from the top K candidates.
      sampling_topp: Keep the most probable tokens whose cumulative probability exceeds this value.
      sampling_temperature: Sampling temperature to generate more random samples.
      return_log_prob: Include the token log probability in the result.
      repetition_penalty: Penalty applied to the score of previously generated tokens
        (set > 1 to penalize).
      no_repeat_ngram_size: Prevent repetitions of ngrams with this size
        (set 0 to disable).
      disable_unk: Disable the generation of the unknown token.
      suppress_sequences: Disable the generation of some sequences of tokens.
      end_token: Stop the decoding on one these tokens (defaults to the model EOS token).
      static_prompt: If the model expects a static prompt (a.k.a. system prompt)
        it can be set here to simplify the inputs and optionally cache the model
        state for this prompt to accelerate future generations.
      cache_static_prompt: Cache the model state after the static prompt and
        reuse it for future generations using the same static prompt.
      callback: Optional function that is called for each generated token when
        obj:`beam_size` is 1. If the callback function returns ``True``, the
        decoding will stop for this batch index.

    Returns:
      A generator iterator over :class:`ctranslate2.GenerationStepResult` instances.

    Note:
      This generation method is not compatible with beam search which requires a complete decoding.
    r   r#   r$   r=   r>   r?   r@   rA   rF   rG   r9   r:   r;   rD   rH   rI   include_prompt_in_resultFrJ   N)len
isinstancestrrE   r2   )r/   rK   r#   r$   rF   rG   r9   r:   r;   r<   r=   r>   r?   r@   rA   rH   rI   rJ   r   r   r   r     sR   8	
r   c                C  s   t |dkrt|d tr|g}t| j|fi d|d|d|
d|d|d|d|d	|d
|d|d|d|d|	d|d|ddd|2 z	3 dH W }|V  qL6 dS )a  Yields tokens asynchronously as they are generated by the model.

    Arguments:
      prompt: Batch of start tokens. If the decoder starts from a
        special start token like <s>, this token should be added to this input.
      max_batch_size: The maximum batch size.
      batch_type: Whether :obj:`max_batch_size` is the number of "examples" or "tokens".
      max_length: Maximum generation length.
      min_length: Minimum generation length.
      sampling_topk: Randomly sample predictions from the top K candidates.
      sampling_topp: Keep the most probable tokens whose cumulative probability exceeds this value.
      sampling_temperature: Sampling temperature to generate more random samples.
      return_log_prob: Include the token log probability in the result.
      repetition_penalty: Penalty applied to the score of previously generated tokens
        (set > 1 to penalize).
      no_repeat_ngram_size: Prevent repetitions of ngrams with this size
        (set 0 to disable).
      disable_unk: Disable the generation of the unknown token.
      suppress_sequences: Disable the generation of some sequences of tokens.
      end_token: Stop the decoding on one of these tokens (defaults to the model EOS token).
      static_prompt: If the model expects a static prompt (a.k.a. system prompt)
        it can be set here to simplify the inputs and optionally cache the model
        state for this prompt to accelerate future generations.
      cache_static_prompt: Cache the model state after the static prompt and
        reuse it for future generations using the same static prompt.
      callback: Optional function that is called for each generated token when
        obj:`beam_size` is 1. If the callback function returns ``True``, the
        decoding will stop for this batch index.

    Returns:
      An async generator iterator over :class:`ctranslate2.GenerationStepResult` instances.

    Note:
      This generation method is not compatible with beam search which requires a complete decoding.
    r   r#   r$   r=   r>   r?   r@   rA   rF   rG   r9   r:   r;   rD   rH   rI   rL   FrJ   N)rM   rN   rO   AsyncGeneratorr2   )r/   rK   r#   r$   rF   rG   r9   r:   r;   r<   r=   r>   r?   r@   rA   rH   rI   rJ   step_resultr   r   r   r   _  sV   8	
r   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
rP   c                 O   s0   t  | _t | _d | _|| _|| _|| _	d S N)
asyncioQueuequeue	threadingEventshutdown_eventiterator_taskprocess_funcargsr)   )selfrZ   r[   r)   r   r   r   __init__  s   


zAsyncGenerator.__init__c                    sj   t | jg| jR i | jD ]}| j|I d H  tdI d H  | j	 r) nq| jd I d H  d S )Ng-C6?)
rE   rZ   r[   r)   rU   putrS   sleeprX   is_set)r\   rQ   r   r   r   producer  s   

zAsyncGenerator.producerc                 C   s   t |  | _| S rR   )rS   create_taskra   rY   )r\   r   r   r   	__aiter__  s   zAsyncGenerator.__aiter__c                    s\   | j  rtz| j I d H }|d u r| j   t|W S  tjy-   | j   tw rR   )rX   r`   StopAsyncIterationrU   getsetrS   CancelledError)r\   itemr   r   r   	__anext__  s   


zAsyncGenerator.__anext__N)__name__
__module____qualname__r]   ra   rc   ri   r   r   r   r   rP     s
    rP   c                 /   s    t  t |dd d u rdd fdd}|dd|d | |i |  fd	d
}tj|dd}|  	  }|d u rKnt|t	rR|z|V  W n t
yd     Y nw qB|  d S )NrJ   c                 S   s   dS )NFr   )rQ   r   r   r   <lambda>  s    z"_generate_tokens.<locals>.<lambda>c                    s   | } |    p|S rR   )r^   r`   )rQ   user_callback_result)generator_closedstep_resultsuser_callbackr   r   	_callback  s   
z#_generate_tokens.<locals>._callbackTr6   )asynchronous	beam_sizerJ   c               
      sT   z D ]} |    qW n ty" } z| W Y d }~nd }~ww d  d S rR   )result	Exceptionr^   )ru   e)async_resultsrp   r   r   _catch_exception  s   
z*_generate_tokens.<locals>._catch_exception)r,   daemon)rU   rT   rV   rW   re   updateThreadstartrN   rv   GeneratorExitrf   join)rZ   r[   r)   rr   ry   threadrQ   r   )rx   ro   rp   rq   r   rE     s>   

rE   c           	      k   s    |dk r	t dt|dkr|d }ntj| }|||dd |dkr*|d n|}t }t|||D ]#}|| |i | |rY|d 	 rY|
  V  |rY|d 	 sJq6|rg|
  V  |s\d S d S )Nr6   zmax_batch_size must be >= 1r   T)r#   r$   rs      )
ValueErrorrM   	itertoolszip_longestr{   collectionsdeque_batch_iteratorextenddonepopleftru   )	rZ   r*   r#   r$   r)   iterableread_batch_sizerU   streamsr   r   r   r'     s.   

r'   c                 c   s   d }d}| D ]u}t |ts|f}|dkr%|r$t|d |kr$|V  d }n-|dkrLt|t|d }|rKt|d d | |krK|V  d }t|d }ntd| |d u r_tdd |D }t||D ]\}}|d u rvt|dkrvtd|| qdq|d ur|V  d S d S )	Nr   r   r4   r6   zInvalid batch type %sc                 s   s    | ]}g V  qd S rR   r   ).0_r   r   r   	<genexpr>F  s    z"_batch_iterator.<locals>.<genexpr>z+Input iterables do not have the same length)rN   tuplerM   maxr   zipr&   )r   
batch_sizer$   r   rF   examplebatchelementr   r   r   r   -  s:   

r   )Nr   r   )r+   r   )r   r   rR   )r   r   )#rS   r   r   rU   rV   typingr   r   r   r   r   r   ctranslate2._extr   r	   r
   r   r   r   r   rO   intr   r   r   r   floatboolr   r   r   rP   rE   r'   r   r   r   r   r   <module>   s     


A


*

(

(
	

E	


T	


R(6