From 33ff3c2954e74db37893d150c9b4e1d7b019157a Mon Sep 17 00:00:00 2001 From: Siyuan Liu Date: Thu, 26 Oct 2023 13:54:18 -0700 Subject: [PATCH] Mangle root scope TF variable name during tf.saved_model export (#5738) * mangle tf root scope name --- torch_xla/tf_saved_model_integration.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/torch_xla/tf_saved_model_integration.py b/torch_xla/tf_saved_model_integration.py index 188653abc3f..511b9e02e9b 100644 --- a/torch_xla/tf_saved_model_integration.py +++ b/torch_xla/tf_saved_model_integration.py @@ -58,6 +58,19 @@ def _make_input_signatures( shape=spec.shape, dtype=getattr(tf, spec.dtype), name=f'args_{i}') +def _mangle_tf_root_scope_name(name): + # TF has more restricted constrain on the variable names at root scope. + # Root scope name constrain: [A-Za-z0-9.][A-Za-z0-9_.\\-/]* + # Non-root scope name constrain: [A-Za-z0-9_.\\-/]* + # https://github.com/tensorflow/tensorflow/blob/51b601fa6bb7e801c0b6ae73c25580e40a8b5745/tensorflow/python/framework/ops.py#L3301-L3302 + # The state_dict key doesn't have such constrain, + # the name need to be mangled when a root-scoped TF variable is created. + if name[0] in "._\\-/": + return 'k' + name + else: + return name + + def save_stablehlo_graph_as_tf( stablehlo_program: stablehlo.StableHLOGraphModule, path: os.PathLike, @@ -81,7 +94,7 @@ def save_stablehlo_graph_as_tf( bundle = copy.deepcopy(stablehlo_program._bundle) tfm = tf.Module() bundle.state_dict = { - k: tf.Variable(v, trainable=False, name=k) + k: tf.Variable(v, trainable=False, name=_mangle_tf_root_scope_name(k)) for k, v in bundle.state_dict.items() } bundle.additional_constants = [