tensorrt: pycuda, onnx, onnxruntime, tensorrt,torch-tensorrt 安装

tensorrt: pycuda, onnx, onnxruntime, tensorrt,torch-tensorrt 安装

pytorch: python

libtorch:c++

tensorRT: 适用于 nvidia的gpu
Intel cpu:使用openvino
amd gpu:请使用opencl和ROCm

1.深度学习模型部署综述(ONNX/NCNN/OpenVINO/TensorRT)

https://developer.aliyun.com/article/1143572?spm=a2c6h.14164896.0.0.56464f534k2fRI
https://docs.nvidia.com/deeplearning/tensorrt/sample-support-guide/index.html

tensorRT:
tensorRT 是一个C++库,从 TensorRT 3 开始提供C++ API和Python API,主要用来针对 NVIDIA GPU进行 高性能推理(Inference)加速。

目前TensorRT4.0 几乎可以支持所有常用的深度学习框架,对于caffe和TensorFlow来说,tensorRT可以直接解析他们的网络模型;对于caffe2,pytorch,mxnet,chainer,CNTK等框架则是首先要将模型转为 ONNX 的通用深度学习模型,然后对ONNX模型做解析。而tensorflow和MATLAB已经将TensorRT集成到框架中去了。

ONNX(Open Neural Network Exchange )是微软和Facebook携手开发的开放式神经网络交换工具,也就是说不管用什么框架训练,只要转换为ONNX模型,就可以放在其他框架上面去inference。这是一种统一的神经网络模型定义和保存方式,上面提到的除了tensorflow之外的其他框架官方应该都对onnx做了支持,而ONNX自己开发了对tensorflow的支持。从深度学习框架方面来说,这是各大厂商对抗谷歌tensorflow垄断地位的一种有效方式;从研究人员和开发者方面来说,这可以使开发者轻易地在不同机器学习工具之间进行转换,并为项目选择最好的组合方式,加快从研究到生产的速度。
https://arleyzhang.github.io/archives/ 关于caffe model使用tensorRT的例子
这里以pytorch为例:
因此第一步:首先将model.pt文件转换为ONNX文件

2.安装tensorrt相关

  1. pycuda版本安装:
    https://www.lfd.uci.edu/~gohlke/pythonlibs/?cm_mc_uid=08085305845514542921829&cm_mc_sid_50200000=1456395916#pycuda
    找不到对应版本的话,尝试:

    pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple 
    conda install -c conda-forge pycuda
    

    pycuda使用入门:https://www.jianshu.com/p/85b536311f9f

  2. onnx安装

    pip install -i https://pypi.tuna.tsinghua.edu.cn/simple onnx

  3. tensorRT安装和示例
    首先查看自己的cuda和cudnn版本

    然后下载对应版本的tensorRT:
    Go to: https://developer.nvidia.com/nvidia-tensorrt-8x-download
    Click GET STARTED, then click Download Now.
    Select the version of TensorRT that you are interested in.
    Select the check-box to agree to the license terms.
    Click the package you want to install. Your download begins.

    然后解压:
    tar -zxvf TensorRT-8.5.1.7.Linux.x86_64-gnu.cuda-11.8.cudnn8.6.tar.gz
    然后添加环境变量,参考:https://zhuanlan.zhihu.com/p/371239130

    然后安装tensorrt python 和 onnxruntime:https://zhuanlan.zhihu.com/p/467401558

    安装tensorrt python:

    cd TensorRT-8.4.1.5/python/
    pip3 install tensorrt-8.2.1.8-cp36-none-linux_x86_64.whl
    
  4. tensorRT 部署流程
    https://docs.nvidia.com/deeplearning/tensorrt/archives/tensorrt-853/quick-start-guide/index.html#install
    https://github.com/NVIDIA/TensorRT/tree/release/8.6/quickstart/IntroNotebooks

    https://docs.nvidia.com/deeplearning/tensorrt/archives/tensorrt-861/quick-start-guide/index.html#run-engine-c

  5. torch-tensorrt 介绍:
    安装: pip install torch-tensorrt==1.4.0
    Accelerating Inference Up to 6x Faster in PyTorch with Torch-TensorRT
    https://developer.nvidia.com/blog/accelerating-inference-up-to-6x-faster-in-pytorch-with-torch-tensorrt/

    示例:关于torch-tensorrt的教程很好的
    https://github.com/pytorch/TensorRT/tree/main/notebooks
    https://pytorch.org/TensorRT/tutorials/getting_started_with_cpp_api.html

猜你喜欢

转载自blog.csdn.net/tywwwww/article/details/134320320