o
    h                     @   sv  d dl Z d dlZd dlZd dlmZ d dlmZmZmZm	Z	 d dl
Z
d dlZd dlmZ d dlZd dlmZ ddlmZmZmZ ddlmZmZ dd	lmZ dd
lmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z% e&e'Z(e
j)j*G dd deZ+e
j)j*G dd deZ,e
j)j*G dd deZ-e
j)j*G dd dZ.e
j)j*G dd dZ/e
j)j*G dd dZ0G dd dZ1dS )    N)partial)AnyDictOptionalUnion)lax   ) FLAX_MODEL_FOR_CAUSAL_LM_MAPPING+FLAX_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING#FLAX_MODEL_FOR_VISION_2_SEQ_MAPPING)ModelOutputlogging   )GenerationConfig)!FlaxForcedBOSTokenLogitsProcessor!FlaxForcedEOSTokenLogitsProcessorFlaxForceTokensLogitsProcessorFlaxLogitsProcessorListFlaxMinLengthLogitsProcessor FlaxNoRepeatNGramLogitsProcessor(FlaxSuppressTokensAtBeginLogitsProcessor!FlaxSuppressTokensLogitsProcessorFlaxTemperatureLogitsWarperFlaxTopKLogitsWarperFlaxTopPLogitsWarperc                   @   $   e Zd ZU dZdZeej ed< dS )FlaxGreedySearchOutputz
    Flax Base class for outputs of decoder-only generation models using greedy search.


    Args:
        sequences (`jnp.ndarray` of shape `(batch_size, max_length)`):
            The generated sequences.
    N	sequences	__name__
__module____qualname____doc__r   r   jnpndarray__annotations__ r&   r&   v/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/transformers/generation/flax_utils.pyr   7      
 	r   c                   @   r   )FlaxSampleOutputz
    Flax Base class for outputs of decoder-only generation models using sampling.


    Args:
        sequences (`jnp.ndarray` of shape `(batch_size, max_length)`):
            The generated sequences.
    Nr   r   r&   r&   r&   r'   r)   E   r(   r)   c                   @   s6   e Zd ZU dZdZeej ed< dZ	eej ed< dS )FlaxBeamSearchOutputaT  
    Flax Base class for outputs of decoder-only generation models using greedy search.


    Args:
        sequences (`jnp.ndarray` of shape `(batch_size, max_length)`):
            The generated sequences.
        scores (`jnp.ndarray` of shape `(batch_size,)`):
            The scores (log probabilities) of the generated sequences.
    Nr   scores)
r   r    r!   r"   r   r   r#   r$   r%   r+   r&   r&   r&   r'   r*   S   s   
 r*   c                   @   sH   e Zd ZU ejed< ejed< ejed< ejed< eeejf ed< dS )GreedyStatecur_lenr   running_tokenis_sent_finishedmodel_kwargsNr   r    r!   r#   r$   r%   r   strr&   r&   r&   r'   r,   d   s   
 



r,   c                   @   sR   e Zd ZU ejed< ejed< ejed< ejed< ejed< eeejf ed< dS )SampleStater-   r   r.   r/   prng_keyr0   Nr1   r&   r&   r&   r'   r3   m   s   
 




r3   c                   @   s\   e Zd ZU ejed< ejed< ejed< ejed< ejed< ejed< eeejf ed< dS )	BeamSearchStater-   running_sequencesrunning_scoresr   r+   r/   r0   Nr1   r&   r&   r&   r'   r5   w   s   
 





r5   c                   @   s  e Zd ZdZdd Zedd Zdd Z			d9d	ed
e	e de	e de	e
eejf  dejf
ddZ	d:d
e	e de	e defddZedd Zdd Zdd Zde
eef fddZ					d;dejde	e de	ej dede	e
eejf  d e	e fd!d"Zdedefd#d$Zded%ed e	e defd&d'Zd(ed)edefd*d+Z							d<ddd,e	e d-e	e d.e	e d e	e dede	e
eejf  de	e
eejf  fd/d0Z									d=ddd,e	e d-e	e d.e	e de	ej d e	e d1e	e dede	e
eejf  de	e
eejf  fd2d3Z										d>ddd,e	e d-e	e d.e	e d4e	e d5e	eeef  d e	e dede	e
eejf  d6e	e de	e
eejf  fd7d8Z dS )?FlaxGenerationMixina  
    A class containing all functions for auto-regressive text generation, to be used as a mixin in
    [`FlaxPreTrainedModel`].

    The class exposes [`~generation.FlaxGenerationMixin.generate`], which can be used for:
            - *greedy decoding* by calling [`~generation.FlaxGenerationMixin._greedy_search`] if `num_beams=1` and
              `do_sample=False`
            - *multinomial sampling* by calling [`~generation.FlaxGenerationMixin._sample`] if `num_beams=1` and
              `do_sample=True`
            - *beam-search decoding* by calling [`~generation.FlaxGenerationMixin._beam_search`] if `num_beams>1` and
              `do_sample=False`

    You do not need to call any of the above methods directly. Pass custom parameter values to 'generate' instead. To
    learn more about decoding strategies refer to the [text generation strategies guide](../generation_strategies).
    c                 O   s   t d)NzbA model class needs to define a `prepare_inputs_for_generation` method in order to use `generate`.)NotImplementedError)selfargskwargsr&   r&   r'   prepare_inputs_for_generation   s   z1FlaxGenerationMixin.prepare_inputs_for_generationc                 C   s    |}| |r||}| |s|S )zc
        Run generation in untraced mode. This should only be used for debugging purposes.
        r&   )cond_fnbody_fn
init_statestater&   r&   r'   _run_loop_in_debug   s
   z&FlaxGenerationMixin._run_loop_in_debugc                 C   s2   dd |  D }| j|f|dd||d< |S )Nc                 S   s*   i | ]\}}| d s| ds||qS )decoder_
cross_attn)
startswith).0argumentvaluer&   r&   r'   
<dictcomp>   s    zVFlaxGenerationMixin._prepare_encoder_decoder_kwargs_for_generation.<locals>.<dictcomp>T)paramsreturn_dictencoder_outputs)itemsencode)r:   	input_idsrJ   r0   encoder_kwargsr&   r&   r'   ._prepare_encoder_decoder_kwargs_for_generation   s
   zBFlaxGenerationMixin._prepare_encoder_decoder_kwargs_for_generationN
batch_sizedecoder_start_token_idbos_token_idr0   returnc                 C   sR   |d urd|v r| d}|d ur|S | ||}tj|ddddj|ddS )Ndecoder_input_idsi4dtyper   r   axis)pop_get_decoder_start_token_idr#   arrayreshaperepeat)r:   rR   rS   rT   r0   rV   r&   r&   r'   )_prepare_decoder_input_ids_for_generation   s   
 z=FlaxGenerationMixin._prepare_decoder_input_ids_for_generationc                 C   s   |d ur|n| j j}|d ur|n| j j}|d ur|S t| jdr3t| jjdr3| jjjd ur3| jjjS |d ur9|S t| jdrRt| jjdrR| jjjd urR| jjjS td)NdecoderrS   rT   z\`decoder_start_token_id` or `bos_token_id` has to be defined for encoder-decoder generation.)generation_configrS   rT   hasattrconfigrc   
ValueError)r:   rS   rT   r&   r&   r'   r^      s0   



z/FlaxGenerationMixin._get_decoder_start_token_idc                 C   s0   t | d d d f | jd |f| jdd   S )Nr   r   )r#   broadcast_toshape)tensor	num_beamsr&   r&   r'   _expand_to_num_beams   s   0z(FlaxGenerationMixin._expand_to_num_beamsc                 C   s   |S )z
        This function can be overwritten in the specific modeling_flax_<model-name>.py classes to allow for custom beam
        search behavior. Note that the only model that overwrites this method is [`~transformes.FlaxMarianMTModel`].
        r&   )r:   logitsr&   r&   r'   _adapt_logits_for_beam_search   s   z1FlaxGenerationMixin._adapt_logits_for_beam_searchc                 C   sx   |   s:tttg}t }|D ]}|jt| jdd}|dur$||j	 qd| j
j	 d}|r6|d| 7 }t|dS )z
        Confirms that the model class is compatible with generation. If not, raises an exception that points to the
        right class to use.
        N)defaultzThe current model class (zQ) is not compatible with `.generate()`, as it doesn't have a language model head.z2 Please use one of the following classes instead: )can_generater	   r   r
   setgettyperf   addr   	__class__	TypeError)r:   generate_compatible_mappingsgenerate_compatible_classesmodel_mappingsupported_modelsexception_messager&   r&   r'   _validate_model_class   s"   z)FlaxGenerationMixin._validate_model_classc                 C   s   g }t t| jj}d|v sd|v r|t t| jjO }| D ]\}}|dur3||vr3|| q"|r>td| ddS )zXValidates model kwargs for generation. Generate argument typos will also be caught here.r<   r0   Nz8The following `model_kwargs` are not used by the model: zG (note: typos in the generate arguments will also show up in this list))	rq   inspect	signaturer=   
