Repository: tensorflow
Issue:  TypeError: this __dict__ descriptor does not support '_DictWrapper' objects during trivial model save
Labels: stat:awaiting tensorflower,type:bug,comp:model,TF 2.16

### Issue type

Bug

### Have you reproduced the bug with TensorFlow Nightly?

No (bug doesn't exist in tf-nightly 2.17.0.dev20240312)

### Source

source

### TensorFlow version

v2.16.1

### Custom code

Yes

### OS platform and distribution

OSX

### Mobile device

_No response_

### Python version

3.12

### Bazel version

_No response_

### GCC/compiler version

_No response_

### CUDA/cuDNN version

_No response_

### GPU model and memory

_No response_

### Current behavior?

When calling ` tf.saved_model.save(model, saved_model_path)`

we see:

```
../../../../miniforge3/envs/local--bighead--v0.0.1/lib/python3.12/site-packages/tensorflow/python/saved_model/save.py:190: in list_children
    for name, child in super(_AugmentedGraphView, self).list_children(
../../../../miniforge3/envs/local--bighead--v0.0.1/lib/python3.12/site-packages/tensorflow/python/checkpoint/graph_view.py:75: in list_children
    for name, ref in super(ObjectGraphView,
../../../../miniforge3/envs/local--bighead--v0.0.1/lib/python3.12/site-packages/tensorflow/python/checkpoint/trackable_view.py:85: in children
    ref = converter.convert_to_trackable(ref, parent=obj)
../../../../miniforge3/envs/local--bighead--v0.0.1/lib/python3.12/site-packages/tensorflow/python/trackable/converter.py:31: in convert_to_trackable
    if (tensor_util.is_tf_type(obj) and
../../../../miniforge3/envs/local--bighead--v0.0.1/lib/python3.12/site-packages/tensorflow/python/framework/tensor_util.py:1156: in is_tf_type
    return isinstance(x, tf_type_classes)
../../../../miniforge3/envs/local--bighead--v0.0.1/lib/python3.12/typing.py:1871: in __instancecheck__
    val = getattr_static(instance, attr)
../../../../miniforge3/envs/local--bighead--v0.0.1/lib/python3.12/inspect.py:1839: in getattr_static
    instance_result = _check_instance(obj, attr)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

obj = DictWrapper({'input_shape': [(None, 16), (None, 32)]}), attr = 'is_tensor_like'

    def _check_instance(obj, attr):
        instance_dict = {}
        try:
>           instance_dict = object.__getattribute__(obj, "__dict__")
E           TypeError: this __dict__ descriptor does not support '_DictWrapper' objects

../../../../miniforge3/envs/local--bighead--v0.0.1/lib/python3.12/inspect.py:1793: TypeError
```

I suspect this is related to #59869 which was supposedly fixed. However, in 2.16.1 TF removes the pin on wrapt and the issue indeed persists. I've even tried downgrading wrapt to 1.14.1 and the issue remains.

### Standalone code to reproduce the issue

```shell
import tensorflow as tf
from tensorflow.keras import layers
import tempfile

def get_two_tower_models():
    online_features = [layers.Input(shape=(32,)), layers.Input(shape=(16,))]
    offline_features = [layers.Input(shape=(16,)), layers.Input(shape=(32,))]
    all_features = []
    all_features.extend(online_features)
    all_features.extend(offline_features)

    def get_offline_tower(offline_features):
        offline_inputs = layers.concatenate(offline_features, name="offline_concatenated")
        offline_hidden = layers.Dense(32, activation="tanh", name="offline_hidden_1")(offline_inputs)
        offline_hidden = layers.Dense(16, activation="tanh", name="offline_hidden_2")(offline_hidden)
        offline_final_embed = layers.Dense(8, name="offline_hidden_3")(offline_hidden)

        return offline_final_embed

    def get_online_tower(online_features):
        online_inputs = layers.concatenate(online_features, name="online_concatenated")
        online_hidden = layers.Dense(32, activation="tanh", name="online_hidden_1")(online_inputs)
        online_hidden = layers.Dense(16, activation="tanh", name="online_hidden_2")(online_hidden)
        online_final_embed = layers.Dense(8, name="online_hidden_3")(online_hidden)

        return online_final_embed

    offline_tower_embed = get_offline_tower(offline_features)
    online_tower_embed = get_online_tower(online_features)

    # We normalize vectors with L2 norm to make sure we get the cosine similarity
    offline_online_dot = layers.Dot(axes=1, normalize=True)([offline_tower_embed, online_tower_embed])

    offline_tower_model = tf.keras.Model(inputs=offline_features, outputs=offline_tower_embed)
    online_tower_model = tf.keras.Model(inputs=online_features, outputs=online_tower_embed)

    full_model = tf.keras.Model(inputs=all_features, outputs=offline_online_dot)

    return (full_model, offline_tower_model, online_tower_model)

full_model, offline_tower_model, online_tower_model = get_two_tower_models()

with tempfile.TemporaryDirectory() as tmpdirname:
    tf.saved_model.save(online_tower_model, tmpdirname)
```
```


### Relevant log output

```shell
Traceback (most recent call last):
  File "/Users/alfredo_luque/repos/git.musta.ch/airbnb/bighead-service/packages/ml-frameworks/tensorflow/tests/minimal_repro.py", line 44, in <module>
    tf.saved_model.save(online_tower_model, tmpdirname)
  File "/Users/alfredo_luque/miniforge3/envs/local--bighead--v0.0.1/lib/python3.12/site-packages/tensorflow/python/saved_model/save.py", line 1392, in save
    save_and_return_nodes(obj, export_dir, signatures, options)
  File "/Users/alfredo_luque/miniforge3/envs/local--bighead--v0.0.1/lib/python3.12/site-packages/tensorflow/python/saved_model/save.py", line 1427, in save_and_return_nodes
    _build_meta_graph(obj, signatures, options, meta_graph_def))
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alfredo_luque/miniforge3/envs/local--bighead--v0.0.1/lib/python3.12/site-packages/tensorflow/python/saved_model/save.py", line 1642, in _build_meta_graph
    return _build_meta_graph_impl(obj, signatures, options, meta_graph_def)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alfredo_luque/miniforge3/envs/local--bighead--v0.0.1/lib/python3.12/site-packages/tensorflow/python/saved_model/save.py", line 1564, in _build_meta_graph_impl
    saveable_view = _SaveableView(augmented_graph_view, options)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alfredo_luque/miniforge3/envs/local--bighead--v0.0.1/lib/python3.12/site-packages/tensorflow/python/saved_model/save.py", line 285, in __init__
    checkpoint_util.objects_ids_and_slot_variables_and_paths(
  File "/Users/alfredo_luque/miniforge3/envs/local--bighead--v0.0.1/lib/python3.12/site-packages/tensorflow/python/checkpoint/util.py", line 160, in objects_ids_and_slot_variables_and_paths
    trackable_objects, node_paths = graph_view.breadth_first_traversal()
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alfredo_luque/miniforge3/envs/local--bighead--v0.0.1/lib/python3.12/site-packages/tensorflow/python/checkpoint/graph_view.py", line 124, in breadth_first_traversal
    return self._breadth_first_traversal()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alfredo_luque/miniforge3/envs/local--bighead--v0.0.1/lib/python3.12/site-packages/tensorflow/python/saved_model/save.py", line 156, in _breadth_first_traversal
    super(_AugmentedGraphView, self)._breadth_first_traversal())
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alfredo_luque/miniforge3/envs/local--bighead--v0.0.1/lib/python3.12/site-packages/tensorflow/python/checkpoint/graph_view.py", line 128, in _breadth_first_traversal
    return super(ObjectGraphView, self)._descendants_with_paths()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alfredo_luque/miniforge3/envs/local--bighead--v0.0.1/lib/python3.12/site-packages/tensorflow/python/checkpoint/trackable_view.py", line 111, in _descendants_with_paths
    for name, dependency in self.children(current_trackable).items():
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alfredo_luque/miniforge3/envs/local--bighead--v0.0.1/lib/python3.12/site-packages/tensorflow/python/checkpoint/graph_view.py", line 97, in children
    for name, ref in self.list_children(obj, **kwargs):
  File "/Users/alfredo_luque/miniforge3/envs/local--bighead--v0.0.1/lib/python3.12/site-packages/tensorflow/python/saved_model/save.py", line 190, in list_children
    for name, child in super(_AugmentedGraphView, self).list_children(
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alfredo_luque/miniforge3/envs/local--bighead--v0.0.1/lib/python3.12/site-packages/tensorflow/python/checkpoint/graph_view.py", line 75, in list_children
    for name, ref in super(ObjectGraphView,
                     ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alfredo_luque/miniforge3/envs/local--bighead--v0.0.1/lib/python3.12/site-packages/tensorflow/python/checkpoint/trackable_view.py", line 85, in children
    ref = converter.convert_to_trackable(ref, parent=obj)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alfredo_luque/miniforge3/envs/local--bighead--v0.0.1/lib/python3.12/site-packages/tensorflow/python/trackable/converter.py", line 31, in convert_to_trackable
    if (tensor_util.is_tf_type(obj) and
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alfredo_luque/miniforge3/envs/local--bighead--v0.0.1/lib/python3.12/site-packages/tensorflow/python/framework/tensor_util.py", line 1156, in is_tf_type
    return isinstance(x, tf_type_classes)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alfredo_luque/miniforge3/envs/local--bighead--v0.0.1/lib/python3.12/typing.py", line 1871, in __instancecheck__
    val = getattr_static(instance, attr)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alfredo_luque/miniforge3/envs/local--bighead--v0.0.1/lib/python3.12/inspect.py", line 1839, in getattr_static
    instance_result = _check_instance(obj, attr)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alfredo_luque/miniforge3/envs/local--bighead--v0.0.1/lib/python3.12/inspect.py", line 1793, in _check_instance
    instance_dict = object.__getattribute__(obj, "__dict__")
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: this __dict__ descriptor does not support '_DictWrapper' objects
```