Why TensorRT ONNX parser fails, while parsing the ONNX model? Tips and tricks to win

Why TensorRT ONNX parser fails, while parsing the ONNX model? Tips and tricks to win

Well, till now you all know about ONNX (ONNX is an open format built to represent machine learning models). My previous article on ONNX can be seen here.

ONNX is cool, but sometimes it’s too complicated

The above line actually means a lot when you start getting TensroRT ONNX parser error. Below are the two most common errors (but not limited to) we get while parsing the ONNX model in TensroRT.

  1.  Please ensure the network was created using the EXPLICIT_BATCH NetworkDefinitionCreationFlag."
  2. Network validation failed.

The first error is more related to the dynamic batch size, for solving this problem, you can see my previous article. In addition to this, you can also open your ONNX file in notepad and manually put {?} [yes, a question mark sign] at the location of batch size. A more code-savvy audience can use the below code to do the same thing-

network_creation_flag = 1 <<        int(trt.NetworkDefinitionCreationFlag.EXPLICIT_PRECISION)

network_creation_flag |= 1 <<int(trt.NetworkDefinitionCreationFlag.EXPLICIT_BATCH)

self.network = self.builder.create_network(network_creation_flag)

Now problem 2 needs more in-depth analysis, as this is related to the ONNX format, opset version, etc…

So, let’s tame this problem

Simplify the ONNX transported model (ONNX Simplifier)

Let’s assume you exported your ONNX model, but have you ever seen the model graph of it? If not then, please do it once! You will find that ONNX model graph is way too complicated even for simple operations and when we talk about a complex model it’s like a cobweb.

Hence, our target is to simplify this complexity first.

ONNX Simplifier [GitHub] – It’s an open-source library which helps in simplifying this complex exported ONNX model. And this simplification, most of the time, solves the second problem of model parsing, stated above. For python lovers, you can directly install it using pip command –

pip3 install onnx-simplifier

ONNX Simplifier (Web version) - It works out of the box and doesn't need any installation. Just open the webpage, choose ONNX as the output format, check the ONNX simplifier, and then select your model to simplify. Note that the web version is in its very early stage, if the web version doesn't work well for you, you can install the Python version.

An example

Let see how a simple operation in PyTorch, when exported with ONNX gets complicated.

import torch
 
 
class JustReshape(torch.nn.Module):
    def __init__(self):
        super(JustReshape, self).__init__()
 
    def forward(self, x):
        return x.view((x.shape[0], x.shape[1], x.shape[3], x.shape[2]))
 
 
net = JustReshape()
model_name = 'just_reshape.onnx'
dummy_input = torch.randn(2, 3, 4, 5)
torch.onnx.export(net, dummy_input, model_name, input_names=['input'], output_names=['output'])

The above code takes a tensor and just reshape it.

Now, let’s visualize the graph using Netron. Netron is a viewer for neural networks, deep learning, and machine learning models.

No alt text provided for this image

Now we simplify the ONNX model and then again visualize it. This can be achieved by below lines of code

import onnx

from onnxsim import simplify


# load your predefined ONNX model

model = onnx.load('just_reshape' + '.onnx')


# convert model

model_simp, check = simplify(model)

onnx.save(model_simp,'simplified.onnx')

 And below is the simplified graph -

No alt text provided for this image

And this is what exactly, I was expecting my operation to be. While the default ONNX model looks so complicated, against the simplified ONNX model. This complication affects the parsing engine in TensorRT

So now you know that even simple operations, when exported can create complex operation graphs, which causes parsing failure while using TensorRT ONNX model parser engine.

The problems discussed above are a select one, there can be other reasons as well, I welcome any suggestion and ready to render my help in this respect.

Articles in this series

1- Model optimization for Fast Inference and Quantization

2- TensorRT Installation: Problems and "Way Arounds"

3 - ONNX Model: Export Using Pytorch, Problems, and Solutions

No alt text provided for this image

Till then enjoy oranges (mandarini)…!!

Thanks

要查看或添加评论,请登录

Pankaj Mishra (Ph.D)的更多文章

  • Principle of Prompting Large Language Models

    Principle of Prompting Large Language Models

    With all the hype around Large Language Models (LLM's), indeed, there are some new use cases that appeared, where these…

  • A meteoric rise of Diffusion Models: A simple understanding

    A meteoric rise of Diffusion Models: A simple understanding

    We all know the generative models (they have been around the corner for now a pretty long time) for example GAN…

    1 条评论
  • How fast is your Data Loader...?

    How fast is your Data Loader...?

    One of the bottleneck Computer Vision (CV) tasks is image loading. It can be a culprit behind the lag you are getting…

  • AI on Edge

    AI on Edge

    TFLite Model Maker: An approach to writing quick, efficient, and customizable AI models for edge devices. Well in…

  • Green AI: Time to know our CO2 load

    Green AI: Time to know our CO2 load

    As we stepped into the new year (2022), first of all, warm wishes to you all. I hope as humanity we grow stronger…

  • ONNX Model: Export Using Pytorch, Problems, and Solutions

    ONNX Model: Export Using Pytorch, Problems, and Solutions

    As we know that there a lot of AI or ML frameworks available in the market (paid or free version) to develop your AI/ML…

  • TensorRT Installation: Problems and "Way Arounds"

    TensorRT Installation: Problems and "Way Arounds"

    “EASIEST WAY TO USE TENSORRT IS DISCUSSED IN NEXT ARTICLE”…..

    5 条评论
  • Model optimization for Fast Inference and Quantization

    Model optimization for Fast Inference and Quantization

    Recently I have been working on a very interesting project with my sponsoring company beanTech and we were trying to…

  • Federated Learning: Example with Code

    Federated Learning: Example with Code

    This is third and last in the series of three articles about Federated Learning. If you missed the motivation and…

  • Federated Learning: Introduction

    Federated Learning: Introduction

    This is second in the series of three articles about Federated Learning. If you missed the motivation for Federated…

    2 条评论

社区洞察

其他会员也浏览了