From 4be7cb57dd74404720ca39bc6eceddad44e7a160 Mon Sep 17 00:00:00 2001 From: Michael Reneer Date: Wed, 31 Jul 2024 09:27:37 -0700 Subject: [PATCH] Update tests in the `aggregators` package to pass numpy values of the correct dtype to computations. 1. Some of these modules are passing tensorflow values to federated computations, instead these should pass numpy values because federated computations will no longer accept tensorflow values. 2. Some of these modules are passing values with the wrong dtype to computations resulting in the need to cast values to which can result in the loss of data. PiperOrigin-RevId: 658034756 --- .../tensorflow_executor_bindings_test.py | 13 +++++++------ tensorflow_federated/python/tests/backend_test.py | 11 ++++++++--- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/tensorflow_federated/python/core/environments/tensorflow_backend/tensorflow_executor_bindings_test.py b/tensorflow_federated/python/core/environments/tensorflow_backend/tensorflow_executor_bindings_test.py index 81787e8afe..609ffc5078 100644 --- a/tensorflow_federated/python/core/environments/tensorflow_backend/tensorflow_executor_bindings_test.py +++ b/tensorflow_federated/python/core/environments/tensorflow_backend/tensorflow_executor_bindings_test.py @@ -94,10 +94,11 @@ def test_create_value(self): ('a', computation_types.TensorType(np.int64, [3])), ('b', computation_types.TensorType(np.float32, [])), ]) - value_pb, _ = value_serialization.serialize_value( - collections.OrderedDict(a=tf.constant([1, 2, 3]), b=tf.constant(42.0)), - expected_type_spec, + value = collections.OrderedDict( + a=np.array([1, 2, 3], np.int64), + b=np.array(42.0, np.float32), ) + value_pb, _ = value_serialization.serialize_value(value, expected_type_spec) value = executor.create_value(value_pb) self.assertIsInstance(value, executor_bindings.OwnedValueId) # Assert the value ID was incremented. @@ -226,7 +227,7 @@ def test_create_struct(self): executor = get_executor() expected_type_spec = computation_types.TensorType(np.int64, [3]) value_pb, _ = value_serialization.serialize_value( - tf.constant([1, 2, 3]), expected_type_spec + np.array([1, 2, 3], np.int64), expected_type_spec ) value = executor.create_value(value_pb) self.assertEqual(value.ref, 0) @@ -264,7 +265,7 @@ def test_create_selection(self): executor = get_executor() expected_type_spec = computation_types.TensorType(np.int64, [3]) value_pb, _ = value_serialization.serialize_value( - tf.constant([1, 2, 3]), expected_type_spec + np.array([1, 2, 3], np.int64), expected_type_spec ) value = executor.create_value(value_pb) self.assertEqual(value.ref, 0) @@ -298,7 +299,7 @@ def test_create_selection(self): def test_call_with_arg(self): executor = get_executor() value_pb, _ = value_serialization.serialize_value( - tf.constant([1, 2, 3]), + np.array([1, 2, 3], np.int64), computation_types.TensorType(np.int64, [3]), ) value_ref = executor.create_value(value_pb) diff --git a/tensorflow_federated/python/tests/backend_test.py b/tensorflow_federated/python/tests/backend_test.py index 22bd0e0959..244799ffa4 100644 --- a/tensorflow_federated/python/tests/backend_test.py +++ b/tensorflow_federated/python/tests/backend_test.py @@ -314,8 +314,8 @@ def map_foo_at_clients(x): def map_foo_at_server(x): return tff.federated_map(foo, x) - bad_tensor = tf.constant([1.0] * 10, dtype=tf.float32) - good_tensor = tf.constant([1.0], dtype=tf.float32) + bad_tensor = np.array([1.0] * 10, dtype=np.float32) + good_tensor = np.array([1.0], dtype=np.float32) # Ensure running this computation at both placements, or unplaced, still # raises. with self.assertRaises(Exception): @@ -405,7 +405,12 @@ def count_one_twice(): self.assertEqual((1, 1, 1), count_one_twice()) - @tff.test.with_contexts(*test_contexts.get_all_contexts()) + @tff.test.with_contexts( + ( + 'native_sync_local', + tff.backends.native.create_sync_local_cpp_execution_context, + ), + ) def test_dynamic_lookup_table(self): @tff.tensorflow.computation(