Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions dgf/src/deploy/vertex.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ def _save_model_to_gcs(
model.save(tmp_dir)

# Export to TF SavedModel for Vertex AI
# TODO(gbm): Use `model.to_tensorflow_saved_model` (requires support for
# Vertex's batched instances).
tf_model = model.to_tensorflow_function(consume_tf_graph_dict=True)

batched_model = _create_batched_serving_model(tf_model)
Expand Down
1 change: 1 addition & 0 deletions dgf/src/learning/ten_lines/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ py_library(
"//dgf/src/util:filesystem",
"//dgf/src/util:log",
"//dgf/src/util:util_py",
"//dgf/src/util/weak_dep:weak_dep_tensorflow",
# jax dep,
# numpy dep,
# orbax/checkpoint dep,
Expand Down
49 changes: 49 additions & 0 deletions dgf/src/learning/ten_lines/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from dgf.src.util import filesystem as fs
from dgf.src.util import log
from dgf.src.util import util
from dgf.src.util.weak_dep.weak_dep_tensorflow import tf
import jax
import numpy as np
import orbax.checkpoint as ocp
Expand Down Expand Up @@ -75,6 +76,13 @@
# size cannot cover it within `num_train_steps`. See `select_batch_size`.
_AUTO_BATCH_SIZE_MIN_EPOCHS = 10

# Name of the output of the signature of the models exported with
# `to_tensorflow_saved_model`. Same as the TF-GNN exported models.
DEFAULT_TF_SAVED_MODEL_OUTPUT_NAME = "predictions"

# Name of the signature of the models exported with `to_tensorflow_saved_model`.
TF_SAVED_MODEL_SIGNATURE_NAME = "serving_default"


class Architecture(enum.Enum):
HETEROGENEOUS_MESSAGE_PASSING = "HETEROGENEOUS_MESSAGE_PASSING"
Expand Down Expand Up @@ -257,6 +265,34 @@ def resolve_tf_function_input_format(
return TFFunctionInputFormat.TF_GRAPH


def save_tf_module_as_saved_model(
tf_module: "tf.Module", path: str, output_name: str
) -> None:
"""Saves a `to_tensorflow_function` module as a single-signature SavedModel."""
call = tf_module.__call__
if not isinstance(call, tf.types.experimental.ConcreteFunction):
# Uses the `input_signature` of the tf.function.
try:
call = call.get_concrete_function()
except (TypeError, ValueError) as e:
raise ValueError(
"Cannot trace the `__call__` method of this TensorFlow module. "
"The method should be a concrete function or a tf.function"
" with an input_signature."
) from e
args_spec, kwargs_spec = call.structured_input_signature

@tf.function(autograph=False)
def serving_fn(*args, **kwargs):
return {output_name: call(*args, **kwargs)}

signature = serving_fn.get_concrete_function(*args_spec, **kwargs_spec)
tf.saved_model.save(
tf_module, path, signatures={TF_SAVED_MODEL_SIGNATURE_NAME: signature}
)
log.info("TensorFlow SavedModel exported to %s", path)


LogItem = jax_common.LogItem
StopReason = jax_common.StopReason

Expand Down Expand Up @@ -473,6 +509,19 @@ def to_tensorflow_function(
" export."
)

def to_tensorflow_saved_model(
self,
path: str,
*,
input_format: TFFunctionInputFormat | str = (
TFFunctionInputFormat.SERIALIZED_TFGNN_GRAPHS
),
output_name: str = DEFAULT_TF_SAVED_MODEL_OUTPUT_NAME,
) -> None:
"""Exports the model as a single-signature TensorFlow SavedModel."""
tf_module = self.to_tensorflow_function(input_format=input_format)
save_tf_module_as_saved_model(tf_module, path, output_name)

def _extract_serving_schemata(
self,
) -> tuple[dict[str, Any], dict[str, Any]]:
Expand Down
54 changes: 54 additions & 0 deletions dgf/src/learning/ten_lines/link_prediction_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1390,6 +1390,60 @@ def __call__(
else:
raise ValueError(f"Invalid encoder: {encoder}")

def to_tensorflow_saved_model(
self,
path: str,
*,
encoder: Literal["source", "target", "both"] = "both",
input_format: common.TFFunctionInputFormat | str = (
common.TFFunctionInputFormat.TF_GRAPH_DICT
),
output_name: str = common.DEFAULT_TF_SAVED_MODEL_OUTPUT_NAME,
) -> None:
"""Exports the model as a TensorFlow SavedModel without the sampling step.

The SavedModel has a single signature, "serving_default", returning a
dictionary with a single item named `output_name`. Depending on `encoder`,
this item contains the source node embeddings ("source"), the target node
embeddings ("target"), or the edge probabilities ("both").

The inputs of the signature are the ones of the function returned by
`to_tensorflow_function`, except that TensorFlow lowercases their names.
With `encoder="both"`, the inputs of the source and target graphs are
prefixed with `source_` and `target_` respectively. With the `TF_GRAPH`
input format, the inputs of the signature are flattened and anonymous:
Instead, call the loaded SavedModel directly with the same arguments as the
function returned by `to_tensorflow_function`. The `SERIALIZED_TFGNN_GRAPHS`
input format is not supported.

Usage example:

```python
model = dgf.learning.train_link_model(...)
model.to_tensorflow_saved_model("/tmp/saved_model", encoder="source")

loaded = tf.saved_model.load("/tmp/saved_model")
tf_graph_dict = dgf.convert.tf_graph_to_tf_graph_dict(
dgf.convert.graph_to_tf_graph(source_sample, schema)
)
embeddings = loaded.signatures["serving_default"](
**{key.lower(): value for key, value in tf_graph_dict.items()},
seed_node_idxs=tf.constant([0]),
)["predictions"]
```

Args:
path: Directory where the SavedModel is written.
encoder: Which part of the model to export: "source" (source encoder),
"target" (target encoder), or "both" (full model). Defaults to "both".
input_format: Format of the inputs of the signature. See
`dgf.learning.TFFunctionInputFormat` and `to_tensorflow_function`.
Defaults to `TF_GRAPH_DICT`.
output_name: Name of the output of the signature.
"""
tf_module = self.to_tensorflow_function(encoder, input_format=input_format)
common.save_tf_module_as_saved_model(tf_module, path, output_name)

def evaluate(
self,
graph: in_memory_graph.InMemoryGraph,
Expand Down
59 changes: 59 additions & 0 deletions dgf/src/learning/ten_lines/link_prediction_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,65 @@ def test_to_tensorflow_function_serialized_tfgnn_graphs_fails(self):
encoder="both", input_format="SERIALIZED_TFGNN_GRAPHS"
)

@parameterized.parameters("source", "both")
def test_to_tensorflow_saved_model(
self, encoder: Literal["source", "target", "both"]
):
schema = self.model.data().schema
source_sample = in_memory_sampler_lib.create_sampler(
plan=self.model.data().source_sampling_plan,
graph=self.graph,
schema=schema,
batch_size=5,
).sample(0)
target_sample = in_memory_sampler_lib.create_sampler(
plan=self.model.data().target_sampling_plan,
graph=self.graph,
schema=schema,
batch_size=5,
).sample(1)
source_graph_dict = tf_io.tf_graph_to_tf_graph_dict(
tf_io.graph_to_tf_graph(source_sample, schema=schema)
)
target_graph_dict = tf_io.tf_graph_to_tf_graph_dict(
tf_io.graph_to_tf_graph(target_sample, schema=schema)
)
if encoder == "source":
kwargs = {**source_graph_dict, "seed_node_idxs": tf.constant([0])}
expected = self.model.predict_embedding(
source_sample, [0], encoder="source"
)
else:
kwargs = {f"source_{k}": v for k, v in source_graph_dict.items()}
kwargs.update({f"target_{k}": v for k, v in target_graph_dict.items()})
kwargs["source_seed_node_idxs"] = tf.constant([0])
kwargs["target_seed_node_idxs"] = tf.constant([0])
expected = self.model.predict(
self.graph, [0], [1], all_combinations=False, verbose=0
)

path = self.create_tempdir().full_path
self.model.to_tensorflow_saved_model(path, encoder=encoder)
loaded = tf.saved_model.load(path)

self.assertEqual(list(loaded.signatures.keys()), ["serving_default"])
signature = loaded.signatures["serving_default"]
self.assertEqual(list(signature.structured_outputs), ["predictions"])
# Note: TensorFlow lowercases the names of the signature inputs.
predictions = signature(**{k.lower(): v for k, v in kwargs.items()})[
"predictions"
]
np.testing.assert_allclose(predictions.numpy(), expected, atol=1e-5)

def test_to_tensorflow_saved_model_serialized_tfgnn_graphs_fails(self):
with self.assertRaisesRegex(
ValueError, "not supported by link prediction models"
):
self.model.to_tensorflow_saved_model(
self.create_tempdir().full_path,
input_format="SERIALIZED_TFGNN_GRAPHS",
)


class LinkPredictionToyStandaloneTest(absltest.TestCase):

Expand Down
36 changes: 36 additions & 0 deletions dgf/src/learning/ten_lines/node_embedding_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,42 @@ def test_to_tensorflow_function_serialized_tfgnn_graphs(self):
self.assertTrue(any("examples" in name for name in input_names))
np.testing.assert_allclose(loaded_embeddings.numpy(), expected, atol=1e-5)

def test_to_tensorflow_saved_model(self):
schema = self.model.data().schema
sampling_plan = self.model.data().sampling_plan
assert sampling_plan is not None
sampler = in_memory_sampler_lib.create_sampler(
graph=self.graph,
plan=sampling_plan,
schema=schema,
batch_size=5,
)
samples = [sampler.sample(0), sampler.sample(1)]
serialized_samples = tf.constant([
tf_graph_sample_lib.graph_to_tfgnn_graph(
sample, schema=schema
).SerializeToString()
for sample in samples
])
expected = np.concatenate(
[self.model.predict(sample, [0], verbose=0) for sample in samples],
axis=0,
)

path = self.create_tempdir().full_path
self.model.to_tensorflow_saved_model(path, output_name="embeddings")
loaded = tf.saved_model.load(path)

self.assertEqual(list(loaded.signatures.keys()), ["serving_default"])
signature = loaded.signatures["serving_default"]
self.assertEqual(
signature.structured_input_signature,
((), {"examples": tf.TensorSpec([None], tf.string, name="examples")}),
)
self.assertEqual(list(signature.structured_outputs), ["embeddings"])
embeddings = signature(examples=serialized_samples)["embeddings"]
np.testing.assert_allclose(embeddings.numpy(), expected, atol=1e-5)

def test_to_tensorflow_function_deprecated_consume_tf_graph_dict(self):
schema = self.model.data().schema
sampling_plan = self.model.data().sampling_plan
Expand Down
54 changes: 54 additions & 0 deletions dgf/src/learning/ten_lines/node_prediction_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,60 @@ def predict_one_example(serialized_example: tf.Tensor) -> tf.Tensor:

return wrapper

def to_tensorflow_saved_model(
self,
path: str,
*,
input_format: common.TFFunctionInputFormat | str = (
common.TFFunctionInputFormat.SERIALIZED_TFGNN_GRAPHS
),
output_name: str = common.DEFAULT_TF_SAVED_MODEL_OUTPUT_NAME,
) -> None:
"""Exports the model as a TensorFlow SavedModel without the sampling step.

The SavedModel has a single signature, "serving_default", returning a
dictionary with a single item named `output_name` containing the
predictions. The inputs of the signature are the ones of the function
returned by `to_tensorflow_function`.

With the default `SERIALIZED_TFGNN_GRAPHS` input format, the signature has
the same interface as the TF-GNN exported models: It consumes a 1D string
tensor named `examples` of serialized TF GNN Graph Samples, and generates
one prediction per graph sample, on the first node of the target nodeset.
Such a SavedModel can be used with TFX BulkInferrer, TF Serving, or
Uniserve.

With the `TF_GRAPH_DICT` input format, TensorFlow lowercases the input names
of the signature. With the `TF_GRAPH` input format, the inputs of the
signature are flattened and anonymous: Instead, call the loaded SavedModel
directly, i.e., `loaded(graph=..., seed_node_idxs=...)`.

Usage example:

```python
model = dgf.learning.train_node_model(...)
model.to_tensorflow_saved_model("/tmp/saved_model")

loaded = tf.saved_model.load("/tmp/saved_model")
examples = tf.constant(
[dgf.convert.graph_to_tfgnn_graph(sample, schema).SerializeToString()]
)
predictions = loaded.signatures["serving_default"](examples=examples)[
"predictions"
]
```

Args:
path: Directory where the SavedModel is written.
input_format: Format of the inputs of the signature. See
`dgf.learning.TFFunctionInputFormat` and `to_tensorflow_function`.
Defaults to `SERIALIZED_TFGNN_GRAPHS`.
output_name: Name of the output of the signature.
"""
super().to_tensorflow_saved_model(
path, input_format=input_format, output_name=output_name
)


def normalized_schema_to_normalized_input_feature_schema(
schema: schema_lib.GraphSchema,
Expand Down
Loading
Loading