Creates a loop that executes `loop_count` times. The returned output is the boolean predicate indicating if the loop is still executing. This is used to drive the gradient computation for the while loop associated with `while_ctx`.
| 94 | // drive the gradient computation for the while loop associated with |
| 95 | // `while_ctx`. |
| 96 | Status AddBackPropLoopCounter(WhileContext* while_ctx, const Output& loop_count, |
| 97 | const Scope& scope, |
| 98 | Output* backprop_execution_pred) { |
| 99 | // Create while loop: |
| 100 | // n = loop_count |
| 101 | // while n > 0: |
| 102 | // --n |
| 103 | |
| 104 | // Condition function that returns input > 0. |
| 105 | CondGraphBuilderFn cond_fn = [](const Scope& scope, |
| 106 | const std::vector<Output>& inputs, |
| 107 | Output* output) { |
| 108 | DCHECK_EQ(inputs.size(), 1); |
| 109 | *output = ops::Greater(scope, inputs[0], 0); |
| 110 | return scope.status(); |
| 111 | }; |
| 112 | |
| 113 | // Body function that subtracts one from input. |
| 114 | BodyGraphBuilderFn body_fn = [](const Scope& scope, |
| 115 | const std::vector<Output>& inputs, |
| 116 | std::vector<Output>* outputs) { |
| 117 | DCHECK_EQ(inputs.size(), 1); |
| 118 | outputs->emplace_back(ops::Subtract(scope, inputs[0], 1)); |
| 119 | return scope.status(); |
| 120 | }; |
| 121 | |
| 122 | string frame_name = BackPropFrameName(while_ctx->frame_name()); |
| 123 | std::vector<Output> outputs; |
| 124 | TF_RETURN_IF_ERROR(BuildWhileLoop( |
| 125 | scope, {loop_count}, cond_fn, body_fn, frame_name, &outputs, |
| 126 | /* create_while_ctx */ false, backprop_execution_pred)); |
| 127 | return Status::OK(); |
| 128 | } |
| 129 | |
| 130 | // Creates the main backprop loop that computes the gradient of the loop |
| 131 | // associated with `while_ctx`. `grad_inputs` are the partial derivatives |
no test coverage detected