MCPcopy Create free account
hub / github.com/PaddlePaddle/Paddle / _dynamic_decode_declarative

Function _dynamic_decode_declarative

python/paddle/nn/decode.py:821–1020  ·  view source on GitHub ↗
(
    decoder,
    inits=None,
    max_step_num=None,
    output_time_major=False,
    impute_finished=False,
    is_test=False,
    return_length=False,
    **kwargs,
)

Source from the content-addressed store, hash-verified

819
820
821def _dynamic_decode_declarative(
822 decoder,
823 inits=None,
824 max_step_num=None,
825 output_time_major=False,
826 impute_finished=False,
827 is_test=False,
828 return_length=False,
829 **kwargs,
830):
831 initial_inputs, initial_states, initial_finished = decoder.initialize(inits)
832 global_inputs, global_states, global_finished = (
833 initial_inputs,
834 initial_states,
835 initial_finished,
836 )
837 global_finished.stop_gradient = True
838 step_idx = paddle.full(shape=[1], fill_value=0, dtype="int64")
839
840 cond = paddle.logical_not(paddle.all(initial_finished))
841 if max_step_num is not None:
842 max_step_num = paddle.full(
843 shape=[1], fill_value=max_step_num, dtype="int64"
844 )
845
846 while_op = paddle.static.nn.control_flow.While(cond, is_test=is_test)
847
848 sequence_lengths = paddle.cast(paddle.zeros_like(initial_finished), "int64")
849 sequence_lengths.stop_gradient = True
850
851 if is_test:
852 # for test, reuse inputs and states variables to save memory
853 inputs = paddle.utils.map_structure(lambda x: x, initial_inputs)
854 states = paddle.utils.map_structure(lambda x: x, initial_states)
855 else:
856 # inputs and states of all steps must be saved for backward and training
857 inputs_arrays = paddle.utils.map_structure(
858 lambda x: paddle.tensor.array.array_write(x, step_idx),
859 initial_inputs,
860 )
861 states_arrays = paddle.utils.map_structure(
862 lambda x: paddle.tensor.array.array_write(x, step_idx),
863 initial_states,
864 )
865
866 def _maybe_copy(state, new_state, step_mask):
867 # TODO: use where_op
868 state_dtype = state.dtype
869 if convert_dtype(state_dtype) in ["bool"]:
870 state = paddle.cast(state, dtype="float32")
871 new_state = paddle.cast(new_state, dtype="float32")
872 if step_mask.dtype != state.dtype:
873 step_mask = paddle.cast(step_mask, dtype=state.dtype)
874 # otherwise, renamed bool gradients of would be summed up leading
875 # to sum(bool) error.
876 step_mask = step_mask.unsqueeze([1])
877 step_mask.stop_gradient = True
878 new_state = paddle.multiply(state, step_mask) - paddle.multiply(

Callers 1

dynamic_decodeFunction · 0.85

Calls 13

blockMethod · 0.95
fullMethod · 0.80
logical_notMethod · 0.80
allMethod · 0.80
_maybe_copyFunction · 0.70
initializeMethod · 0.45
castMethod · 0.45
stepMethod · 0.45
addMethod · 0.45
warnMethod · 0.45
assignMethod · 0.45

Tested by

no test coverage detected