Control Flow

In programming languages, the control flow determines the order in which statements are executed. Common control flows contain sequential execution, branching, and looping. PaddlePaddle Fluid inherits this concept and provides a variety of control flow APIs to control the execution logic of the deep learning model during training or prediction.


Conditional branch, for the input of a batch, according to the given conditions, select the process in true_block or false_block to execute respectively, and then merge the outputs of the two branches into one after the execution. In general, conditional expressions can be generated by a logical comparison API such as less_than, equal.

Please refer to IfElse


Switch, like the switch-case declaration commonly found in programming languages, selects different branch to execute depending on the value of the input expression. Specifically, the Switch control flow defined by Fluid has the following characteristics:

  • The condition of the case is a bool type value, which is a tensor type Variable in the Program;
  • It checks each case one by one, selects the first case that satisfies the condition, and exits the block after completion of the execution;
  • If all cases do not meet the conditions, the default case will be selected for execution.

Please refer to Switch


When the condition is true, repeatedly execute logic in the block which While flow belongs to until the condition is judged to be false and the loop will be ended. The related APIs are as follows:

  • increment : It is usually used to count the number of loops;
  • array_read : Reads Variable from the specified location in LOD_TENSOR_ARRAY to perform calculations;
  • array_write : Writes the Variable back to the specified location in LOD_TENSOR_ARRAY and stores the result of the calculation.

Please refer to While


Dynamic RNN can process a batch of unequal(variable)-length sequence data, which accepts the variable with lod_level=1 as input. In the block of DynamicRNN, the user needs to customize RNN’s single-step calculation logic. At each time step, the user can write the state to be remembered to the memory of DynamicRNN and write the required output to its output.

sequence_last_step gets the output of the last time step of DynamicRNN.

Please refer to DynamicRNN


Static RNN can only process fixed-length sequence data, and accept Variable with lod_level=0 as input. Similar to DynamicRNN, at each single time step of the RNN, the user needs to customize the calculation logic and export the status and output.

Please refer to StaticRNN