tensorflow serving中enable_model_warmup在部署中的使用

enable_model_warmup參數:在tensorflow serving中通過saved_model模型中自帶客戶端的請求來預加載模型,減少第一次的請求的延遲

命名:在saved_model模型文件夾下的asserts.extra文件夾,放入命名爲tf_serving_warmup_requests的tf record文件即可

其中assets.extra文件下:

 



# coding:utf-8

# @author: “”
# @file: tf_serving_warmup_requests_client.py
# @time:
# @desc:





import tensorflow as tf

from tensorflow_serving.apis import predict_pb2
from tensorflow_serving.apis import prediction_log_pb2

request_dict = {'inputs': {'input_ids': [[101, 2644, 1962, 8024, 6821, 7027, 3221, 1921, 1921, 2864, 6756, 8024, 1744, 1079, 3297, 1920, 4638, 757, 5468, 5381, 1297, 6756, 2398, 1378, 8024, 6435, 7309, 2644, 4385, 1762, 3221, 3300, 6775, 6756, 7444, 6206, 1139, 1545, 1408, 8043, 172, 3300, 702, 6756, 1762, 1297, 102, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], 'input_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], 'segment_ids': [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], 'is_training': False}, 'signature_name': 'serving_default'}

input_ids = request_dict["inputs"]["input_ids"]
input_mask = request_dict["inputs"]["input_mask"]
segment_ids = request_dict["inputs"]["segment_ids"]
is_training = request_dict["inputs"]["is_training"]

print(input_ids)


with tf.io.TFRecordWriter("tf_serving_warmup_requests") as writer:
    predict_request = predict_pb2.PredictRequest()
    predict_request.model_spec.name = "sellcarintent_models"  ### 對應tf serving中的MODEL_NAME
    predict_request.model_spec.signature_name = "serving_default"   # 這個是預測簽名, serving_default
    predict_request.inputs["input_ids"].CopyFrom(tf.make_tensor_proto(input_ids, tf.int32))
    predict_request.inputs["input_mask"].CopyFrom(tf.make_tensor_proto(input_mask, tf.int32))
    predict_request.inputs["segment_ids"].CopyFrom(tf.make_tensor_proto(segment_ids, tf.int32))
    predict_request.inputs["is_training"].CopyFrom(tf.make_tensor_proto(is_training, tf.bool))

    # log = prediction_log_pb2.PredictLog(request=predict_request)
    log = prediction_log_pb2.PredictionLog(predict_log=prediction_log_pb2.PredictLog(request=predict_request))
    for i in range(100):
        writer.write(log.SerializeToString())



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章