TensorRT加速 ——NVIDIA终端AI芯片加速用,可以直接利用caffe或TensorFlow生成的模型来predict(inference)

简介:

官网:https://developer.nvidia.com/tensorrt

作用:NVIDIA TensorRT™ is a high-performance deep learning inference optimizer and runtime that delivers low latency, high-throughput inference for deep learning applications. TensorRT can be used to rapidly optimize, validate, and deploy trained neural networks for inference to hyperscale data centers, embedded, or automotive product platforms.

(Click to Zoom)

Developers can use TensorRT to deliver fast inference using INT8 or FP16 optimized precision that significantly reduces latency, as demanded by real-time services such as streaming video categorization on the cloud or object detection and segmentation on embedded and automotive platforms.

With TensorRT developers can focus on developing novel AI-powered applications rather than performance tuning for inference deployment. TensorRT runtime ensures optimal inference performance that can meet the most demanding latency and throughput requirements.

TensorRT can be deployed to Tesla GPUs in the datacenter, Jetson embedded platforms, and NVIDIA DRIVE autonomous driving platforms.

What's New in TensorRT 3?

TensorRT 3 is the key to unlocking optimal inference performance on Volta GPUs. It delivers up to 40x higher throughput in under 7ms real-time latency vs. CPU-Only inference.
Highlights from this release include:

  • Deliver up to 3.7x faster inference on Tesla V100 vs. Tesla P100 under 7ms real-time latency
  • Optimize and deploy TensorFlow models up to 18x faster compared to TensorFlow framework inference on Tesla V100
  • Improved productivity with easy to use Python API

Learn more about how to get started with TensorRT 3 in the following technical blog posts:


 

见:https://stackoverflow.com/questions/41142284/run-tensorflow-with-nvidia-tensorrt-inference-engine 可以知道已经支持导入TensorFlow的模型

TensorRT 3.0 supports import/conversion of TensorFlow graphs via it's UFF (universal framework format). Some layer implementations are missing and will require custom implementations via IPlugin interface.

Previous versions didn't support native import of TensorFlow models/checkpoints.

 

 

from:http://blog.csdn.net/jsa158/article/details/53944159

TensorRT介绍 
TensorRT 现在是inference 精度最高,速度最快的,而且在不断的改进过程中,在保证软件精度的同时,不断提高速度;TensorRT只能用来做Inference,不能用来做train;

1、TensorRT的需要的文件 
需要的基本文件(不是必须的) 
1>网络结构文件(deploy.prototxt) 
2>训练的权重模型(net.caffemodel) 
TensorRT 2.0 EA版中的sampleMNISTAPI和TensorRT 1.0中的sampleMNISTGIE 几乎没有变化,就是不使用caffemodel 文件构建network 的例子。 
2、TensorRT支持的层 
Convolution: 2D 
Activation: ReLU, tanh and sigmoid 
Pooling: max and average 
ElementWise: sum, product or max of two tensors 
LRN: cross-channel only 
Fully-connected: with or without bias 
SoftMax: cross-channel only 
Deconvolution 
对于TensorRT 不支持的层,可以先将支持的层跑完,然后将输出作为caffe的输入,用caffe再跑,V1不支持TensorRT 和caffe同时工作,V2支持。(例子NVIDIA正在做,后期可能会上传github) 
3、TensorRT 处理流程 
基本处理过程:1>caffe model 转化 gie的model,或者从磁盘或者网络加载gie可用的model;2>运行GIE引擎(数据提前copy到GPU中);3>提取结果

三、TensorRT Optimization 
使用了很多 优化网络和层计算的方法 
内存优化、网络优化,层合并,层删除以及GPU汇编指令,内部函数,提高GPU利用率,减少精度需求,cuDNN优化,根据不同的batchsize设置不同的计算模式或者GPU clock;卷积的优化,使用Winograd(提升3倍) 等算法或者特定硬件方式实现;

 批量处理尽可能并行处理,在cuda中 使用warp对齐,提高GPU命令命中率,除此还有CPU可以使用,CPU做一部分工作,GPU做一部分工作;

数据布局可以使用半精度FP16 , textute memory , 13% inference speedup 
网络优化中,网络的垂直融合,网络的水平融合,级联层可以删掉(concat) 
内部使用稀疏矩阵编码

 

Jetson TX1 开发教程(4)--TensorRT加速Caffe初探

转自:http://blog.csdn.net/amds123/article/details/72234167?locationnum=13&fps=1

 

项目地址:NVIDIA TensorRT

前言

TensorRT(GIE)是一个C++库,适用于Jetson TX1和Pascal架构的显卡(Tesla P100, K80, M4 and Titan X等),支持fp16特性,也就是半精度运算。由于采用了“精度换速度”的策略,在精度无明显下降的同时,其对inference的加速很明显,往往可以有一倍的性能提升,而且还支持使用caffe模型。目前网上关于TensorRT的介绍很少,这里博主尝试着写一些,有空还会继续补充。

TensorRT简介

