【解决方案】onnx 转 TensorRT (.plan, .trt) 报错 Error[10]: Could not find any implementation 或 OutOfMemory 问题

通过以下 bash 将动态输入输出的 .onnx 模型转化为 .plan 过程中,出现了 Error[10] Could not find any implementation for node {ForeignNode 报错

trtexec --onnx=swinir_real_sr_large_model_dynamic_sim_folded.onnx --saveEngine=model-folded.plan --timingCacheFile=model-folded.cache --minShapes=input:1x3x36x36 --optShapes=input:2x3x512x512 --maxShapes=input:3x3x1024x1024 --verbose

具体报错如下

[04/02/2024-06:02:45] [E] Error[10]: Could not find any implementation for node {ForeignNode[(Unnamed Layer* 89) [Constant] + (Unnamed Layer* 90) [Shuffle].../layers.0/patch_unembed/Transpose + /layers.0/patch_unembed/Reshape]}.
[04/02/2024-06:02:45] [E] Error[10]: [optimizer.cpp::computeCosts::3869] Error Code 10: Internal Error (Could not find any implementation for node {ForeignNode[(Unnamed Layer* 89) [Constant] + (Unnamed Layer* 90) [Shuffle].../layers.0/patch_unembed/Transpose + /layers.0/patch_unembed/Reshape]}.)
[04/02/2024-06:02:45] [E] Engine could not be created from network
[04/02/2024-06:02:45] [V] [TRT] Deleting timing cache: 5 entries, served 0 hits since creation.
[04/02/2024-06:02:45] [E] Building engine failed
[04/02/2024-06:02:45] [E] Failed to create engine from model or file.
[04/02/2024-06:02:45] [E] Engine set up failed

尝试各种方法后,最终发现主要问题其实是 Out of memory,但这个报错提供的信息是说不支持 node (Could not find any implementation for node ),非常非常具有误导性!

尝试方法1: polygraphy surgeon

使用 polygraphy surgeon 将 input_model.onnx 中的一些常量进行折叠,得到 folded_model.onnx

polygraphy surgeon sanitize --fold-constants input_model.onnx  -o folded_model.onnx

来源:https://github.com/NVIDIA/TensorRT/issues/3357

尝试方法2: onnx simplifier

使用 onnx-simplifier 对 onnx 模型进行简化, 将 input_onnx_model.onnx 简化为 output_onnx_model.onnx

# 先下载
pip3 install -U pip && pip3 install onnxsim
# 或者这样下载
pip install onnx-simplifier
# 将 input_onnx_model.onnx 简化为 output_onnx_model.onnx
onnxsim input_onnx_model.onnx output_onnx_model.onnx

成功简化后的提示,如果模型比较大,可能得等一段时间

Simplifying...
Finish! Here is the difference:
┏━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━┓
┃                    ┃ Original Model ┃ Simplified Model ┃
┡━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━┩
│ Add                │ 1955547              │
│ Cast               │ 27600                │
│ Concat             │ 1913404              │
│ Constant           │ 25917834              │
│ ConstantOfShape    │ 248518               │
│ Conv               │ 3636               │
│ Div                │ 381231              │
│ Equal              │ 205219               │
│ Erf                │ 5454               │
│ Expand             │ 243085               │
│ Gather             │ 2877601              │
│ LayerNormalization │ 110110              │
│ LeakyRelu          │ 2424               │
│ MatMul             │ 324324              │
│ Mod                │ 44                │
│ Mul                │ 2110165              │
│ Not                │ 542                │
│ Pad                │ 11                │
│ Range              │ 194464               │
│ Reshape            │ 2776605              │
│ Resize             │ 22                │
│ ScatterND          │ 48617               │
│ Shape              │ 6613236              │
│ Slice              │ 2335271              │
│ Softmax            │ 5454               │
│ Sub                │ 585                │
│ Transpose          │ 345293              │
│ Unsqueeze          │ 4194472              │
│ Where              │ 205221               │
│ Model Size         │ 125.4MiB       │ 114.7MiB         │
└────────────────────┴────────────────┴──────────────────┘

官方文档:https://pypi.org/project/onnx-simplifier/

最终解决方案

降低模型的显存占用!

  1. 减小最大输入的尺寸和 batch size: 这个没啥好说的,只能自己多试试
  2. 降低模型精度:一般用 fp16 或者 int8 就可以
    –noTF32 Disable tf32 precision (default is to enable tf32, in addition to fp32)
    –fp16 Enable fp16 precision, in addition to fp32 (default = disabled)
    –int8 Enable int8 precision, in addition to fp32 (default = disabled)
    –fp8 Enable fp8 precision, in addition to fp32 (default = disabled)

关于模型精度还不错的博客:

  • https://zhuanlan.zhihu.com/p/673708074
  • https://blog.csdn.net/szxcv9876/article/details/104589125
  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
TensorFlow中使用TensorRT进行加速时,有时可能会遇到"TF-TRT Warning: Could not find TensorRT Traceback (most recent call last)"的警告信息。这个警告通常表示TensorRT的安装或配置存在问题。 要解决这个问题,你可以按照以下步骤操作: 1. 首先,确保你已经按照正确的版本安装了TensorRT。你可以在TensorRT的官方文档中找到对应版本的安装指南。如果你下载的是特定版本的TensorRT,比如TensorRT-8.0.0.3.Linux.x86_64-gnu.cuda-10.2.cudnn8.2.tar.gz,请确保你正确地安装了该版本。 2. 你还可以尝试重新安装TensorRT,并确保安装过程中没有出现任何错误。如果之前安装TensorRT的过程中出现了错误或警告信息,请尝试解决这些问题并重新安装。 3. 如果你已经按照上述步骤正确地安装了TensorRT,但仍然遇到警告信息,那么有可能是TensorFlow和TensorRT之间的版本兼容性问题。你可以尝试使用与TensorRT兼容的TensorFlow版本,并确保你的代码和模型与这个版本匹配。 综上所述,要解决"TF-TRT Warning: Could not find TensorRT Traceback (most recent call last)"的警告信息,你可以检查TensorRT的安装和配置是否正确,尝试重新安装TensorRT,以及确保TensorFlow和TensorRT之间的版本兼容性。希望这些信息对你有帮助。 引用: ONNX-TensorRT: https://github.com/onnx/onnx-tensorrt TensorFlow Documentation: https://www.tensorflow.org/install/source#linux<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [ONNX-TensorRT:用于ONNXTensorRT后端-Python开发](https://download.csdn.net/download/weixin_42178963/19058078)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Ubuntu:E: 无法定位软件包 tensorrt(包括如何安装tensorrt)](https://blog.csdn.net/LYiiiiiii/article/details/120544390)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值