parameters__call__rM   appendrg   )r:   r0   unused_model_args
model_argskeyrH   r&   r&   r'   _validate_model_kwargs   s   

z*FlaxGenerationMixin._validate_model_kwargsTrO   rd   r4   tracerJ   logits_processorc                 K   s  |    |du r+| jjr(| jjt| jkr(t| j}|| jkr(t	d || _| j}t
|}|jd&i |}	| |	
  |durE|nt }|durN|ntjd}|jdu ry|jdury|	ddu rjtd |j}
t|
trv|
d }
|
|_|jdu r| jjrtd| jjs|s|jdurt|dddf |jkdkrtd |jd }| jjr|	d	du r| |||	}	| j ||j|j!|	d
}|jd }|ddu o|j"du}|r|j#du r|j"dkrt	d|j" dt$ nE|j#dur|s|j"durtd|j# d|j" d |j#| |_"n!|j"t j"kr6|j"| |_"t%| jdd}|dur6t&|j"||_"|j'durP|j'|j"krPtd|j' d|j" d||j"kro| jjr]dnd}td| d| d|j" d | j(|||d}|j)s|j*dkr| j+||j"|j|j||||	dS |j)r|j*dkr| j,|d}| j-||j"|j|j||||||	d 
S |j)s|j*dkr| j.||j*d!}d	|	v r| j.|	d	 d" |j*d!|	d	 d"< d#D ]}||	v r| j.|	| |j*d!|	|< q| j/||j"|j|j|j0|j1||||j2|	d$S t3d%)'a  
        Generates sequences of token ids for models with a language modeling head.

        Parameters:
            input_ids (`jnp.ndarray` of shape `(batch_size, sequence_length)`):
                The sequence used as a prompt for the generation.
            generation_config (`~generation.GenerationConfig`, *optional*):
                The generation configuration to be used as base parametrization for the generation call. `**kwargs`
                passed to generate matching the attributes of `generation_config` will override them. If
                `generation_config` is not provided, the default will be used, which had the following loading
                priority: 1) from the `generation_config.json` model file, if it exists; 2) from the model
                configuration. Please note that unspecified parameters will inherit [`~generation.GenerationConfig`]'s
                default values, whose documentation should be checked to parameterize generation.
            trace (`bool`, *optional*, defaults to `True`):
                Whether to trace generation. Setting `trace=False` should only be used for debugging and will lead to a
                considerably slower runtime.
            params (`Dict[str, jnp.ndarray]`, *optional*):
                Optionally the model parameters can be passed. Can be useful for parallelized generation.
            logits_processor (`FlaxLogitsProcessorList `, *optional*):
                Custom logits processors that complement the default logits processors built from arguments and
                generation config. If a logit processor is passed that is already created with the arguments or a
                generation config an error is thrown. This feature is intended for advanced users.
            kwargs (`Dict[str, Any]`, *optional*):
                Ad hoc parametrization of `generate_config` and/or additional model-specific kwargs that will be
                forwarded to the `forward` function of the model. If the model is an encoder-decoder model, encoder
                specific kwargs should not be prefixed and decoder specific kwargs should be prefixed with *decoder_*.

        Return:
            [`~utils.ModelOutput`].

        NaS  You have modified the pretrained model configuration to control generation. This is a deprecated strategy to control generation and will be removed soon, in a future version. Please use and modify the model generation configuration (see https://huggingface.co/docs/transformers/generation_strategies#default-text-generation-configuration )r   attention_maskzThe attention mask and the pad token id were not set. As a consequence, you may observe unexpected behavior. Please pass your input's `attention_mask` to obtain reliable results.zJ`decoder_start_token_id` has to be defined for encoder-decoder generation.rZ   zA decoder-only architecture is being used, but right-padding was detected! For correct generation results, please set `padding_side='left'` when initializing the tokenizer.rL   )rS   rT   r0   
max_length   z0Using the model-agnostic default `max_length` (=zx) to control the generation length.  recommend setting `max_new_tokens` to control the maximum length of the generation.zBoth `max_new_tokens` (=z) and `max_length`(=z) seem to have been set. `max_new_tokens` will take precedence. Please refer to the documentation for more information. (https://huggingface.co/docs/transformers/main/en/main_classes/text_generation)max_position_embeddingsz3Unfeasable length constraints: the minimum length (z%) is larger than the maximum length ()rV   rO   zInput length of z is z, but `max_length` is set to zW. This can lead to unexpected behavior. You should consider increasing`max_new_tokens`.)rd   input_ids_seq_lengthr   r   )r   r   rJ   r0   )rd   )logits_warperr   r   rJ   r0   )rk   last_hidden_stater   decoder_attention_mask)length_penaltyearly_stoppingr   r   rJ   num_return_sequencesr0   z,`Beam sampling is currently not implemented.r&   )4r|   rd   _from_model_config_original_object_hashhashr   from_model_configrf   warningswarncopydeepcopyupdater   r   jaxrandomPRNGKeypad_token_ideos_token_idrr   loggerwarning
isinstancelistrS   is_encoder_decoderrg   r#   sumri   rQ   rb   rT   r   max_new_tokensUserWarninggetattrmin
min_length_get_logits_processor	do_samplerk   _greedy_search_get_logits_warper_samplerl   _beam_searchr   r   r   r9   )r:   rO   rd   r4   r   rJ   r   r<   new_generation_configr0   r   rR   r   has_default_max_lengthr   input_ids_stringr   kwargr&   r&   r'   generate  s
  *



 









zFlaxGenerationMixin.generatec                 C   s~   t  }|jdur|jdkr|t|j |jdur)|jdkr)|t|jdd |jdur=|jdk r=|t|jdd |S )z
        This class returns a [`FlaxLogitsProcessorList`] list object that contains all relevant [`FlaxLogitsWarper`]
        instances used for multinomial sampling.
        Ng      ?r   r   )top_kmin_tokens_to_keep)top_pr   )r   temperaturer   r   r   r   r   r   )r:   rd   warpersr&   r&   r'   r     s   z&FlaxGenerationMixin._get_logits_warperr   c                    sR  t  }|jdur|jdur|jdkr|t|j|j |jdur)|t|j |jdur8|t|j	|j |j
durE|t|j
 |jdury } dksU|jdu rW|n|d }|jdurpt|jdkrp||jd d 7 }|t|j| |jdur fdd|jD }|t| |jdur|jdkr|t|j | ||}|S )z
        This class returns a [`FlaxLogitsProcessorList`] list object that contains all relevant [`FlaxLogitsProcessor`]
        instances used to modify the scores of the language model head.
        NrZ   r   r   c                    s$   g | ]} |d   d |d gqS )r   r   r&   )rF   ir   r&   r'   
<listcomp>  s    z=FlaxGenerationMixin._get_logits_processor.<locals>.<listcomp>)r   r   r   r   r   forced_bos_token_idr   forced_eos_token_idr   r   suppress_tokensr   begin_suppress_tokensforced_decoder_idslenr   r   no_repeat_ngram_sizer   _merge_criteria_processor_list)r:   rd   r   r   
processorsbegin_indexr   r&   r   r'   r     sF   










z)FlaxGenerationMixin._get_logits_processordefault_listcustom_listc                 C   s~   t |dkr|S |D ]-}|D ](}t|t|u r6d}td| dt| d| d| d| d| d	| d
qq
|| |S )Nr   zlogits processorz	A custom z	 of type z with values zP has been passed to `generate`, but it has already been created with the values z. z has been created by passing the corresponding arguments to generate or by the model's config default values. If you just want to change the default values of zL consider passing them as arguments to `generate` instead of using a custom .)r   rs   rg   extend)r:   r   r   ro   customobject_typer&   r&   r'   r   '  s*   

