# Outputs of pytorch and tensorrt be accurately aligned

**URL:** <https://forums.developer.nvidia.com/t/outputs-of-pytorch-and-tensorrt-be-accurately-aligned/285621>\
**Category:** TensorRT\
**Tags:** cudnn\
**Created:** [March 11, 2024, 12:17pm UTC](https://forums.developer.nvidia.com/t/outputs-of-pytorch-and-tensorrt-be-accurately-aligned/285621 "2024-03-11T12:17:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![2087025789](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@2087025789](https://forums.developer.nvidia.com/u/2087025789)\
**Post date:** [March 11, 2024, 12:17pm UTC](https://forums.developer.nvidia.com/t/outputs-of-pytorch-and-tensorrt-be-accurately-aligned/285621/1 "2024-03-11T12:17:17Z")

</div>

## Description

In fact, I am currently working on a project to verify whether the outputs of pytorch and tensorrt can be accurately aligned.

I have tested both cases of conv and conv+bn, and the netron visualizations of the two models are shown in the following figure.

 ![resnet50-part-conv](https://global.discourse-cdn.com/nvidia/original/4X/0/8/e/08e73d752f38c81286dd0866acb7e7471ffb92c2.png)  
 ![resnet50-part-cb](https://global.discourse-cdn.com/nvidia/original/4X/f/3/6/f3660a79198d990849de5e450bca775bbf5bb7ac.png)

After testing, under model conv, the results of pytorch and tensorrt are completely consistent.

However, under model conv+bn, there is a difference of approximately 1e-6 in accuracy between the results of pytorch and tensorrt.

I suspect that Tensorrt did some optimization work, such as operator fusion. When building the Tensorrt engine, I added – builderOptimizationLevel=0, I think operator fusion may not be performed at this optimization level, but there is still a slight difference in accuracy between the final results of pytorch and tensorrt.

So, may I ask what is the internal reason for the difference in results, and is there any way to make this difference disappear

## Environment

**TensorRT Version** : 8.6.1  
**GPU Type** : GTX1060  
**Nvidia Driver Version** : 545.29.06  
**CUDA Version** : 12.1  
**CUDNN Version** : 8.9.7  
**Operating System + Version** : ubuntu20.04  
**Python Version (if applicable)**: python3.8  
**TensorFlow Version (if applicable)**: NO  
**PyTorch Version (if applicable)**: 2.1.2+cu121  
**Baremetal or Container (if container which image + tag)**: NO

## Relevant Files

my onnx(from pytorch) and trt models are here:  
[test\_model.zip](https://forums.developer.nvidia.com/uploads/short-url/v1Igc4k97KokP28RvSMqGrmW6Hf.zip) (144.4 KB)

## Steps To Reproduce

The following is a screenshot of the source code for torchvision.resnet. I added a return to end the inference earlier.

 ![2024-03-11 20-08-03](https://global.discourse-cdn.com/nvidia/original/4X/1/a/3/1a30b81bb01888e094765077198f8e0c397f6dd1.png)

After entering a data for infer, you can also obtain that the result of “resnet50-part-conv” is the same, but there are differences in the result of “resnet50-part-cb”.

---

<div class="post-metadata">

**Author:** ![AakankshaS](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/aakankshas/32/14047_2.png) [@AakankshaS](https://forums.developer.nvidia.com/u/AakankshaS)\
**Post date:** [March 30, 2024, 9:41am UTC](https://forums.developer.nvidia.com/t/outputs-of-pytorch-and-tensorrt-be-accurately-aligned/285621/2 "2024-03-30T09:41:19Z")

</div>

Hi @2087025789 ,  
I am checking on this with Engineering team and shall update you soon.

Thanks
