AI预测模型工程化性能调优

版权声明 本站原创文章 由 萌叔 发表 转载请注明 萌叔 | http://vearne.cc 1. 前言 最近经历了一次AI预测模型工程化性能调优,这里分享出来,希望对大家有所启发。 模型是keras训练的神经网络预测模型,输入一段文本,输出文本的分类结果。为了对外提供服务,已经将模型用 gunicorn + flask对模型进行封装,提供http接口。 环境 OS:ubuntu 18.04 CPU:32核 内存: 256GB 显卡: Tesla P100 前期的压测结果 wrk -t4 -c100 -d30s --script=post.lua --latency http://localhost:8080/api/predict 类型 CPU GPU QPS 2.4 12.41 2. 观察性能指标 QPS过低不能满足业务方的要求,所以萌叔尝试对封装好的服务进行调优。 仔细观察了各项系统指标之后(cpu、load、磁盘IO、内存使用率、虚拟内存页换入换出) 萌叔发现了一个疑点。 2.1 keras 使用CPU 在压测的过程中,CPU的使用率只能达到70%左右 2.2 keras 使用GPU 在压测的过程中,GPU的使用率只能达到30%左右 神经网络模型对文本分类的预测过程,实际就是将输入文本转换成数值,再带入神经网络,进行的2次计算的过程。这是一个计算密集型的场景,性能瓶颈应该是CPU/GPU; 如果CPU、GPU没有打满,则说明性能仍有提升的空间。 3. 调优 3.1 初始协程模式 web使用Flask开发,启动脚本如下: gunicorn -k gevent -t 300 -b 0.0.0.0:8080 run:app gunicorn有几种工作模式 sync (进程模式) eventlet gevent (协程模式) tornado gthread gevent是一种协程模式,它的特点是在请求的处理过程中有IO调用时(或者当前的协程主动让出CPU),会自动触发切换上下文切换,启动另一个协程,提高CPU的利用率。它特别适用于IO密集型的场景,尤其是对于处理过程中有对数据库的调用,第三方服务调用的情况。 ...

March 17, 2020 · 2 min

用grequests实现并发http请求

起因 要用http请求探测服务的有效性,多进程,多线程,感觉似乎没有必要,看看有没有协程的方案 1. 简单用法 grequests 利用 requests和gevent库,做了一个简单封装,使用起来非常方便 import grequests import time import requests urls = [ 'https://docs.python.org/2.7/library/index.html', 'https://docs.python.org/2.7/library/dl.html', 'http://www.iciba.com/partial', 'http://2489843.blog.51cto.com/2479843/1407808', 'http://blog.csdn.net/woshiaotian/article/details/61027814', 'https://docs.python.org/2.7/library/unix.html', 'http://2489843.blog.51cto.com/2479843/1386820', 'http://www.bazhuayu.com/tutorial/extract_loop_url.aspx?t=0', ] def method1(): t1 = time.time() for url in urls: res = requests.get(url) #print res.status_code t2 = time.time() print 'method1', t2 - t1 def method2(): tasks = [grequests.get(u) for u in urls] t1 = time.time() res = grequests.map(tasks, size=3) # print res t2 = time.time() print 'method2', t2 - t1 def method3(): tasks = [grequests.get(u) for u in urls] t1 = time.time() res = grequests.map(tasks, size=6) # print res t2 = time.time() if __name__ == '__main__': method1() method2() method3() 运行结果如下: ...

January 1, 2018 · 1 min