z2FlaxGenerationMixin._merge_criteria_processor_listr   r   r   c	                    sP  d urnj jd urnj j d ur nj j |j\}	}
tj  d ur-tjnd d tjtjdt|
}
tj|	ftjd}t	
||d}tj|	ftjd}jjr`jnj|fi |}t|
||||d}fdd} fdd}|jd dkr||}|s|||}nt	|||}t|jd	S )
NrX   r   r   r-   r   r.   r/   r0   c                    (   | j  k}t| j}t||}| S zstate termination condition fn.r-   r#   allr/   
logical_orrA   has_reached_max_lengthall_sequence_finishedfinish_generationr   r&   r'   greedy_search_cond_fnh     
zAFlaxGenerationMixin._greedy_search.<locals>.greedy_search_cond_fnc                    s   | j fdi| j}|jdddf }| j|| j}tj|dd}|| j  | j  }| j| kB }|dddf }t	| j|d| jf}
|| j}t| jd ||||dS )state update fn.rJ   NrZ   r[   r   r   r   )r.   r0   rm   r   r-   r#   argmaxr/   r   dynamic_update_sliceupdate_inputs_for_generationr,   )rA   model_outputsrm   
next_tokennext_is_sent_finishednext_sequencesnext_model_kwargs)r   r   modelr   rJ   r:   r&   r'   greedy_search_body_fno  s    zAFlaxGenerationMixin._greedy_search.<locals>.greedy_search_body_fnr   r   )rd   r   r   r   ri   r#   r_   int32fullr   r   zerosbool_rf   r   decoder=   r,   rB   
while_loopr   r   )r:   rO   r   r   r   r   r   rJ   r0   rR   r-   r   r/   rA   r   r   r&   )r   r   r   r   r   rJ   r:   r'   r   <  s6   

z"FlaxGenerationMixin._greedy_searchr   c                    sl  d urnj jd urnj j d ur nj j |d ur$|ntjd}|j\}}tj	  d ur9tj
nd d tj	tj
dt	|}tj|ftj
d}t||d}tj|ftjd}jjrljnj|fi |
}
t||||||
d}fdd} fdd}|jd	 d	kr||}|s|||}nt|||}t|jd
S )Nr   rX   r   )r-   r   r.   r/   r4   r0   c                    r   r   r   r   r   r&   r'   sample_search_cond_fn  r   z:FlaxGenerationMixin._sample.<locals>.sample_search_cond_fnc           	         s   t j| j\}}| jfdi| j}|jdddf }| j|| j}||| j}t jj	||dd}|| j
  | j
  }| j
| kB }|dddf }t| j|d| jf}|| j}t| jd |||||dS )r   rJ   NrZ   r[   r   r   )r-   r   r.   r/   r0   r4   )r   r   splitr4   r.   r0   rm   r   r-   categoricalr/   r   r   r   r3   )	rA   r4   prng_key_nextr   rm   r   r   r   r   )r   r   r   r   r   rJ   r:   r&   r'   sample_search_body_fn  s&   z:FlaxGenerationMixin._sample.<locals>.sample_search_body_fnr   r   )rd   r   r   r   r   r   r   ri   r#   r_   r   r   r   r   r   r   rf   r   r   r=   r3   rB   r   r)   r   )r:   rO   r   r   r   r4   r   r   r   rJ   r0   rR   r-   r   r/   rA   r   r   r&   )r   r   r   r   r   r   rJ   r:   r'   r     s:   

	zFlaxGenerationMixin._sampler   r   r   c                    s  dd dd dd durnj j|dur|nj j}dur&nj jdur0nj jdur:nj j|
durD|
nj j}
|j\ 
}tj	durXtj
nddtj	|tj
d}t	|}|jd	 tj 
f|tj
d}tj 
f|tj
d}t||d
}tj 
ftjd}tt	dgt	dg
d    dg}t 
ft	d }jjrjn	d|v r҈|d d |d d< dD ]}||v r|| ||< qԈj|fi |}t|||||||d}fdd}d 	
fdd	}t||jd	 d|}|s,|||}nt|||}tj|jdd}t|ddddf |j|j}t|dddf |j |j!}|ddd|
ddf }|ddd|
f }t"||dS )z
        This beam search function is heavily inspired by Flax's official example:
        https://github.com/google/flax/blob/main/examples/wmt/decode.py
        c                 S   s8   | j dkr| S | | jd | jd  f| jdd  S )z8Flattens the first two dimensions of a non-scalar array.r   r   r   Nndimr`   ri   rj   r&   r&   r'   flatten_beam_dim  s   
*z:FlaxGenerationMixin._beam_search.<locals>.flatten_beam_dimc                 S   s*   | j dkr| S | ||f| jdd  S )zFUnflattens the first, flat batch*beam dimension of a non-scalar array.r   r   Nr   )rj   rR   rk   r&   r&   r'   unflatten_beam_dim  s   
z<FlaxGenerationMixin._beam_search.<locals>.unflatten_beam_dimc                    s:   t t || | ||f  fdd}tj|| S )z^
            Gathers the beam slices indexed by beam_indices into new beam array.
            c                    s   | j dkr| S |  f S )Nr   )r   r   batch_indicesbeam_indicesr&   r'   	gather_fn  s   
