
    gY                        d dl Z d dlZd dlmZ d dlmZ d dlmZ ddl	m
Z
 ddlmZ  ee          ZdZ G d d          Z G d	 d
          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d d e          Z G d! d"e          ZdS )#    N)sparse   )add_start_docstrings)
get_loggerad  
    Args:
        input_ids (`jnp.ndarray` of shape `(batch_size, sequence_length)`):
            Indices of input sequence tokens in the vocabulary.

            Indices can be obtained using [`PreTrainedTokenizer`]. See [`PreTrainedTokenizer.encode`] and
            [`PreTrainedTokenizer.__call__`] for details.

            [What are input IDs?](../glossary#input-ids)
        scores (`jnp.ndarray` of shape `(batch_size, config.vocab_size)`):
            Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam
            search or log softmax for each vocabulary token when using beam search
        kwargs (`Dict[str, Any]`, *optional*):
            Additional logits processor specific kwargs.

    Return:
        `jnp.ndarray` of shape `(batch_size, config.vocab_size)`: The processed prediction scores.

c                   f    e Zd ZdZ ee          dej        dej        dej        fd            ZdS )FlaxLogitsProcessorzSAbstract base class for all logit processors that can be applied during generation.	input_idsscoresreturnc                 0    t          | j         d          )z"Flax method for processing logits.H is an abstract class. Only classes inheriting this class can be called.NotImplementedError	__class__selfr	   r
   s      g/var/www/html/ai-engine/env/lib/python3.11/site-packages/transformers/generation/flax_logits_process.py__call__zFlaxLogitsProcessor.__call__6   $     "~ggg
 
 	
    N	__name__
__module____qualname____doc__r   !LOGITS_PROCESSOR_INPUTS_DOCSTRINGjnpndarrayr    r   r   r   r   3   s`        ]];<<
#+ 
s{ 
s{ 
 
 
 =<
 
 
r   r   c                   f    e Zd ZdZ ee          dej        dej        dej        fd            ZdS )FlaxLogitsWarperzjAbstract base class for all logit warpers that can be applied during generation with multinomial sampling.r	   r
   r   c                 0    t          | j         d          )zFlax method for warping logits.r   r   r   s      r   r   zFlaxLogitsWarper.__call__A   r   r   Nr   r   r   r   r!   r!   >   s`        tt;<<
#+ 
s{ 
s{ 
 
 
 =<
 
 
r   r!   c            	       j    e Zd ZdZ ee          dej        dej        dedej        fd            Z	dS )FlaxLogitsProcessorLista.  
    This class can be used to create a list of [`FlaxLogitsProcessor`] or [`FlaxLogitsWarper`] to subsequently process
    a `scores` input tensor. This class inherits from list and adds a specific *__call__* method to apply each
    [`FlaxLogitsProcessor`] or [`FlaxLogitsWarper`] to the inputs.
    r	   r
   cur_lenr   c                    | D ]}t          j        |j                  j        }t	          |          dk    rt          fdt          |                                          dd          D                       s:t          dt          |                                           d|j	         d           ||||fi } ||||          }|S )N   c              3       K   | ]}|v V  	d S Nr   ).0argkwargss     r   	<genexpr>z3FlaxLogitsProcessorList.__call__.<locals>.<genexpr>U   s'      SSS3&=SSSSSSr   r   z,Make sure that all the required parameters: z for z$ are passed to the logits processor.)
