# I can't get result from TensorRT model

**URL:** <https://forums.developer.nvidia.com/t/i-cant-get-result-from-tensorrt-model/215842>\
**Category:** TensorRT\
**Tags:** tensorrt\
**Created:** [May 29, 2022, 12:28pm UTC](https://forums.developer.nvidia.com/t/i-cant-get-result-from-tensorrt-model/215842 "2022-05-29T12:28:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![837720757](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@837720757](https://forums.developer.nvidia.com/u/837720757)\
**Post date:** [May 29, 2022, 12:28pm UTC](https://forums.developer.nvidia.com/t/i-cant-get-result-from-tensorrt-model/215842/1 "2022-05-29T12:28:29Z")

</div>

## Description

I tried to convert the GPT model from pytorch to onnx and then to tensorRT, I successfully converted to tensorRT engine, but I can’t get the results I want during the inference phase, I can guarantee that the onnx model is correct. These two warnings appeared in the process of converting the onnx model to the tensorRT engine. I don’t know if these two warnings will affect the engine conversion.

[05/29/2022-19:08:00] [TRT] [W] onnx2trt\_utils.cpp:392: One or more weights outside the range of INT32 was clamped  
[05/29/2022-19:08:01] [TRT] [W] ShapedWeights.cpp:173: Weights transformer.h.8.attn.c\_attn.weight has been transposed with permutation of (1, 0)! If you plan on overwriting the weights with the Refitter API, the new weights must be pre-transposed.

**The code that onnx converts to tensorRT：**

```auto
import tensorrt as trt

logger = trt.Logger(trt.Logger.WARNING)

builder = trt.Builder(logger)

network = builder.create_network(1 << int(trt.NetworkDefinitionCreationFlag.EXPLICIT_BATCH))

parser = trt.OnnxParser(network, logger)

success = parser.parse_from_file('model.onnx')
# for idx in range(parser.num_errors):
# print(parser.get_error(idx))

if not success:
    pass # Error handling code here

config = builder.create_builder_config()
#config.set_memory_pool_limit(trt.MemoryPoolType.WORKSPACE, 1 << 20) # 1 MiB
config.max_workspace_size = 1 << 31

profile = builder.create_optimization_profile()  
profile.set_shape("input_ids", (1, 1), (1, 20), (1, 300))
profile.set_shape("token_type_ids", (1, 1), (1, 20), (1, 300))
config.add_optimization_profile(profile)

serialized_engine = builder.build_serialized_network(network, config)
with open("sample4.engine", "wb") as f:
    f.write(serialized_engine)

```

**The main code to inference, input\_ids and token\_type\_ids is two input for the model.**

```auto
context.active_optimization_profile = 0
origin_inputshape = context.get_binding_shape(0)
origin_inputshape[0],origin_inputshape[1] = input_ids.shape
context.set_binding_shape(0,(origin_inputshape))
context.set_binding_shape(1,(origin_inputshape))

inputs, outputs, bindings, stream = common.allocate_buffers(engine)
inputs[1].host = input_ids
inputs[0].host = token_type_ids

logits, *_= common.do_inference_v2(context,bindings = bindings, inputs= inputs, outputs=outputs, stream = stream)

```

 ![1234](https://global.discourse-cdn.com/nvidia/original/3X/2/e/2e621619e74af17e20aac57d26eebcae598bcf72.png)

the model I want to convert is OpenAIGPTLMHeadModel, I can only put one link, but you can cheack it from huggingface

## Environment

**TensorRT Version** : 8.2.5.1  
**GPU Type** : RTX 3060  
**Nvidia Driver Version** : 497.38  
**CUDA Version** : 11.5.1  
**CUDNN Version** : 8.2.1.32  
**Operating System + Version** : Windows11  
**Python Version (if applicable)**: 3.8.13  
**TensorFlow Version (if applicable)**:  
**PyTorch Version (if applicable)**: 1.11  
**Baremetal or Container (if container which image + tag)**:

## Relevant Files

github link to my code  
RuntensorRT is inference phase

> **[GitHub - YBAgg/onnx2tensorRT](https://github.com/YBAgg/onnx2tensorRT)**
>
> Contribute to YBAgg/onnx2tensorRT development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![NVES](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/nves/32/14043_2.png) [@NVES](https://forums.developer.nvidia.com/u/NVES)\
**Post date:** [May 29, 2022, 12:37pm UTC](https://forums.developer.nvidia.com/t/i-cant-get-result-from-tensorrt-model/215842/2 "2022-05-29T12:37:47Z")

</div>

Hi,  
Request you to share the ONNX model and the script if not shared already so that we can assist you better.  
Alongside you can try few things:

> **[Quick Start Guide — NVIDIA TensorRT](https://docs.nvidia.com/deeplearning/tensorrt/latest/getting-started/quick-start-guide.html)**

1. validating your model with the below snippet

# check\_model.py

import sys  
import onnx  
filename = yourONNXmodel  
model = onnx.load(filename)  
onnx.checker.check\_model(model).  
2) Try running your model with trtexec command.

> **[TensorRT/samples/trtexec at master · NVIDIA/TensorRT](https://github.com/NVIDIA/TensorRT/tree/master/samples/trtexec)**
>
> NVIDIA® TensorRT™ is an SDK for high-performance deep learning inference on NVIDIA GPUs. This repository contains the open source components of TensorRT. - NVIDIA/TensorRT

In case you are still facing issue, request you to share the trtexec “”–verbose"" log for further debugging  
Thanks!

---

<div class="post-metadata">

**Author:** ![837720757](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@837720757](https://forums.developer.nvidia.com/u/837720757)\
**Post date:** [May 29, 2022, 1:18pm UTC](https://forums.developer.nvidia.com/t/i-cant-get-result-from-tensorrt-model/215842/4 "2022-05-29T13:18:53Z")

</div>

1.Validation results

 ![image](https://global.discourse-cdn.com/nvidia/original/3X/1/f/1f667aca0726d42ea6be3abd94e72c6661605991.jpeg)  
2.I try run trtexec with ‘./trtexec --onnx=D:\Subject\dialogue\CDial-GPT\model.onnx --saveEngine=D:\Subject\dialogue\CDial-GPT\sample.engine --fp16 --workspace=10000 --minShapes=input\_ids:1x1,token\_type\_ids:1x1 --optShapes=input\_ids:1x300,token\_type\_ids:1x300 --maxShapes=input\_ids:1x300,token\_type\_ids:1x300 --device=0 --verbose --exportTimes=trace.json’  
here are all the logs I can get  
[logs.txt](https://forums.developer.nvidia.com/uploads/short-url/gW5MRCuTK20w5BnTxsaQrfNGhcE.txt) (683.1 KB)

---

<div class="post-metadata">

**Author:** ![837720757](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@837720757](https://forums.developer.nvidia.com/u/837720757)\
**Post date:** [May 29, 2022, 1:31pm UTC](https://forums.developer.nvidia.com/t/i-cant-get-result-from-tensorrt-model/215842/5 "2022-05-29T13:31:22Z")

</div>

The onnx file is too big to upload,I am uploading the onnx model to google drive, can I have your email so I can share with you, or you have a more convenient way.

---

<div class="post-metadata">

**Author:** ![837720757](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@837720757](https://forums.developer.nvidia.com/u/837720757)\
**Post date:** [May 29, 2022, 1:35pm UTC](https://forums.developer.nvidia.com/t/i-cant-get-result-from-tensorrt-model/215842/6 "2022-05-29T13:35:21Z")

</div>

It’s trace.josn  
[trace.json](https://forums.developer.nvidia.com/uploads/short-url/q6VkTFH2VHFpOYVBV0yjuU6o9SX.json) (169.2 KB)

---

<div class="post-metadata">

**Author:** ![837720757](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@837720757](https://forums.developer.nvidia.com/u/837720757)\
**Post date:** [May 29, 2022, 1:38pm UTC](https://forums.developer.nvidia.com/t/i-cant-get-result-from-tensorrt-model/215842/7 "2022-05-29T13:38:24Z")

</div>

I have reply blow my question, please check.

---

<div class="post-metadata">

**Author:** ![837720757](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@837720757](https://forums.developer.nvidia.com/u/837720757)\
**Post date:** [May 29, 2022, 1:51pm UTC](https://forums.developer.nvidia.com/t/i-cant-get-result-from-tensorrt-model/215842/8 "2022-05-29T13:51:07Z")

</div>

The result is no longer 0, but the dimension is still wrong, the correct dimension is 3.

 ![image](https://global.discourse-cdn.com/nvidia/original/3X/d/6/d666b95286cbe08f80e5844e7248b087ccc2d19f.png)

---

<div class="post-metadata">

**Author:** ![837720757](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@837720757](https://forums.developer.nvidia.com/u/837720757)\
**Post date:** [May 29, 2022, 2:08pm UTC](https://forums.developer.nvidia.com/t/i-cant-get-result-from-tensorrt-model/215842/9 "2022-05-29T14:08:19Z")

</div>

The problem seems to be in _**allocate\_buffers(engine):**_, I changed the **size** to quantitative before, because the **size** obtained by _**trt.volume(engine.get\_binding\_shape(binding)) \* engine.max\_batch\_size**_ is a negative number, so in _**host\_mem = cuda.pagelocked\_empty(size, dtype)**_, the error is ‘pycuda.\_driver.MemoryError: cuMemHostAlloc failed: out of memory’ , how can I solve this problem?

```auto
def allocate_buffers(engine):
    inputs = []
    outputs = []
    bindings = []
    stream = cuda.Stream()
    for binding in engine:
        print(engine.get_binding_shape(binding))
        size = trt.volume(engine.get_binding_shape(binding)) * engine.max_batch_size
        dtype = trt.nptype(engine.get_binding_dtype(binding))
        # Allocate host and device buffers
        host_mem = cuda.pagelocked_empty(size, dtype)
        device_mem = cuda.mem_alloc(host_mem.nbytes)
        # Append the device buffer to device bindings.
        bindings.append(int(device_mem))
        # Append to the appropriate list.
        if engine.binding_is_input(binding):
            inputs.append(HostDeviceMem(host_mem, device_mem))
        else:
            outputs.append(HostDeviceMem(host_mem, device_mem))
    return inputs, outputs, bindings, stream

```

---

<div class="post-metadata">

**Author:** ![spolisetty](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/spolisetty/32/14047_2.png) [@spolisetty](https://forums.developer.nvidia.com/u/spolisetty)\
**Post date:** [May 31, 2022, 1:13pm UTC](https://forums.developer.nvidia.com/t/i-cant-get-result-from-tensorrt-model/215842/10 "2022-05-31T13:13:07Z")

</div>

Hi,

The above error is related to dimensions, maybe you’re not handling the dynamic shape correctly.  
Could you use `context.get_binding_shape` correctly for the engine with dynamic shape.

> **[Developer Guide :: NVIDIA Deep Learning TensorRT Documentation](https://docs.nvidia.com/deeplearning/tensorrt/developer-guide/index.html#work_dynamic_shapes)**
>
> This is the revision history of the NVIDIA TensorRT 8.4 Developer Guide.

> **[Developer Guide :: NVIDIA Deep Learning TensorRT Documentation](https://docs.nvidia.com/deeplearning/tensorrt/developer-guide/index.html#runtime_dimensions)**
>
> This is the revision history of the NVIDIA TensorRT 8.4 Developer Guide.

Please share with us issue repro script and model to try from our end if you still face this issue.

Thank you.
