Repository: tensorflow
Issue: InvalidArgumentError: Node '.../while_grad': Connecting to invalid output 4 of source node while which has 4 outputs
Labels: stat:awaiting tensorflower,type:bug,comp:ops,TF 2.8

**System information**
- Have I written custom code (as opposed to using a stock example script provided in TensorFlow): No
- OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Colab
- Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device:
- TensorFlow installed from (source or binary): Colab
- TensorFlow version (use command below): 2.8.0 (Colab)
- Python version: 3.7.12
- Bazel version (if compiling from source):
- GCC/Compiler version (if compiling from source):
- CUDA/cuDNN version:
- GPU model and memory:

**Describe the current behavior**

I get the exception
```
Node 'gradients/while_grad/while_grad': Connecting to invalid output 4 of source node while which has 4 outputs. Try using tf.compat.v1.experimental.output_all_intermediates(True).
```
when executing a `NoOp` without any control dependencies.

But also when executing my real code, I get the same exception. I'm not sure if these are two separate issues or the same.

**Describe the expected behavior**

A NoOp without control dependencies should not depend on anything, so I would never expect such exception.

For my real code, I also would not expect such exception. 

**Standalone code to reproduce the issue**

Code (to be executed with disabled eager mode):
```
v = tf.Variable(1.)

def cond(i, x):
  return tf.less(i, 10)

def body(i, x):
  return i + 1, x * 1.

j, y = tf.while_loop(cond, body, [0., v])
loss = tf.reduce_sum(y ** 2)

session.run(tf.compat.v1.global_variables_initializer())

opt = tf.compat.v1.train.GradientDescentOptimizer(0.1)
opt_op = opt.minimize(loss)

no_op = tf.no_op()
session.run(no_op)  # here it crashes already!

print(session.run((j, y, opt_op)))
```

Colab: https://colab.research.google.com/drive/1jjXpz8SAeU-8Cg6nuWu7tjxK_sAU5lVc?usp=sharing

**Other info / logs**

When executing locally, I additionally see this log output:
```
...
2022-02-19 23:07:57.636413: W tensorflow/c/c_api.cc:349] Operation '{name:'while' id:11 op device:{requested: '', assigned: ''} def:{{{node while}} = StatelessWhile[T=[DT_INT32, DT_INT32, DT_FLOAT, DT_FLOAT, DT_VARIANT], _lower_using_switch_merge=true, _num_original_outputs=5, _read_only_resource_inputs=[], _stateful_parallelism=false, body=while_body_8_rewritten[], cond=while_cond_7_rewritten[], output_shapes=[[], [], [], [], []], parallel_iterations=10](while/loop_counter, while/maximum_iterations, Const, ReadVariableOp, gradients/while_grad/Placeholder_1_0/accumulator:0)}}' was changed by setting attribute after it was run by a session. This mutation will have no effect, and will trigger an error in the future. Either don't modify nodes after running them or create a new session.
Traceback (most recent call last):
  File "/Users/az/miniforge3/lib/python3.9/site-packages/tensorflow/python/client/session.py", line 1380, in _do_call
    return fn(*args)
  File "/Users/az/miniforge3/lib/python3.9/site-packages/tensorflow/python/client/session.py", line 1362, in _run_fn
    self._extend_graph()
  File "/Users/az/miniforge3/lib/python3.9/site-packages/tensorflow/python/client/session.py", line 1403, in _extend_graph
    tf_session.ExtendSession(self._session)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Node 'gradients/while_grad/while_grad': Connecting to invalid output 4 of source node while which has 4 outputs. Try using tf.compat.v1.experimental.output_all_intermediates(True).

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/az/Programmierung/playground/tf-while-v2.py", line 37, in <module>
    main()
  File "/Users/az/Programmierung/playground/tf-while-v2.py", line 31, in main
    session.run(no_op)
  File "/Users/az/miniforge3/lib/python3.9/site-packages/tensorflow/python/client/session.py", line 970, in run
    result = self._run(None, fetches, feed_dict, options_ptr,
  File "/Users/az/miniforge3/lib/python3.9/site-packages/tensorflow/python/client/session.py", line 1193, in _run
    results = self._do_run(handle, final_targets, final_fetches,
  File "/Users/az/miniforge3/lib/python3.9/site-packages/tensorflow/python/client/session.py", line 1373, in _do_run
    return self._do_call(_run_fn, feeds, fetches, targets, options,
  File "/Users/az/miniforge3/lib/python3.9/site-packages/tensorflow/python/client/session.py", line 1399, in _do_call
    raise type(e)(node_def, op, message)  # pylint: disable=no-value-for-parameter
tensorflow.python.framework.errors_impl.InvalidArgumentError: Node 'gradients/while_grad/while_grad': Connecting to invalid output 4 of source node while which has 4 outputs. Try using tf.compat.v1.experimental.output_all_intermediates(True).
```

My hypothesis is that the first session call `session.run(tf.compat.v1.global_variables_initializer())` will somehow freeze the while loop function graph (which is strange though as it would not depend on it), and then the further code which adds the gradients causes this warning `Operation ... StatelessWhile ... was changed by setting attribute after it was run by a session`.

I don't really understand why the first session call does that even though it is independent from the loop.

I don't really understand why it causes the error for the NoOp execution.

Is there any workaround?