Skip to content

Commit 0650101

Browse files
jaingauravtensorflower-gardener
authored andcommitted
Remove unnecessary uses of object_identity
PiperOrigin-RevId: 328565845 Change-Id: I77e05eb8c264d6d8ab8f9bbf1477608aa9e00755
1 parent 977fa3e commit 0650101

5 files changed

Lines changed: 23 additions & 26 deletions

File tree

‎tensorflow/python/keras/engine/base_layer.py‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2586,10 +2586,10 @@ def _set_connectivity_metadata(self, args, kwargs, outputs):
25862586
# we copy them to avoid loss of KerasHistory metadata.
25872587
flat_outputs = nest.flatten(outputs)
25882588
flat_inputs = nest.flatten((args, kwargs))
2589-
inputs_set = object_identity.ObjectIdentitySet(flat_inputs)
2589+
input_ids_set = {id(i) for i in flat_inputs}
25902590
outputs_copy = []
25912591
for x in flat_outputs:
2592-
if x in inputs_set:
2592+
if id(x) in input_ids_set:
25932593
with backend.name_scope(self.name):
25942594
x = array_ops.identity(x)
25952595
outputs_copy.append(x)
@@ -2985,12 +2985,13 @@ def _eager_losses(self, losses):
29852985

29862986
def _dedup_weights(self, weights):
29872987
"""Dedupe weights while maintaining order as much as possible."""
2988-
output, seen_weights = [], object_identity.ObjectIdentitySet()
2988+
output, seen_ids = [], set()
29892989
for w in weights:
2990-
if w not in seen_weights:
2990+
if id(w) not in seen_ids:
29912991
output.append(w)
29922992
# Track the Variable's identity to avoid __eq__ issues.
2993-
seen_weights.add(w)
2993+
seen_ids.add(id(w))
2994+
29942995
return output
29952996

29962997
def _split_out_first_arg(self, args, kwargs):

‎tensorflow/python/keras/engine/base_layer_v1.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2378,12 +2378,13 @@ def _should_compute_mask(self):
23782378

23792379
def _dedup_weights(self, weights):
23802380
"""Dedupe weights while maintaining order as much as possible."""
2381-
output, seen_weights = [], object_identity.ObjectIdentitySet()
2381+
output, seen_ids = [], set()
23822382
for w in weights:
2383-
if w not in seen_weights:
2383+
if id(w) not in seen_ids:
23842384
output.append(w)
23852385
# Track the Variable's identity to avoid __eq__ issues.
2386-
seen_weights.add(w)
2386+
seen_ids.add(id(w))
2387+
23872388
return output
23882389

23892390
# SavedModel properties. Please see keras/saving/saved_model for details.

‎tensorflow/python/keras/layers/wrappers_test.py‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
from tensorflow.python.platform import test
4545
from tensorflow.python.training.tracking import util as trackable_util
4646
from tensorflow.python.util import nest
47-
from tensorflow.python.util import object_identity
4847

4948

5049
class _RNNCellWithConstants(keras.layers.Layer):
@@ -130,10 +129,11 @@ def test_timedistributed_dense(self):
130129

131130
# check whether the model variables are present in the
132131
# trackable list of objects
133-
checkpointed_objects = object_identity.ObjectIdentitySet(
134-
trackable_util.list_objects(model))
132+
checkpointed_object_ids = {
133+
id(o) for o in trackable_util.list_objects(model)
134+
}
135135
for v in model.variables:
136-
self.assertIn(v, checkpointed_objects)
136+
self.assertIn(id(v), checkpointed_object_ids)
137137

138138
def test_timedistributed_static_batch_size(self):
139139
model = keras.models.Sequential()
@@ -492,10 +492,11 @@ def test_bidirectional(self, mode):
492492

493493
# check whether the model variables are present in the
494494
# trackable list of objects
495-
checkpointed_objects = object_identity.ObjectIdentitySet(
496-
trackable_util.list_objects(model))
495+
checkpointed_object_ids = {
496+
id(o) for o in trackable_util.list_objects(model)
497+
}
497498
for v in model.variables:
498-
self.assertIn(v, checkpointed_objects)
499+
self.assertIn(id(v), checkpointed_object_ids)
499500

500501
# test compute output shape
501502
ref_shape = model.layers[-1].output.shape
@@ -1030,10 +1031,11 @@ def test_custom_backward_layer(self, mode):
10301031

10311032
# check whether the model variables are present in the
10321033
# trackable list of objects
1033-
checkpointed_objects = object_identity.ObjectIdentitySet(
1034-
trackable_util.list_objects(model))
1034+
checkpointed_object_ids = {
1035+
id(o) for o in trackable_util.list_objects(model)
1036+
}
10351037
for v in model.variables:
1036-
self.assertIn(v, checkpointed_objects)
1038+
self.assertIn(id(v), checkpointed_object_ids)
10371039

10381040
# test compute output shape
10391041
ref_shape = model.layers[-1].output.shape

‎tensorflow/python/keras/saving/saved_model/load.py‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
from tensorflow.python.training.tracking.tracking import delete_tracking
4444
from tensorflow.python.util import compat
4545
from tensorflow.python.util import nest
46-
from tensorflow.python.util import object_identity
4746

4847
# To avoid circular dependencies between keras/engine and keras/saving,
4948
# code in keras/saving must delay imports.
@@ -179,8 +178,6 @@ def __init__(self, *args, **kwargs):
179178
# records all nodes that were generated directly/indirectly from the config,
180179
# so that they do not get recreated multiple times.
181180
self._nodes_recreated_from_config = {}
182-
self._all_nodes_recreated_from_config = (
183-
object_identity.ObjectIdentityWeakSet())
184181
# Store all node ids that have already been traversed when tracking nodes
185182
# that were recreated from the config.
186183
self._traversed_nodes_from_config = []
@@ -293,7 +290,6 @@ def _add_children_recreated_from_config(self, obj, proto, node_id):
293290
'Object: {}'.format(obj_child))
294291
self._nodes_recreated_from_config[child_id] = (
295292
obj_child, self._config_node_setter(setter))
296-
self._all_nodes_recreated_from_config.add(obj_child)
297293
self._add_children_recreated_from_config(
298294
obj_child, child_proto, child_id)
299295

@@ -363,7 +359,6 @@ def _revive_from_config(self, identifier, metadata, node_id):
363359

364360
setter = self._config_node_setter(_revive_setter)
365361
self._nodes_recreated_from_config[node_id] = obj, setter
366-
self._all_nodes_recreated_from_config.add(obj)
367362
self._add_children_recreated_from_config(
368363
obj, self._proto.nodes[node_id], node_id)
369364
return obj, setter

‎tensorflow/python/keras/utils/layer_utils.py‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
from tensorflow.python.keras.utils.conv_utils import convert_kernel
3030
from tensorflow.python.util import deprecation
3131
from tensorflow.python.util import nest
32-
from tensorflow.python.util import object_identity
3332
from tensorflow.python.util.tf_export import keras_export
3433

3534

@@ -104,7 +103,7 @@ def count_params(weights):
104103
Returns:
105104
The total number of scalars composing the weights
106105
"""
107-
unique_weights = object_identity.ObjectIdentitySet(weights)
106+
unique_weights = {id(w): w for w in weights}.values()
108107
weight_shapes = [w.shape.as_list() for w in unique_weights]
109108
standardized_weight_shapes = [
110109
[0 if w_i is None else w_i for w_i in w] for w in weight_shapes
@@ -502,4 +501,3 @@ def wrapped(item):
502501

503502
wrapped.cache = cache
504503
return wrapped
505-

0 commit comments

Comments
 (0)