Skip to content

Exporting a model with MultiheadAttention and dynamic shapes fails #165041

@f-fuchs

Description

@f-fuchs

test_reshape_minimal.py

onnx_export_option_B_pt_export.md
onnx_export_option_A_success.md

🐛 Describe the bug

Hey,

I am trying to export a PyTorch UNet model with MultiheadAttention in the bottleneck and dynamic shapes.
Without MultiheadAttention I can use dynamo=False and inference is successful with variable input shapes but this fails with MultiheadAttention .

In this gist I tried exporting with dynamo and dynamic_axes, with dynamo and dynamic_shapes and without dynamo but every option fails either during exporting or during inference.

https://gist.github.com/f-fuchs/012f7efb2984700d2eca93e5a8acc3f3

Option A: dynamo with dynamic_axes

export succeeds but inference with but size =! 1 leads to:

onnxruntime.capi.onnxruntime_pybind11_state.InvalidArgument: [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Got invalid dimensions for input: input for the following indices
index: 0 Got: 2 Expected: 1
Please fix either the inputs/outputs or the model.

Option B: dynamo with dynamic_shapes

export fails with convoluted guards, report is attached

Option C: dynamo=False with dynamic_axes

export works but error during inference due to MultiheadAttention
onnxruntime.capi.onnxruntime_pybind11_state.RuntimeException: [ONNXRuntimeError] : 6 : RUNTIME_EXCEPTION : Non-zero status code returned while running Reshape node. Name:'/attention_bottleneck/transformer_blocks/transformer_blocks.0/attn_self/Reshape_4' Status Message: /onnxruntime_src/onnxruntime/core/providers/cpu/tensor/reshape_helper.h:45 onnxruntime::ReshapeHelper::ReshapeHelper(const onnxruntime::TensorShape&, onnxruntime::TensorShapeVector&, bool) input_shape_size == size was false. The input tensor cannot be reshaped to the requested shape. Input shape:{1024,1,512}, requested shape:{256,8,64}

Versions

torch 2.7.1
onnx 1.18.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions