Need a sample program for layer fusion

Hi all, I want to know how I should write code to perform layer fusion.
I want to modify the following sample files present in the tensorrt files to perform layer fusion

  1. /usr/src/tensorrt/samples/python/introductory_parser_samples/onnx_resnet50.py
  2. /usr/src/tensorrt/samples/python/network_api_pytorch_mnist/sample.py

or even if you give a general code to perform layer fusion then it is well and good.

I have already seen the trtexec code and no information is present in this. I need a fresh sample code to perform layer fusion

Thanks and Regards

Nagaraj Trivedi

Dear @trivedi.nagaraj,
layer fusion optimization happens automatically during build phase. Please see if Best Practices For TensorRT Performance :: NVIDIA Deep Learning TensorRT Documentation helps to clarify.

Hi SivaRamaKrishna, thank you for providing this information. I have read this link and have further clarifications.
These clarifications are w.r.t

  1. When the code parsers the pretrained model and builds the engine

    • In this case it says it tries to optimize by fusing layers which are possible. This information can be available (seen) through the log information logged by the ILogger.
      I am pasting the information from the document “To investigate which fusions have happened, or has not happened, the builder logs its operations to the logger object provided during construction. Optimization steps are at the kINFO log level. To see these messages, ensure you log them in the ILogger callback.”
      Let me know where in the code and how I should write the code to log for seeing this information?
  2. This is the second clarification I have. Let us take an example code provided in the path /usr/src/tensorrt/samples/python/network_api_pytorch_mnist/sample.py
    In this file it manually creates the network with the similar layers, weights and bias taken from the pretrained model (present in the file model.py in the same directory).

I have a question on this. Does it perform layer fusion automatically in this case also?. If ‘yes’ then provide me the information about how it does?. If ‘no’ then let me know how to perform it manually by ourselves. The order of the layers present in this network are

Input Layer ->CONV1->POOL1->CONV2->POOL2->FC1->RELU1->FC2

Request you to please provide this information.

Thanks and Regards

Nagaraj Trivedi

Hi all, please update me on this query.

Thanks and Regards

Nagaraj Trivedi

Dear @trivedi.nagaraj,
The layer fusion happen at the time of TRT engine building step. You can use verbose flag in trtexec to get more information about engine building. Please check trtexec --help for details.

Hi SivaRamaKrishna, it is not about the trtexec. I was specifically asking about the python sample program
/usr/src/tensorrt/samples/python/network_api_pytorch_mnist/sample.py

In this file how I can make use of TRT logger to dump layer fusion information. I have already pasted the information from the TensorRT SDK manual in the previous post. I am again pasting it here
“To investigate which fusions have happened, or has not happened, the builder logs its operations to the logger object provided during construction. Optimization steps are at the kINFO log level. To see these messages, ensure you log them in the ILogger callback.”

My query is how I can make use of the logger to get layer fusion information. How and from where the logger gets this information about the builder that it has fused few layers, particularly for the programs present in the python sample directory
/usr/src/tensorrt/samples/python/network_api_pytorch_mnist/

Thanks and Regards

Nagaraj Trivedi

Dear @trivedi.nagaraj,
Could you confirm if usingtrt.Logger.INFO at TensorRT/samples/python/network_api_pytorch_mnist/sample.py at release/8.6 · NVIDIA/TensorRT · GitHub gets the needed info?

See Logger — NVIDIA TensorRT Standard Python API Documentation 8.6.1 documentation and TensorRT: nvinfer1::ILogger Class Reference

Hi SivaRamaKrishnan, it is working. I have changed from WARNING to INFO and it worked.
Thank you for your timely help.

Thanks and Regards

Nagaraj Trivedi

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.