
    sgY                        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y)#    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                   v    e Zd ZdZ ee      dej                  dej                  dej                  fd       Zy)FlaxLogitsProcessorzSAbstract base class for all logit processors that can be applied during generation.	input_idsscoresreturnc                 2    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      ^/var/www/html/venv/lib/python3.12/site-packages/transformers/generation/flax_logits_process.py__call__zFlaxLogitsProcessor.__call__6   !     "~~fg
 	
    N	__name__
__module____qualname____doc__r   !LOGITS_PROCESSOR_INPUTS_DOCSTRINGjnpndarrayr    r   r   r   r   3   s>    ];<
#++ 
s{{ 
s{{ 
 =
r   r   c                   v    e Zd ZdZ ee      dej                  dej                  dej                  fd       Zy)FlaxLogitsWarperzjAbstract base class for all logit warpers that can be applied during generation with multinomial sampling.r	   r
   r   c                 2    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;<
#++ 
s{{ 
s{{ 
 =
r   r!   c            	       z    e Zd ZdZ ee      dej                  dej                  dedej                  fd       Z	y)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                 r   | D ]  }t        j                  |j                        j                  }t	        |      dkD  rmt        fdt        |j                               dd  D              s3t        dt        |j                                d|j                   d       ||||fi } ||||      } |S )N   c              3   &   K   | ]  }|v  
 y wNr   ).0argkwargss     r   	<genexpr>z3FlaxLogitsProcessorList.__call__.<locals>.<genexpr>U   s     SS3&=Ss   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LLM=!A%SD9K9K9M4Nqr4RSS$FtML^L^L`GaFbbg$..//SU  #9fgHH"9fg>
	? r   N)
r   r   r   r   r   r   r   r   intr   r   r   r   r$   r$   I   sL     ;<#++ s{{ S _b_j_j  =r   r$   c                   p    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	y	)
FlaxTemperatureLogitsWarperz
    [`FlaxLogitsWarper`] for temperature (exponential scaling output probability distribution).

    Args:
        temperature (`float`):
            The value used to module the logits distribution.
    temperaturec                 X    t        |t              r|dkD  st        d|       || _        y )Nr   z:`temperature` has to be a strictly positive float, but is )
isinstancefloatr5   r;   )r   r;   s     r   __init__z$FlaxTemperatureLogitsWarper.__init__i   s/    +u-kAoYZeYfg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:   `   sC    'E '#++ s{{ S UXU`U` r   r:   c                       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	y)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kD  rt        d|       t        |t              r|dk  rt        d|       || _        || _        || _        y )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__   sj    %'EAIMeWUVV,c27IA7MYZlYmnoo
("4r   r	   r
   r%   r   c                 X   t        j                  ||j                  d         \  }}t        j                  || j
                        }t        j                  j                  |d      j                  d      }|| j                  k  }t        j                  |d      }||j                  d d df   j                  d      z  }|j                  d d d | j                  f   j                  d      }t        j                  |||      }	t        j                   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    $'IIffll26F$G!\mmFD,=,=>66>>+B>?FFBFO%

2
 XXj!,
jmmAqD)--d33
  ]]1&?(?(?&?#?@DDTJ
99ZkJgg**<9IJ2Nr   N
r   r   r   r   r>   r8   r?   r   r   r   r   r   r   rC   rC   t   sa     =B%L=de 5e 55 5^a 5#++ s{{ S UXU`U` r   rC   c                       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	y)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                 z    t        |t              r|dk  rt        d|       t        ||      | _        || _        y )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__   s>    %%!UV[U\]^^ 2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      j                         }
|j                         }|	j                         |
z   }|j                  |   j                  |      }|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88J$;T=N=NO4::v||B/0$'IIfd$;!\  #**Z"8:"Eq$w!OR\^bQcdlln&..0(002U:+../@AEEFVW&..z:Fr   Nrb   r   r   r   rd   rd      sa    
 ;@,bc )c ) )\_ )#++ s{{ S UXU`U` r   rd   c                   p    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y	)
!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                     || _         y 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   j                  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__   sk    XXfllU5\M:
CIIgk22=*--4;L;L8L*M*Q*QRS*TV\]r   N	r   r   r   r   r8   r?   r   r   r   r   r   r   rv   rv      sC    )S )#++ s{{ S UXU`U` r   rv   c                   t    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y
)!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                      || _         || _        y 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   j                  d      |      }|S rz   )
r   rh   rP   r>   r|   r   rY   rW   r   rX   r}   s         r   r   z*FlaxForcedEOSTokenLogitsProcessor.__call__   su    XXfllU5\M:
CIIg&?!&CDD=*--4;L;L8L*M*Q*QRS*TV\]r   Nr   r   r   r   r   r      sJ    )3 )c )#++ s{{ S UXU`U` r   r   c                   t    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y
)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|       || _        || _        y )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*j1nQR\Q]^__,,q0@ST`Sabcc$(r   r	   r
   r%   r   c                     dt        j                  || j                  z
  dd      z
  }t        j                  ||j                  d d | j
                  f   j                  t        d             |      }|S )NrE   r   r{   )r   clipr   rY   rW   r   rX   r>   r   r	   r
   r%   r   s        r   r   z%FlaxMinLengthLogitsProcessor.__call__  s`    CHHWt%>1EE=&))At7H7H4H*I*M*MuUZ|m*\^der   Nr   r   r   r   r   r      sJ    )3 )c )#++ s{{ S UXU`U` r   r   c                   "    e Zd ZdZd ZdefdZy)(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
    beginning of the generation.

    Args:
        begin_suppress_tokens (`List[int]`):
            Tokens to not sample.
        begin_index (`int`):
            Index where the tokens are suppressed.
    c                 2    t        |      | _        || _        y 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   j                  t        d             |      }|S )NrE   r{   )r   r|   r   rY   rW   r   rX   r>   r   s        r   r   z1FlaxSuppressTokensAtBeginLogitsProcessor.__call__!  sa    CIIg0@0@&@AA=&))At7Q7Q4Q*R*V*VX]^cXdWd*egmnr   N)r   r   r   r   r?   r8   r   r   r   r   r   r     s    