inspect	signaturer   
parameterslenalllistkeys
ValueErrorr   )r   r	   r
   r%   r,   	processorfunction_argss       `  r   r   z FlaxLogitsProcessorList.__call__P   s    
	? 
	?I#-i.@AALM=!!A%%SSSSD9K9K9M9M4N4Nqrr4RSSSSS $UtML^L^L`L`GaGa U U$.U U U   #9fgHHHH"9fg>>r   N)
r   r   r   r   r   r   r   r   intr   r   r   r   r$   r$   I   so          ;<<#+ s{ S _b_j    =<  r   r$   c                   T    e Zd ZdZdefdZdej        dej        dedej        fdZ	d	S )
FlaxTemperatureLogitsWarperz
    [`FlaxLogitsWarper`] for temperature (exponential scaling output probability distribution).

    Args:
        temperature (`float`):
            The value used to module the logits distribution.
    temperaturec                 n    t          |t                    r|dk    st          d|           || _        d S )Nr   z:`temperature` has to be a strictly positive float, but is )
isinstancefloatr5   r;   )r   r;   s     r   __init__z$FlaxTemperatureLogitsWarper.__init__i   sC    +u-- 	ikAoogZegghhh&r   r	   r
   r%   r   c                     || j         z  }|S r)   )r;   r   r	   r
   r%   s       r   r   z$FlaxTemperatureLogitsWarper.__call__o   s    $**r   N)
r   r   r   r   r>   r?   r   r   r8   r   r   r   r   r:   r:   `   sq         'E ' ' ' '#+ s{ S UXU`      r   r:   c                   v    e Zd ZdZ ed           dfdededefdZdej        d	ej        d
edej        fdZ	dS )FlaxTopPLogitsWarpera=  
    [`FlaxLogitsWarper`] that performs top-p, i.e. restricting to top tokens summing to prob_cut_off <= prob_cut_off.

    Args:
        top_p (`float`):
            If set to < 1, only the smallest set of most probable tokens with probabilities that add up to `top_p` or
            higher are kept for generation.
        filter_value (`float`, *optional*, defaults to -inf):
            All filtered values will be set to this float value.
        min_tokens_to_keep (`int`, *optional*, defaults to 1):
            Minimum number of tokens that cannot be filtered.
    Inf   top_pfilter_valuemin_tokens_to_keepc                     t          |t                    r|dk     s|dk    rt          d|           t          |t                    r|dk     rt          d|           || _        || _        || _        d S )Nr   g      ?z.`top_p` has to be a float > 0 and < 1, but is rE   z:`min_tokens_to_keep` has to be a positive integer, but is )r=   r>   r5   r8   rF   rG   rH   )r   rF   rG   rH   s       r   r?   zFlaxTopPLogitsWarper.__init__   s    %'' 	WEAIIUeUUVVV,c22 	p7IA7M7MnZlnnooo
("4r   r	   r
   r%   r   c                 F   t          j        ||j        d                   \  }}t          j        || j                  }t          j                            |d          	                    d          }|| j
        k     }t          j        |d          }||j        d d df                             d          z  }|j        d d d | j        f                             d          }t          j        |||          }	t          j                             ||	          d         }
|
S )NaxisrE   r   T)laxtop_kshaper   	full_likerG   jaxnnsoftmaxcumsumrF   rollatsetrH   wheresort_key_val)r   r	   r
   r%   topk_scorestopk_indicesmask_scorescumulative_probs
score_masktopk_next_scoresnext_scoress              r   r   zFlaxTopPLogitsWarper.__call__   s   $'Iffl26F$G$G!\mFD,=>>6>>+B>??FFBFOO%
2
 Xj!,,
jmAAAqD)--d333
  ]111&?(?&?#?@DDTJJ
9ZkJJg**<9IJJ2Nr   N
r   r   r   r   r>   r8   r?   r   r   r   r   r   r   rC   rC   t   s          =BE%LL=de 5 5e 55 5^a 5 5 5 5#+ s{ S UXU`      r   rC   c                   v    e Zd ZdZ ed           dfdededefdZdej        d	ej        d
edej        fdZ	dS )FlaxTopKLogitsWarpera  
    [`FlaxLogitsWarper`] that performs top-k, i.e. restricting to the k highest probability elements.

    Args:
        top_k (`int`):
            The number of highest probability vocabulary tokens to keep for top-k-filtering.
        filter_value (`float`, *optional*, defaults to -inf):
            All filtered values will be set to this float value.
        min_tokens_to_keep (`int`, *optional*, defaults to 1):
            Minimum number of tokens that cannot be filtered.
    rD   rE   rO   rG   rH   c                     t          |t                    r|dk    rt          d|           t          ||          | _        || _        d S )Nr   z6`top_k` has to be a strictly positive integer, but is )r=   r8   r5   maxrO   rG   )r   rO   rG   rH   s       r   r?   zFlaxTopKLogitsWarper.__init__   sU    %%% 	_!]V[]]^^^ 233
(r   r	   r
   r%   r   c                    |j         \  }}t          j        ||z  | j                  }t	          | j        |j         d                   }t          j        ||          \  }}	t          j        t          j        |          |z  d d d f         ||f          	                                }
|	                                }|		                                |
z   }|j
        |                             |          }|                    ||          }|S )NrK   )rP   r   fullrG   minrO   rN   broadcast_toarangeflattenrW   rX   reshape)r   r	   r
   r%   
batch_size
vocab_sizenext_scores_flattopkr[   r\   shifttopk_scores_flattopk_indices_flatra   s                 r   r   zFlaxTopKLogitsWarper.__call__   s    !'
J8J$;T=NOO4:v|B/00$'Ifd$;$;!\ #*Z"8"8:"Eqqq$w!OR\^bQcddllnn&..00(0022U:+./@AEEFVWW&..z:FFr   Nrb   r   r   r   rd   rd      s        
 
 ;@%,,bc ) )c ) )\_ ) ) ) )#+ s{ S UXU`      r   rd   c                   T    e Zd ZdZdefdZdej        dej        dedej        fdZd	S )
!FlaxForcedBOSTokenLogitsProcessorz
    [`FlaxLogitsProcessor`] that enforces the specified token as the first generated token.

    Args:
        bos_token_id (`int`):
            The id of the token to force as the first generated token.
    bos_token_idc                     || _         d S r)   )rw   )r   rw   s     r   r?   z*FlaxForcedBOSTokenLogitsProcessor.__init__   s    (r   r	   r
   r%   r   c                    t          j        |j        t          d                     }dt          j        |dz
            z
  }t          j        ||j        d d | j        f                             d          |          }|S NinfrE   r   )	r   rh   rP   r>   bool_rY   rW   rw   rX   r   r	   r
   r%   
new_scoresapply_penaltys         r   r   z*FlaxForcedBOSTokenLogitsProcessor.__call__   sp    XflU5\\M::
CIgk222=*-4;L8L*M*Q*QRS*T*TV\]]r   N	r   r   r   r   r8   r?   r   r   r   r   r   r   rv   rv      sq         )S ) ) ) )#+ s{ S UXU`      r   rv   c                   X    e Zd ZdZdedefdZdej        dej        dedej        fd	Zd
S )!FlaxForcedEOSTokenLogitsProcessorae  
    [`FlaxLogitsProcessor`] that enforces the specified token as the last generated token when `max_length` is reached.

    Args:
        max_length (`int`):
            The maximum length of the sequence to be generated.
        eos_token_id (`int`):
            The id of the token to force as the last generated token when `max_length` is reached.
    
max_lengtheos_token_idc                 "    || _         || _        d S r)   )r   r   )r   r   r   s      r   r?   z*FlaxForcedEOSTokenLogitsProcessor.__init__   s    $(r   r	   r
   r%   r   c                    t          j        |j        t          d                     }dt          j        || j        z
  dz             z
  }t          j        ||j        d d | j        f         	                    d          |          }|S rz   )
r   rh   rP   r>   r|   r   rY   rW   r   rX   r}   s         r   r   z*FlaxForcedEOSTokenLogitsProcessor.__call__   sx    XflU5\\M::
CIg&?!&CDDD=*-4;L8L*M*Q*QRS*T*TV\]]r   Nr   r   r   r   r   r      sx         )3 )c ) ) ) )#+ s{ S UXU`      r   r   c                   X    e Zd ZdZdedefdZdej        dej        dedej        fd	Zd
S )FlaxMinLengthLogitsProcessora3  
    [`FlaxLogitsProcessor`] enforcing a min-length by setting EOS probability to 0.

    Args:
        min_length (`int`):
            The minimum length below which the score of `eos_token_id` is set to `-float("Inf")`.
        eos_token_id (`int`):
            The id of the *end-of-sequence* token.
    
