pytorch / pytorch/TensorRT

🐛 [Bug] BUGS encountered when using Dynamic shapes

Open
#2,529 3 comments 0 reactions 1 assignee View on GitHub

@peri044 is already working on this.

Since Dec 12, 2023.

bug story: Dynamic Shapes & Symbolic Tracing
Dominant language
Python
Stars
3k
Forks
410
Avg merge
3d 18h
Merged PRs (30d)
78

Description

Bug Description

I am trying to speed up inference on huggingface Chinese BERT (https://huggingface.co/bert-base-chinese), and I need to do dynamic dimension inference acceleration.But errors in the dynamic dimension confuse me

To Reproduce

When I use the Tensorrt API, I can achieve dynamic dimension acceleration normally. The key code is as follows

profile.set_shape(network.get_input(0).name, (1, 1), (16, 64), (32, 128))
profile.set_shape(network.get_input(1).name, (1, 1), (16, 64), (32, 128))

But when I use torch_ tensorrt. compile, i got an error message. the key code is as follows

trt_model = torch_tensorrt.compile(
    traced_mlm_model, 
    inputs= (torch_tensorrt.Input(min_shape=[1, 1],opt_shape=[16, 64],max_shape=[32, 128],dtype=torch.int32),  # input_ids
             torch_tensorrt.Input(min_shape=[1, 1],opt_shape=[16, 64],max_shape=[32, 128],dtype=torch.int32)),  # attention_mask
    enabled_precisions= {torch.float32}, # Run with 32-bit precision
    workspace_size=2000000000,
    truncate_long_and_double=True,
    debug=True
)

the error message is as follows

ERROR: [Torch-TensorRT TorchScript Conversion Context] - 4: [shapeContext.cpp::operator()::3602] Error Code 4: Shape Error (reshape dimension with more than one -1 wildcard. Reshaping [(+ (MAX 0 (+ (# 0 (SHAPE input_1)) -1)) 1),(+ (MAX 0 (+ (# 1 (SHAPE input_1)) -1)) 1)] to [-1,1,-1].)
ERROR: [Torch-TensorRT TorchScript Conversion Context] - 4: [graphShapeAnalyzer.cpp::needTypeAndDimensions::2212] Error Code 4: Internal Error ((Unnamed Layer* 31) [Shuffle]: output shape can not be computed)
ERROR: [Torch-TensorRT TorchScript Conversion Context] - 4: [graphShapeAnalyzer.cpp::needTypeAndDimensions::2212] Error Code 4: Internal Error ((Unnamed Layer* 31) [Shuffle]: output shape can not be computed)

Traceback (most recent call last):
File "xxx/recall_tensorrt/speedup.py", line 61, in
trt_model = torch_tensorrt.compile(
File "xxx/.conda/envs/trtinfer/lib/python3.8/site-packages/torch_tensorrt/_compile.py", line 133, in compile
return torch_tensorrt.ts.compile(
File "xxx/.conda/envs/trtinfer/lib/python3.8/site-packages/torch_tensorrt/ts/_compiler.py", line 139, in compile
compiled_cpp_mod = _C.compile_graph(module._c, _parse_compile_spec(spec))
RuntimeError: [Error thrown at core/conversion/converters/impl/unsqueeze.cpp:25] Expected dim <= nbDims && dim >= -(nbDims + 1) to be true but got false
Dimension out of range (expected to be in range of [-1, 0], but got 2)

Only when I adjust one of the dimensions to be consistent, will it not report an error. like this

trt_model = torch_tensorrt.compile(
    traced_mlm_model, 
    inputs= (torch_tensorrt.Input(min_shape=[1, 128],opt_shape=[16, 128],max_shape=[32, 128],dtype=torch.int32),  # input_ids
             torch_tensorrt.Input(min_shape=[1, 128],opt_shape=[16, 128],max_shape=[32, 128],dtype=torch.int32)),  # attention_mask
    enabled_precisions= {torch.float32}, # Run with 32-bit precision
    workspace_size=2000000000,
    truncate_long_and_double=True,
    debug=True
)

But this is not what I want :(

Expected behavior

Environment

Build information about Torch-TensorRT can be found by turning on debug messages

  • Torch-TensorRT Version (e.g. 1.4.0):
  • PyTorch Version (e.g. 2.0.1):
  • CPU Architecture: x64
  • OS (e.g., Linux): Linux
  • How you installed PyTorch (conda, pip, libtorch, source): pip
  • Build command you used (if compiling from source): .whl
  • Are you using local sources or building from archives:
  • Python version: 3.8
  • CUDA version: 11.7
  • GPU models and configuration: 4090
  • Any other relevant information:

Additional context

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.