张量流版本:1.14
我们当前的设置是使用张量流估计器进行实时 NER,即一次对一个文档进行推理。我们有 30 个不同的字段需要提取,每个字段运行一个模型,因此总共有 30 个模型。
我们当前的设置使用 python 多重处理来并行进行推理。 (推理是在 CPU 上完成的。)这种方法会在每次进行预测时重新加载模型权重。
使用提到的方法here https://guillaumegenthial.github.io/serving-tensorflow-estimator.html,我们将估计模型导出为tf.saved_model
。这按预期工作,因为它不会为每个请求重新加载权重。它也适用于一个进程中的单字段推理,但不适用于多处理。当我们执行预测函数时,所有进程都会挂起(predict_fn
在链接的帖子中)致电。
这个帖子 https://stackoverflow.com/questions/36610290/tensorflow-and-multiprocessing-passing-sessions相关,但不确定如何使其适应保存的模型。
为每个预测变量单独导入张量流也不起作用:
class SavedModelPredictor():
def __init__(self, model_path):
import tensorflow as tf
self.predictor_fn = tf.contrib.predictor.from_saved_model(model_path)
def predictor_fn(self, input_dict):
return self.predictor_fn(input_dict)
怎么做tf.saved_model
使用多处理?
Ray Serve是ray的模型服务解决方案,也支持离线批处理。您可以将模型包装在 Ray Serve 的后端中,并将其缩放到您想要的数量副本。
from ray import serve
client = serve.start()
class MyTFModel:
def __init__(self, model_path):
self.model = ... # load model
@serve.accept_batch
def __call__(self, input_batch):
assert isinstance(input_batch, list)
# forward pass
self.model([item.data for item in input_batch])
# return a list of response
return [...]
client.create_backend("tf", MyTFModel,
# configure resources
ray_actor_options={"num_cpus": 2, "num_gpus": 1},
# configure replicas
config={
"num_replicas": 2,
"max_batch_size": 24,
"batch_wait_timeout": 0.5
}
)
client.create_endpoint("tf", backend="tf")
handle = serve.get_handle("tf")
# perform inference on a list of input
futures = [handle.remote(data) for data in fields]
result = ray.get(futures)
尝试使用夜间轮,以下是教程:https://docs.ray.io/en/master/serve/tutorials/batch.html https://docs.ray.io/en/master/serve/tutorials/batch.html
编辑:更新了 Ray 1.0 的代码示例
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)