# Constructing a loop

**URL:** <https://discuss.ai.google.dev/t/constructing-a-loop/31839>\
**Category:** General Discussion\
**Tags:** tfcore, help\_request\
**Created:** [October 20, 2021, 4:28pm UTC](https://discuss.ai.google.dev/t/constructing-a-loop/31839 "2021-10-20T16:28:34Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zsolt\_Szakaly](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/zsolt_szakaly/32/9255_2.png) [@Zsolt\_Szakaly](https://discuss.ai.google.dev/u/Zsolt_Szakaly)\
**Post date:** [October 20, 2021, 4:28pm UTC](https://discuss.ai.google.dev/t/constructing-a-loop/31839/1 "2021-10-20T16:28:34Z")

</div>

I am trying to construct a while loop from elementary operations Enter, Merge, Switch, NextIteration, Exit. Normally, when I build a Graph, I add the Operations one-by-one following the flow of tensors, i.e. when I create an operation I can name the tensor used as an input (coming as an output of another operation). The problem is with the loop, where by nature there always exists an operation (the first to add regardless where I start to build the loop), where I cannot specify the input tensor yet. Without that I cannot add the operation.  
Somewhere I read about a “workaround” to use a dummy tensor and later change it to the output of the last element of the loop. The problem is that I cannot do that either, as a Graph is append only, no existing operation can be changed.  
Can someone direct me to a page, where I can learn how to make a while loop (in TF 1.x, i.e. without tf.while)?

---

<div class="post-metadata">

**Author:** ![Bhack](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/bhack/32/6388_2.png) [@Bhack](https://discuss.ai.google.dev/u/Bhack)\
**Post date:** [October 20, 2021, 4:49pm UTC](https://discuss.ai.google.dev/t/constructing-a-loop/31839/3 "2021-10-20T16:49:58Z")

</div>

TF 1.x is EOL.

For TF 2 please take a look at:

> **[tf.function によるパフォーマンスの改善  |  TensorFlow Core](https://www.tensorflow.org/guide/function?hl=ja)**

---

<div class="post-metadata">

**Author:** ![Zsolt\_Szakaly](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/zsolt_szakaly/32/9255_2.png) [@Zsolt\_Szakaly](https://discuss.ai.google.dev/u/Zsolt_Szakaly)\
**Post date:** [October 20, 2021, 5:48pm UTC](https://discuss.ai.google.dev/t/constructing-a-loop/31839/4 "2021-10-20T17:48:59Z")

</div>

Thanks, but I do not see the relevance of that article. I would like to construct a loop in a Graph from the five elementary operations, while the linked article does not even mention them.  
I also might be in the dark regarding versions, but my understanding is that new TF2 features like _while\_loop_ actually generate under the hood the same old elementary operations that are used in TF1. Maybe the terminology I used was incorrect, but the emphasis is to build the Graph from elementary operations.  
Also, the link is around Python code, while I want to use a more direct access. Actually I work on a Pascal wrapper around the C calls. So I could simplify my question, how to make a while loop using the C API. Actually I did not even find a C++ solution. For TF2 C++ API the _control\_flow\_ops_ still contain only the elementary operations.  
Yet another approach to the same question: I can use the 800+ operations ops.pbtxt. Those include the 5 mentioned ops, but not _while\_loop_. Maybe the elementary _while_ could be used somehow?

---

<div class="post-metadata">

**Author:** ![Bhack](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/bhack/32/6388_2.png) [@Bhack](https://discuss.ai.google.dev/u/Bhack)\
**Post date:** [October 20, 2021, 7:07pm UTC](https://discuss.ai.google.dev/t/constructing-a-loop/31839/5 "2021-10-20T19:07:49Z")

</div>

> Can someone direct me to a page, where I can learn how to make a while loop without tf.while  
> My reply was related to your final claim.  
> Yes it was in python and authograph will create the graph for tconditionals and loops as in the linked example.

Are you looking for something like:

> <https://github.com/tensorflow/tensorflow/blob/master/tensorflow/c/c_api.cc#L2012>

---

<div class="post-metadata">

**Author:** ![Zsolt\_Szakaly](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/zsolt_szakaly/32/9255_2.png) [@Zsolt\_Szakaly](https://discuss.ai.google.dev/u/Zsolt_Szakaly)\
**Post date:** [October 21, 2021, 5:57am UTC](https://discuss.ai.google.dev/t/constructing-a-loop/31839/7 "2021-10-21T05:57:42Z")

</div>

Thank you,  
Probably the answer to my question is hidden around here, or even more in [tensorflow/tensorflow/cc/ops/while\_loop.cc at 5dcfc51118817f27fad5246812d83e5dccdc5f72 · tensorflow/tensorflow · GitHub](https://github.com/tensorflow/tensorflow/blob/5dcfc51118817f27fad5246812d83e5dccdc5f72/tensorflow/cc/ops/while_loop.cc), but not sure. What I would like to understand is the concept how to do it.  
What I normally do when adding an Operational Node (and call it AddXXXX):

- Create an elementary operation with TF\_NewOperation
- Set all inputs (TF\_AddInput), inputlists (TF\_AddInputList)
- Set all attributes (e.g. TF\_SetAttrBool)
- Finalise it (TF\_FinishOperation).

The problem is in TF\_AddInput. It requires a TF\_Output structure and that includes the existing operation that gives that output. In a linear Graph it is not a problem:

- AddPlaceholder (name: PH)
- AddSquare(input: PH, name: RES).  
And then I can make a session and run it with one input tensor and use RES as output.

For a loop it should be something like:  
AddPlaceholder(name: INP)  
AddEnter(input: INP, name: ENT)  
AddMerge(input: ENT, input : NEXT, name: MERGE)  
[make a COND output]  
AddSwitch(input MERGE, input COND, name SWITCH)  
AddExit(input SWITCH[true])  
[body using SWITCH[false] returning BODY ]  
AddNextIteration(input: BODY, name: NEXT)

The problem is that AddMerge cannot be constructed as it would need NEXT as an input, but at that step NextIteration is not yet added. If I trick it with some dummy input, I cannot change it later to the output of NEXT.

I think the trick is somewhere around NextIterationName and CreateMerge (lines 55 and 66 in while\_loop.cc), but I am lost.

---

<div class="post-metadata">

**Author:** ![Bhack](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/bhack/32/6388_2.png) [@Bhack](https://discuss.ai.google.dev/u/Bhack)\
**Post date:** [October 21, 2021, 5:12pm UTC](https://discuss.ai.google.dev/t/constructing-a-loop/31839/9 "2021-10-21T17:12:42Z")

</div>

@markdaoust do you have any hint on this use case of lowlevel internal?

---

<div class="post-metadata">

**Author:** ![Mark\_Daoust](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/mark_daoust/32/411_2.png) [@Mark\_Daoust](https://discuss.ai.google.dev/u/Mark_Daoust)\
**Post date:** [October 21, 2021, 5:37pm UTC](https://discuss.ai.google.dev/t/constructing-a-loop/31839/10 "2021-10-21T17:37:35Z")

</div>

> Enter, Merge, Switch, NextIteration, Exit

These were a mistake. IIUC one of the major changes for TF2 is [`control_flow_v2`](https://tensorflow.devsite.corp.google.com/api_docs/python/tf/compat/v1/enable_control_flow_v2?hl=en), where they switched these all to use a new implementation based on functions (`PartitionedCall`, `StatefulPartitionedCall`), so that downstream compilers have a chance to understand what the original code’s structure was.

Skye explains it in this TF-internals:

https://www.youtube.com/embed/IzKXEbpT9Lg?feature=oembed&wmode=opaque&list=PLQY2H8rRoyvzIuB8rZXs7pfyjiSUs8Vza

At some point the runtime does lower it to the old version for execution but my understanding was that TF2 doesn’t ever generate these into the graph anymore (does it? It’s explained in the video somewhere).

We never delete deprecated ops, so graphs containing these will continue to work.

---

<div class="post-metadata">

**Author:** ![Zsolt\_Szakaly](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/zsolt_szakaly/32/9255_2.png) [@Zsolt\_Szakaly](https://discuss.ai.google.dev/u/Zsolt_Szakaly)\
**Post date:** [October 21, 2021, 7:29pm UTC](https://discuss.ai.google.dev/t/constructing-a-loop/31839/11 "2021-10-21T19:29:45Z")

</div>

Thank you. I shall study PartitionedCall and StatefulPartitionedCall and how they can be used to make a loop. Actually I have not yet found a C++ description for that and did not get much smarter from the Python description either, but will make some more efforts.

Nonetheless, your reply made me maybe even more confused:

- I had watched the attached Skye’s video few times earlier (and asked the same question there as a comment), but there she also talks about tf.while\_loop being broken down to these 5 elementary operations (see chart at 2:20), but does not even mention PartitionedCall. The details at 9:53 also show the classic set-up.
- Maybe you do not delete deprecated ops, but at least they are marked as such in the ops.pbtxt (e.g. AdjustContrast). These five ops are not marked as deprecated, so I would assume that they are still to be used.
- Even if these 5 were a mistake and now there are better ways, I still wonder how these were supposed to work, when an ops cannot be created without its input, but its input is to be declared later.

---

<div class="post-metadata">

**Author:** ![Zsolt\_Szakaly](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/zsolt_szakaly/32/9255_2.png) [@Zsolt\_Szakaly](https://discuss.ai.google.dev/u/Zsolt_Szakaly)\
**Post date:** [July 17, 2025, 6:19am UTC](https://discuss.ai.google.dev/t/constructing-a-loop/31839/13 "2025-07-17T06:19:35Z")

</div>

Well, it took me almost four years, but finally I found the solution. So, should there be anyone else crazy enough to make a loop from the elementary operations through the C (or actually I use the Pascal wrapper around that) interface, here is the solution:  
Add the operations step by step:  
ENTER - make sure to add a frame name and use it consistently for all tensors used in the loop.  
MERGE - use two inputs, but both can be the same, the output of ENTER (this is the first trick).  
Make the loop operations  
SWITCH - again, nothing special  
NEXTITERATION - here we give the real output we want to use in the MERGE.  
EXIT - again, nothing special  
And the final touch, final trick (this is the step I was missing for years!):  
Make a TF\_UpdateEdge call to use the output tensor of the NEXTITERATION in the destination as the first (index 0) input of MERGE.  
That’s it.  
It is available on GitHub: [GitHub - zsoltszakaly/tensorflowforpascal: Use TensorFlow from Pascal (FreePascal, Lazarus, etc.)](https://github.com/zsoltszakaly/tensorflowforpascal)
