
    ёi                       S SK Jr  S SKrS SKJrJrJrJrJr  S SK	r
S SKrS SKJr  S SKJr  SSKJr  \(       a  S SKJr  S S	KJr  S S
KJrJrJr  / r " S S5      r " S S5      r " S S\5      r      SS jr      SS jr      SS jr\      S                 SS jj5       r \      S                 SS jj5       r \      S                 SS jj5       r       SS jr g)    )annotationsN)TYPE_CHECKINGAnyLiteral
NamedTupleoverloaddefault_main_program)in_dynamic_mode   )convert_dtype)Callable)Tensor)	EmbeddingLayerRNNCellBasec                  &    \ rS rSrS rS rS rSrg)ArrayWrapper&   c                    U/U l         g Narrayselfxs     P/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/nn/decode.py__init__ArrayWrapper.__init__'   s    S
    c                <    U R                   R                  U5        U $ r   )r   appendr   s     r   r"   ArrayWrapper.append*   s    

!r    c                8    U R                   R                  U5      $ r   )r   __getitem__)r   items     r   r%   ArrayWrapper.__getitem__.   s    zz%%d++r    r   N)__name__
__module____qualname____firstlineno__r   r"   r%   __static_attributes__ r    r   r   r   &   s    ,r    r   c                  :    \ rS rSrSrS rS rS r\S 5       r	Sr
g)	Decoder2   a  
Decoder is the base class for any decoder instance used in `dynamic_decode`.
It provides interface for output generation for one time step, which can be
used to generate sequences.

The key abstraction provided by Decoder is:

1. :code:`(initial_input, initial_state, finished) = initialize(inits)` ,
which generates the input and state for the first decoding step, and gives the
initial status telling whether each sequence in the batch is finished.
It would be called once before the decoding iterations.

2. :code:`(output, next_state, next_input, finished) = step(time, input, state)` ,
which transforms the input and state to the output and new state, generates
input for the next decoding step, and emits the flag indicating finished status.
It is the main part for each decoding iteration.

3. :code:`(final_outputs, final_state) = finalize(outputs, final_state, sequence_lengths)` ,
which revises the outputs(stack of all time steps' output) and final state(state from the
last decoding step) to get the counterpart for special usage.
Not necessary to be implemented if no need to revise the stacked outputs and
state from the last decoding step. If implemented, it would be called after
the decoding iterations.

Decoder is more general compared to RNNCell, since the returned `next_input`
and `finished` make it can determine the input and when to finish by itself
when used in dynamic decoding. Decoder always wraps a RNNCell instance though
not necessary.
c                    [         e)al  
Called once before the decoding iterations.

Parameters:
    inits: Argument provided by the caller.

Returns:
    tuple: A tuple( :code:`(initial_inputs, initial_states, finished)` ). \
        `initial_inputs` and `initial_states` both are a (possibly nested \
        structure of) tensor variable[s], and `finished` is a tensor with \
        bool data type.
NotImplementedError)r   initss     r   
initializeDecoder.initializeQ   s
     "!r    c                    [         e)a  
Called per step of decoding.

Parameters:
    time(Tensor): A Tensor with shape :math:`[1]` provided by the caller.
        The data type is int64.
    inputs(Tensor): A (possibly nested structure of) tensor variable[s].
    states(Tensor): A (possibly nested structure of) tensor variable[s].
    **kwargs: Additional keyword arguments, provided by the caller.

Returns:
    tuple: A tuple( :code:(outputs, next_states, next_inputs, finished)` ). \
        `next_inputs` and `next_states` both are a (possibly nested \
        structure of) tensor variable[s], and the structure, shape and \
        data type must be same as the counterpart from input arguments. \
        `outputs` is a (possibly nested structure of) tensor variable[s]. \
        `finished` is a Tensor with bool data type.
r2   )r   timeinputsstateskwargss        r   stepDecoder.step`   
    & "!r    c                    [         e)a  
Called once after the decoding iterations if implemented.

Parameters:
    outputs(Tensor): A (possibly nested structure of) tensor variable[s].
        The structure and data type is same as `output_dtype`.
        The tensor stacks all time steps' output thus has shape
        :math:`[time\_step, batch\_size, ...]` , which is done by the caller.
    final_states(Tensor): A (possibly nested structure of) tensor variable[s].
        It is the `next_states` returned by `decoder.step` at last decoding step,
        thus has the same structure, shape and data type with states at any time
        step.

Returns:
    tuple: A tuple( :code:`(final_outputs, final_states)` ). \
        `final_outputs` and `final_states` both are a (possibly nested \
        structure of) tensor variable[s].
r2   )r   outputsfinal_statessequence_lengthss       r   finalizeDecoder.finalizeu   r>   r    c                    g)a  
Describes whether the Decoder keeps track of finished states by itself.

`decoder.step()` would emit a bool `finished` value at each decoding
step. The emitted `finished` can be used to determine whether every
batch entries is finished directly, or it can be combined with the
finished tracker keeped in `dynamic_decode` by performing a logical OR
to take the already finished into account.

If `False`, the latter would be took when performing `dynamic_decode`,
which is the default. Otherwise, the former would be took, which uses
the finished value emitted by the decoder as all batch entry finished
status directly, and it is the case when batch entries might be
reordered such as beams in BeamSearchDecoder.

Returns:
    bool: A python bool `False`.
Fr-   r   s    r   tracks_own_finishedDecoder.tracks_own_finished   s    ( r    r-   N)r(   r)   r*   r+   __doc__r5   r<   rC   propertyrG   r,   r-   r    r   r/   r/   2   s*    <""*"*  r    r/   c                  D   \ rS rSr% SrS\S'   S\S'   S\S'   S	\S
'   S	\S'   S	\S'     S             S S jjr\S!S j5       rS r	S r
S rS rS r " S S\5      r " S S\5      r    S"S jrS r          S#S jr        S$S jr\S%S j5       rSrg)&BeamSearchDecoder   a	  
Decoder with beam search decoding strategy. It wraps a cell to get probabilities,
and follows a beam search step to calculate scores and select candidate
token ids for each decoding step.