TensorRT目前基于gcc4.8而写成,其独立于任何深度学习框架。对于caffe而言,TensorRT是把caffe那一套东西转化后独立运行,能够解析caffe模型的相关工具叫做 NvCaffeParser,它根据prototxt文件和caffemodel权值,转化为支持半精度的新的模型。

目前TensorRT 支持caffe大部分常用的层,包括:

  • Convolution(卷积层), with or without bias. Currently only 2D convolutions (i.e. 4D input and output tensors) are supported. Note: The operation this layer performs is actually a correlation, which is a consideration if you are formatting weights to import via GIE’s API rather than the caffe parser library.
  • Activation(激活层): ReLU, tanh and sigmoid.
  • Pooling(池化层): max and average.
  • Scale(尺度变换层): per-tensor, per channel or per-weight affine transformation and exponentiation by constant values. Batch Normalization can be implemented using the Scale layer.
  • ElementWise(矩阵元素运算): sum, product or max of two tensors.
  • LRN(局部相应归一化层): cross-channel only.
  • Fully-connected(全连接层) with or without bias
  • SoftMax: cross-channel only
  • Deconvolution(反卷积层), with and without bias

不支持的层包括:

  • Deconvolution groups
  • PReLU
  • Scale, other than per-channel scaling
  • EltWise with more than two inputs

使用TensorRT主要有两个步骤(C++代码):

  • In the build phase, the toolkit takes a network definition, performs optimizations, and generates the inference engine.
  • In the execution phase, the engine runs inference tasks using input and output buffers on the GPU.

想要具体了解TensorRT的相关原理的,可以参看这篇官方博客: 
Production Deep Learning with NVIDIA GPU Inference Engine

这里暂时对原理不做太多涉及,下面以mnist手写体数字检测为例,结合官方例程,说明TensorRT的使用步骤。

TensorRT运行caffe模型实战

获取TensorRT支持

首先,Jetson TX1可以通过Jetpack 2.3.1的完全安装而自动获得TensorRT的支持,可参考博主之前的教程。TX1刷机之后,已经添加了一系列的C++运行库去支持TensorRT,如果掌握API的话,写一个C++程序就可以实现功能。

没有TX1,只有Pascal架构的显卡(如TITAN X),那也能感受TensorRT的效果,方法是去官网申NVIDIA TensorRT请测试资格,需要详细说明自己的研究目的,一般经过一两次邮件沟通后就能通过。博主目前已经获得TensorRT 1.0和2.0的测试资格,有机会也会进行TITAN X的TensorRT测试。

运行官方例程

这里,博主就先以Jetson TX1为例,看看官方自带的例程是如何运行的。自带例程的地址是:/usr/src/gie_samples/samples,我们打开文件夹,发现如下文件:

这里写图片描述

其中,data文件夹存放LeNet和GoogleNet的模型描述文件和权值,giexec文件夹是TensorRT通用接口的源代码,剩下的文件夹是特定网络的接口源代码。Makefile是配置文件,在gie_sample文件夹位置打开终端,输入sudo make就能完成编译,生成一系列可执行文件,存放在bin文件夹中,那我们就来看看bin文件夹的内容:

这里写图片描述

首先测试giexec文件:

cd /usr/src/gie_samples/samples
./bin/giexec

