NVIDIA / NVIDIA/TensorRT

LSTM model converted to TensorRT is slower than PyTorch on RTX 4090

Open
#4,490 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

internal-bug-tracked Module:Performance triaged
Dominant language
C++
Stars
13.4k
Forks
2.4k
Avg merge
5d 3h
Merged PRs (30d)
2

Description

System Information

  • OS: Ubuntu 22.04
  • GPU: NVIDIA RTX 4090
  • TensorRT Version: 10.11.0.33
  • PyTorch Version: 2.7.0
  • ONNX Opset: 14

🧠 Problem Summary

I converted a very basic bidirectional LSTM model from PyTorch to ONNX, and then to TensorRT using trtexec. However, inference with the TensorRT engine is slower than PyTorch, which is unexpected.

  • PyTorch: ~0.5ms per forward pass
  • TensorRT: ~1ms per forward pass

📦 Model Description
# Model config
INPUT_SIZE   = 293
HIDDEN_SIZE  = 128
NUM_LAYERS   = 2
BIDIRECTION  = True
BATCH_FIRST  = True
DROPOUT      = 0.0

# Model instantiation
lstm = nn.LSTM(INPUT_SIZE, HIDDEN_SIZE, NUM_LAYERS,
               bidirectional=BIDIRECTION,
               batch_first=BATCH_FIRST,
               dropout=DROPOUT)

🔄 Conversion Steps
  1. Export to ONNX:
dummy = torch.randn(32, 60, INPUT_SIZE)

torch.onnx.export(
    lstm, dummy, "pyannet_lstm.onnx",
    opset_version=14,
    input_names=["input"],
    output_names=["output", "h_out", "c_out"],
    dynamic_axes={
        "input":  {0: "batch", 1: "time"},
        "output": {0: "batch", 1: "time"},
        "h_out":  {1: "batch"},
        "c_out":  {1: "batch"}
    }
)
  1. Build TensorRT engine:
trtexec \
  --onnx=pyannet_lstm.onnx \
  --minShapes=input:1x60x293 \
  --optShapes=input:32x60x293 \
  --maxShapes=input:32x60x293 \
  --saveEngine=lstm.engine

🧪 Performance Benchmark

PyTorch benchmark code:

x = torch.randn(32, 60, 293).cuda()
lstm.to("cuda").eval()

with torch.no_grad():
    for _ in range(1000):
        output, (h, c) = lstm(x)
  • Average PyTorch time per batch: ~0.5ms
  • Average TensorRT time per batch: ~1.0ms

🔍 Profiling Observation

Using NVIDIA Nsight Systems, I observed:

  • PyTorch uses a fused kernel: RNN_blockPersist_fp_LSTM
  • TensorRT seems to decompose the model into many small ops instead of using a fused LSTM kernel

❓Questions
  • Is it expected that TensorRT does not fuse the LSTM into a single kernel like RNN_blockPersist_fp_LSTM?
  • Are there flags or version requirements to enable such fusion?
  • Is this a known limitation with ONNX -> TensorRT conversion for LSTM?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided PyTorch export code, the trtexec command, and the Nsight Systems observation comparing RNN_blockPersist_fp_LSTM with TensorRT's smaller operations. Reproduce the reported timings on the stated RTX 4090, then determine whether the conversion uses an equivalent fused LSTM path and document the relevant flags, version requirements, or limitation.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
machine-learning, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.