OpenVINO Benchmark Python Tool

$ benchmark_app -i catdog.jpg -m /data/wjj/openvino/ir/public/googlenet-v1/FP32/googlenet-v1.xml -d CPU -api sync -t 10
[Step 2/11] Loading OpenVINO
[ WARNING ] PerformanceMode was not explicitly specified in command line. Device CPU performance hint will be set to LATENCY.
[Step 10/11] Measuring performance (Start inference synchronously, inference only: True, limits: 10000 ms duration)
[Step 11/11] Dumping statistics report
Count:          1674 iterations
Duration:       10003.75 ms
Latency:
    Median:     5.86 ms
    AVG:        5.89 ms
    MIN:        5.77 ms
    MAX:        14.02 ms
Throughput: 170.76 FPS

可以看到设备性能模式自动设置为 LATENCY。

OpenVINO Cross Check Tool

Windows 版本有 BUG:在文件 cross_check_tool/utils.py 中的 input_processing 函数。

tensor_name = 'data' # ADD
if tensor_name not in input_names:
  raise Exception(f"Input with name {tensor_name} doesn't exist in the model!")

Deep Learning Accuracy Validation Framework

可以看到在目录下使用当前时间生成新目录。

$ head -n 4 2022-05-18_11-17-20/SampLeNet_example_openvino_CPU__sample_dataset_classification.csv 
identifier,annotation_label,prediction_label,accuracy_result
domestic_cat_s_000907.png,3,[3],1.0
hydrofoil_s_000078.png,8,[8],1.0
sea_boat_s_001456.png,8,[8],1.0

Ubuntu 上将 NVIDIA GPU 切换为 Intel 集成显卡 IGD

IGD(Integrated Graphics Device)

操作系统:Ubuntu 18.04,主机有一张 NVIDIA 的独立显卡 GP106 [GeForce GTX 1060 6GB],还有 Intel 酷睿处理器 i5 8500 自带的集成显卡(Intel UHD Graphics 630)。为了更充分的使用独立显卡用于深度学习计算,需要把集成显卡用于显示。在这个过程中遇到了各种各样的问题:

  • 鼠标和键盘失灵。
  • 登录 X Window 时,输入正确的密码不能登录。

选择 IGD,保存退出。

NVIDIA 软件栈搭建

  1. 安装驱动
sudo apt install nvidia-driver-510
  1. 重启系统
sudo reboot
  1. 查看
nvidia-smi
  1. 卸载驱动
sudo apt purge nvidia*

CUDA Toolkit 下载

cuDNN 下载

  1. 层和张量融合(Layer and Tensor Fusion) 通过融合内核中的节点来优化GPU内存和带宽的使用。
  1. 内核自动调谐(Kernel Auto-Tuning) 根据目标GPU平台选择最佳数据层和算法。
  1. 动态张量内存(Dynamic Tensor Memory) 最大限度地减少内存占用,并有效地将内存重用到张量上。
  1. 多流执行(Multi-Stream Execution) 使用可扩展的设计并行处理多个输入流。
  1. 时间融合(Time Fusion) 使用动态生成的内核优化时间步骤中的循环神经网络。

TensorRT 下载

配置环境变量 LD_LIBRARY_PATH export LD_LIBRARY_PATH=$LD_LIBRARY

OpenVINO 神经网络性能分析

FP16 模型

$ ll -h public/ssd300/FP16/
总用量 51M
-rw-rw-r-- 1 wjunjian wjunjian  51M 4月  27 09:10 ssd300.bin
-rw-rw-r-- 1 wjunjian wjunjian  14K 4月  27 09:10 ssd300.mapping
-rw-rw-r-- 1 wjunjian wjunjian 217K 4月  27 09:10 ssd300.xml

FP32 模型

$ ll -h public/ssd300/FP32/
总用量 101M
-rw-rw-r-- 1 wjunjian wjunjian 101M 4月  27 09:10 ssd300.bin
-rw-rw-r-- 1 wjunjian wjunjian  14K 4月  27 09:10 ssd300.mapping
-rw-rw-r-- 1 wjunjian wjunjian 179K 4月  27 09:10 ssd300.xml

FP16 CPU $ python object_detection.py public/ssd300/FP16/ssd300.xml catdog.jpg CPU [ INFO ] 1. Creating OpenVINO Runtime Core [ INFO ] 2.

OpenVINO 图像分类

本程序可以接收多个图像文件,通过获得模型的输入尺寸,使用 OpenCV 对每个图像重置为模型的输入尺寸。这部分图像尺寸的预处理并没有构建到模型中,所以不能和模型一起编译到指定的设备上。

OpenVINO 的工作原理

🎃 0 计划和设置1 训练模型2 转换和优化3 调整性能4 部署应用程序

<layer type="Convolution" ...>
    <data dilations="2,2,2" pads_begin="0,0,0" pads_end="0,0,0" strides="3,3,3" auto_pad="explicit"/>
    <input>
        <port id="0">
            <dim>1</dim>
            <dim>7</dim>
            <dim>320</dim>
            <dim>320</dim>
            <dim>320</dim>
        </port>
        <port id="1">
            <dim>32</dim>
            <dim>7</dim>
            <dim>3</dim>
            <dim>3</dim>