#得到如下的使用方法:
Mandatory params:
  --model=<file>       Caffe model file
  --deploy=<file>      Caffe deploy file
  --output=<name> Output blob name (can be specified multiple times Optional params: --batch=N Set batch size (default = 1) --device=N Set cuda device to N (default = 0) --iterations=N Run N iterations (default = 10) --avgRuns=N Set avgRuns to N - perf is measured as an average of avgRuns (default=10) --workspace=N Set workspace size in megabytes (default = 16) --half2 Run in paired fp16 mode - default = false --verbose Use verbose logging - default = false --hostTime Measure host time rather than GPU time - default = false --engine=<file> Generate a serialized GIE engine
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

二者比较的话,发现使用半精度的话,确实速度确实上去了一些,但是提升幅度一般,大约快了50%的样子。当然,博主认为这个mnist例子太简单了,可能并不具备太大的说服力,大家可以参看官方给出的加速效果图。

这里写图片描述

那么剩下的几个可执行程序分别有什么用呢?我们还是来一一试验,结果如图: 
./bin/sample_mnist 
这里写图片描述

./bin/sample_mnist_gie 
这里写图片描述

以上二者并没有带什么参数,貌似都在进行随机图片的检测,具体区别需要看源代码。

./bin/sample_googlenet 
这里写图片描述

这个也没有参数可选,只是跑了一遍,最后得出一个时间。

写在后面

TX1自带的TensorRT例程操作起来并不难,博主认为,其最重要的价值在于那些cpp源代码,只有参考这些官方例程,我们才能独立写出C++代码,从而加速自己的caffe模型。giexec.cpp总共代码300多行,还是有点长的,虽然有些注释,但是如果没有仔细研究其API的话,里面的很多函数还是不会用。TensorRT的API文档内容有点多,本人暂时没空研究。 
拥有Jetson TX1的小伙伴可以打开/usr/share/doc/gie/doc/API/index.html查看官方API文档,我这里连同例程源代码一起,都上传到了csdn,有兴趣者可以下载来看看。(貌似离开了TX1,API文档效果不佳)

TensorRT之例程源代码 
TensorRT之官方API文档

有小伙伴在我之前的博客问道,TensorRT能不能加快目标检测框架如SSD的运行速度呢。博主所知道的是,目前还不能。因为现在的TensorRT有较大的局限性,仅支持caffe模型转化后进行前向推理(inference),训练部分还不支持。而且在我看来,目前应用99%都是在图片分类上,比如实现一下Alexlet或者GoogleNet模型的半精度转化,这样就把图片分类的速度提升一倍。由于SSD等目标检测框架含有特殊层以及结构复杂的原因,现有的那些TensorRT函数根本无法去进行转化和定义。

不过Nvidia官方已经开始重视目标检测这一块了,博主和Nvidia技术人员的邮件往来中,获悉未来的TensorRT将会支持Faster RCNN以及SSD,他们已经在开发中了,相信到时使用Jetson TX1进行目标检测,帧率达到10fps以上不是梦。

You mentioned the SSD Single Shot Detector. We are working on SSD right now. I think you’ll find there are some layers needed for SSD that aren’t supported in the versions of TensorRT available through these early release programs. We are adding a custom layer capability to the next version and providing support using that mechanism for Faster R-CNN and SSD.



























本文转自张昺华-sky博客园博客,原文链接:

http://www.cnblogs.com/bonelee/p/8311445.html

,如需转载请自行联系原作者

相关实践学习
在云上部署ChatGLM2-6B大模型(GPU版)
ChatGLM2-6B是由智谱AI及清华KEG实验室于2023年6月发布的中英双语对话开源大模型。通过本实验,可以学习如何配置AIGC开发环境,如何部署ChatGLM2-6B大模型。
相关文章
|
12月前
|
云安全 人工智能 自然语言处理
阿里云x硅基流动:AI安全护栏助力构建可信模型生态
阿里云AI安全护栏:大模型的“智能过滤系统”。
3090 120
|
人工智能 自然语言处理 IDE
模型微调不再被代码难住!PAI和Qwen3-Coder加速AI开发新体验
通义千问 AI 编程大模型 Qwen3-Coder 正式开源,阿里云人工智能平台 PAI 支持云上一键部署 Qwen3-Coder 模型,并可在交互式建模环境中使用 Qwen3-Coder 模型。
1652 109
|
人工智能 自然语言处理 搜索推荐
AI Compass前沿速览:Qoder Agentic编程、vivo Vision头显、AIRI桌面伴侣、RM-Gallery奖励模型平台
AI Compass前沿速览:Qoder Agentic编程、vivo Vision头显、AIRI桌面伴侣、RM-Gallery奖励模型平台
AI Compass前沿速览:Qoder Agentic编程、vivo Vision头显、AIRI桌面伴侣、RM-Gallery奖励模型平台
|
人工智能 Cloud Native Java
书本大纲:从芯片、分布式到云计算AI时代
本文深入探讨并发编程、JVM原理、RPC框架、高并发系统、分布式架构及云原生技术,涵盖内存模型、同步机制、垃圾回收、网络协议、存储优化、弹性伸缩等核心议题,揭示多线程运行逻辑与高并发实现路径,助你掌握现代软件底层原理与工程实践。
380 7
|
12月前
|
人工智能 搜索推荐 程序员
当AI学会“跨界思考”:多模态模型如何重塑人工智能
当AI学会“跨界思考”:多模态模型如何重塑人工智能
1482 120
|
人工智能 JavaScript 测试技术
Cradle:颠覆AI Agent 操作本地软件,AI驱动的通用计算机控制框架,如何让基础模型像人一样操作你的电脑?
Cradle 是由 BAAI‑Agents 团队开源的通用计算机控制(GCC)多模态 AI Agent 框架,具备视觉输入、键鼠操作输出、自主学习与反思能力,可操作各类本地软件及游戏,实现任务自动化与复杂逻辑执行。
1567 6
|
机器学习/深度学习 人工智能 监控
大型动作模型LAM:让企业重复任务实现80%效率提升的AI技术架构与实现方案
大型动作模型(LAMs)作为人工智能新架构,融合神经网络与符号逻辑,实现企业重复任务的自动化处理。通过神经符号集成、动作执行管道、模式学习、任务分解等核心技术,系统可高效解析用户意图并执行复杂操作,显著提升企业运营效率并降低人工成本。其自适应学习能力与上下文感知机制,使自动化流程更智能、灵活,为企业数字化转型提供坚实支撑。
739 0
大型动作模型LAM:让企业重复任务实现80%效率提升的AI技术架构与实现方案
|
人工智能 监控 Kubernetes
稳定支撑大规模模型调用,携程旅游的 AI 网关实践
为了进一步提升服务水平和服务质量,携程很早就开始在人工智能大模型领域进行探索。而随着工作的深入,大模型服务的应用领域不断扩大,公司内部需要访问大模型服务的应用也越来越多,不可避免的就遇到了几个问题,我们自然就会想到使用网关来对这些服务接入进行统一管理,并增加各种切面上的流量治理功能。
1254 94

热门文章

最新文章