beam_search_decoder.py 30.8 KB
Newer Older
Q
Qingsheng Li 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This module provides a general beam search decoder API for RNN based decoders.
The purpose of this API is to allow users to highly customize the behavior
within their RNN decoder(vanilla RNN, LSTM, attention + LSTM, future etc.),
without using the low level API such as while ops.

This API is still under active development and may change drastically.
"""

S
rename  
sneaxiy 已提交
23
from ...wrapped_decorator import signature_safe_contextmanager
Q
Qingsheng Li 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
import numpy as np

from ... import layers
from ...framework import Variable
from ... import core
from ... import framework, unique_name
from ...layer_helper import LayerHelper

__all__ = ['InitState', 'StateCell', 'TrainingDecoder', 'BeamSearchDecoder']


class _DecoderType:
    TRAINING = 1
    BEAM_SEARCH = 2


40
class InitState:
Q
Qingsheng Li 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    """
    The initial hidden state object. The state objects holds a variable, and may
    use it to initialize the hidden state cell of RNN. Usually used as input to
    `StateCell` class.

    Args:
        init (Variable): The initial variable of the hidden state. If set None,
            the variable will be created as a tensor with constant value based
            on `shape` and `value` param.
        shape (tuple|list): If `init` is None, new Variable's shape. Default
            None.
        value (float): If `init` is None, new Variable's value. Default None.
        init_boot (Variable): If provided, the initial variable will be created
            with the same shape as this variable.
        need_reorder (bool): If set true, the init will be sorted by its lod
            rank within its batches. This should be used if `batch_size > 1`.
        dtype (np.dtype|core.VarDesc.VarType|str): Data type of the initial
            variable.

    Returns:
        An initialized state object.

    Examples:
        See `StateCell`.
    """

67 68 69 70 71 72 73 74 75
    def __init__(
        self,
        init=None,
        shape=None,
        value=0.0,
        init_boot=None,
        need_reorder=False,
        dtype='float32',
    ):
Q
Qingsheng Li 已提交
76 77 78 79
        if init is not None:
            self._init = init
        elif init_boot is None:
            raise ValueError(
80 81
                'init_boot must be provided to infer the shape of InitState .\n'
            )
Q
Qingsheng Li 已提交
82
        else:
83 84 85
            self._init = layers.fill_constant_batch_size_like(
                input=init_boot, value=value, shape=shape, dtype=dtype
            )
Q
Qingsheng Li 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

        self._shape = shape
        self._value = value
        self._need_reorder = need_reorder
        self._dtype = dtype

    @property
    def value(self):
        return self._init

    @property
    def need_reorder(self):
        return self._need_reorder


101
class _MemoryState:
Q
Qingsheng Li 已提交
102 103 104 105
    def __init__(self, state_name, rnn_obj, init_state):
        self._state_name = state_name  # each is a rnn.memory
        self._rnn_obj = rnn_obj
        self._state_mem = self._rnn_obj.memory(
106 107
            init=init_state.value, need_reorder=init_state.need_reorder
        )
Q
Qingsheng Li 已提交
108 109 110 111 112 113 114 115

    def get_state(self):
        return self._state_mem

    def update_state(self, state):
        self._rnn_obj.update_memory(self._state_mem, state)


116
class _ArrayState:
Q
Qingsheng Li 已提交
117 118 119 120 121 122 123
    def __init__(self, state_name, block, init_state):
        self._state_name = state_name
        self._block = block

        self._state_array = self._block.create_var(
            name=unique_name.generate('array_state_array'),
            type=core.VarDesc.VarType.LOD_TENSOR_ARRAY,
124 125
            dtype=init_state.value.dtype,
        )
Q
Qingsheng Li 已提交
126 127 128 129

        self._counter = self._block.create_var(
            name=unique_name.generate('array_state_counter'),
            type=core.VarDesc.VarType.LOD_TENSOR,
130 131
            dtype='int64',
        )
Q
Qingsheng Li 已提交
132 133

        # initialize counter
134 135 136 137 138 139 140 141 142 143 144
        self._block.append_op(
            type='fill_constant',
            inputs={},
            outputs={'Out': [self._counter]},
            attrs={
                'shape': [1],
                'dtype': self._counter.dtype,
                'value': float(0.0),
                'force_cpu': True,
            },
        )
Q
Qingsheng Li 已提交
145 146 147 148

        self._counter.stop_gradient = True

        # write initial state
149 150 151 152 153
        block.append_op(
            type='write_to_array',
            inputs={'X': init_state.value, 'I': self._counter},
            outputs={'Out': self._state_array},
        )
Q
Qingsheng Li 已提交
154 155 156 157 158 159 160 161 162 163

    def get_state(self):
        state = layers.array_read(array=self._state_array, i=self._counter)
        return state

    def update_state(self, state):
        layers.increment(x=self._counter, value=1, in_place=True)
        layers.array_write(state, array=self._state_array, i=self._counter)


164
class StateCell:
Q
Qingsheng Li 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    """
    The state cell class stores the hidden state of the RNN cell. A typical RNN
    cell has one or more hidden states, and one or more step inputs. This class
    allows you to defines the name of hidden states as well as step inputs, and
    their associated variables.

    Args:
        inputs (dict): A feeding dict of {name(str) : Variable}. It specifies
            the names of step inputs for RNN cell, and the associated variables.
            The variable could initially be None and set manually during each
            RNN step.
        states (dict): A feeding dict of {name(str) : InitState object}. It
            specifies the names of hidden states and their initialized state.
        out_state (str): A string that specifies the name of hidden state that
            will be used to compute the score in beam search process.
        name (str): The name of the RNN cell. Default None.

    Raises:
        `ValueError`: If the initial state is not an instance of InitState, or
            the out_state is not in the dict of states.

    Returns:
        StateCell: The initialized StateCell object.

    Examples:
        .. code-block:: python
          hidden_state = InitState(init=encoder_out, need_reorder=True)
          state_cell = StateCell(
              inputs={'current_word': None},
              states={'h': hidden_state},
              out_state='h')
    """

    def __init__(self, inputs, states, out_state, name=None):
        self._helper = LayerHelper('state_cell', name=name)
        self._cur_states = {}
        self._state_names = []
202
        for state_name, state in states.items():
Q
Qingsheng Li 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
            if not isinstance(state, InitState):
                raise ValueError('state must be an InitState object.')
            self._cur_states[state_name] = state
            self._state_names.append(state_name)
        self._inputs = inputs  # inputs is place holder here
        self._cur_decoder_obj = None
        self._in_decoder = False
        self._states_holder = {}
        self._switched_decoder = False
        self._state_updater = None
        self._out_state = out_state
        if self._out_state not in self._cur_states:
            raise ValueError('out_state must be one state in states')

    def _enter_decoder(self, decoder_obj):
        if self._in_decoder == True or self._cur_decoder_obj is not None:
            raise ValueError('StateCell has already entered a decoder.')
        self._in_decoder = True
        self._cur_decoder_obj = decoder_obj
        self._switched_decoder = False

    def _leave_decoder(self, decoder_obj):
        if not self._in_decoder:
226 227 228
            raise ValueError(
                'StateCell not in decoder, ' 'invalid leaving operation.'
            )
Q
Qingsheng Li 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248

        if self._cur_decoder_obj != decoder_obj:
            raise ValueError('Inconsistent decoder object in StateCell.')

        self._in_decoder = False
        self._cur_decoder_obj = None
        self._switched_decoder = False

    def _switch_decoder(self):  # lazy switch
        if not self._in_decoder:
            raise ValueError('StateCell must be enter a decoder.')

        if self._switched_decoder:
            raise ValueError('StateCell already done switching.')

        for state_name in self._state_names:
            if state_name not in self._states_holder:
                state = self._cur_states[state_name]

                if not isinstance(state, InitState):
249 250 251 252
                    raise ValueError(
                        'Current type of state is %s, should be '
                        'an InitState object.' % type(state)
                    )
Q
Qingsheng Li 已提交
253 254 255 256

                self._states_holder[state_name] = {}

                if self._cur_decoder_obj.type == _DecoderType.TRAINING:
257 258 259 260 261
                    self._states_holder[state_name][
                        id(self._cur_decoder_obj)
                    ] = _MemoryState(
                        state_name, self._cur_decoder_obj.dynamic_rnn, state
                    )
Q
Qingsheng Li 已提交
262
                elif self._cur_decoder_obj.type == _DecoderType.BEAM_SEARCH:
263 264 265 266 267
                    self._states_holder[state_name][
                        id(self._cur_decoder_obj)
                    ] = _ArrayState(
                        state_name, self._cur_decoder_obj._parent_block(), state
                    )
Q
Qingsheng Li 已提交
268
                else:
269 270 271 272
                    raise ValueError(
                        'Unknown decoder type, only support '
                        '[TRAINING, BEAM_SEARCH]'
                    )
Q
Qingsheng Li 已提交
273 274

            # Read back, since current state should be LoDTensor
275 276 277
            self._cur_states[state_name] = self._states_holder[state_name][
                id(self._cur_decoder_obj)
            ].get_state()
Q
Qingsheng Li 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296

        self._switched_decoder = True

    def get_state(self, state_name):
        """
        The getter of state object. Find the state variable by its name.

        Args:
            state_name (str): A string of the state's name.

        Returns:
            The associated state object.
        """
        if self._in_decoder and not self._switched_decoder:
            self._switch_decoder()

        if state_name not in self._cur_states:
            raise ValueError(
                'Unknown state %s. Please make sure _switch_decoder() '
297 298
                'invoked.' % state_name
            )
Q
Qingsheng Li 已提交
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341

        return self._cur_states[state_name]

    def get_input(self, input_name):
        """
        The getter of input variable. Find the input variable by its name.

        Args:
            input_name (str): The string of the input's name.

        Returns:
            The associated input variable.
        """
        if input_name not in self._inputs or self._inputs[input_name] is None:
            raise ValueError('Invalid input %s.' % input_name)
        return self._inputs[input_name]

    def set_state(self, state_name, state_value):
        """
        The setter of the state variable. Change the variable of the given
        `state_name`.

        Args:
            state_name (str): The name of the state to change.
            state_value (Var): The variable of the new state.
        """
        self._cur_states[state_name] = state_value

    def state_updater(self, updater):
        """
        Set up the updater to update the hidden state every RNN step. The
        behavior of updater could be customized by users. The updater should be
        a function that takes a `StateCell` object as input and update the
        hidden state within it. The hidden state could be accessed through
        `get_state` method.

        Args:
            updater (func): the updater to update the state cell.
        """
        self._state_updater = updater

        def _decorator(state_cell):
            if state_cell == self:
342 343 344 345
                raise TypeError(
                    'Updater should only accept a StateCell object '
                    'as argument.'
                )
Q
Qingsheng Li 已提交
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
            updater(state_cell)

        return _decorator

    def compute_state(self, inputs):
        """
        Provide the step input of RNN cell, and compute the new hidden state
        with updater and give step input.

        Args:
            inputs (dict): A feed dict, {name(str): Variable}. name should be
            the names of step inputs for this RNN cell, and Variable should be
            the associated variables.

        Examples:
        .. code-block:: python
          state_cell.compute_state(inputs={'x': current_word})
        """
        if self._in_decoder and not self._switched_decoder:
            self._switch_decoder()

367
        for input_name, input_value in inputs.items():
Q
Qingsheng Li 已提交
368
            if input_name not in self._inputs:
369 370 371 372 373
                raise ValueError(
                    'Unknown input %s. '
                    'Please make sure %s in input '
                    'place holder.' % (input_name, input_name)
                )
Q
Qingsheng Li 已提交
374 375 376 377 378 379 380 381 382 383
            self._inputs[input_name] = input_value
        self._state_updater(self)

    def update_states(self):
        """
        Update and record state information after each RNN step.
        """
        if self._in_decoder and not self._switched_decoder:
            self._switched_decoder()

384
        for state_name, decoder_state in self._states_holder.items():
Q
Qingsheng Li 已提交
385
            if id(self._cur_decoder_obj) not in decoder_state:
386 387 388 389
                raise ValueError(
                    'Unknown decoder object, please make sure '
                    'switch_decoder been invoked.'
                )
Q
Qingsheng Li 已提交
390
            decoder_state[id(self._cur_decoder_obj)].update_state(
391 392
                self._cur_states[state_name]
            )
Q
Qingsheng Li 已提交
393 394 395 396 397 398 399 400 401 402 403

    def out_state(self):
        """
        Get the output state variable. This must be called after update_states.

        Returns:
            The output variable of the RNN cell.
        """
        return self._cur_states[self._out_state]


404
class TrainingDecoder:
Q
Qingsheng Li 已提交
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
    """
    A decoder that can only be used for training. The decoder could be
    initialized with a `StateCell` object. The computation within the RNN cell
    could be defined with decoder's block.

    Args:
        state_cell (StateCell): A StateCell object that handles the input and
            state variables.
        name (str): The name of this decoder. Default None.

    Returns:
        TrainingDecoder: The initialized TrainingDecoder object.

    Examples:
        .. code-block:: python
          decoder = TrainingDecoder(state_cell)
          with decoder.block():
              current_word = decoder.step_input(trg_embedding)
              decoder.state_cell.compute_state(inputs={'x': current_word})
              current_score = layers.fc(input=decoder.state_cell.get_state('h'),
                                        size=32,
                                        act='softmax')
              decoder.state_cell.update_states()
              decoder.output(current_score)
    """
430

Q
Qingsheng Li 已提交
431 432 433 434 435 436 437 438 439 440 441 442
    BEFORE_DECODER = 0
    IN_DECODER = 1
    AFTER_DECODER = 2

    def __init__(self, state_cell, name=None):
        self._helper = LayerHelper('training_decoder', name=name)
        self._status = TrainingDecoder.BEFORE_DECODER
        self._dynamic_rnn = layers.DynamicRNN()
        self._type = _DecoderType.TRAINING
        self._state_cell = state_cell
        self._state_cell._enter_decoder(self)

S
rename  
sneaxiy 已提交
443
    @signature_safe_contextmanager
Q
Qingsheng Li 已提交
444 445 446 447 448 449 450
    def block(self):
        """
        Define the behavior of the decoder for each RNN time step.
        """
        if self._status != TrainingDecoder.BEFORE_DECODER:
            raise ValueError('decoder.block() can only be invoked once')
        self._status = TrainingDecoder.IN_DECODER
451

Q
Qingsheng Li 已提交
452 453
        with self._dynamic_rnn.block():
            yield
454

Q
Qingsheng Li 已提交
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
        self._status = TrainingDecoder.AFTER_DECODER
        self._state_cell._leave_decoder(self)

    @property
    def state_cell(self):
        self._assert_in_decoder_block('state_cell')
        return self._state_cell

    @property
    def dynamic_rnn(self):
        return self._dynamic_rnn

    @property
    def type(self):
        return self._type

    def step_input(self, x):
        """
        Set the input variable as a step input to the RNN cell. For example,
        in machine translation, each time step we read one word from the target
        sentences, then the target sentence is a step input to the RNN cell.

        Args:
            x (Variable): the variable to be used as step input.

        Returns:
            Variable: The variable as input of current step.

        Examples:
        .. code-block:: python
          current_word = decoder.step_input(trg_embedding)
        """
        self._assert_in_decoder_block('step_input')
        return self._dynamic_rnn.step_input(x)

    def static_input(self, x):
        """
        Set the input variable as a static input of RNN cell. In contrast to
        step input, this variable will be used as a whole within the RNN decode
        loop and will not be scattered into time steps.

        Args:
            x (Variable): the variable to be used as static input.

        Returns:
            Variable: The variable as input of current step.

        Examples:
        .. code-block:: python
          encoder_vec = decoder.static_input(encoded_vector)
        """
        self._assert_in_decoder_block('static_input')
        return self._dynamic_rnn.static_input(x)

    def __call__(self, *args, **kwargs):
        """
        Get the output of RNN. This API should only be invoked after RNN.block()

        Returns:
            Variable: The specified output of the RNN cell.
        """
        if self._status != TrainingDecoder.AFTER_DECODER:
517 518 519 520
            raise ValueError(
                'Output of training decoder can only be visited '
                'outside the block.'
            )
Q
Qingsheng Li 已提交
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
        return self._dynamic_rnn(*args, **kwargs)

    def output(self, *outputs):
        """
        Set the output variable of the RNN cell.

        Args:
            *outputs (Variables): a series of variables that treated as output
                of the RNN cell.

        Examples:
        .. code-block:: python
          out = fluid.layers.fc(input=h,
                                size=32,
                                bias_attr=True,
                                act='softmax')
          decoder.output(out)
        """
        self._assert_in_decoder_block('output')
        self._dynamic_rnn.output(*outputs)

    def _assert_in_decoder_block(self, method):
        if self._status != TrainingDecoder.IN_DECODER:
544 545 546 547
            raise ValueError(
                '%s should be invoked inside block of '
                'TrainingDecoder object.' % method
            )
Q
Qingsheng Li 已提交
548 549


550
class BeamSearchDecoder:
Q
Qingsheng Li 已提交
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
    """
    A beam search decoder that can be used for inference. The decoder should be
    initialized with a `StateCell` object. The decode process can be defined
    within its block.

    Args:
        state_cell (StateCell): A StateCell object that handles the input and
            state variables.
        init_ids (Variable): The init beam search token ids.
        init_scores (Variable): The associated score of each id.
        target_dict_dim (int): Size of dictionary.
        word_dim (int): Word embedding dimension.
        input_var_dict (dict): A feeding dict to feed the required input
            variables to the state cell. It will be used by state_cell 's
            compute method. Default empty.
        topk_size (int): The topk size used for beam search. Default 50.
        max_len (int): The maximum allowed length of the generated sentence.
            Default 100.
        beam_size (int): The beam width of beam search decode. Default 1.
        end_id (int): The id of end token within beam search.
        name (str): The name of this decoder. Default None.

    Returns:
        BeamSearchDecoder: A initialized BeamSearchDecoder object.

    Examples:
    .. code-block:: python
      decoder = BeamSearchDecoder(
          state_cell=state_cell,
          init_ids=init_ids,
          init_scores=init_scores,
          target_dict_dim=target_dict_dim,
          word_dim=word_dim,
          init_var_dict={},
          topk_size=topk_size,
          sparse_emb=IS_SPARSE,
          max_len=max_length,
          beam_size=beam_size,
          end_id=1,
          name=None
      )
      decoder.decode()
      translation_ids, translation_scores = decoder()
    """
595

Q
Qingsheng Li 已提交
596 597 598 599
    BEFORE_BEAM_SEARCH_DECODER = 0
    IN_BEAM_SEARCH_DECODER = 1
    AFTER_BEAM_SEARCH_DECODER = 2

600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
    def __init__(
        self,
        state_cell,
        init_ids,
        init_scores,
        target_dict_dim,
        word_dim,
        input_var_dict={},
        topk_size=50,
        sparse_emb=True,
        max_len=100,
        beam_size=1,
        end_id=1,
        name=None,
    ):
Q
Qingsheng Li 已提交
615 616 617 618
        self._helper = LayerHelper('beam_search_decoder', name=name)
        self._counter = layers.zeros(shape=[1], dtype='int64')
        self._counter.stop_gradient = True
        self._type = _DecoderType.BEAM_SEARCH
619 620 621 622 623 624 625
        self._max_len = layers.fill_constant(
            shape=[1], dtype='int64', value=max_len
        )
        self._cond = layers.less_than(
            x=self._counter,
            y=layers.fill_constant(shape=[1], dtype='int64', value=max_len),
        )
Q
Qingsheng Li 已提交
626 627 628 629
        self._while_op = layers.While(self._cond)
        self._state_cell = state_cell
        self._state_cell._enter_decoder(self)
        self._status = BeamSearchDecoder.BEFORE_BEAM_SEARCH_DECODER
630 631 632
        self._zero_idx = layers.fill_constant(
            shape=[1], value=0, dtype='int64', force_cpu=True
        )
Q
Qingsheng Li 已提交
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
        self._array_dict = {}
        self._array_link = []
        self._ids_array = None
        self._scores_array = None
        self._beam_size = beam_size
        self._end_id = end_id

        self._init_ids = init_ids
        self._init_scores = init_scores
        self._target_dict_dim = target_dict_dim
        self._topk_size = topk_size
        self._sparse_emb = sparse_emb
        self._word_dim = word_dim
        self._input_var_dict = input_var_dict

S
rename  
sneaxiy 已提交
648
    @signature_safe_contextmanager
Q
Qingsheng Li 已提交
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
    def block(self):
        """
        Define the behavior of the decoder for each RNN time step.
        """
        if self._status != BeamSearchDecoder.BEFORE_BEAM_SEARCH_DECODER:
            raise ValueError('block() can only be invoke once.')

        self._status = BeamSearchDecoder.IN_BEAM_SEARCH_DECODER

        with self._while_op.block():
            yield
            with layers.Switch() as switch:
                with switch.case(self._cond):
                    layers.increment(x=self._counter, value=1.0, in_place=True)

                    for value, array in self._array_link:
665 666 667
                        layers.array_write(
                            x=value, i=self._counter, array=array
                        )
Q
Qingsheng Li 已提交
668

669 670 671
                    layers.less_than(
                        x=self._counter, y=self._max_len, cond=self._cond
                    )
Q
Qingsheng Li 已提交
672 673 674 675 676 677 678 679 680 681 682 683

        self._status = BeamSearchDecoder.AFTER_BEAM_SEARCH_DECODER
        self._state_cell._leave_decoder(self)

    @property
    def type(self):
        return self._type

    def early_stop(self):
        """
        Stop the generation process in advance. Could be used as "break".
        """
684 685 686
        layers.fill_constant(
            shape=[1], value=0, dtype='bool', force_cpu=True, out=self._cond
        )
Q
Qingsheng Li 已提交
687 688 689 690 691 692 693 694 695 696 697 698 699 700

    def decode(self):
        """
        Set up the computation within the decoder. Then you could call the
        decoder to get the result of beam search decode. If you want to define
        a more specific decoder, you could override this function.

        Examples:
        .. code-block:: python
          decoder.decode()
          translation_ids, translation_scores = decoder()
        """
        with self.block():
            prev_ids = self.read_array(init=self._init_ids, is_ids=True)
701 702 703
            prev_scores = self.read_array(
                init=self._init_scores, is_scores=True
            )
Q
Qingsheng Li 已提交
704 705 706 707
            prev_ids_embedding = layers.embedding(
                input=prev_ids,
                size=[self._target_dict_dim, self._word_dim],
                dtype='float32',
708 709
                is_sparse=self._sparse_emb,
            )
Q
Qingsheng Li 已提交
710 711 712 713

            feed_dict = {}
            update_dict = {}

714
            for init_var_name, init_var in self._input_var_dict.items():
Q
Qingsheng Li 已提交
715
                if init_var_name not in self.state_cell._inputs:
716 717 718 719 720
                    raise ValueError(
                        'Variable '
                        + init_var_name
                        + ' not found in StateCell!\n'
                    )
Q
Qingsheng Li 已提交
721 722 723

                read_var = self.read_array(init=init_var)
                update_dict[init_var_name] = read_var
724
                feed_var_expanded = layers.sequence_expand(
725 726
                    read_var, prev_scores
                )
Q
Qingsheng Li 已提交
727 728 729 730
                feed_dict[init_var_name] = feed_var_expanded

            for state_str in self._state_cell._state_names:
                prev_state = self.state_cell.get_state(state_str)
731
                prev_state_expanded = layers.sequence_expand(
732 733
                    prev_state, prev_scores
                )
Q
Qingsheng Li 已提交
734 735 736 737 738 739 740 741
                self.state_cell.set_state(state_str, prev_state_expanded)

            for i, input_name in enumerate(self._state_cell._inputs):
                if input_name not in feed_dict:
                    feed_dict[input_name] = prev_ids_embedding

            self.state_cell.compute_state(inputs=feed_dict)
            current_state = self.state_cell.out_state()
742 743 744 745 746 747 748 749
            current_state_with_lod = layers.lod_reset(
                x=current_state, y=prev_scores
            )
            scores = layers.fc(
                input=current_state_with_lod,
                size=self._target_dict_dim,
                act='softmax',
            )
Q
Qingsheng Li 已提交
750
            topk_scores, topk_indices = layers.topk(scores, k=self._topk_size)
751 752 753 754 755 756 757 758 759 760 761 762 763 764
            accu_scores = layers.elementwise_add(
                x=layers.log(x=topk_scores),
                y=layers.reshape(prev_scores, shape=[-1]),
                axis=0,
            )
            selected_ids, selected_scores = layers.beam_search(
                prev_ids,
                prev_scores,
                topk_indices,
                accu_scores,
                self._beam_size,
                end_id=1,
                level=0,
            )
Q
Qingsheng Li 已提交
765 766 767 768 769 770 771 772

            with layers.Switch() as switch:
                with switch.case(layers.is_empty(selected_ids)):
                    self.early_stop()
                with switch.default():
                    self.state_cell.update_states()
                    self.update_array(prev_ids, selected_ids)
                    self.update_array(prev_scores, selected_scores)
773
                    for update_name, var_to_update in update_dict.items():
Q
Qingsheng Li 已提交
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
                        self.update_array(var_to_update, feed_dict[update_name])

    def read_array(self, init, is_ids=False, is_scores=False):
        """
        Read an array to get the decoded ids and scores generated by previous
        RNN step. At the first step of RNN, the init variable mut be used to
        initialize the array.

        Args:
            init (Variable): The initial variable for first step usage. init
                must be provided.
            is_ids (bool): Specify whether the variable is an id.
            is_scores (bool): Specify whether the variable is a score.

        Returns:
            The associated variable generated during previous RNN steps.

        Examples:
            .. code-block:: python
              prev_ids = decoder.read_array(init=init_ids, is_ids=True)
              prev_scores = decoder.read_array(init=init_scores, is_scores=True)
        """
        self._assert_in_decoder_block('read_array')

        if is_ids and is_scores:
799 800 801 802
            raise ValueError(
                'Shouldn\'t mark current array be ids array and'
                'scores array at the same time.'
            )
Q
Qingsheng Li 已提交
803 804 805 806 807 808 809 810

        if not isinstance(init, Variable):
            raise TypeError('The input argument `init` must be a Variable.')

        parent_block = self._parent_block()
        array = parent_block.create_var(
            name=unique_name.generate('beam_search_decoder_array'),
            type=core.VarDesc.VarType.LOD_TENSOR_ARRAY,
811 812 813 814 815 816 817
            dtype=init.dtype,
        )
        parent_block.append_op(
            type='write_to_array',
            inputs={'X': init, 'I': self._zero_idx},
            outputs={'Out': array},
        )
Q
Qingsheng Li 已提交
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840

        if is_ids:
            self._ids_array = array
        elif is_scores:
            self._scores_array = array

        read_value = layers.array_read(array=array, i=self._counter)
        self._array_dict[read_value.name] = array
        return read_value

    def update_array(self, array, value):
        """
        Store the value generated in current step in an array for each RNN step.
        This array could be accessed by read_array method.

        Args:
            array (Variable): The array to append the new variable to.
            value (Variable): The newly generated value to be stored.
        """
        self._assert_in_decoder_block('update_array')

        if not isinstance(array, Variable):
            raise TypeError(
841 842
                'The input argument `array` of  must be a Variable.'
            )
Q
Qingsheng Li 已提交
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
        if not isinstance(value, Variable):
            raise TypeError('The input argument `value` of must be a Variable.')

        array = self._array_dict.get(array.name, None)
        if array is None:
            raise ValueError('Please invoke read_array before update_array.')
        self._array_link.append((value, array))

    def __call__(self):
        """
        Run the decode process and return the final decode result.

        Returns:
            A tuple of decoded (id, score) pairs. id is a Variable that holds
            the generated tokens, and score is a Variable with the same shape
            as id, holds the score for each generated token.
        """
        if self._status != BeamSearchDecoder.AFTER_BEAM_SEARCH_DECODER:
861 862 863 864 865 866 867 868 869 870
            raise ValueError(
                'Output of BeamSearchDecoder object can '
                'only be visited outside the block.'
            )
        return layers.beam_search_decode(
            ids=self._ids_array,
            scores=self._scores_array,
            beam_size=self._beam_size,
            end_id=self._end_id,
        )
Q
Qingsheng Li 已提交
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892

    @property
    def state_cell(self):
        self._assert_in_decoder_block('state_cell')
        return self._state_cell

    def _parent_block(self):
        """
        Getter of parent block.

        Returns:
            The parent block of decoder.
        """
        program = self._helper.main_program
        parent_block_idx = program.current_block().parent_idx
        if parent_block_idx < 0:
            raise ValueError('Invalid block with index %d.' % parent_block_idx)
        parent_block = program.block(parent_block_idx)
        return parent_block

    def _assert_in_decoder_block(self, method):
        if self._status != BeamSearchDecoder.IN_BEAM_SEARCH_DECODER:
893 894 895 896
            raise ValueError(
                '%s should be invoked inside block of '
                'BeamSearchDecoder object.' % method
            )