Please refer to `Beam search <https://en.wikipedia.org/wiki/Beam_search>`_
for more details.

Note:
    When decoding with beam search, the `inputs` and `states` of cell
    would be tiled to `beam_size` (unsqueeze and tile), resulting to shapes like
    `[batch_size * beam_size, ...]` , which is built into `BeamSearchDecoder` and
    done automatically. Thus any other tensor with shape `[batch_size, ...]` used
    in `cell.call` needs to be tiled manually first, which can be completed by using
    :code:`BeamSearchDecoder.tile_beam_merge_with_batch` . The most common case
    for this is the encoder output in attention mechanism.

Parameters:
    cell (RNNCellBase): An instance of `RNNCellBase` or object with the same interface.
    start_token (int): The start token id.
    end_token (int): The end token id.
    beam_size (int): The beam width used in beam search.
    embedding_fn (optional): A callable to apply to selected candidate ids.
        Mostly it is an embedding layer to transform ids to embeddings,
        and the returned value acts as the `input` argument for `cell.call`.
        If not provided, the id to embedding transformation must be built into
        `cell.call`. Default None.
    output_fn (optional): A callable to apply to the cell's output prior to
        calculate scores and select candidate token ids. Default None.

Returns:
    BeamSearchDecoder: An instance of decoder which can be used in             `paddle.nn.dynamic_decode` to implement decoding.

Examples:

    .. code-block:: python

        >>> import numpy as np
        >>> import paddle
        >>> from paddle.nn import BeamSearchDecoder, dynamic_decode
        >>> from paddle.nn import GRUCell, Linear, Embedding
        >>> trg_embedder = Embedding(100, 32)
        >>> output_layer = Linear(32, 32)
        >>> decoder_cell = GRUCell(input_size=32, hidden_size=32)
        >>> decoder = BeamSearchDecoder(decoder_cell,
        ...                             start_token=0,
        ...                             end_token=1,
        ...                             beam_size=4,
        ...                             embedding_fn=trg_embedder,
        ...                             output_fn=output_layer)
        ...
r   cell%Embedding | Callable[..., Any] | Noneembedding_fn!Layer | Callable[..., Any] | None	output_fnintstart_token	end_token	beam_sizeNc                L    Xl         XPl        X`l        X l        X0l        X@l        g)a  
Constructor of BeamSearchDecoder.

Parameters:
    cell(RNNCellBase): An instance of `RNNCellBase` or object with the same interface.
    start_token(int): The start token id.
    end_token(int): The end token id.
    beam_size(int): The beam width used in beam search.
    embedding_fn(optional): A callable to apply to selected candidate ids.
        Mostly it is an embedding layer to transform ids to embeddings,
        and the returned value acts as the `input` argument for `cell.call`.
        If not provided, the id to embedding transformation must be built into
        `cell.call`. Default None.
    output_fn(optional): A callable to apply to the cell's output prior to
        calculate scores and select candidate token ids. Default None.
N)rN   rP   rR   rT   rU   rV   )r   rN   rT   rU   rV   rP   rR   s          r   r   BeamSearchDecoder.__init__   s$    2 	("&""r    c                :   [         R                  " U S/5      n S/[        U R                  5      -  nXS'   [         R                  " X5      n [         R
                  " U / [        [        S[        U R                  5      5      5      QSPSP5      n [         R                  " U S/[        U R                  5      S-
  -  S/-   S9n [         R
                  " U [        U R                  5      S-
  /[        [        S[        U R                  5      S-
  5      5      Q5      n U $ )a  
Tile the batch dimension of a tensor. Specifically, this function takes
a tensor t shaped `[batch_size, s0, s1, ...]` composed of minibatch
entries `t[0], ..., t[batch_size - 1]` and tiles it to have a shape
`[batch_size * beam_size, s0, s1, ...]` composed of minibatch entries
`t[0], t[0], ..., t[1], t[1], ...` where each minibatch entry is repeated
`beam_size` times.

Parameters:
    x(Tensor): A tensor with shape `[batch_size, ...]`. The data type
        should be float32, float64, int32, int64 or bool.
    beam_size(int): The beam width used in beam search.

Returns:
    Tensor: A tensor with shape `[batch_size * beam_size, ...]`, whose \
        data type is same as `x`.
   r   r   shape)	paddle	unsqueezelenr]   tile	transposelistrangereshape)r   rV   expand_timess      r   tile_beam_merge_with_batch,BeamSearchDecoder.tile_beam_merge_with_batch   s    & Q$sS\)#QKK(4eAs177|,-4q4!4
 NNaSCL1,-4
 AGGq D4aQWW1A(B#CD
 r    c           	     t    [         R                  " USU R                  /[        UR                  SS 5      QS9$ )a  
Reshape a tensor with shape `[batch_size * beam_size, ...]` to a new
tensor with shape `[batch_size, beam_size, ...]`.

Parameters:
    x(Tensor): A tensor with shape `[batch_size * beam_size, ...]`. The
        data type should be float32, float64, int32, int64 or bool.

Returns:
    Tensor: A tensor with shape `[batch_size, beam_size, ...]`, whose \
        data type is same as `x`.
r[   rZ   Nr\   )r^   re   rV   rc   r]   r   s     r   _split_batch_beams$BeamSearchDecoder._split_batch_beams"  s2     ~~aDNN'OT!''!"+=N'OPPr    c           	     ^    [         R                  " US/[        UR                  SS 5      QS9$ )a  
Reshape a tensor with shape `[batch_size, beam_size, ...]` to a new
tensor with shape `[batch_size * beam_size, ...]`.

Parameters:
    x(Tensor): A tensor with shape `[batch_size, beam_size, ...]`. The
        data type should be float32, float64, int32, int64 or bool.

Returns:
    Tensor: A tensor with shape `[batch_size * beam_size, ...]`, whose \
        data type is same as `x`.
r[   r   Nr\   )r^   re   rc   r]   r   s     r   _merge_batch_beams$BeamSearchDecoder._merge_batch_beams2  s,     ~~a'?T!''!"+->'?@@r    c                    [         R                  " US/5      nS/[        UR                  5      -  nU R                  US'   [         R
                  " X5      nU$ )a@  
This function takes a tensor t shaped `[batch_size, s0, s1, ...]` composed
of minibatch entries `t[0], ..., t[batch_size - 1]` and tiles it to have a
shape `[batch_size, beam_size, s0, s1, ...]` composed of minibatch entries
`t[0], t[0], ..., t[1], t[1], ...` where each minibatch entry is repeated
`beam_size` times.

Parameters:
    x(Tensor): A tensor with shape `[batch_size, ...]`, The data type
        should be float32, float64, int32, int64 or bool.

Returns:
    Tensor: A tensor with shape `[batch_size, beam_size, ...]`, whose \
        data type is same as `x`.
rZ   )r^   r_   r`   r]   rV   ra   )r   r   rf   s      r   _expand_to_beam_size&BeamSearchDecoder._expand_to_beam_sizeB  sL      Q$sS\)..QKK(r    c                H   [         R                  " X!R                  S9n[         R                  " [         R                  " [         R
                  " US/5      SSU R                  /5      U R                  5      [         R                  " XS-
  R                  S/5      5      -
  nU$ )a  
Mask log probabilities. It forces finished beams to allocate all probability
mass to eos and unfinished beams to remain unchanged.

Parameters:
    probs(Tensor): A tensor with shape `[batch_size, beam_size, vocab_size]`,
        representing the log probabilities. Its data type should be float32 or float64.
    finished(Tensor): A tensor with shape `[batch_size, beam_size]`,
        representing the finished status for all beams. Its data type
        should be bool.

Returns:
    Tensor: A tensor with the same shape and data type as `x`, \
        where unfinished beams stay unchanged and finished beams are \
        replaced with a tensor with all probability on the EOS token.
dtyper   rZ   )r^   castrt   multiplyra   r_   
vocab_sizenoend_mask_tensor)r   probsfinisheds      r   _mask_probsBeamSearchDecoder._mask_probsX  s    $ ;;x{{;KK  A3/!Q1H ""	

 OOEqL#;#;QC#@AB r    c                   UR                   UR                   :w  a   [        R                  " X2R                   5      OUnSUl        [        R                  " [        R
                  " [        R                  " SUSUR                   S9S/5      SU R                  /5      n[        R                  " XB/SS9nSUl        [        R                  " X5      $ )a  
Gather from the tensor `x` using `indices`.

Parameters:
    x(Tensor): A tensor with shape `[batch_size, beam_size, ...]`.
    indices(Tensor): A `int64` tensor with shape `[batch_size, beam_size]`,
        representing the indices that we use to gather.
    batch_size(Tensor): A tensor with shape `[1]`. Its data type should
        be int32 or int64.

Returns:
    Tensor: A tensor with the same shape and data type as `x`, \
        representing the gathered tensor.
Tr   rZ   rs   r   axis)
rt   r^   ru   stop_gradientra   r_   arangerV   stack	gather_nd)r   r   indices
batch_size	batch_postopk_coordinatess         r   _gatherBeamSearchDecoder._gatheru  s    $ 7==0 KK
MM2 	
 $(
 KKaQgmmDqc 	
	 "<<(<1E)-&44r    c                  8    \ rS rSr% SrS\S'   S\S'   S\S'   Srg)	BeamSearchDecoder.OutputWrapperi  z
The structure for the returned value `outputs` of `decoder.step`.
A namedtuple includes scores, predicted_ids, parent_ids as fields.
r   scorespredicted_ids
parent_idsr-   Nr(   r)   r*   r+   rI   __annotations__r,   r-   r    r   OutputWrapperr     s    	
 r    r   c                  B    \ rS rSr% SrS\S'   S\S'   S\S'   S\S'   Srg	)
