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大模型。
相关文章
|
6天前
|
人工智能 Java API
AI 超级智能体全栈项目阶段一:AI大模型概述、选型、项目初始化以及基于阿里云灵积模型 Qwen-Plus实现模型接入四种方式(SDK/HTTP/SpringAI/langchain4j)
本文介绍AI大模型的核心概念、分类及开发者学习路径,重点讲解如何选择与接入大模型。项目基于Spring Boot,使用阿里云灵积模型(Qwen-Plus),对比SDK、HTTP、Spring AI和LangChain4j四种接入方式,助力开发者高效构建AI应用。
309 122
AI 超级智能体全栈项目阶段一:AI大模型概述、选型、项目初始化以及基于阿里云灵积模型 Qwen-Plus实现模型接入四种方式(SDK/HTTP/SpringAI/langchain4j)
|
18天前
|
机器学习/深度学习 数据采集 人工智能
PyTorch学习实战:AI从数学基础到模型优化全流程精解
本文系统讲解人工智能、机器学习与深度学习的层级关系,涵盖PyTorch环境配置、张量操作、数据预处理、神经网络基础及模型训练全流程,结合数学原理与代码实践,深入浅出地介绍激活函数、反向传播等核心概念,助力快速入门深度学习。
71 1
|
8天前
|
机器学习/深度学习 人工智能 自然语言处理
AI Compass前沿速览:Qwen3-Max、Mixboard、Qwen3-VL、Audio2Face、Vidu Q2 AI视频生成模型、Qwen3-LiveTranslate-全模态同传大模型
AI Compass前沿速览:Qwen3-Max、Mixboard、Qwen3-VL、Audio2Face、Vidu Q2 AI视频生成模型、Qwen3-LiveTranslate-全模态同传大模型
AI Compass前沿速览:Qwen3-Max、Mixboard、Qwen3-VL、Audio2Face、Vidu Q2 AI视频生成模型、Qwen3-LiveTranslate-全模态同传大模型
|
9天前
|
人工智能 负载均衡 API
Vercel 发布 AI Gateway 神器!可一键访问数百个模型,助力零门槛开发 AI 应用
大家好,我是Immerse,独立开发者、AGI实践者。分享编程、AI干货、开源项目与个人思考。关注公众号“沉浸式趣谈”,获取独家内容。Vercel新推出的AI Gateway,统一多模型API,支持自动切换、负载均衡与零加价调用,让AI开发更高效稳定。一行代码切换模型,告别接口烦恼!
105 1
Vercel 发布 AI Gateway 神器!可一键访问数百个模型,助力零门槛开发 AI 应用
|
12天前
|
机器学习/深度学习 人工智能 自然语言处理
如何让AI更“聪明”?VLM模型的优化策略与测试方法全解析​
本文系统解析视觉语言模型(VLM)的核心机制、推理优化、评测方法与挑战。涵盖多模态对齐、KV Cache优化、性能测试及主流基准,助你全面掌握VLM技术前沿。建议点赞收藏,深入学习。
200 8
|
10月前
|
机器学习/深度学习 人工智能 算法
猫狗宠物识别系统Python+TensorFlow+人工智能+深度学习+卷积网络算法
宠物识别系统使用Python和TensorFlow搭建卷积神经网络,基于37种常见猫狗数据集训练高精度模型,并保存为h5格式。通过Django框架搭建Web平台,用户上传宠物图片即可识别其名称,提供便捷的宠物识别服务。
836 55
|
11月前
|
机器学习/深度学习 数据采集 数据可视化
TensorFlow,一款由谷歌开发的开源深度学习框架,详细讲解了使用 TensorFlow 构建深度学习模型的步骤
本文介绍了 TensorFlow,一款由谷歌开发的开源深度学习框架,详细讲解了使用 TensorFlow 构建深度学习模型的步骤,包括数据准备、模型定义、损失函数与优化器选择、模型训练与评估、模型保存与部署,并展示了构建全连接神经网络的具体示例。此外,还探讨了 TensorFlow 的高级特性,如自动微分、模型可视化和分布式训练,以及其在未来的发展前景。
897 5
|
11月前
|
机器学习/深度学习 人工智能 TensorFlow
基于TensorFlow的深度学习模型训练与优化实战
基于TensorFlow的深度学习模型训练与优化实战
470 3
|
机器学习/深度学习 人工智能 算法
鸟类识别系统Python+卷积神经网络算法+深度学习+人工智能+TensorFlow+ResNet50算法模型+图像识别
鸟类识别系统。本系统采用Python作为主要开发语言,通过使用加利福利亚大学开源的200种鸟类图像作为数据集。使用TensorFlow搭建ResNet50卷积神经网络算法模型,然后进行模型的迭代训练,得到一个识别精度较高的模型,然后在保存为本地的H5格式文件。在使用Django开发Web网页端操作界面,实现用户上传一张鸟类图像,识别其名称。
420 12
鸟类识别系统Python+卷积神经网络算法+深度学习+人工智能+TensorFlow+ResNet50算法模型+图像识别
|
11月前
|
机器学习/深度学习 人工智能 算法
基于Python深度学习的【垃圾识别系统】实现~TensorFlow+人工智能+算法网络
垃圾识别分类系统。本系统采用Python作为主要编程语言,通过收集了5种常见的垃圾数据集('塑料', '玻璃', '纸张', '纸板', '金属'),然后基于TensorFlow搭建卷积神经网络算法模型,通过对图像数据集进行多轮迭代训练,最后得到一个识别精度较高的模型文件。然后使用Django搭建Web网页端可视化操作界面,实现用户在网页端上传一张垃圾图片识别其名称。
433 0
基于Python深度学习的【垃圾识别系统】实现~TensorFlow+人工智能+算法网络

热门文章

最新文章