Skip to content

Commit c575e8d

Browse files
ratgrtensorflower-gardener
authored andcommitted
The change ensures that the elements are packed back into the original structure in the correct order by using keys derived from the structure's flattened paths.
PiperOrigin-RevId: 924714412
1 parent b607fa3 commit c575e8d

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,12 @@ def decode_fn(encoded_structure):
134134
encoded_structure = py_utils.merge_dicts(encoded_structure,
135135
encoded_py_structure['flat_py'])
136136
encoded_structure = tf.nest.pack_sequence_as(
137-
encoded_py_structure['full'], tf.nest.flatten(encoded_structure))
137+
encoded_py_structure['full'],
138+
[
139+
encoded_structure[k] for k, _ in
140+
py_utils.flatten_with_joined_string_paths(
141+
encoded_py_structure['full'])
142+
])
138143
return encoder.decode(encoded_structure[_TENSORS],
139144
encoded_structure[_PARAMS],
140145
encoded_structure[_SHAPES])

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,37 @@ def test_python_constants_not_exposed(self):
141141
self.assertAllClose(x, decoded_x_tf)
142142
self.assertAllClose(x, decoded_x_py)
143143

144+
@tf_test_util.run_all_in_graph_and_eager_modes
145+
def test_interleaved_py_tf_parameters(self):
146+
"""Tests that interleaved Python and TF parameters are decoded correctly."""
147+
148+
class SwappedKeysStage(test_utils.SimpleLinearEncodingStage):
149+
def get_params(self, name=None):
150+
# Return parameters in a specific order to test if encoding/decoding
151+
# respects this order or sorts keys.
152+
params = {self.B_PARAM_KEY: self._b, self.A_PARAM_KEY: self._a}
153+
return params, params
154+
155+
x = tf.constant(2.0)
156+
tensorspec = tf.TensorSpec.from_tensor(x)
157+
158+
# Use one Python constant and one TF Variable to ensure they are split and
159+
# merged.
160+
b_var = tf.compat.v1.get_variable('b_var_interleaved', initializer=3.0)
161+
162+
encoder = simple_encoder.SimpleEncoder(
163+
core_encoder.EncoderComposer(
164+
SwappedKeysStage(2.0, b_var)).make(),
165+
tensorspec)
166+
167+
state = encoder.initial_state()
168+
iteration = _make_iteration_function(encoder)
169+
170+
self.evaluate(tf.compat.v1.global_variables_initializer())
171+
172+
x_val, _, decoded_x, _ = self.evaluate(iteration(x, state))
173+
self.assertAllClose(x_val, decoded_x)
174+
144175
@tf_test_util.run_all_in_graph_and_eager_modes
145176
def test_decode_needs_input_shape_static(self):
146177
"""Tests that mechanism for passing input shape works with static shape."""

0 commit comments

Comments
 (0)