MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / AddForwardLoopCounter

Function AddForwardLoopCounter

tensorflow/cc/framework/while_gradients.cc:57–90  ·  view source on GitHub ↗

Creates a loop that counts the number of iterations performed by the while loop associated with `while_ctx`. The returned output yields the iteration count.

Source from the content-addressed store, hash-verified

55// while loop associated with `while_ctx`. The returned output yields the
56// iteration count.
57Status AddForwardLoopCounter(WhileContext* while_ctx, const Scope& scope,
58 Output* count) {
59 // Create while loop:
60 // i = 0
61 // while forward loop predicate is true:
62 // ++i
63
64 Output zero = ops::Const(scope, 0, {});
65
66 // Condition function that returns condition output from original while loop.
67 CondGraphBuilderFn cond_fn = [while_ctx](const Scope& scope,
68 const std::vector<Output>& inputs,
69 Output* output) {
70 *output = ToOutput(while_ctx->cond_output());
71 return Status::OK();
72 };
73
74 // Body function that adds one to input.
75 BodyGraphBuilderFn body_fn = [](const Scope& scope,
76 const std::vector<Output>& inputs,
77 std::vector<Output>* outputs) {
78 DCHECK_EQ(inputs.size(), 1);
79 outputs->emplace_back(ops::Add(scope, inputs[0], 1));
80 return scope.status();
81 };
82
83 // Note that this loop runs in the same execution frame as the forward loop.
84 std::vector<Output> outputs;
85 TF_RETURN_IF_ERROR(BuildWhileLoop(scope, {zero}, cond_fn, body_fn,
86 while_ctx->frame_name(), &outputs,
87 /* create_while_ctx */ false));
88 *count = outputs[0];
89 return Status::OK();
90}
91
92// Creates a loop that executes `loop_count` times. The returned output is the
93// boolean predicate indicating if the loop is still executing. This is used to

Callers 1

AddWhileLoopGradientFunction · 0.85

Calls 7

ToOutputFunction · 0.85
BuildWhileLoopFunction · 0.85
ConstFunction · 0.50
AddFunction · 0.50
sizeMethod · 0.45
emplace_backMethod · 0.45
statusMethod · 0.45

Tested by

no test coverage detected