min_lengthr   c                     t          |t                    r|dk     rt          d|           t          |t                    r|dk     rt          d|           || _        || _        d S )Nr   z2`min_length` has to be a positive integer, but is z4`eos_token_id` has to be a positive integer, but is )r=   r8   r5   r   r   )r   r   r   s      r   r?   z%FlaxMinLengthLogitsProcessor.__init__   s    *c** 	`j1nn^R\^^___,,, 	dq0@0@bT`bbccc$(r   r	   r
   r%   r   c                     dt          j        || j        z
  dd          z
  }t          j        ||j        d d | j        f                             t          d                     |          }|S )NrE   r   r{   )r   clipr   rY   rW   r   rX   r>   r   r	   r
   r%   r   s        r   r   z%FlaxMinLengthLogitsProcessor.__call__  sd    CHWt%>1EEE=&)AAAt7H4H*I*M*MuUZ||m*\*\^deer   Nr   r   r   r   r   r      sx         )3 )c ) ) ) )#+ s{ S UXU`      r   r   c                   $    e Zd ZdZd ZdefdZdS )(FlaxSuppressTokensAtBeginLogitsProcessora  
    [`FlaxLogitsProcessor`] supressing a list of tokens as soon as the `generate` function starts generating using
    `begin_index` tokens. This should ensure that the tokens defined by `begin_suppress_tokens` are not sampled at the
    begining of the generation.

    Args:
        begin_suppress_tokens (`List[int]`):
            Tokens to not sample.
        begin_index (`int`):
            Index where the tokens are suppressed.
    c                 <    t          |          | _        || _        d S r)   )r3   begin_suppress_tokensbegin_index)r   r   r   s      r   r?   z1FlaxSuppressTokensAtBeginLogitsProcessor.__init__  s!    %)*?%@%@"&r   r%   c                     dt          j        || j        z
            z
  }t          j        ||j        d d | j        f                             t          d                     |          }|S )NrE   r{   )r   r|   r   rY   rW   r   rX   r>   r   s        r   r   z1FlaxSuppressTokensAtBeginLogitsProcessor.__call__!  se    CIg0@&@AAA=&)AAAt7Q4Q*R*V*VX]^cXdXdWd*e*egmnnr   N)r   r   r   r   r?   r8   r   r   r   r   r   r     sH        
 
' ' '3      r   r   c                   T    e Zd ZdZdefdZdej        dej        dedej        fdZ	d	S )
!FlaxSuppressTokensLogitsProcessorz
    [`FlaxLogitsProcessor`] suppressing a list of tokens at each decoding step. The processor will set their log probs
    to be `-inf` so they are not sampled.

    Args:
        suppress_tokens (`list`):
            Tokens to not sample.
    suppress_tokensc                 .    t          |          | _        d S r)   )r3   r   )r   r   s     r   r?   z*FlaxSuppressTokensLogitsProcessor.__init__3  s    #O44r   r	   r
   r%   r   c                 p    |j         d| j        f                             t          d                     }|S )N.r{   )rW   r   rX   r>   rA   s       r   r   z*FlaxSuppressTokensLogitsProcessor.__call__6  s1    3 445995<<-HHr   N)
r   r   r   r   r3   r?   r   r   r8   r   r   r   r   r   r   )  sq         5 5 5 5 5#+ s{ S UXU`      r   r   c                   N    e Zd ZdZd Zdej        dej        dedej        fdZdS )	FlaxForceTokensLogitsProcessora  
    [`FlaxLogitsProcessor`] that takes a list of pairs of integers which indicates a mapping from generation indices to
    token indices that will be forced before sampling. The processor will set their log probs to 0 and all other tokens
    to `-inf` so that they are sampled at their corresponding index.

    Args:
        force_token_map (`list`):
            Map giving token ids and indices where they will be forced to be sampled.
    c                 X   t          |          }t          j        t          |                                          dz   t          j                  dz  }|                                D ]'\  }}| |j        |                             |          }(t          j        |          | _	        d S )NrE   dtyperK   )
