onnx-tensorrt icon indicating copy to clipboard operation
onnx-tensorrt copied to clipboard

Bug for F.Interpolate during conversion from Torch->Onnx->TensorRT

Open jovialio opened this issue 4 years ago • 3 comments

Hi,

There is a bug in the conversion F.Interpolate operation from Torch->Onnx->TensorRT. F.Interpolate was able to conversion from Torch to Onnx but not from Onnx to TensorRT.

This is due to scale_factor being converted to Double Type that is not allowed in TensorRT conversion. Below is work around code by manually calculating the final size to input size instead of scale_factor during interpolation.

Bug was found in Pytorch=1.4 Opset=11 TensorRT=7.2.1.6

def forward(self, x):
  # TODO: Temp solution to enable Torch->Onnx->TensorRT to work scale_factor in kwdargs was given as Double type which cause error in conversion
  resized_h = x.size()[2]
  resize_w = x.size()[3]
  temp_dict = self.kwdargs.copy()
  scale_factor = self.kwdargs['scale_factor']
  temp_dict['size'] = (resized_h*scale_factor, resize_w*scale_factor)
  del temp_dict['scale_factor']
  
  return F.interpolate(x, *self.args, **temp_dict)
  #return F.interpolate(x, *self.args, **self.kwdargs)

jovialio avatar Jan 08 '21 03:01 jovialio

Thanks @jovialio for helping to root cause the issue. Since TRT does not accept the DOUBLE type at the moment, using the sizes parameter for the resize/interpolate op is a good WAR.

I'll do more investigation on my end on the proper way of supporting this, and this may involve opening up an issue upstream to pytorch to see why they are exporting DOUBLE types for values that can be presented by FLOATs.

kevinch-nv avatar Jan 08 '21 16:01 kevinch-nv

I have the same problem. Thank you very much for the solve. But one more question, where should I inserted the custom code? Rewrite the F.interpolate function in pytorch? or just onnx export model?

simmed00 avatar Mar 03 '21 02:03 simmed00

The change should be done in the ONNX model export step.

However, in the latest TensorRT release we support DOUBLE / FLOAT types for resize scales. You can try the latest TensorRT version

kevinch-nv avatar Jun 16 '22 19:06 kevinch-nv