def _check_target_program(self, benchmark): """Checks that a benchmark's target program is consistent with its examples. Args: benchmark: A Benchmark to verify. """ self.assertIsNotNone(benchmark.target_program) for example in benchmark.examples: # Turn inputs into constant tensors and assign them to variables using a # new global namespace. global_namespace = {'tf': tf} input_names_to_objects = value_search._input_names_to_objects( example.inputs) for input_name, input_object in input_names_to_objects.items(): input_value = value_module.InputValue(input_object, name='dummy_name') global_namespace[input_name] = input_value.value # Evaluate the target program, which uses the canonical variables. target_program_output = eval(benchmark.target_program, global_namespace) # pylint: disable=eval-used # Check that the two outputs have equal string representation. expected_output = tf_coder_utils.convert_to_tensor(example.output) self.assertEqual( tf_coder_utils.object_to_string(expected_output), tf_coder_utils.object_to_string(target_program_output))
def test_object_to_string_sequence(self): # `Named` is a class and should be capitalized. It is only used in this test # so it is declared here, not in the global scope. Named = collections.namedtuple('Named', ('a', 'b')) # pylint: disable=invalid-name sequence = [123, tf.constant([1, 2]), (), Named(a=False, b=1.5)] self.assertEqual(tf_coder_utils.object_to_string(sequence), 'seq[123, tf.int32:[1, 2], seq[], seq[False, 1.5]]')
def __repr__(self): """Returns a string representation of the value. Values are considered equal if and only if their string representations (as computed by this function) are equal. """ if self._repr_cache is None: self._repr_cache = tf_coder_utils.object_to_string(self.value) return self._repr_cache
def test_object_to_string_raises_if_unsupported(self): with self.assertRaises(ValueError): tf_coder_utils.object_to_string({'key': 'value'})
def test_object_to_string_dtype(self): self.assertEqual(tf_coder_utils.object_to_string(tf.int32), 'tf.int32')
def test_object_to_string_primitive(self, primitive, expected_result): self.assertEqual(tf_coder_utils.object_to_string(primitive), expected_result)
def test_object_to_string_tensor(self): tensor = tf.constant([[1, 2], [3, 4]]) self.assertEqual(tf_coder_utils.object_to_string(tensor), 'tf.int32:[[1, 2], [3, 4]]')