TensorRT icon indicating copy to clipboard operation
TensorRT copied to clipboard

CUDNN_STATUS_BAD_PARAM when infer with dynamic shape

Open ysyyork opened this issue 5 years ago • 7 comments

Description

I'm using dynamic shaped trt engine generated from trtexec to do serving but I met this error:

 [TensorRT] ERROR: ../rtSafe/cuda/cudaConvolutionRunner.cpp (483) - Cudnn Error in executeConv: 3 (CUDNN_STATUS_BAD_PARAM)
 [TensorRT] ERROR: FAILED_EXECUTION: std::exception

Environment

TensorRT Version: 7.2.3.4 NVIDIA GPU: 2080 ti NVIDIA Driver Version: 460.73.01 CUDA Version: 11.2 CUDNN Version: 8 Operating System: Ubuntu 18.04 Python Version (if applicable): 3.6.9 Tensorflow Version (if applicable): 2.4.0 PyTorch Version (if applicable): Baremetal or Container (if so, version):

Relevant Files

import tensorrt as trt
import pycuda.driver as cuda
import threading, numpy as np
import functools, operator

INITED = False
LOCK = threading.Lock()

def init():
    global TRT_LOGGER, runtime, cfx, INITED
    LOCK.acquire()
    if not INITED:
        TRT_LOGGER = trt.Logger(trt.Logger.INFO)
        trt.init_libnvinfer_plugins(TRT_LOGGER, '')
        runtime = trt.Runtime(TRT_LOGGER)
        cfx = cuda.Device(0).make_context()
        INITED = True
    LOCK.release()


class TRTInference:
    def __init__(self, trt_engine_path, max_batch_size=1):
        init()
        stream = cuda.Stream()

        # deserialize engine
        with open(trt_engine_path, 'rb') as f:
            buf = f.read()
            engine = runtime.deserialize_cuda_engine(buf)
        context = engine.create_execution_context()

        # prepare buffer
        host_inputs = []
        cuda_inputs = []
        host_outputs = []
        cuda_outputs = []
        bindings = []

        self.max_batch_size = max_batch_size

        for binding in engine:
            dim = engine.get_binding_shape(binding)[1:]
            size = functools.reduce(operator.mul, dim) * self.max_batch_size

            host_mem = cuda.pagelocked_empty(size, np.float32)
            cuda_mem = cuda.mem_alloc(host_mem.nbytes)

            bindings.append(int(cuda_mem))
            if engine.binding_is_input(binding):
                self.input_dim = dim
                self.input_unit_size = functools.reduce(operator.mul, dim)
                host_inputs.append(host_mem)
                cuda_inputs.append(cuda_mem)
            else:
                self.output_dim = dim
                self.output_unit_size = functools.reduce(operator.mul, dim)
                host_outputs.append(host_mem)
                cuda_outputs.append(cuda_mem)

        # store
        self.stream = stream
        self.context = context
        self.engine = engine

        self.host_inputs = host_inputs
        self.cuda_inputs = cuda_inputs
        self.host_outputs = host_outputs
        self.cuda_outputs = cuda_outputs
        self.bindings = bindings


    def infer(self, data):
        threading.Thread.__init__(self)
        batch_size = data.shape[0]
        data = np.ravel(data)

        cfx.push()

        # restore
        stream = self.stream
        context = self.context
        context.set_binding_shape(0, (batch_size, *self.input_dim))

        host_inputs = self.host_inputs
        cuda_inputs = self.cuda_inputs
        host_outputs = self.host_outputs
        cuda_outputs = self.cuda_outputs
        bindings = self.bindings

        # read image
        np.copyto(host_inputs[0][:batch_size * self.input_unit_size], data)

        # inference
        cuda.memcpy_htod_async(cuda_inputs[0], host_inputs[0], stream)
        context.execute_async(bindings=bindings, stream_handle=stream.handle)
        cuda.memcpy_dtoh_async(host_outputs[0], cuda_outputs[0], stream)
        stream.synchronize()

        cfx.pop()

        return np.reshape(host_outputs[0][:batch_size * self.output_unit_size], (batch_size, *self.output_dim))

    def destory(self):
        cfx.pop()

Engine model: https://drive.google.com/file/d/1es2HktMGl4_murFvmRjE6OG1ki_Tm_dl/view?usp=sharing

Steps To Reproduce

  1. Use the model I provided
  2. Use the TRTInference class above
  3. do the below:
for i in range(1, 65):
    model.infer(np.ones((i, 260, 260, 15), dtype=np.float32))

You will see there are some batch sizes working well but some are not. Very weird. This model i generated, the min, opt, and max batch size are 1, 4, 64 respectively. I also tried using different combination of batch sizes but the behavior seems random. there will always be some random batch size not working. Very strange!

I also tried some other model structures, but same results.

However, if you use trtexec --loadEngine and set the shape to a fixed one, all batch sizes work.

One side note: this model is from tensorflow -> onnx -> trt

ysyyork avatar May 31 '21 14:05 ysyyork

Encountered the same problem with: https://ngc.nvidia.com/catalog/models/nvidia:nemo:stt_en_citrinet_1024 Is there any workaround or solution?

durson avatar Jun 09 '21 16:06 durson

where are the NVIDIA people ???

ysyyork avatar Jun 10 '21 03:06 ysyyork

The same problem happened to me today

TensorRT Version: 7.2.3.4 NVIDIA GPU: 2080 ti NVIDIA Driver Version: 460.73.01 CUDA Version: 11.1 CUDNN Version: 8 Operating System: Windows 10

JiaLiangAl avatar Jun 23 '21 11:06 JiaLiangAl

hello @ysyyork , could you take a try:

  • make trt.Runtime per thread resource
  • all the cuda.mem* API also called within the range of cfx.push() and cfx.pop()
  • make sure there is no other code using cuda in your script (e.g, pytorch, tf and cupy)

Thanks

ttyio avatar Jul 02 '21 10:07 ttyio

@ttyio what is the main reason when generate the trt (onnx 2 trt? instanceNormalizationPlugin.cu (292) - Cudnn Error in enqueue: 3 (CUDNN_STATUS_BAD_PARAM)

alicera avatar Jul 29 '21 04:07 alicera

Hello @alicera , there are some global state in cudnn, and the state might invalid when mix the framework code and trt code in the same cuda context.

ttyio avatar Aug 02 '21 05:08 ttyio

close since no activity for more than 3 weeks, please reopen if you still have question, thanks!

ttyio avatar Jan 25 '22 01:01 ttyio