def testGetTensorFromInfoRaisesErrors(self): expected = array_ops.placeholder(dtypes.float32, 1, name="x") tensor_info = utils.build_tensor_info(expected) tensor_info.name = "blah:0" # Nonexistent name. with self.assertRaises(KeyError): utils.get_tensor_from_tensor_info(tensor_info) tensor_info.ClearField("name") # Malformed (missing encoding). with self.assertRaises(ValueError): utils.get_tensor_from_tensor_info(tensor_info)
def testGetTensorFromInfoRaisesErrors(self): expected = array_ops.placeholder(dtypes.float32, 1, name="x") tensor_info = utils.build_tensor_info(expected) tensor_info.name = "blah:0" # Nonexistant name. with self.assertRaises(KeyError): utils.get_tensor_from_tensor_info(tensor_info) tensor_info.ClearField("name") # Malformed (missing encoding). with self.assertRaises(ValueError): utils.get_tensor_from_tensor_info(tensor_info)
def testGetTensorFromInfoRagged(self): expected = ragged_factory_ops.constant([[1, 2], [3]], name="x") tensor_info = utils.build_tensor_info(expected) actual = utils.get_tensor_from_tensor_info(tensor_info) self.assertIsInstance(actual, ragged_tensor.RaggedTensor) self.assertEqual(expected.values.name, actual.values.name) self.assertEqual(expected.row_splits.name, actual.row_splits.name)
def testGetTensorFromInfoSparse(self): expected = array_ops.sparse_placeholder(dtypes.float32, name="x") tensor_info = utils.build_tensor_info(expected) actual = utils.get_tensor_from_tensor_info(tensor_info) self.assertIsInstance(actual, sparse_tensor.SparseTensor) self.assertEqual(expected.values.name, actual.values.name) self.assertEqual(expected.indices.name, actual.indices.name) self.assertEqual(expected.dense_shape.name, actual.dense_shape.name)
def testGetTensorFromInfoSparse(self): expected = array_ops.sparse_placeholder(dtypes.float32, name="x") tensor_info = utils.build_tensor_info(expected) actual = utils.get_tensor_from_tensor_info(tensor_info) self.assertIsInstance(actual, sparse_tensor.SparseTensor) self.assertEqual(expected.values.name, actual.values.name) self.assertEqual(expected.indices.name, actual.indices.name) self.assertEqual(expected.dense_shape.name, actual.dense_shape.name)
def testGetTensorFromInfoInOtherGraph(self): with ops.Graph().as_default() as expected_graph: expected = array_ops.placeholder(dtypes.float32, 1, name="right") tensor_info = utils.build_tensor_info(expected) with ops.Graph().as_default(): # Some other graph. array_ops.placeholder(dtypes.float32, 1, name="other") actual = utils.get_tensor_from_tensor_info(tensor_info, graph=expected_graph) self.assertIsInstance(actual, ops.Tensor) self.assertIs(actual.graph, expected_graph) self.assertEqual(expected.name, actual.name)
def testGetTensorFromInfoInOtherGraph(self): with ops.Graph().as_default() as expected_graph: expected = array_ops.placeholder(dtypes.float32, 1, name="right") tensor_info = utils.build_tensor_info(expected) with ops.Graph().as_default(): # Some other graph. array_ops.placeholder(dtypes.float32, 1, name="other") actual = utils.get_tensor_from_tensor_info(tensor_info, graph=expected_graph) self.assertIsInstance(actual, ops.Tensor) self.assertIs(actual.graph, expected_graph) self.assertEqual(expected.name, actual.name)
def testGetTensorFromInfoInScope(self): # Build a TensorInfo with name "bar/x:0". with ops.Graph().as_default(): with ops.name_scope("bar"): unscoped = array_ops.placeholder(dtypes.float32, 1, name="x") tensor_info = utils.build_tensor_info(unscoped) self.assertEqual("bar/x:0", tensor_info.name) # Build a graph with node "foo/bar/x:0", akin to importing into scope foo. with ops.Graph().as_default(): with ops.name_scope("foo"): with ops.name_scope("bar"): expected = array_ops.placeholder(dtypes.float32, 1, name="x") self.assertEqual("foo/bar/x:0", expected.name) # Test that tensor is found by prepending the import scope. actual = utils.get_tensor_from_tensor_info(tensor_info, import_scope="foo") self.assertEqual(expected.name, actual.name)
def testGetTensorFromInfoInScope(self): # Build a TensorInfo with name "bar/x:0". with ops.Graph().as_default(): with ops.name_scope("bar"): unscoped = array_ops.placeholder(dtypes.float32, 1, name="x") tensor_info = utils.build_tensor_info(unscoped) self.assertEqual("bar/x:0", tensor_info.name) # Build a graph with node "foo/bar/x:0", akin to importing into scope foo. with ops.Graph().as_default(): with ops.name_scope("foo"): with ops.name_scope("bar"): expected = array_ops.placeholder(dtypes.float32, 1, name="x") self.assertEqual("foo/bar/x:0", expected.name) # Test that tensor is found by prepending the import scope. actual = utils.get_tensor_from_tensor_info(tensor_info, import_scope="foo") self.assertEqual(expected.name, actual.name)
def _ensure_servable(input_tensors, names_to_output_tensor_infos): """Check that the signature outputs don't depend on unreachable placeholders. Args: input_tensors: An iterable of `Tensor`s specified as the signature's inputs. names_to_output_tensor_infos: An mapping from output names to respective `TensorInfo`s corresponding to the signature's output tensors. Raises: ValueError: If any of the signature's outputs depend on placeholders not provided as signature's inputs. """ plain_input_tensors = nest.flatten(input_tensors, expand_composites=True) graph = op_selector.get_unique_graph(plain_input_tensors) output_tensors = [ utils.get_tensor_from_tensor_info(tensor, graph=graph) for tensor in names_to_output_tensor_infos.values() ] plain_output_tensors = nest.flatten(output_tensors, expand_composites=True) dependency_ops = op_selector.get_backward_walk_ops( plain_output_tensors, stop_at_ts=plain_input_tensors) fed_tensors = object_identity.ObjectIdentitySet(plain_input_tensors) for dependency_op in dependency_ops: if _must_be_fed(dependency_op) and (not all( output in fed_tensors for output in dependency_op.outputs)): input_tensor_names = [tensor.name for tensor in plain_input_tensors] output_tensor_keys = list(names_to_output_tensor_infos.keys()) output_tensor_names = [tensor.name for tensor in plain_output_tensors] dependency_path = op_selector.show_path(dependency_op, plain_output_tensors, plain_input_tensors) raise ValueError( f'The signature\'s input tensors {input_tensor_names} are ' f'insufficient to compute its output keys {output_tensor_keys} ' f'(respectively, tensors {output_tensor_names}) because of the ' f'dependency on `{dependency_op.name}` which is not given as ' 'a signature input, as illustrated by the following dependency path: ' f'{dependency_path}')
def testGetTensorFromInfoDense(self): expected = array_ops.placeholder(dtypes.float32, 1, name="x") tensor_info = utils.build_tensor_info(expected) actual = utils.get_tensor_from_tensor_info(tensor_info) self.assertIsInstance(actual, ops.Tensor) self.assertEqual(expected.name, actual.name)
def testGetTensorFromInfoDense(self): expected = array_ops.placeholder(dtypes.float32, 1, name="x") tensor_info = utils.build_tensor_info(expected) actual = utils.get_tensor_from_tensor_info(tensor_info) self.assertIsInstance(actual, ops.Tensor) self.assertEqual(expected.name, actual.name)