'3 r   r   c                   p    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	y	)
!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        |      | _        y r)   )r3   r   )r   r   s     r   r?   z*FlaxSuppressTokensLogitsProcessor.__init__3  s    #O4r   r	   r
   r%   r   c                 n    |j                   d| j                  f   j                  t        d             }|S )N.r{   )rW   r   rX   r>   rA   s       r   r   z*FlaxSuppressTokensLogitsProcessor.__call__6  s1    3 4 445995<-Hr   N)
r   r   r   r   r3   r?   r   r   r8   r   r   r   r   r   r   )  sC    5 5#++ s{{ S UXU`U` r   r   c                   j    e Zd ZdZd Zdej                  dej                  dedej                  fdZy)	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                 D   t        |      }t        j                  t        |j	                               dz   t        j
                        dz  }|j                         D ]&  \  }}|	|j                  |   j                  |      }( t        j
                  |      | _	        y )NrE   dtyperK   )
dictr   onesrf   r4   int32itemsrW   rX   force_token_array)r   force_token_mapr   indextokens        r   r?   z'FlaxForceTokensLogitsProcessor.__init__G  s    /  HHc/*>*>*@&AA&EciiX[]]+113 	KLE5 $5$8$8$?$C$CE$J!	K "%+<!=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  sv    aJ 22>BMvV\\BeEl]RJiiQv||DG11*g=GY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)    CHH&&w/14- r   )rN   r   r   rP   )r   r	   r
   r%   r   s   ` ``@r   r   z'FlaxForceTokensLogitsProcessor.__call__R  s@    	 t--33A66
 r   N)	r   r   r   r   r?   r   r   r8   r   r   r   r   r   r   <  s<    	>#++ s{{ S UXU`U` r   r   c                       e Zd ZdZd Zd Zy)#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                  | _        y y )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8&5&L&L#.EEI/!3**!?$AB/>/Z/ZD,/;/F/FD,++3/;/F/FD, 4r   c                     |j                   d d  j                  f   j                  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   j                  t        d             |      }t        j                  j                  |d      } fd} t	        j
                  |      ||      }|S )	Nr{   c                 f   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kD  |j                  j                  d  j                  t        d             |j                  d j                   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"   !$Gd6F6F,F1+LdTY!Z!$GaK(D,@,@@+" ),		7T=M=M3MQR2RTXZ_(`%(+		GaK(D,@,@@))% 99"		-1KK 4 4 67;;U5\MJKK 3$"3"3488%,G
  r   TFrE   rK   rL   c                 8   t         j                  j                  | j                  d  d      }t	        j
                  | d j                         }t	        j                  ||kD  |j                  d j                   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<P<R1SZ\ 0 ]%(WWZ8N$:N:N-O%P"99!$::2d22377u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__  s"   1d999:>>e}M	2 (,'	6:&)ii4;K;K0KTSX&Y#&)ii,,D80'
# ++d.N.NN'IIa)++,00%,?
 66%%f2%6	 3128VDr   N)r   r   r   r   r?   r   r   r   r   r   r   l  s     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
y) 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                 X    t        |t              r|dk  rt        d|       || _        y )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*jAoZ[eZfg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                  j	                  d|z  |t        j                  |j                              }	t        j                  |z        |z  k  j                  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                   |    j                  t        j                  |gt	        j
                        D cg c]  }t        j                        |||z   f   ! c}z               S c c}w r)   )rW   rX   r   arrayranger   )ivalbposjrn   r	   r   s        r   body_funzFFlaxNoRepeatNGramLogitsProcessor.get_previous_ngrams.<locals>.body_fun  s    JAz/C66!9==		 BGtAWXAsyy+AsQwJ7XY 
 Ys   $A=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     (oo
G! 34
! 34

	 j($//A*=> WW..zJ&#))E2Y

 

:
23j:6MMUUV_`{{D"45j]j]]a]l]lMl=l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88r   )r   sparsifyrR   r   bcoo_todense)r   r   r   r   s       r   get_banned_tokens_maskz7FlaxNoRepeatNGramLogitsProcessor.get_banned_tokens_mask  s@     
		9 
 
	9 ""8M?#KLLr   r
   c                       fd}t         j                  j                   j                  dz
  k\  |fd      }|S )Nc            
      "   j                   \  } }j                  |      }t        j                  j                   d   j                  dz
  fj
                        }t        j                  j                  |t        j                  j                  dj                  dz
  z
  fj                   d   j                  dz
  f      d      }j                  ||      j                  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    "LLMAz"66y*gVO  IIyq'94??Q;N&OW`WfWfgMGG88%%7doo.A#BCiooVWFX[_[j[jmn[nEp M *.)D)D]Tc)d)k)klr)s&997%,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__  s4    	P& w$//A*==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DMCKK MUXU`U` M#++ s{{ S UXU`U` 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r   