(
decoder,
inits=None,
max_step_num=None,
output_time_major=False,
impute_finished=False,
is_test=False,
return_length=False,
**kwargs,
)
| 819 | |
| 820 | |
| 821 | def _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( |
no test coverage detected