I’m working on something similar like

  
      github.com
  
  
    NVIDIA-AI-IOT/torch2trt/blob/master/torch2trt/torch2trt.py 4
import torch
import tensorrt as trt
from copy import copy
import numpy as np
from .calibration import TensorBatchDataset, DatasetCalibrator, DEFAULT_CALIBRATION_ALGORITHM


# UTILITY FUNCTIONS


def torch_dtype_to_trt(dtype):
    if dtype == torch.int8:
        return trt.int8
    elif dtype == torch.int32:
        return trt.int32
    elif dtype == torch.float16:
        return trt.float16
    elif dtype == torch.float32:
        return trt.float32
    else:


  This file has been truncated. show original

  
  
    
    
  
  


replacing some pytorch method/function , do some tracing , then replace them back.
most other function/method is ok besides torch.Tensor.getitem
here’s the example code
import torch

a=torch.randint(1,10,(15,))

b=torch.randint(1,10,(35,))

print(a[b]) # OK

print(b[a]) # OK

getitem = torch.Tensor.__getitem__

def new_getitem(self, key):

    print(key)

torch.Tensor.__getitem__ = new_getitem

torch.Tensor.__getitem__ = getitem

print(a[b]) # OK

print(b[a]) # IndexError: too many indices for tensor of dimension 1

it seems the behavior of getitem changed after replacing,  only accept index with larger size, why did this happened and how do I fix it?
thanks