Fix GraphUpdate from_config for nested serialized layers (improves Keras / tfmot compatibility)#923
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. |
|
Hi @ur-miya , thank you for sending a contribution to TensorFlow GNN! Your problem analysis is spot-on: As of TF/Keras version 2.13, (de)serialization to/from JSON configs has changed as part of the switch to the new Before we go any further: please be aware that, at this point, there is no known timeline for a next release of TF-GNN. That means, merging your pull request will not directly help users that rely on How does (de)serialization work for the layers passed into the various More esoterically, there is also the question of sub-objects that are shared between higher-level layers to express weight sharing: Suppose two How would you like to proceed? |
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: