Fix GraphUpdate from_config for nested serialized layers (improves Keras / tfmot compatibility)#923
Open
ur-miya wants to merge 1 commit into
Open
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR makes several TF‑GNN Keras layers (
GraphUpdate,EdgeSetUpdate,NodeSetUpdate,ContextUpdate) more robust when reconstructed from serialized configs, especially in setups where tooling such astfmot.quantization.kerasserializes nested layers as config dicts instead of already-instantiatedLayerobjects.The constructors and
_check_is_layer(...)contract are unchanged; onlyfrom_config(...)paths are made tolerant to seeing serialized sub‑layer configs and normalize them back totf.keras.layers.Layerinstances before calling__init__.Problem
In some environments (e.g. TF 2.16 +
tf-keras+ tfmot QAT), the following pattern can occur:GraphUpdate) is cloned/serialized via Keras (clone_model,model.to_json, etc.).EdgeSetUpdate,NodeSetUpdate.next_state, context/node/edge inputs) are stored in the config as dicts of the form{class_name, config, ...}rather than asLayerinstances.GraphUpdate.from_config(config)/EdgeSetUpdate.from_config(config). The corresponding__init__implementations still expect actualLayerobjects and call_check_is_layer(...), leading to errors like:This makes it difficult to combine TF‑GNN models with tooling that relies on cloning and graph transformations via Keras configs (e.g. QAT with tfmot).
Approach
The idea is to leave
_check_is_layer(...)and the type guarantees in the constructors unchanged, and instead makefrom_config(...)smart enough to recognize serialized Keras layers and deserialize them back intoLayerinstances.from_config(...)methods:GraphUpdate
Keep the existing
du.pop_by_prefixlogic:After that, run
_maybe_deserialize_layeron:config["edge_sets"]andconfig["node_sets"];config["context"](if present).EdgeSetUpdate
from_configthat:_maybe_deserialize_layertoconfig["next_state"](if present);cls(**config).NodeSetUpdate
from_configto:du.pop_by_prefix(config, "edge_set_inputs/")as before;_maybe_deserialize_layeron eachedge_set_inputs[...];_maybe_deserialize_layeronnext_state(if present).ContextUpdate
from_configto:du.pop_by_prefixonnode_set_inputs/*andedge_set_inputs/*;_maybe_deserialize_layeron eachnode_set_inputs[...]andedge_set_inputs[...];_maybe_deserialize_layeronnext_state(if present).In all cases,
_check_is_layer(...)in the constructors is unchanged and will still reject non‑Layer values that could not be normalized.Rationale / why this is safe
tf.keras.layers.Layerinstances and_check_is_layer(...)continues to enforce that.from_config(...)code paths are touched, i.e. reconstruction from serialized configs (load_model,clone_model, etc.), not ordinary model building._maybe_deserialize_layer(...)is conservative:objis already aLayer, it is returned as‑is;objis a dict withclass_nameandconfig, it matches the standard Keras serialization format and is deserialized viatf.keras.layers.deserialize;objis left unchanged and_check_is_layer(...)will still raise if it is not aLayer.The pattern is applied consistently across
GraphUpdate,EdgeSetUpdate,NodeSetUpdateandContextUpdate, which makes future maintenance and reasoning about serialization behavior easier.Tests
New tests in
tensorflow_gnn/keras/layers/graph_update_test.py:GraphUpdateSerializationTest.test_graph_update_from_config_deserializes_nested_layersGraphUpdatewith nestedEdgeSetUpdate/NodeSetUpdateusing TF‑GNN layers (includingPoolandNextStateFromConcat), callsget_config()andGraphUpdate.from_config(config), and asserts the rebuilt instance is aGraphUpdatewhoseget_config()still contains keys like"edge_sets/edge"and"node_sets/node".GraphUpdateSerializationTest.test_edge_set_update_from_config_deserializes_next_stateEdgeSetUpdate.from_config(...)correctly restoresnext_stateand thatget_config()on the rebuilt layer includes"next_state".GraphUpdateSerializationTest.test_node_set_update_from_config_deserializes_nested_layersNodeSetUpdate.from_config(...)correctly restoresedge_set_inputsandnext_state, and thatget_config()on the rebuilt layer includes"edge_set_inputs/edge"and"next_state".Existing tests in
graph_update_test.pycontinue to pass.All tests were run under a fresh virtualenv with TensorFlow 2.16+,
tensorflow-gnn1.0.3 andtf-keras, with: