(
decoder,
inits=None,
max_step_num=None,
output_time_major=False,
impute_finished=False,
is_test=False,
return_length=False,
**kwargs,
)
| 1021 | |
| 1022 | |
| 1023 | def _dynamic_decode_pir_declarative( |
| 1024 | decoder, |
| 1025 | inits=None, |
| 1026 | max_step_num=None, |
| 1027 | output_time_major=False, |
| 1028 | impute_finished=False, |
| 1029 | is_test=False, |
| 1030 | return_length=False, |
| 1031 | **kwargs, |
| 1032 | ): |
| 1033 | initial_inputs, initial_states, initial_finished = decoder.initialize(inits) |
| 1034 | global_inputs, global_states, global_finished = ( |
| 1035 | initial_inputs, |
| 1036 | initial_states, |
| 1037 | initial_finished, |
| 1038 | ) |
| 1039 | global_finished.stop_gradient = True |
| 1040 | step_idx = paddle.full(shape=[1], fill_value=0, dtype="int64") |
| 1041 | |
| 1042 | cond = paddle.logical_not(paddle.all(initial_finished)) |
| 1043 | a = paddle.to_tensor(1) |
| 1044 | b = paddle.to_tensor(5) |
| 1045 | cond1 = paddle.less_than(a, b) |
| 1046 | |
| 1047 | if max_step_num is not None: |
| 1048 | max_step_num = paddle.full( |
| 1049 | shape=[1], fill_value=max_step_num, dtype="int64" |
| 1050 | ) |
| 1051 | |
| 1052 | while_op = paddle.static.nn.control_flow.While(cond, is_test=is_test) |
| 1053 | |
| 1054 | sequence_lengths = paddle.cast(paddle.zeros_like(initial_finished), "int64") |
| 1055 | sequence_lengths.stop_gradient = True |
| 1056 | |
| 1057 | if is_test: |
| 1058 | # for test, reuse inputs and states variables to save memory |
| 1059 | inputs = paddle.utils.map_structure(lambda x: x, initial_inputs) |
| 1060 | states = paddle.utils.map_structure(lambda x: x, initial_states) |
| 1061 | else: |
| 1062 | # inputs and states of all steps must be saved for backward and training |
| 1063 | inputs_arrays = paddle.utils.map_structure( |
| 1064 | lambda x: paddle.tensor.array.array_write(x, step_idx), |
| 1065 | initial_inputs, |
| 1066 | ) |
| 1067 | states_arrays = paddle.utils.map_structure( |
| 1068 | lambda x: paddle.tensor.array.array_write(x, step_idx), |
| 1069 | initial_states, |
| 1070 | ) |
| 1071 | |
| 1072 | def _maybe_copy(state, new_state, step_mask): |
| 1073 | # TODO: use where_op |
| 1074 | state_dtype = state.dtype |
| 1075 | if convert_dtype(state_dtype) in ["bool"]: |
| 1076 | state = paddle.cast(state, dtype="float32") |
| 1077 | new_state = paddle.cast(new_state, dtype="float32") |
| 1078 | if step_mask.dtype != state.dtype: |
| 1079 | step_mask = paddle.cast(step_mask, dtype=state.dtype) |
| 1080 | # otherwise, renamed bool gradients of would be summed up leading |
no test coverage detected