当主机具有多个核心时,我无法在docker中的模型上运行推理。通过PyTorch 1.0 ONNX导出器导出模型:
torch.onnx.export(pytorch_net, dummyseq, ONNX_MODEL_PATH)
使用单核启动模型服务器(包装在Flask中)会产生可接受的性能(cpuset将进程固定到特定的cpus)docker run --rm -p 8081:8080 --cpus 0.5 --cpuset-cpus 0 my_container
来自ab -c 1 -n 1000 http://0.0.0.0:8081/predict\?itemids\=5,100
Percentage of the requests served within a certain time (ms)
50% 5
66% 5
75% 5
80% 5
90% 7
95% 46
98% 48
99% 49
但是将其固定到四个内核将为完全相同的Ab-call docker run --rm -p 8081:8080 --cpus 0.5 --cpuset-cpus 0,1,2,3 my_container
Percentage of the requests served within a certain time (ms)
50% 9
66% 12
75% 14
80% 18
90% 62
95% 66
98% 69
99% 69
100% 77 (longest request)
模型推断是这样完成的,除了这个问题,它似乎按预期工作。 (这在与模型导出完全独立的环境中运行)
from caffe2.python import workspace
from caffe2.python.onnx.backend import Caffe2Backend as c2
from onnx import ModelProto
class Model:
def __init__(self):
self.predictor = create_caffe2_predictor(path)
@staticmethod
def create_caffe2_predictor(onnx_file_path):
with open(onnx_file_path, 'rb') as onnx_model:
onnx_model_proto = ModelProto()
onnx_model_proto.ParseFromString(onnx_model.read())
init_net, predict_net = c2.onnx_graph_to_caffe2_net(onnx_model_proto)
predictor = workspace.Predictor(init_net, predict_net)
return predictor
def predict(self, numpy_array):
return self.predictor.run({'0': numpy_array})
** wrapper flask app which calls Model.predict() on calls to /predict **
OMP_NUM_THREADS=1
也存在于容器环境中,它具有 some 的作用,但这不是最终问题。
您在这里看到的基准统计数据是在具有8个超线程的本地计算机上运行的,因此我不应该使我的计算机饱和并影响测试。这些结果也显示在我的kubernetes环境中,并且在那里出现了大量的CFS(完全公平调度程序)。
我正在kubernetes环境中运行,因此我无法控制主机公开的CPU数量,并且在那里进行某种固定似乎也很麻烦。
有没有办法将caffe2模型推论固定到单个处理器?我在这里做错什么吗? caffe2.Predictor对象是否不适合此任务?
任何帮助表示赞赏。
编辑:
我添加了我在这里可以想到的最简单的可复制示例,其中包含docker-container和run-script:https://github.com/NegatioN/Caffe2Struggles
答案 0 :(得分:3)
这不是问题的直接答案,但是如果您的目标是在生产中提供PyTorch模型(现在只有PyTorch模型,现在是我的),那么简单地使用PyTorch Tracing似乎是更好的选择。
然后可以像通过Caffe2一样将其直接加载到C ++前端中,但是PyTorch跟踪似乎维护得更好。据我所知,速度没有降低,但是配置起来要容易得多。
在单核容器上获得良好性能的一个示例是像以前一样使用ProvID / Total_Count / Denied Percentage
-----------------------------------------
X12345 / 77 / 0
运行,并按如下所示导出模型:
OMP_NUM_THREADS=1
然后按照上述指南,或者通过Python界面,在纯C ++中简单地在生产环境中运行模型:
from torch import jit
### Create a model
model.eval()
traced = jit.trace(model, torch.from_numpy(an_array_with_input_size))
traced.save("traced.pt")
答案 1 :(得分:0)
我认为这应该可行:
workspace.GlobalInit(["caffe2", "--caffe2_omp_num_threads=1"])