How to decode against WMT ENDE model via Tensorflow Serving
Somewhat similar to https://github.com/tensorflow/tensor2tensor/issues/344, I have my model served by TensorFlow Serving API, how can I translate an English sentence against this model now?
Model created by setting export_saved_model=True during training, and served by tensorflow_model_server --model_base_path=/the/path/ --port=9000.
So I've gone further into the abyss, trying to figure this out, and have gotten to the point of sending a predict request to my served model.
Took quite a bit of digging through the t2t source code but I've encoded the text as input_ids = vocabulary.encode(text) and converted to array by np.array(x).astype(np.int32).
I construct request as request.inputs['inputs'].CopyFrom(
tf.contrib.util.make_tensor_proto(input_ids, dtype=tf.int32, shape=????)) but don't know what to put in for shape, from going through t2t's code I've found reshape calls of either [1, 299, 299, -1] or [1, -1, 1, 1], neither of these work and the error I receive is ValueError: Too many elements provided. Needed at most -89401, but received 4 and ValueError: Too many elements provided. Needed at most 1, but received 4 respectively. I've guessed that the max elements it requires is determined by the multiplication of elements 1 and 2 in the arrays. If I increase these, the request just hangs.
By leaving shape unspecified the error is AbortionError(code=StatusCode.INVALID_ARGUMENT, details="Expected multiples argument to be a vector of length 2 but got length 5
Any help would be much appreciated. R.
What is the result of saved_model_cli show?
In my case,
request.inputs['inputs'].CopyFrom(tf.contrib.util.make_tensor_proto([[[[1]]], [[[2]]]]))
worked for this model
# saved_model_cli show --dir /path/to/saved/model/dir --tag_set serve --signature_def serving_default
The given SavedModel SignatureDef contains the following input(s):
inputs['inputs'] tensor_info:
dtype: DT_INT32
shape: (-1, 1, 1, 1)
name: ExpandDims_2:0
The given SavedModel SignatureDef contains the following output(s):
outputs['outputs'] tensor_info:
dtype: DT_INT32
shape: (-1, -1)
name: strided_slice_8:0
Method name is: tensorflow/serving/predict
The response is
(outputs {
key: "outputs"
value {
dtype: DT_INT32
tensor_shape {
dim {
size: 2
}
dim {
size: 9
}
}
int_val: 5
int_val: 3
int_val: 1
int_val: 0
int_val: 0
int_val: 0
int_val: 0
int_val: 0
int_val: 0
int_val: 5
int_val: 3
int_val: 1
int_val: 0
int_val: 0
int_val: 0
int_val: 0
int_val: 0
int_val: 0
}
}
The server side log is
2017-10-20 06:55:26.749815: I external/org_tensorflow/tensorflow/core/kernels/logging_ops.cc:79] beam_decode batch_size=[2]
Now I wonder how I can input a sequence of multiple words in batch size 1, not 2 sequences of one word. And I also wonder how the beam size, etc. can be specified in a request.
Hi @ninotoshi,
I get the same output for saved_model_cli show and running request.inputs['inputs'].CopyFrom(tf.contrib.util.make_tensor_proto([[[[1]]], [[[2]]]])) does successfully return a response
outputs {
key: "outputs"
value {
dtype: DT_INT32
tensor_shape {
dim {
size: 2
}
dim {
size: 19
}
}
int_val: 337
int_val: 468
int_val: 226
int_val: 1
int_val: 0
int_val: 0
int_val: 0
int_val: 0
int_val: 0
int_val: 0
int_val: 0
int_val: 0
int_val: 0
int_val: 0
int_val: 0
int_val: 0
int_val: 0
int_val: 0
int_val: 0
int_val: 3
int_val: 1
int_val: 0
int_val: 0
int_val: 0
int_val: 0
int_val: 0
int_val: 0
int_val: 0
int_val: 0
int_val: 0
int_val: 0
int_val: 0
int_val: 0
int_val: 0
int_val: 0
int_val: 0
int_val: 0
int_val: 0
}
}
In your make_tensor_proto call what does [[[[1]]], [[[2]]]] encode? I have been getting inputs_ids for string 'hello world', and putting that directly in make_tensor_proto, which gives me errors as it's an array.
Cheers, R.
[[[[1]]], [[[2]]]] is just test data. I assume this must be encoded from a list of strings such as ['hello world', 'foo bar baz'] with encode() like
https://github.com/tensorflow/tensor2tensor/blob/f1b75861d8c9927fbc13643a6d58b60d2f3d08b0/tensor2tensor/data_generators/text_encoder_test.py#L132
using a vocabulary file (e.g. vocab.enfr.32768).
Because of
inputs['inputs'] tensor_info:
dtype: DT_INT32
shape: (-1, 1, 1, 1)
it seems make_tensor_proto only accepts a list of integers such as [[[[1]]], [[[2]]]] where -1 in shape means a variable length.
In a translation problem, the input should be (-1, -1). I don't know why the shape is (-1, 1, 1, 1).
It is also painful to convert text to integers to make a request and convert integers in a response to text on the client side.
Hi,
I've followed the encoding, as is done in the decoding.py file https://github.com/tensorflow/tensor2tensor/blob/f1b75861d8c9927fbc13643a6d58b60d2f3d08b0/tensor2tensor/utils/decoding.py#L428
Following from that you can see how they encode the numpy array. Neither the input_ids array, or the inputs array as encoded in the above file, work in the make_tensor_proto function.
Furthermore, setting shape in make_tensor_proto to (-1, 1, 1, 1), gives varying errors like ValueError: Too many elements provided. Needed at most -1, but received 4.
I agree that it's not ideal having to convert text to integers on client side and the opposite with the result, but if there was documentation on how to do it, then it would be fine. I opened this issue 10 days ago, and am not much closer to a solution than I was, when I started.
R.
Just to let you know: we're looking at it too, but got sidetracked by other things. Converting to ints on the client allows to do tokenization (which is really hard in TF graphs), or did you have sth else in mind?
Hi,
I don't have a problem with converting to ints on the client at all, in fact, I expected that to be the case when I looked into serving one of these models. All that I am concerned with, is how to send the array of ints that represent a string to a served model. I've tried to see if I can figure it out from T2T's source code, but have gotten only so far.
I think I'm correct in how I'm encoding the string, following the decoding.py file as I mentioned previously, just need to find out how to send that to the server, and following from that, how to decode the result received back to a string.
It's not a pressing concern for me, as I've looked into alternatives for machine translation in the meantime, but I would like to get this running also at some point too.
Thanks, R.
@lukaszkaiser if tokenizing on the server side is not easy at all with TF graphs, does this mean that the best route to serve a model + (tok / detok layers) is to go with an adhoc API ?
Not sure what you mean by adhoc API, but yes, you'll have to encode, then send your request, then decode the response.
And just fyi, the serving input function is defined here: https://github.com/tensorflow/tensor2tensor/blob/master/tensor2tensor/utils/data_reader.py#L369
If you're able to get the predict result from the server, you can use something along these lines to convert the results to a user readable string of target language text:
output_ids = decoding._save_until_eos(result["outputs"].flatten())
output_pieces = targets_vocab.decode_list(output_ids)
output_token = [{"text": piece} for piece in output_pieces]
output = targets_vocab.decode(output_ids)
This requires a private method that I'd like to make public _save_until_eos that the binaries use internally to convert token indices.
I have a webserver wrapping t2t that does all this internally that I'm hoping to check in soonish as a demonstration.
Hi @lukaszkaiser and @rsepassi , is there any update on this? Like @robertBrnnn , I am struggling with encoding the input to the translation server. Following the discussion above, I have created preprocessing layers to tokenize the input sentence, and then encoded to integer using vocabulary. Now how do I convert this integer array to request input?
There is some sample code in the notebook of tensor2tensor, which showed how to encode tokenized string to int (input for serving), and then decode int (output from serving) to string for translation problems.
We can follow the notebook to create our own encode and decode function.
Then, use this code to communicate with tensorflow serving:
from grpc.beta import implementations
from tensorflow_serving.apis import prediction_service_pb2
from tensorflow_serving.apis import predict_pb2
import tensorflow as tf
def request_tf_serving(model_name, req_src_text):
tf_request = predict_pb2.PredictRequest()
tf_request.model_spec.name = model_name
inputs_array = encode(req_src_text)
tenosr_proto = tf.contrib.util.make_tensor_proto(inputs_array, dtype=tf.int32)
tf_request.inputs['inputs'].CopyFrom(tenosr_proto)
channel = implementations.insecure_channel(host, port)
stub = prediction_service_pb2.beta_create_PredictionService_stub(channel)
tf_response = stub.Predict(tf_request, 10.0)
outputs = tf_response.outputs['outputs']
return decode(outputs.int_val)
The libraries need to be installed except tensorflow :
pip install grpcio tensorflow-serving-api
By the way, tensorflow-serving-api seems to only have python2.x version.
This method works fine for me, the only problem is the speed, as I said in #504 .
See the updated serving README and export.py and query.py in that folder.