dictr   onesrf   r4   int32itemsrW   rX   force_token_array)r   force_token_mapr   indextokens        r   r?   z'FlaxForceTokensLogitsProcessor.__init__G  s    //  Hc/*>*>*@*@&A&AA&EciXXX[]]+1133 	K 	KLE5 $5$8$?$C$CE$J$J!!$+<!=!=r   r	   r
   r%   r   c                       fdt          j         j        j        d         k    fd fd          S )Nc                     j         d         }j        |          }t          j        j                  t          d           z  }t          j        |dfj                  }t          j        ||d|f          }|S )Nr   r   r{   rE   )	rP   r   r   	ones_liker   r>   zerosrN   dynamic_update_slice)generation_idxrn   current_tokenr~   updatesr
   r   s        r   _force_tokenz=FlaxForceTokensLogitsProcessor.__call__.<locals>._force_tokenS  sw    aJ 2>BMvV\BBBeEll]RJiQv|DDDG1*g=GYZZJr   r   c                       S r)   r   r
   s   r   <lambda>z9FlaxForceTokensLogitsProcessor.__call__.<locals>.<lambda>_  s    F r   c                  \    t          j        j                 dk     fdfd          S )Nr   c                                  S r)   r   )r   r%   s   r   r   zKFlaxForceTokensLogitsProcessor.__call__.<locals>.<lambda>.<locals>.<lambda>d  s    W-- r   c                       S r)   r   r   s   r   r   zKFlaxForceTokensLogitsProcessor.__call__.<locals>.<lambda>.<locals>.<lambda>f  s     r   )rN   condr   )r   r%   r
   r   s   r   r   z9FlaxForceTokensLogitsProcessor.__call__.<locals>.<lambda>a  s;    CH&w/14-----  r   )rN   r   r   rP   )r   r	   r
   r%   r   s   ` ``@r   r   z'FlaxForceTokensLogitsProcessor.__call__R  s}    	 	 	 	 	 	 t-3A66NNN      
 
 r   N)	r   r   r   r   r?   r   r   r8   r   r   r   r   r   r   <  se         	> 	> 	>#+ s{ S UXU`      r   r   c                       e Zd ZdZd Zd ZdS )#FlaxWhisperTimeStampLogitsProcessora{  
    Whisper specific Processor. This processor can be used to force a list of tokens. The processor will set their log
    probs to `inf` so that they are sampled at their corresponding index.

    Args:
        generate_config (`GenerateConfig`):
            The generate config used to generate the output. The following parameters are required:
                eos_token_id (`int`, *optional*, defaults to 50257):
                    The id of the *end-of-sequence* token.
                no_timestamps_token_id (`int`, *optional*, defaults to 50363):
                    The id of the `"<|notimestamps|>"` token.
                max_initial_timestamp_index (`int`, *optional*, defaults to 1):
                    Used to set the maximum value of the initial timestamp. This is used to prevent the model from
                    predicting timestamps that are too far in the future.
    c                    |j         | _         |j        | _        |j        dz   | _        |dz   | _        |j        r| xj        dz  c_        t          |d          r|j        | _        n|j        | _        | j        |j        | _        d S d S )NrE   r   max_initial_timestamp_index)r   no_timestamps_token_idtimestamp_beginr   is_multilingualhasattrr   ro   )r   generate_configmodel_configdecoder_input_lengths       r   r?   z,FlaxWhisperTimeStampLogitsProcessor.__init__}  s    +8&5&L#.EI/!3* 	"!?$ABB 	G/>/ZD,,/;/FD,+3/;/FD,,, 43r   c                 t    |j         d d  j        f                             t          d                     } fd} t	          j        |          ||          }t          j         j        k    dd          }t          j         j	        d u|d          } j
         j	        z   }t          j        ||j         d d |dz   d f                             t          d                     |          }t          j                            |d          } fd} t	          j        |          ||          }|S )	Nr{   c                 T   t          j        j        z
  dk    dd          }t          j        | dz
           j        k    |d          }t          j        j        z
  dk     dd          }t          j        | dz
           j        k    d|          }t          j        |t          j        |dk    |j        j        d                              t          d                     |j        d j                                     t          d                               |          S )NrE   TFr   r   r{   )r   rY   r   r   rW   rX   r>   r   )input_ids_kscores_klast_was_timestamppenultimate_was_timestampr%   r   s       r   handle_pairszBFlaxWhisperTimeStampLogitsProcessor.__call__.<locals>.handle_pairs  s*   !$Gd6F,F1+LdTY!Z!Z!$GaK(D,@@+" " ),	7T=M3MQR2RTXZ_(`(`%(+	GaK(D,@@)) )% 9"	-1K 4 6 67;;U5\\MJJK 3$"3 3488%,,GG 
   r   TFrE   rK   rL   c                 <   t           j                            | j        d          d          }t	          j        | d j                           }t	          j        ||k    |j        d j                                     t          d                     |          S )NrK   rL   r{   )
rR   rS   	logsumexpr   r   rf   rY   rW   rX   r>   )
logprobs_kr   timestamp_logprobmax_text_token_logprobr   s       r   handle_cumulative_probszMFlaxWhisperTimeStampLogitsProcessor.__call__.<locals>.handle_cumulative_probs  s     # 0 0D<P<R<R1SZ\ 0 ] ]%(WZ8N$:N8N-O%P%P"9!$::2d22377uFF  r   )rW   r   rX   r>   rR   vmapr   rY   r   r   r   rS   log_softmax)	r   r	   r
   r%   r   apply_max_initial_timestamplast_allowedlogprobsr   s	   `  `     r   r   z,FlaxWhisperTimeStampLogitsProcessor.__call__  sb   111d99:>>e}MM	 	 	 	 	 	2 (,''	6::&)i4;K0KTSX&Y&Y#&)i,D80'
 '
# +d.NN'Iaaa)+++,00%,,??
 
 6%%f2%66	 	 	 	 	 31228VDDr   N)r   r   r   r   r?   r   r   r   r   r   r   l  s?          G G G"< < < < <r   r   c                       e Zd ZdZdefdZdej        dedefdZdej        d	ej        fd
Z	dej        dej        ded	ej        fdZ
dS ) FlaxNoRepeatNGramLogitsProcessora9  
    [`FlaxLogitsProcessor`] that enforces no repetition of n-grams. See
    [Fairseq](https://github.com/pytorch/fairseq/blob/a07cb6f40480928c9e0548b737aadd36ee66ac76/fairseq/sequence_generator.py#L345).

    Args:
        ngram_size (`int`):
            All ngrams of size `ngram_size` can only occur once.
    
ngram_sizec                 n    t          |t                    r|dk    rt          d|           || _        d S )Nr   z;`ngram_size` has to be a strictly positive integer, but is )r=   r8   r5   r   )r   r   s     r   r?   z)FlaxNoRepeatNGramLogitsProcessor.__init__  s@    *c** 	ijAoog[egghhh$r   r	   ro   r%   c           	          j         \  }| j        dz
  z
  }| j        dz
  z
  } fd}|z   j        dz   f}t          j                            d|z  |t          j        |j                            }	t          j        |z            |z  k     	                    d          }
t          j        |
|	ff|f j        z  z             S )a  
        get a matrix of size (batch_size,) + (vocab_size,)*n (for n-grams) that
        represent the n-grams that occurred previously.
        The BCOO representation allow to store only the few non-zero entries, instead of the full (huge) matrix
        rE   c           	          | z  | z  |j         |                              t          j        gfdt	          j                  D             z                       S )Nc                 N    g | ]!}t          j                  |z   f         "S r   )r   array)r*   jbr	   poss     r   
<listcomp>zZFlaxNoRepeatNGramLogitsProcessor.get_previous_ngrams.<locals>.body_fun.<locals>.<listcomp>  s0    XXXAsy++AsQwJ7XXXr   )rW   rX   r   r   ranger   )ivalr   r   rn   r	   r   s     @@r   body_funzFFlaxNoRepeatNGramLogitsProcessor.get_previous_ngrams.<locals>.body_fun  sz    JAz/C6!9==	 YXXXXXtAWAWXXXY   r   r   r   float32)rP   )rP   r   rR   rN   	fori_loopr   r   r   rk   astyper   BCOO)r   r	   ro   r%   seq_len
seq_ngrams
cur_ngramsr   rP   all_update_indicesdatarn   s   ``         @r   get_previous_ngramsz4FlaxNoRepeatNGramLogitsProcessor.get_previous_ngrams  s    (o
G! 34
! 34

	 
	 
	 
	 
	 
	 
	 j($/A*=> W..zJ&#)E2Y2Y2Y
 

 
:
233j:6MMUUV_``{D"45j]j]]a]lMl=lmmmmr   latest_tokensr   c                     t           j        t          j        d                         }t          j         |||                    S )zt
        Determines which tokens must be banned given latest tokens and the previously seen
        ngrams.
        c                 ,    |t          |                    S r)   )tuple)r   previous_ngramss     r   inner_fnzIFlaxNoRepeatNGramLogitsProcessor.get_banned_tokens_mask.<locals>.inner_fn  s     #5#7#788r   )r   sparsifyrR   r   bcoo_todense)r   r   r   r   s       r   get_banned_tokens_maskz7FlaxNoRepeatNGramLogitsProcessor.get_banned_tokens_mask  sJ     
		9 	9 
 
	9 "88M?#K#KLLLr   r
   c                 ~      fd}t           j                             j        dz
  k    |fd          }|S )Nc            
         j         \  } }                    |          }t          j        j         d         j        dz
  fj                  }t          j                            |t          j        	                    dj        dz
  z
  fj         d         j        dz
  f          d          }
                    ||                              d          }t          j        |t          d                     S )Nr   rE   r   )r   r   boolr{   )rP   r   r   r   r   r   rR   rN   r   dynamic_slicer   r   rY   r>   )	_ro   r   r   banned_tokens_indices_maskr%   r	   r
   r   s	        r   true_fnz:FlaxNoRepeatNGramLogitsProcessor.__call__.<locals>.true_fn  s    "LMAz"66y*gVVO  Iyq'94?Q;N&OW`WfgggMG88%%7do.A#BCioVWFX[_[jmn[nEp   M *.)D)D]Tc)d)d)k)klr)s)s&97%,,OOOr   rE   c                       S r)   r   r   s   r   r   z;FlaxNoRepeatNGramLogitsProcessor.__call__.<locals>.<lambda>  s    QW r   )rR   rN   r   r   )r   r	   r
   r%   r   outputs   ````  r   r   z)FlaxNoRepeatNGramLogitsProcessor.__call__  sf    	P 	P 	P 	P 	P 	P 	P 	P& w$/A*==XXr   N)r   r   r   r   r8   r?   r   r   r   r   r   r   r   r   r   r     s         %3 % % % %
 nS[  nc  nTW  n  n  n  nDMCK MUXU` M M M M#+ s{ S UXU`      r   r   )r.   rR   jax.laxrN   	jax.numpynumpyr   jax.experimentalr   utilsr   utils.loggingr   r   loggerr   r   r!   r3   r$   r:   rC   rd   rv   r   r   r   r   r   r   r   r   r   r   <module>r     s     



             # # # # # # ( ( ( ( ( ( & & & & & & 
H		% !*
 
 
 
 
 
 
 

 
 
 
 
 
 
 
    d   .    "2   () ) ) ) )+ ) ) )X         +      F    (;   ,    (;   2    #6   <    /B   2    (;   &- - - - -%8 - - -`^ ^ ^ ^ ^*= ^ ^ ^BS S S S S': S S S S Sr   