zIFlaxGenerationMixin._beam_search.<locals>.gather_beams.<locals>.gather_fn)r#   r`   aranger   	tree_utiltree_map)nestedr   rR   new_num_beamsr   r&   r   r'   gather_beams  s
   z6FlaxGenerationMixin._beam_search.<locals>.gather_beamsNrX   rZ   )r   r   r               cr   rL   r   r   )r-   r6   r7   r   r+   r/   r0   c                    s   | j k }dkrdkr| jddddf     }n| jddddf | j     }t| jtj| jdddtd}t	||k}t
| jdu @  }||@ |@ S )z+beam search state termination condition fn.neverr  Nr   Tr\   keepdimsr  )r-   r7   r#   wherer/   r   r+   npr_   anyr   )rA   not_max_length_yetbest_running_scoreworst_finished_scoreimprovement_still_possiblestill_open_beam)decoder_prompt_lenr   r   r   r&   r'   beam_search_cond_fn_  s   

z=FlaxGenerationMixin._beam_search.<locals>.beam_search_cond_fnc               	      s  t | jdd| j| f 	|f}|fd
i| j}|jdddf  	}tj 	fdd|j	}
|}tj|}| j|| j}| 	}|tj| jdd }|jd }| 	| f}d	 }t j||d	\}	}
|
| }| j| |}tj|
| dd}t ||dd| jf}|dddd| jf k}|	|td
  }t j|	d	d }||g| 	\}}|	| jd    }	t| jjddd|jdu @ }| |B }|	|td
 7 }	tj| j|gdd}tj| j|	gdd}tj| j|gdd}t j|	d	d }|||g| 	\}}}|| 	}|| 	}tjfdd||d< || j}t| jd ||||||dS )zbeam search state update fn.r   rJ   NrZ   c                    s   |  S Nr&   r   )rR   rk   r   r&   r'   <lambda>  s    zOFlaxGenerationMixin._beam_search.<locals>.beam_search_body_fn.<locals>.<lambda>r   r[   )kr  r   Tr  c                    s    | S r  r&   )x)r   r&   r'   r    s    past_key_values)r-   r7   r6   r+   r   r/   r0   )r   dynamic_slicer6   r-   r0   rm   r   r   r  r  rn   nnlog_softmaxr#   expand_dimsr7   ri   r`   r   r   r  r_   rh   r/   r   concatenater   r+   r   r5   ) rA   input_ids_lengthinput_tokenr   rm   cache	log_probs
vocab_sizebeams_to_keeptopk_log_probstopk_indicestopk_beam_indicestopk_running_sequencestopk_idstopk_sequencesdid_topk_just_finishedrunning_topk_log_probsnext_topk_indicesnext_running_sequencesnext_running_scoresbeams_in_batch_are_fulladd_penaltymerged_sequencesmerged_scoresmerged_is_sent_finishedtopk_merged_indicesr   next_scoresr   next_running_indices
next_cacher   )rR   r  r   r   r   r  r   r   r   rk   rJ   r:   r   r&   r'   beam_search_body_fn}  s~   


	

z=FlaxGenerationMixin._beam_search.<locals>.beam_search_body_fn)r  r[   )r   r+   )r   )#rd   r   r   r   r   r   r   ri   r#   r_   r   r   r   r   r   r   tiler  onesrf   r   r   r=   r5   r   rB   r   r  r/   r
  r   r6   r+   r7   r*   )r:   rO   r   r   r   r   r   r   r   rJ   r   r0   r-   r   r6   r/   r7   r+   r   rA   r  r8  none_finishedr&   )rR   r  r   r   r   r  r   r   r   r   rk   rJ   r:   r   r'   r     sh   

,

&w z FlaxGenerationMixin._beam_search)NNN)NN)NNTNN)NNNNTNN)	NNNNNNTNN)
NNNNNNTNNN)!r   r    r!   r"   r=   staticmethodrB   rQ   intr   r   r2   r#   r$   rb   r^   rl   rn   r|   r   r   r   boolr   r   r   r   r   r   r   floatr   r   r&   r&   r&   r'   r8      s8   
	



 V
4
	
Y	

d	
r8   )2r   r}   r   	functoolsr   typingr   r   r   r   flaxr   	jax.numpynumpyr#   r  r   models.autor	   r
   r   utilsr   r   configuration_utilsr   flax_logits_processr   r   r   r   r   r   r   r   r   r   r   
get_loggerr   r   struct	dataclassr   r)   r*   r,   r3   r5   r8   r&   r&   r&   r'   <module>   s8   4
	
