Exception in tf.function due to operation in inactive condition branch
@sachinprasadhs,
I was able to reproduce the issue on tensorflow v2.12 and tf-nightly. Kindly find the gist of it [here](https://colab.research.google.com/gist/tilakrayal/f4758eb0ec3f8d66c124cb67f0988566/untitled1193.ipynb). I found something in the docs that explained this:

![AutoGraphDocs](https://github.com/tensorflow/tensorflow/assets/95854922/0607ec3c-55c9-4a15-8f3d-79d15447c073)

![EffectsoftheTracingProcess](https://github.com/tensorflow/tensorflow/assets/95854922/1afb2b38-f7df-4d5e-a54b-ed799c6ebf2d)

https://www.tensorflow.org/guide/function

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/autograph/g3doc/reference/control_flow.md#effects-of-the-tracing-process

In a @tf.function, the code executes each branch of the conditional block whether the condition is true or not, which gives you this error. @0Waffle I appreciate the comment, but I am not sure your connclusion is accurate. As I initially posted, the issue does not happen when the shape of the input spec is left completely undefined (with `@tf.function(input_signature=[tf.TensorSpec(None, tf.int32)])`), which suggests the operation is not really being executed. I think (although I'm not completely sure) that the error may come from some shape-checking logic that is executed once the inputs are known (but which does not kick in with fully undefined shapes for some reason).

Besides that, it would be inefficient to execute both branches of the conditional in all cases. It would also be rather limiting, it was easy to work around this case, but it may not be so in other cases. It was also a pain to find the cause, as the problematic operation was much deeper in the call stack in my case.

As an example, consider this other function which works as expected in all cases, even when the inactive conditional branch cannot be executed.

```python
import tensorflow as tf

@tf.function(input_signature=[tf.TensorSpec([None], tf.int32), tf.TensorSpec([None], tf.int32)])
def f(x, y):
    return tf.cond(tf.size(y) > 0, lambda: x[y[0]], lambda: x[0])

# Works
tf.print(f(tf.constant([1, 2, 3]), tf.constant([1])))
# 2

# Works too, even though true branch of conditional cannot be executed with these inputs
tf.print(f(tf.constant([1, 2, 3]), tf.constant([], tf.int32)))
# 1
``` I'm sorry I was inaccurate. From debugging, in case None it executes lambda: x at the pywrap_tfe line, while in case [None] it executes lambda: tf.fill(...) at the pywrap_tfe line and I don't know where in the call stack it decides which one to execute since it should depend on tf.size(x)==1. It doesn't seem to be an autograph problem because it gives the error with autograph=False in the @tf.function parameters.

It gives the error with case None and jit_compile=True, so the behavior might have something to do with the way it compiles functions non-XLA.
<details><summary>Click to expand!</summary> 
 
 ### Issue Type

Bug

### Have you reproduced the bug with TF nightly?

Yes

### Source

binary

### Tensorflow Version

2.14.0-dev20230606

### Custom Code

Yes

### OS Platform and Distribution

Windows 11 x64

### Mobile device

NA

### Python version

3.10

### Bazel version

NA

### GCC/Compiler version

NA

### CUDA/cuDNN version

NA

### GPU model and memory

NA

### Current Behaviour?

A function decorated with `@tf.function` may fail to execute if it contains a conditional operation where the non-executing branch cannot be executed correctly for the current inputs. Whether the issue arises or not may depend on the input signature given to `tf.function`. See example for clarity.

### Standalone code to reproduce the issue

```shell
import tensorflow as tf

@tf.function(input_signature=[tf.TensorSpec([None], tf.int32)])
def f(x):
    return tf.cond(tf.size(x) == 1,
                   # The reshape in this branch can only execute properly when the condition is true
                   lambda: tf.fill(tf.shape(x), tf.reshape(x, ())),
                   lambda: x)

# Works: this input is valid for both condition branches
tf.print(f(tf.constant([1])))
# [1]

# Fails: this input is only valid for the false branch, which is the active one
tf.print(f(tf.constant([1, 2])))
# tensorflow.python.framework.errors_impl.InvalidArgumentError: Graph execution error: (see below)


# If the shape in the function input signature is left completely undefined it works
# If the tf.function is defined with no input signature it works correctly as well

@tf.function(input_signature=[tf.TensorSpec(None, tf.int32)])
def f(x):
    return tf.cond(tf.size(x) == 1,
                   lambda: tf.fill(tf.shape(x), tf.reshape(x, ())),
                   lambda: x)

tf.print(f(tf.constant([1])))
# [1]

tf.print(f(tf.constant([1, 2])))
# [1 2]
```


### Relevant log output

```shell
Traceback (most recent call last):
  File ...
  File "...\site-packages\tensorflow\python\util\traceback_utils.py", line 153, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "...\site-packages\tensorflow\python\eager\execute.py", line 53, in quick_execute
    tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
tensorflow.python.framework.errors_impl.InvalidArgumentError: Graph execution error:

Detected at node cond/Reshape defined at (most recent call last):
  ...

Input to reshape is a tensor with 2 values, but the requested shape has 1
         [[{{node cond/Reshape}}]] [Op:__inference_f_23]
```
</details>