使用Pydot和graphviz画TensorRT的Engine图

使用Pydot和graphviz画TensorRT的Engine图

TensorRT支持很多层的融合,目的是为了优化访存,减少数据在每层之间传输的消耗。

融之后的模型,一般无法通过Netron查看,毕竟TensorRT是闭源的,如果我们想看到融合后的模型长什么样,只要在build engine开启verbose模式即可。

复制代码
./trtexec --explicitBatch --onnx=debug.onnx --saveEngine=debug.trt  --verbose

[V] [TRT] Engine Layer Information:
Layer(Scale): QuantizeLinear_2_quantize_scale_node, Tactic: 0, input[Float(1,3,-17,-18)] -> 255[Int8(1,3,-17,-18)]
Layer(CaskConvolution): conv1.weight + QuantizeLinear_7_quantize_scale_node + Conv_9 + Relu_11, Tactic: 4438325421691896755, 255[Int8(1,3,-17,-18)] -> 267[Int8(1,64,-40,-44)]
Layer(CudaPooling): MaxPool_12, Tactic: -3, 267[Int8(1,64,-40,-44)] -> Reformatted Output Tensor 0 to MaxPool_12[Int8(1,64,-21,-24)]
Layer(Reformat): Reformatting CopyNode for Output Tensor 0 to MaxPool_12, Tactic: 0, Reformatted Output Tensor 0 to MaxPool_12[Int8(1,64,-21,-24)] -> 270[Int8(1,64,-21,-24)]
Layer(CaskConvolution): layer1.0.conv1.weight + QuantizeLinear_20_quantize_scale_node + Conv_22 + Relu_24, Tactic: 4871133328510103657, 270[Int8(1,64,-21,-24)] -> 284[Int8(1,64,-21,-24)]
Layer(CaskConvolution): layer1.0.conv2.weight + QuantizeLinear_32_quantize_scale_node + Conv_34 + Add_42 + Relu_43, Tactic: 4871133328510103657, 284[Int8(1,64,-21,-24)], 270[Int8(1,64,-21,-24)] -> 305[Int8(1,64,-21,-24)]

pyplot代码

https://github.com/pytorch/pytorch/pull/66431/files

engine_layer_visualize.py

这是jerryzh168大神开源的Facebook内部查看engine的工具,使用pydot和graphviz来画神经网络结构图

复制代码
pip install pydot
conda install python-graphviz

需要注意我们需要输入log_file也就是刚才开启Verbose的构建信息,然后profile_file则是使用TensorRT来profile的信息,最简单的可以通过trtexec这样获取到:

复制代码
./trtexec --loadEngine=debug_int8.trt --dumpProfile --shapes=input:1x3x512x512 --exportProfile=debug_profile

然后通过上述代码生成EngineLayers_0.dot

复制代码
import pydot

graphs = pydot.graph_from_dot_file("EngineLayers_0.dot")
graph = graphs[0]
graph.write_png("trt_engine.png")

TensorRT-engine 模型输入是Float而输出是Int8。这个模型是由TensorRT官方提供的pytorch-quantization[1]工具对Pytorch模型进行量化后导出ONNX,然后再由TensorRT-8转化得到的engine,这个engine的精度是INT8。

相关推荐
一只韩非子5 分钟前
什么是MCP?为什么引入MCP?(通俗易懂版)
人工智能·aigc·mcp
新智元8 分钟前
毛骨悚然!o3 精准破译照片位置,只靠几行 Python 代码?人类在 AI 面前已裸奔
人工智能·openai
Tech Synapse36 分钟前
电商商品推荐系统实战:基于TensorFlow Recommenders构建智能推荐引擎
人工智能·python·tensorflow
帅帅的Python36 分钟前
2015-2023 各省 GDP 数据,用QuickBI 进行数据可视化——堆叠图!
大数据·人工智能
weixin_430750931 小时前
智能小助手部署 Win10 + ollama的Deepseek + CentOS+ maxKB
linux·人工智能·机器学习·语言模型·自然语言处理·centos
Panesle1 小时前
大模型微调与蒸馏的差异性与相似性分析
人工智能·微调·蒸馏
多巴胺与内啡肽.1 小时前
深度学习--循环神经网络RNN
人工智能·rnn·深度学习
子燕若水1 小时前
解释PyTorch中的广播机制
人工智能·pytorch·python
计算机真好丸1 小时前
第R4周:LSTM-火灾温度预测
人工智能·rnn·lstm
数据与人工智能律师1 小时前
正确应对监管部门的数据安全审查
大数据·网络·数据库·人工智能·区块链