Repository navigation
Description
Please make sure that this is a bug. As per our GitHub Policy, we only address code/doc bugs, performance issues, feature requests and build/installation issues on GitHub. tag:bug_template
System information
- Have I written custom code (as opposed to using a stock example script provided in TensorFlow): No
- OS Platform and Distribution (e.g., Linux Ubuntu 16.04 x86_64): Windows 10
- TensorFlow Python installation : pip install tensorflow==2.4.1
- TensorFlow Java version : 2.3.1
- Java version (i.e., the output of
java -version): Java 8 - Python version (if transferring a model trained in Python): python 3.8
- Bazel version (if compiling from source):
- GCC/Compiler version (if compiling from source):
- CUDA/cuDNN version: No Cuda
- GPU model and memory: No GPU
Describe the current behavior
I keep getting the Error No Operation named [StatefulPartitionedCall_2:0] in the Graph
when Using SavedModelBundle.exporter to save the model
ConcreteFunction serveFunction = savedModel.function("serve_model");
SavedModelBundle.exporter(exportDir)
.withFunction(serveFunction)
.export();To access and inspect Graph operations, i can see the StatefulPartitionedCall_2
But without the : at the end of the operation name.
Iterator<Operation> operationIterator = serveFunction.graph().operations();
while(operationIterator.hasNext()){
System.out.println(operationIterator.next().name());
}code snippet output
Adam/iter
Adam/iter/Read/ReadVariableOp
Adam/beta_1
Adam/beta_1/Read/ReadVariableOp
Adam/beta_2
...
...
...
train_model_labels
StatefulPartitionedCall_1
saver_filename
StatefulPartitionedCall_2
StatefulPartitionedCall_3Works fine when invoking directly the Op from session.runner()
String checkpointPath = "...";
session.runner()
.feed("saver_filename:0", checkpointPath)
.fetch("StatefulPartitionedCall_2:0").run() ;The model was saved using a regular tf.saved_model.save function
def save_module(module, model_dir):
tf.saved_model.save(module, model_dir,
signatures={
'serve_model':
module.__call__.get_concrete_function(signature_dict),
'train_model':
module.train.get_concrete_function(
signature_dict,
tf.TensorSpec(shape=[None, None], dtype=tf.int64, name="labels"))})How can I reproduce the error:
Error could be reproduced using this scripts which defines than saves the model (credits to Thierry Herrmann)
import tensorflow as tf
import tensorflow.keras as keras
from tensorflow.keras import layers
def make_model():
class CustomLayer(keras.layers.Layer):
def __init__(self, **kwargs):
super().__init__(**kwargs)
l2_reg = keras.regularizers.l2(0.1)
self.dense = layers.Dense(1, kernel_regularizer=l2_reg,
name='my_layer_dense')
def call(self, data):
return self.dense(data)
inputs = keras.Input(shape=(8,))
x1 = layers.Dense(30, activation="relu", name='my_dense')(inputs)
outputs = CustomLayer()(x1)
return keras.Model(inputs=inputs, outputs=outputs)
class CustomModule(tf.Module):
def __init__(self):
super(CustomModule, self).__init__()
self.model = make_model()
self.opt = keras.optimizers.Adam(learning_rate=0.001)
@tf.function(input_signature=[tf.TensorSpec([None, 8], tf.float32)])
def __call__(self, X):
return self.model(X)
# the my_train function processes one batch (one step): computes the loss and apply the
# loss gradient to update the model weights
@tf.function(input_signature=[tf.TensorSpec([None, 8], tf.float32), tf.TensorSpec([None], tf.float32)])
def my_train(self, X, y):
with tf.GradientTape() as tape:
logits = self.model(X, training=True)
main_loss = tf.reduce_mean(keras.losses.mean_squared_error(y, logits))
# self.model.losses contains the reularization loss (see l2_reg above)
loss_value = tf.add_n([main_loss] + self.model.losses)
grads = tape.gradient(loss_value, self.model.trainable_weights)
self.opt.apply_gradients(zip(grads, self.model.trainable_weights))
return loss_value
# instantiate the module
module = CustomModule()
def save_module(module, model_dir):
tf.saved_model.save(module, model_dir,
signatures={
'serve_model' :
module.__call__.get_concrete_function(tf.TensorSpec([None, 8], tf.float32)),
'train_model' :
module.my_train.get_concrete_function(tf.TensorSpec([None, 8], tf.float32),
tf.TensorSpec([None], tf.float32))})
MODEL_OUTPUT_DIR ="..."
save_module(module, MODEL_OUTPUT_DIR)@karllessard could you please take a quick look ? Thank you
Activity
karllessard commentedon Oct 13, 2021
Thanks for the report @HichemMaiza , I'll take a look at it. Meanwhile, out of curiosity, would you mind trying the same thing but with our actual snapshot (
0.4.0-SNAPSHOT)? Check for the instructions here. I know that there have been some rework around saved models export/imports in Java.HichemMaiza commentedon Oct 13, 2021
@karllessard Thank you for the reply. I keep getting the same error using the actual snapshot (0.4.0-SNAPSHOT).
The Api have changed a little bit, I had to make changes to the exporter builder arguments, code snippet
I have also tried to construct the exporter builder with the new added
TensorFunctioninstead ofSignature, see the following snippetkarllessard commentedon Oct 13, 2021
Ok thanks. But yes, I can imagine what is going wrong and you were probably right, the code is looking for an operation
StatefulPartitionedCallwhile the target provided is an operation outputStatefulPartitionedCall:0. That might require some updates in TF Java code.Good news is that we are targeting to release the 0.4.0 branch pretty soon and also I'll take the opportunity to update it so you don't have to do this explicit cast by returning a
SessionFunctiondirectly fromsavedModel.function.HichemMaiza commentedon Oct 13, 2021
@karllessard Great News Thank you! Meanwhile, I will update the checkpoint using a direct access approach
karllessard commentedon Oct 15, 2021
Hi @HichemMaiza , I've just created PR #388 to solve this issue, that was a very small fix. Thanks a lot for documenting the issue so well, that was very useful. I was able to reproduce the problem and now it's working with the following code:
HichemMaiza commentedon Oct 15, 2021
Hi @karllessard, thank you for your prompt response ! i've tried the new SNAPSHOT and the new export method works fine for the provided sample code. However, I'am running through another issue now,
Can not make a placeholder <Placeholder 'asset_path_initializer'> an init op. I explain, The latter model has a simple savedModel folder structure withvariablesdirectory andsaved_model.pbfile. My actual model has an extra folderassetswhich contains modelvocab.txt(Bert). Using the new SNAPSHOT i am no longer able to load the model usingHere is the complete Error Trace
How to reproduce the error. The following is a sample Python script that saves bert Model
karllessard commentedon Oct 15, 2021
Thanks @HichemMaiza , this init op exception is coming from a new feature that was added recently to the current snapshot, @rnett can you please take a look at this issue?
rnett commentedon Oct 15, 2021
Will do. My guess is that somehow, there is a placeholder in the Python's model init scope, which gets exported in the saved model. I would think this is a bug on their end, since I can't think of a way that would work, but we'll see. @HichemMaiza would it be possible for you to provide the saved model bundle?
karllessard commentedon Oct 15, 2021
@rnett, see Hichem's previous comment above, just running this script will create the bundle for you
HichemMaiza commentedon Oct 15, 2021
Thank you @karllessard. @rnett you have just to run the script above. You need just to specify the
MODEL_OUTPUT_DIR = ""and hit run ;)karllessard commentedon Oct 18, 2021
@rnett even if it is a bit unexpected to have a placeholder added to the init scope, it looks like it can happen using basic Keras like @HichemMaiza did above. Can we simply remove this validation, or maybe comment it out, to unblock any users who will face this problem?
I can take care of doing if you want, thanks.
rnett commentedon Oct 18, 2021
Yeah, that seems fine, I should be able to do it this afternoon, unless you think you can get it sooner.
karllessard commentedon Oct 21, 2021
@HichemMaiza , can you please try again with the latest snashot? You shouldn't get that placeholder init issue anymore
HichemMaiza commentedon Oct 22, 2021
@karllessard @rnett Thank you for your Help, it works with the last Update. For future Googlers pay attention to the saved signatures, if you are interested in many signatures remember to export them for the future
SavedModelBundle.load. Two signatures for me :maziyarpanahi commentedon Mar 14, 2022
Hi @karllessard
We are experiencing the same issue however in the
0.4.0release (after this fix #393 was included). Previously in0.2.xand0.3.x, we were able to load a model, save it on disk, and load it back (both models from TF Hub and HuggingFace as SavedModel).However, now in
0.4.0, we can load any SavedModel from anywhere, but once it's saved on disk we face this similar error when trying to load it back (only TF Hub models):asset_path_initializerplaceholder, they used to work in the0.2.xand0.3.xreleases without a need to feed it or deal with it in any way.asset_path_initializerplaceholder, so we don't have an issue with thoseBERT on TF Hub:
BERT on HuggingFace:
Is there anything we need to do since
0.4.xis on TensorFlow2.7.x, I am not sure if this is a change in newer TF or intensorflow-java.PS: We don't use any exporters, we saved the model differently. A very quick and simple example of how we do this:
We have been doing this since TF v1 and it has been compatible with all the TF v2 up until
0.4.0, but now if we save it like this (only models from TF Hub and I am guessing those withassets) we see that error)karllessard commentedon Mar 17, 2022
Thanks @maziyarpanahi , this seems to be a different issue. I also suspect that the change did not happened in TF Java but in TF 2.7.0, I'll start to investigate on this. Maybe we should track this new bug in a separate issue then?
UPDATE: I've created the issue: #434