Skip to content

Commit b607fa3

Browse files
ratgrtensorflower-gardener
authored andcommitted
The fully_commutes_with_sum property was incorrectly using sum() on the flattened commuting structure, which does not yield a boolean. Changed to use all() to ensure the property returns True only if all sub-encoders fully commute with sum. Added type assertions in tests to verify the return type is boolean.
PiperOrigin-RevId: 924707489
1 parent 6fbc2ce commit b607fa3

2 files changed

Lines changed: 10 additions & 4 deletions

File tree

tensorflow_model_optimization/python/core/internal/tensor_encoding/core/gather_encoder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ def input_tensorspec(self):
370370
@property
371371
def fully_commutes_with_sum(self):
372372
# If any element is not True, the whole thing does not fully commute.
373-
return sum(tf.nest.flatten(self._commuting_structure))
373+
return all(tf.nest.flatten(self._commuting_structure))
374374

375375
@property
376376
def state_update_aggregation_modes(self):

tensorflow_model_optimization/python/core/internal/tensor_encoding/core/gather_encoder_test.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,13 @@ def test_full_commutativity_with_sum(self):
197197
encoder = gather_encoder.GatherEncoder.from_encoder(
198198
core_encoder.EncoderComposer(test_utils.TimesTwoEncodingStage()).make(),
199199
spec)
200-
self.assertTrue(encoder.fully_commutes_with_sum)
200+
self.assertIs(encoder.fully_commutes_with_sum, True)
201201

202202
encoder = gather_encoder.GatherEncoder.from_encoder(
203203
core_encoder.EncoderComposer(
204204
test_utils.TimesTwoEncodingStage()).add_parent(
205205
test_utils.TimesTwoEncodingStage(), T2_VALS).make(), spec)
206-
self.assertTrue(encoder.fully_commutes_with_sum)
206+
self.assertIs(encoder.fully_commutes_with_sum, True)
207207

208208
encoder = core_encoder.EncoderComposer(
209209
test_utils.SignIntFloatEncodingStage())
@@ -212,7 +212,13 @@ def test_full_commutativity_with_sum(self):
212212
encoder.add_child(test_utils.TimesTwoEncodingStage(), SIF_FLOATS).add_child(
213213
test_utils.PlusOneOverNEncodingStage(), T2_VALS)
214214
encoder = gather_encoder.GatherEncoder.from_encoder(encoder.make(), spec)
215-
self.assertFalse(encoder.fully_commutes_with_sum)
215+
self.assertIs(encoder.fully_commutes_with_sum, False)
216+
217+
encoder = core_encoder.EncoderComposer(
218+
test_utils.TimesTwoEncodingStage())
219+
encoder.add_child(test_utils.PlusOneEncodingStage(), T2_VALS)
220+
encoder = gather_encoder.GatherEncoder.from_encoder(encoder.make(), spec)
221+
self.assertIs(encoder.fully_commutes_with_sum, False)
216222

217223
@tf_test_util.run_all_in_graph_and_eager_modes
218224
def test_state_aggregation_modes(self):

0 commit comments

Comments
 (0)