BeamSearchDecoder.StateWrapperi  z
The structure for the argument `states` of `decoder.step`.
A namedtuple includes cell_states, log_probs, finished, lengths as fields.
r   cell_states	log_probsrz   lengthsr-   Nr   r-   r    r   StateWrapperr     s     	
 r    r   c           
     V   SU l         [        R                  R                  U5      S   n[        R                  " U5      S   U l        [        R                  " S/SU R                  S9U l        [        R                  " S/SU R                  S9U l
        [        R                  R                  U R                  U5      n[        R                  " U R
                  U R                  /U R                  U R                  R                  S9n[        R                  " [        R                   " ["        R$                  " S/U R                   * /U R                  S-
  -  -   /SS	95      U R
                  S/5      n[        R&                  " 5       S
:X  a  [        R(                  " US
5      n[        R                  " [        R                  " U5      S   U R                  /SSS9n[        R*                  " U5      nU R,                  (       a  U R-                  U5      OUnUU R/                  X5Xg5      U4$ )a  
Initialize the BeamSearchDecoder.

Parameters:
    initial_cell_states(Tensor): A (possibly nested structure of)
        tensor variable[s]. An argument provided by the caller.

Returns:
    tuple: A tuple( :code:`(initial_inputs, initial_states, finished)` ). \
        `initial_inputs` is a tensor t filled by `start_token` with shape \
        `[batch_size, beam_size]` when `embedding_fn` is None, or the \
        returned value of `embedding_fn(t)` when `embedding_fn` is provided. \
        `initial_states` is a nested structure(namedtuple including cell_states, \
        log_probs, finished, lengths as fields) of tensor variables, where \
        `log_probs, finished, lengths` all has a tensor value shaped \
        `[batch_size, beam_size]` with data type `float32, bool, int64`. \
        cell_states has a value with the same structure as the input \
        argument `initial_cell_states` but with tiled shape `[batch_size, beam_size, ...]`. \
        `finished` is a `bool` tensor filled by False with shape `[batch_size, beam_size]`.
g    eAr   rZ   int64r]   rt   
fill_valuer]   r   rt   g        float32rs   float64Fbool)kinfr^   utilsflattenr]   r   fullrT   start_token_tensorrU   end_token_tensormap_structurerp   rV   rt   ra   assignnpr   get_default_dtyperu   
zeros_likerP   r   )r   initial_cell_statesstateinit_cell_statesinit_inputsr   init_finishedinit_lengthss           r   r5   BeamSearchDecoder.initialize  s   . 	$$%89!< ,,u-a0"(++#W1A1A#
 !'#W!
 "<<55%%':
 kk??DNN3..))//

 KKMMUtyyj\T^^a-?@@A# __a 
	 ##%2Iy9I<<&q)4>>:
 ((5.2.?.?Dk*[ 	  ] 
 	
r    c                  ^ ^ UR                   S   T l        [        R                  " S/ST R                  S9T l        T R
                  * /T R                  -  nSUT R                  '   [        R                  " [        R                  " US5      5      T l
        [        R                  " 5       S:X  a&  [        R                  " T R                  S5      T l
        [        R                  " [        R                  R                  R!                  U5      5      nT R#                  XdR$                  5      n[        R&                  " XdR(                  R+                  S/5      5      nUn[        R,                  " UST R.                  T R                  -  /5      n[        R0                  " UT R.                  S	9u  p[        R2                  " U
T R                  5      m[        R4                  " U
T R                  5      nT R7                  [        R,                  " UST R.                  T R                  -  /5      U
T R8                  5      n[        R:                  R=                  UU 4S
 jU5      nT R7                  UR$                  TT R8                  5      nT R7                  UR>                  TT R8                  5      nU[        R                  " [        R@                  " U5      UR>                  RB                  5      -   n[        RD                  " U[        RF                  " UT RH                  5      5      nT RK                  XT5      nT RM                  X<X5      nUU4$ )aL  
Calculate scores and select candidate token ids.

Parameters:
    time(Tensor): An `int64` tensor with shape `[1]` provided by the caller,
        representing the current time step number of decoding.
    logits(Tensor): A tensor with shape `[batch_size, beam_size, vocab_size]`,
        representing the logits at the current time step. Its data type is float32.
    next_cell_states(Tensor): A (possibly nested structure of) tensor variable[s].
        It has the same structure, shape and data type as the `cell_states` of
        `initial_states` returned by `initialize()`. It represents the next state
        from the cell.
    beam_state(Tensor): A structure of tensor variables.
        It is same as the `initial_states` returned by `initialize()` for
        the first decoding step and `beam_search_state` returned by
        `step()` for the others.

Returns:
    tuple: A tuple( :code:`(beam_search_output, beam_search_state)` ). \
        `beam_search_output` is a namedtuple(including scores, predicted_ids, \
        parent_ids as fields) of tensor variables, where \
        `scores, predicted_ids, parent_ids` all has a tensor value shaped \
        `[batch_size, beam_size]` with data type `float32, int64, int64`.
        `beam_search_state` has the same structure, shape and data type \
        as the input argument `beam_state`.

r[   rZ   r   r   r   r   r   r   )r   kc                >   > TR                  U TTR                  5      $ r   )r   r   )r   beam_indicesr   s    r   <lambda>5BeamSearchDecoder._beam_search_step.<locals>.<lambda>/  s    dll1lDOODr    )'r]   rw   r^   r   vocab_size_tensorr   rU   r   r   r   rx   r   ru   lognn
functionalsoftmaxr{   rz   addr   r_   re   rV   topkfloor_divide	remainderr   r   r   r   r   logical_notrt   
logical_orequalr   r   r   )r   r8   logitsnext_cell_states
beam_statenoend_arraystep_log_probsr   r   topk_scorestopk_indicestoken_indicesnext_log_probsnext_finishednext_lengthsbeam_search_outputbeam_search_stater   s   `                @r   _beam_search_step#BeamSearchDecoder._beam_search_step  s   8 !,,r*!'#W"
 		zlT__4&'DNN#!'rxxY/O!P##%2%+[[&&	&D"  FII$8$8$@$@$HI)).:M:MNJJ00::A3?
	
 T^^doo-M(NO$*KK&DNN$K!**<9O9OP((t7M7MNNN9r4>>DOO+K&LMOO

 "<<55D
 t
 ||doo
 $fkk}-z/A/A/G/G'
 
 ))LL(=(=>

 "//
 !--m
 "#444r    c                   [         R                  R                  U R                  U5      n[         R                  R                  U R                  UR                  5      nU R
                  " X%40 UD6u  pg[         R                  R                  U R                  U5      n[         R                  R                  U R                  U5      nU R                  b  U R                  U5      nU R                  UUUUS9u  pU	R                  n
UR                  nSUl        U R                  (       a  U R                  U5      OUnXX4$ )a  
Perform a beam search decoding step, which uses `cell` to get probabilities,
and follows a beam search step to calculate scores and select candidate
token ids.

Parameters:
    time(Tensor): An `int64` tensor with shape `[1]` provided by the caller,
        representing the current time step number of decoding.
    inputs(Tensor): A tensor variable. It is same as `initial_inputs`
        returned by `initialize()` for the first decoding step and
        `next_inputs` returned by `step()` for the others.
    states(Tensor): A structure of tensor variables.
        It is same as the `initial_states` returned by `initialize()` for
        the first decoding step and `beam_search_state` returned by
        `step()` for the others.
    **kwargs: Additional keyword arguments, provided by the caller.

Returns:
    tuple: A tuple( :code:`(beam_search_output, beam_search_state, next_inputs, finished)` ). \
        `beam_search_state` and `next_inputs` have the same structure, \
        shape and data type as the input arguments `states` and `inputs` separately. \
        `beam_search_output` is a namedtuple(including scores, predicted_ids, \
        parent_ids as fields) of tensor variables, where \
        `scores, predicted_ids, parent_ids` all has a tensor value shaped \
        `[batch_size, beam_size]` with data type `float32, int64, int64`. \
        `finished` is a `bool` tensor with shape `[batch_size, beam_size]`.
)r8   r   r   r   T)r^   r   r   rm   r   rN   rj   rR   r   rz   r   r   rP   )r   r8   r9   r:   r;   r   cell_outputsr   r   r   rz   
sample_idsnext_inputss                r   r<   BeamSearchDecoder.stepH  s1   < ++D,C,CVLll00##V%7%7
 *.*
#)*
& ||11##\
 "<<55##%5
 >>%>>,7L040F0F-	 1G 1
- %--'55
#'
 -1->->Dj)J 	 #{MMr    c                    [         R                  R                  R                  UR                  UR
                  5      n[         R                  " XAR                  R                  S9nXB4$ )a  
Use `gather_tree` to backtrace along the beam search tree and construct
the full predicted sequences.

Parameters:
    outputs(Tensor): A structure(namedtuple) of tensor variables,
        The structure and data type is same as `output_dtype`.
        The tensor stacks all time steps' output thus has shape
        `[time_step, batch_size, ...]`, which is done by the caller.
    final_states(Tensor): A structure(namedtuple) of tensor variables.
        It is the `next_states` returned by `decoder.step` at last
        decoding step, thus has the same structure, shape and data type
        with states at any time step.
    sequence_lengths(Tensor): An `int64` tensor shaped `[batch_size, beam_size]`.
        It contains sequence lengths for each beam determined during
        decoding.

Returns:
    tuple: A tuple( :code:`(predicted_ids, final_states)` ). \
        `predicted_ids` is an `int64` tensor shaped \
        `[time_step, batch_size, beam_size]`. `final_states` is the same \
        as the input argument `final_states`.
rs   )r^   r   r   gather_treer   r   ru   rt   )r   r@   rA   rB   r   s        r   rC   BeamSearchDecoder.finalize  sY    4 		,,88!!7#5#5
 !6!6!<!<
 **r    c                    g)a(  
BeamSearchDecoder reorders its beams and their finished state. Thus it
conflicts with `dynamic_decode` function's tracking of finished states.
Setting this property to true to avoid early stopping of decoding due
to mismanagement of the finished state.

Returns:
    bool: A python bool `True`.
Tr-   rF   s    r   rG   %BeamSearchDecoder.tracks_own_finished  s     r    )r   rV   rN   rP   rU   r   r   rx   rR   rT   r   rw   r   )NN)rN   r   rT   rS   rU   rS   rV   rS   rP   rO   rR   rQ   returnNone)r   r   rV   rS   r   r   )r   r   r   z#tuple[Tensor, StateWrapper, Tensor])
r8   r   r9   r   r:   r   r;   r   r   z2tuple[OutputWrapper, StateWrapper, Tensor, Tensor])r@   r   rA   r   rB   r   r   ztuple[Tensor, Tensor])r   Literal[True])r(   r)   r*   r+   rI   r   r   staticmethodrg   rj   rm   rp   r{   r   r   r   r   r5   r   r<   rC   rJ   rG   r,   r-   r    r   rL   rL      sV   4l 7700NN ?C7;## # 	#
 # <# 5# 
#@    DQ A ,:5@
 	z 	F
#)F
	,F
PT5l<N<N$*<N4:<NFI<N	;<N|!+!+-3!+GM!+	!+F 
 
r    rL   c                  ^^ S mU R                  U5      u  pn
UU	U
spm[        R                  " [        R                  " U
5      5      n[        R                  " [        R
                  " U
5      S5      nS nSn[        R                  " S/USS9n[        R                  " U5      R                  5       (       Ga  U R                  " UX40 UD6u  nnnnU R                  (       d  [        R                  " UT5      n[        R                  " UT5        [        R                  " U[        R                  " [        R                  " T5      UR                  5      5      nU(       a&  [        R                   R#                  UU4S jUU5      nO6[%        US5      (       d  [&        R(                  " S5      OS   [+        USU5      nUS:X  a!  [        R                   R#                  S	 U5      O![        R                   R#                  S
 UU5      nUUUU4u  pmn[        R,                  " USS9nUS-  n[        R                  " [        R                  " T5      5      nUb  UU:  a  O,[        R                  " U5      R                  5       (       a  GM  [        R                   R#                  S U5      nUn U R/                  UUU5      u  nnU(       d!  [        R                   R#                  S U5      nU(       a  UUU4$ UU4$ ! [0         a     NDf = f)Nc                   U R                   n[        U5      S;   a*  [        R                  " U SS9n [        R                  " USS9nUR                   U R                   :w  a  [        R                  " X R                   S9nUR	                  S/5      nSUl        [        R                  " X5      [        R                  " XS-
  5      -
  n[        U5      S;   a  [        R                  " XS9nU$ N)r   r   rs   rZ   Trt   r   r^   ru   r_   r   rv   r   	new_state	step_maskstate_dtypes       r   _maybe_copy/_dynamic_decode_imperative.<locals>._maybe_copy      kk%1KKY7EIY?I??ekk)I[[AI '',	"&	OOE5A9
 
	 %1IAIr    r   r   rZ   r   c                   > T" XT5      $ r   r-   )r   yr   rz   s     r   r   ,_dynamic_decode_imperative.<locals>.<lambda>  s    Q8!<r    r   ]`next_states` has no `lengths` attribute, the returned `sequence_lengths` would be all zeros.c                    [        U 5      $ r   )r   r   s    r   r   r     s    ar    c                $    UR                  U 5      $ r   )r"   )r   x_arrays     r   r   r   	  s    7>>!#4r          ?r   valuec                @    [         R                  " U R                  SS9$ )Nr   r~   )r^   r   r   r   s    r   r   r     s    &,,qwwQ/r    c                    [         R                  " U SS/[        [        S[	        U R
                  5      5      5      Q5      $ NrZ   r   r   r^   rb   rc   rd   r`   r]   r   s    r   r   r   (  s2    f&&Aq84aQWW 678r    )r5   r^   r   allru   r   r   r   r   r&   r<   rG   r   r   r   rt   r   r   hasattrwarningswarngetattr	incrementrC   r3   )decoderr4   max_step_numoutput_time_majorimpute_finishedis_testreturn_lengthr;   initial_inputsinitial_statesinitial_finishedr9   r:   condrB   r@   step_idxstep_idx_tensorstep_outputsnext_statesr   r   next_sequence_lengthsfinal_outputsrA   r   rz   s                            @@r   _dynamic_decode_imperativer    s   & 8?7I7I%7P4N$4 FH
 fjj)9:;D{{6#4#45E#FPGHkkPO
((4.



BI,,VC
/5C
?{K **
 #--mXFM MM-2$*JJ &&x02B2H2H%! $ll88< {I66 s $+Y(8%! 1} LL&&'@,O++4lG 	 !	6
2"2 !**_CHA!!&**X"67#<(?q ((4.



t LL../M L&-&6&6<)9'
#| 22 	
  
&67 \*  s   1K? ?
LLc           	     
  ^^^^  U R                  U5      u  pn
UU	U
spmSTl        [        R                  " S/SSS9m [        R                  " [        R
                  " U
5      5      nUb  [        R                  " S/USS9n[        R                  R                  R                  R                  XS9n[        R                  " [        R                  " U
5      S5      nSUl        U(       aC  [        R                  R                  S U5      n[        R                  R                  S U	5      nOH[        R                  R                  U 4S	 jU5      n[        R                  R                  U 4S
 jU	5      nS mS nS mUR                  5          U(       dH  [        R                  R                  U 4S jW5      n[        R                  R                  U 4S jW5      nU R                  " T WW40 UD6u  nnnnU R                   (       d  [        R"                  " UT5      n[        R$                  " U[        R                  " [        R                  " T5      UR&                  5      5      nU(       a&  [        R                  R                  UU4S jUU5      nO6[)        US5      (       d  [*        R,                  " S5      OS   [/        USU5      n[        R                  R                  U4S jU5      n[        R                  R                  U 4S jUU5        [        R0                  " T SS9m [        R2                  " UT5        [        R2                  " UU5        U(       a_  [        R                  R                  [        R2                  UU5        [        R                  R                  [        R2                  UU5        OJ[        R                  R                  U 4S jUW5        [        R                  R                  U 4S jUW5        UbV  [        R4                  " [        R                  " [        R
                  " T5      5      [        R6                  " T U5      U5        O+[        R                  " [        R
                  " T5      U5        S S S 5        [        R                  R                  S W5      nU(       a  UnO$[        R                  R                  U 4S jW5      n U R9                  UUU5      u  nnU(       d   [        R                  R                  UU5      nU(       a  UUU4$ UU4$ ! , (       d  f       N= f! [:         a     NTf = f)NTrZ   r   r   r   r  c                    U $ r   r-   r   s    r   r   -_dynamic_decode_declarative.<locals>.<lambda>U      ar    c                    U $ r   r-   r   s    r   r   r  V  r  r    c                X   > [         R                  R                  R                  U T5      $ r   r^   tensorr   array_writer   r  s    r   r   r  Z      fmm))55aBr    c                X   > [         R                  R                  R                  U T5      $ r   r  r  s    r   r   r  ^  r  r    c                   U R                   n[        U5      S;   a*  [        R                  " U SS9n [        R                  " USS9nUR                   U R                   :w  a  [        R                  " X R                   S9nUR	                  S/5      nSUl        [        R                  " X5      [        R                  " XS-
  5      -
  n[        U5      S;   a  [        R                  " XS9nU$ r   r   r   s       r   r   0_dynamic_decode_declarative.<locals>._maybe_copyb  r   r    c                    [         R                  " U SS/[        [        S[	        U R
                  5      5      5      Q5      $ r   r   r   s    r   _transpose_batch_time:_dynamic_decode_declarative.<locals>._transpose_batch_timeu  3    Aq#H4aQWW0F+G#HIIr    c                    [        5       R                  n[        5       R                  5       R                  [        5       l        [        R
                  R                  R                  U 5      nU[        5       l        U$ r   )r
   current_block_idxcurrent_block
parent_idxr^   r  r   create_array)rt   r&  tensor_arrays      r   _create_array_out_of_while?_dynamic_decode_declarative.<locals>._create_array_out_of_whilex  s_    02DD "002== 	0 }}**77>3D0r    c                X   > [         R                  R                  R                  U T5      $ r   r^   r  r   
array_readr   r  s    r   r   r        fmm11<<UHMr    c                X   > [         R                  R                  R                  U T5      $ r   r.  r0  s    r   r   r    r1  r    c                   > T" XT5      $ r   r-   r   r   r   global_finisheds     r   r   r        Q?!Cr    r   r   c                (   > T" U R                   5      $ r   rs   r   r+  s    r   r   r        09r    c                V   > [         R                  R                  R                  U TUS9$ N)ir   r  r   r   r  s     r   r   r    &    v}}22>>XW  ?  r    r   r   c                V   > [         R                  R                  R                  U TUS9$ r;  r  r=  s     r   r   r    '    6==#6#6#B#B $C $r    c                V   > [         R                  R                  R                  U TUS9$ r;  r  r=  s     r   r   r    r@  r    c                Z    [         R                  R                  R                  U SSS9S   $ Nr   T)r   	use_stackr^   r  manipulationtensor_array_to_tensorr   s    r   r   r    .    fmm00GGT H 

r    c                X   > [         R                  R                  R                  U T5      $ r   r.  r0  s    r   r   r        &----88Ir    )r5   r   r^   r   r   r   staticr   control_flowWhileru   r   r   r   blockr<   rG   r   r   rt   r   r   r   r   r   r   logical_and
less_equalrC   r3   )!r  r4   r  r  r  r  r  r;   r  r  r	  global_inputsglobal_statesr
  while_oprB   r9   r:   inputs_arraysstates_arraysr"  r@   r  r   r   r  outputs_arraysr  rA   r+  r   r5  r  s!                                @@@@r   _dynamic_decode_declarativerW  5  s}    8?7I7I%7P4N$4 2M/
 %)O!{{!'BHfjj)9:;D{{#,g
 }},,2242IH{{6#4#45E#FP%)"++KH++KH 22B
 22B

&J 
	\\//MF \\//MF >E\\ff>
(.>
:+{M **
 #--m_MM$*JJ &&7$**%! $ll88C {I66 s $+Y(8%!
  3397
 	"" 	
 ##hc: 	m_5+-=>LL&&{M LL&&{M LL&&  LL&&  #""6::o#>?!!(L9 vzz/:DAu 
x LL..	 		M $||11I

&-&6&6<)9'
#| 22!=
  
&67 \*m 
	\  s   L T4'U 4
U
UUc           	       ^"^#^$^%^& U R                  U5      u  pn
UU	U
spm%ST%l        [        R                  " S/SSS9m&[        R                  " [        R
                  " U
5      5      n[        R                  " S5      n[        R                  " S5      n[        R                  " X5      nUb  [        R                  " S/USS9n[        R                  R                  R                  R                  XS9n[        R                  " [        R                  " U
5      S5      nSUl        U(       aC  [        R                  R                  S U5      n[        R                  R                  S	 U	5      nOH[        R                  R                  U&4S
 jU5      n[        R                  R                  U&4S jU	5      nS m#S nSSKJm$  U$4S jm"UR%                  5          [        R&                  " US9n[        R(                  " [        R                  " XS9U5        U(       dH  [        R                  R                  U&4S jW5      n[        R                  R                  U&4S jW5      nU R*                  " T&WW40 UD6u  nnnnU R,                  (       d  [        R.                  " UT%5      n[        R0                  " U[        R                  " [        R                  " T%5      UR2                  5      5      nU(       a&  [        R                  R                  U#U%4S jUU5      nO6[5        US5      (       d  [6        R8                  " S5      OS   [;        USU5      n[        R                  R                  U"4S jU5      n[        R                  R                  U&4S jUU5        [        R&                  " T&SS9m&[        R(                  " UT%5        [        R(                  " UU5        U(       a_  [        R                  R                  [        R(                  UU5        [        R                  R                  [        R(                  UU5        OJ[        R                  R                  U&4S jUW5        [        R                  R                  U&4S jUW5        Ubl  [        R<                  " [        R>                  " T&U5      [        R                  " [        R
                  " T%5      5      5      n[        R(                  " UU5        O?[        R(                  " [        R                  " [        R
                  " T%5      5      U5        S S S 5        [        R                  R                  S W5      nU(       a  Un O$[        R                  R                  U&4S jW5      n  U RA                  UU U5      u  nn U(       d   [        R                  R                  UU5      nU(       a  UU U4$ UU 4$ ! , (       d  f       N= f! [B         a
  n! S n!A!NWS n!A!ff = f)NTrZ   r   r   r      r  c                    U $ r   r-   r   s    r   r   1_dynamic_decode_pir_declarative.<locals>.<lambda>#  r  r    c                    U $ r   r-   r   s    r   r   r[  $  r  r    c                X   > [         R                  R                  R                  U T5      $ r   r  r  s    r   r   r[  (  r  r    c                X   > [         R                  R                  R                  U T5      $ r   r  r  s    r   r   r[  ,  r  r    c                   U R                   n[        U5      S;   a*  [        R                  " U SS9n [        R                  " USS9nUR                   U R                   :w  a  [        R                  " X R                   S9nUR	                  S/5      nSUl        [        R                  " X5      [        R                  " XS-
  5      -
  n[        U5      S;   a  [        R                  " XS9nU$ r   r   r   s       r   r   4_dynamic_decode_pir_declarative.<locals>._maybe_copy0  r   r    c                    [         R                  " U SS/[        [        S[	        U R
                  5      5      5      Q5      $ r   r   r   s    r   r"  >_dynamic_decode_pir_declarative.<locals>._transpose_batch_timeC  r$  r    r	   c                  > [         R                  R                  T" 5       5         [         R                  R	                  5       n[         R                  R                  T" 5       R                  5       R                  5       5        [         R                  R                  R                  U 5      n[         R                  R                  U5        S S S 5        U$ ! , (       d  f       W$ = fr   )r^   baseprogram_guardpirget_current_insertion_pointset_insertion_pointglobal_blockbackr  r   r)  )rt   prev_insertion_pointr*  r
   s      r   r+  C_dynamic_decode_pir_declarative.<locals>._create_array_out_of_whileH  s    [[&&';'=>#)::#I#I#K JJ**$&335::< "==..;;EBLJJ**+?@ ?  ?> s   B'C
C&r   )r   r   c                X   > [         R                  R                  R                  U T5      $ r   r.  r0  s    r   r   r[  Y  r1  r    c                X   > [         R                  R                  R                  U T5      $ r   r.  r0  s    r   r   r[  ]  r1  r    c                   > T" XT5      $ r   r-   r4  s     r   r   r[  s  r6  r    r   r   c                (   > T" U R                   5      $ r   rs   r8  s    r   r   r[    r9  r    c                V   > [         R                  R                  R                  U TUS9$ r;  r  r=  s     r   r   r[    r>  r    r   r   c                V   > [         R                  R                  R                  U TUS9$ r;  r  r=  s     r   r   r[    r@  r    c                V   > [         R                  R                  R                  U TUS9$ r;  r  r=  s     r   r   r[    r@  r    c                Z    [         R                  R                  R                  U SSS9S   $ rC  rE  r   s    r   r   r[    rH  r    c                X   > [         R                  R                  R                  U T5      $ r   r.  r0  s    r   r   r[    rJ  r    )"r5   r   r^   r   r   r   	to_tensor	less_thanrK  r   rL  rM  ru   r   r   r   paddle.pir.corer
   rN  r   r   r<   rG   r   r   rt   r   r   r   r   rO  rP  rC   r3   )'r  r4   r  r  r  r  r  r;   r  r  r	  rQ  rR  r
  abcond1rS  rB   r9   r:   rT  rU  r"  r@   r  r   r   r  rV  cond_tmpr  rA   er+  r   r
   r5  r  s'                                     @@@@@r   _dynamic_decode_pir_declarativer~    s    8?7I7I%7P4N$4 2M/
 %)O!{{!'BHfjj)9:;DAAQ"E{{#,g
 }},,2242IH{{6#4#45E#FP%)"++KH++KH 22B
 22B

&J 5 
	q!f&&0%8\\//MF \\//MF >E\\ff>
(.>
:+{M **
 #--m_MM$*JJ &&7$**%! $ll88C {I66 s $+Y(8%!
  3397
 	"" 	
 ##hc: 	m_5+-=>LL&&{M LL&&{M LL&&  LL&&  #))!!(L9""6::o#>?H MM(D)MM&,,VZZ-HI4P 
B LL..	 		M $||11I

&-&6&6<)9'
#| 22!=
  
&67 \*w 
	f  s   M'W'W8 '
W58
XXc                    g r   r-   r  r4   r  r  r  r  r  r;   s           r   dynamic_decoder    s     58r    c                    g r   r-   r  s           r   r  r    s     =@r    c                    g r   r-   r  s           r   r  r    s     r    c           	         [        5       (       a  [        U UUUUUU40 UD6$ [        R                  R	                  5       (       a  [        U UUUUUU40 UD6$ [        U UUUUUU40 UD6$ )a  
Dynamic decoding performs :code:`decoder.step()` repeatedly until the returned
Tensor indicating finished status contains all True values or the number of
decoding step reaches to :attr:`max_step_num`.

:code:`decoder.initialize()` would be called once before the decoding loop.
If the `decoder` has implemented `finalize` method, :code:`decoder.finalize()`
would be called once after the decoding loop.

Parameters:
    decoder(Decoder): An instance of `Decoder`.
    inits(object, optional): Argument passed to `decoder.initialize`.
        Default `None`.
    max_step_num(int, optional): The maximum number of steps. If not provided,
        decode until the decoder is fully done, or in other words, the returned
        Tensor by :code:`decoder.step()` indicating finished status contains
        all True. Default `None`.
    output_time_major(bool, optional): Indicate the data layout of Tensor included
        in the final outputs(the first returned value of this method). If
        attr:`False`, the data layout would be batch major with shape
        `[batch_size, seq_len, ...]`.  If attr:`True`, the data layout would
        be time major with shape `[seq_len, batch_size, ...]`. Default: `False`.
    impute_finished(bool, optional): If `True` and `decoder.tracks_own_finished`
        is False, then states get copied through for batch entries which are
        marked as finished, which differs with the unfinished using the new states
        returned by :code:`decoder.step()` and ensures that the final states have
        the correct values. Otherwise, states wouldn't be copied through when
        finished. If the returned `final_states` is needed, it should be set as
        True, which causes some slowdown. Default `False`.
    is_test(bool, optional): A flag indicating whether to use test mode. In
        test mode, it is more memory saving. Default `False`.
    return_length(bool, optional):  A flag indicating whether to return an
        extra Tensor variable in the output tuple, which stores the actual
        lengths of all decoded sequences. Default `False`.
    **kwargs: Additional keyword arguments. Arguments passed to `decoder.step`.

Returns:

    - final_outputs (Tensor, nested structure of Tensor), each Tensor in :code:`final_outputs` is the stacked of all decoding steps' outputs, which might be revised
        by :code:`decoder.finalize()` if the decoder has implemented finalize.
        And :code:`final_outputs` has the same structure and data types as the :code:`outputs`
        returned by :code:`decoder.step()`
    - final_states (Tensor, nested structure of Tensor), :code:`final_states` is the counterpart at last time step of initial states \
        returned by :code:`decoder.initialize()` , thus has the same structure
        with it and has tensors with same shapes and data types.
    - sequence_lengths (Tensor), stores the actual lengths of all decoded sequences.
        sequence_lengths is provided only if :code:`return_length` is True.


Examples:

    .. code-block:: pycon

        >>> import paddle
        >>> from paddle.nn import BeamSearchDecoder, dynamic_decode
        >>> from paddle.nn import GRUCell, Linear, Embedding
        >>> trg_embedder = Embedding(100, 32)
        >>> output_layer = Linear(32, 32)
        >>> decoder_cell = GRUCell(input_size=32, hidden_size=32)
        >>> decoder = BeamSearchDecoder(decoder_cell,
        ...                             start_token=0,
        ...                             end_token=1,
        ...                             beam_size=4,
        ...                             embedding_fn=trg_embedder,
        ...                             output_fn=output_layer)
        >>> encoder_output = paddle.ones((4, 8, 32), dtype=paddle.get_default_dtype())
        >>> outputs = dynamic_decode(decoder=decoder,
        ...                          inits=decoder_cell.get_initial_states(encoder_output),
        ...                          max_step_num=10)
        >>> print(outputs[0].shape)
        paddle.Size([4, 11, 4])
)r   r  r^   	frameworkin_pir_moder~  rW  r  s           r   r  r    s    d )	
 	
 		
 
			%	%	'	'.	
 	
 		
 +	
 	
 		
r    )NNFFFF)......)r  r/   r4   object | Noner  
int | Noner  r   r  r   r  r   r  zLiteral[False]r;   r   r   z-tuple[Tensor, BeamSearchDecoder.StateWrapper])r  r/   r4   r  r  r  r  r   r  r   r  r   r  r   r;   r   r   z5tuple[Tensor, BeamSearchDecoder.StateWrapper, Tensor])r  r/   r4   r  r  r  r  r   r  r   r  r   r  r   r;   r   r   zetuple[Tensor, BeamSearchDecoder.StateWrapper] | tuple[Tensor, BeamSearchDecoder.StateWrapper, Tensor])!
__future__r   r   typingr   r   r   r   r   numpyr   r^   paddle.common_ops_importr
   paddle.frameworkr   base.data_feederr   collections.abcr   r   	paddle.nnr   r   r   __all__r   r/   rL   r  rW  r~  r  r-   r    r   <module>r     sN   #  D D   9 , ,(77 	, 	,l l^S Sp {@ GX Sl 
 "!$'	8	8	8 	8 		8
 	8 	8 "	8 	8 3	8 
	8 
 "!#&	@	@	@ 	@ 		@
 	@ 	@ !	@ 	@ ;	@ 
	@ 
 "!  	
    < 
" r
r    