// ...

Get Started OpenVINO

安装 OpenVINO 开发工具

python -m venv openvino_env
  • Linux
source openvino_env/bin/activate
  • Windows
openvino_env\Scripts\activate.bat
python -m pip install --upgrade pip
pip install openvino-dev[onnx,pytorch,kaldi,mxnet,caffe,tensorflow2]==2022.1.0 -i https://mirrors.aliyun.com/pypi/simple/

使用 FastAPI 开发 RESTAPI 服务

main.py

import uvicorn

from fastapi import FastAPI

from .routers import users
from .routers import files


app = FastAPI(title='REST API Interface', version='1.0', 
              description="基于 FastAPI 的 REST API 接口。")


app.include_router(users.router)
app.include_router(files.router)


@app.get('/')
async def index():
    return {'Hello': 'World!'}


if __name__ == '__main__':
    uvicorn.run(app, host='0.0.0.0', port=8000)

dependencies.

使用 wrk 对 FastAPI 上传和下载文件的基准测试

服务器 CPU 40核,内存 256G,操作系统 Ubuntu 20.04,Python3.9

wrk 的 lua 脚本:postfile_formdata.lua

wrk.method = "POST"
local f = io.open("postdata", "rb")
wrk.body   = f:read("*all")
wrk.headers["Content-Type"] = "multipart/form-data; boundary=gouchicao0123456789"

wrk 的 lua 脚本:postfile_json.lua

wrk.method = "POST"
local f = io.open("postdata.json", "rb")
wrk.body   = f:read("*all")
wrk.headers["Content-Type"] = "application/json"

/file_benchmarking/upload/binary/chunk/async_func/async_rw wrk -c100

Linux 性能优化

当平均负载高于 CPU 数量 70% 的时候,你就应该分析排查负载高的问题了。一旦负载过高,就可能导致进程响应变慢,进而影响服务的正常功能。 70% 这个数字并不是绝对的,最推荐的方法,还是把系统的平均负载监控起来,然后根据更多的历史数据,判断负载的变化趋势。当发现负载有明显升高趋势时,比如说负载翻倍了,你再去做分析和调查。

$ uptime
 12:51:13 up 754 days,  2:02,  3 users,  load average: 0.41, 0.65, 2.63

strees: --cpu cpu压测选项,-i io压测选项,-c 进程数压测选项,--timeout 执行时间

  • mpstat 是一个常用的多核 CPU 性能分析工具,用来实时查看每个 CPU 的性能指标,以及所有 CPU 的平均指标。-P ALL监视所有cpu
  • pidstat 是一个常用的进程性能分析工具,用来实时查看进程的 CPU、内存、I/O 以及上下文切换等性能指标。-u 显示cpu利用率

运行 uptime 查看平均负载的变化情况

$ watch -d uptime
 13:00:09 up 754 days,  2:11,  3 users,  load average: 2.84, 1.87, 2.24

运行 mpstat 查看 CPU 使用率的变化情况 # -P ALL 表示监控所有CPU,后面数字5表示间隔

FastAPI 上传和下载文件的基准测试

使用 FastAPI 实现了文件的上传和下载,部署服务使用了 uvicorn 和 gunicorn+uvicorn 两种方法。

基准测试工具使用的是 wrk

服务器 CPU 40核,内存 256G,操作系统 Ubuntu 20.04,Python3.9

Install Python3.9 in Ubuntu20.04

在看到这个错误,首先想到的就是安装包 python3.9-distutils,但是在安装的时候看到它依赖的安装包都是 python3.8 的。没有找到更好的解决方法,这里找到一个解释

I believe this is a bug in Debian's Python package. Their modifications to Python have been a source of a long standing debate: https://gist.github.com/tiran/2dec9e03c6f901814f6d1e8dad09528e has a lot of discussion.
I think that is the issue, Ubuntu packages are inconsistent, i.e. python3.8 and python3.9 bring different set of modules, as well as have different set of decencies, while all that should really differ is just Python version. Same applies for python3.[89]-minimal. However, all 4 are consentient in one thing - not having sys.prefix/lib/pythonX.Y/site-packages in sys.path
To have that (and distutils) sorted, one needs to install a collection of python3 packages, but those are Python3.8 (and bring tons of semi-random libraries and so).

All Ubuntu provided Python3.[89] 'installations' have ensurepip removed. I think, the 'logic' is to force people to use python3-pip that (unless --no-install-recommends is used) brings whole tone of things including make, cpp and perl(!)

I strongly recommend filing an issue with Debian and Ubuntu for this -- while pip can do things to paper over the issue, the fundamental problem is that the Python installation is not proper. Part of the problem is that Debian users don't ask Debian's maintainers to make fixes for the things they break.
I will consider that.

manually installing distutils
How did you do this? If you've used what is available in CPython's source tree, then you've installed an incompatible distutils for the Python interpreter -- Debian relies on patches they make to distutils to keep things working.
Installing might be a bit of overstatement, since I am working with Docker container I am doing:

COPY --from=python:3.9-slim /usr/local/lib/python3.9/distutils /usr/lib/python3.9/distutils
I would do FROM python:3.9-slim but I have to rely on specific 'base' container.
python:3.9-slim has FROM debian:bullseye-slim