Skip to content

[ONNX] index_put does not support dynamic shapes #158739

@fnobis

Description

@fnobis

🐛 Describe the bug

Moving this post from the pytorch forum since this seems to be a bug.
With the old dynamo_export in pytorch 2.3 , I can export a model. With pytorch 2.7 and onnx.export(dynamo=True), it throws an error

Excerpt from the model which fails in pytorch 2.7:

    n_max = 1000  # fixed output size
    class_out = torch.zeros(
        n_max, dtype=class_all_.dtype, device=class_all_.device
    )    
    indices = torch.arange(
        0, class_all_.size(0), dtype=torch.long, device=class_all_.device
    )  # class_all_ is of a dynamic size

    # Copy into fixed size array (bigger than dynamic array size)
    class_out.index_copy_(0, indices, class_all_) # this line throws the error

Errors message:


<class 'TypeError'>: unsupported operand type(s) for *: 'int' and 'SymbolicDim'
⬆️
<class 'torch.onnx._internal.exporter._errors.GraphConstructionError'>: Error when calling function 'TracedOnnxFunction(<function aten_index_put at 0x7575b2cee830>)' with args '[SymbolicTensor(name='zeros', type=Tensor(FLOAT), shape=Shape([1000, 4]), producer='node_Expand_267', index=0), [SymbolicTensor(name='arange_2', type=Tensor(INT64), shape=Shape([SymbolicDim(u0)]), producer='node_Range_279', index=0)], SymbolicTensor(name='detach_2', type=Tensor(FLOAT), shape=Shape([SymbolicDim(u0), 4]), producer='node_Identity_254', index=0)]' and kwargs '{}'
⬆️
<class 'torch.onnx._internal.exporter._errors.ConversionError'>: Error when translating node %index_put : [num_users=1] = call_function[target=torch.ops.aten.index_put.default](args = (%zeros, [%arange_2], %detach_2), kwargs = {}). See the stack trace for more information.

Versions

PyTorch version: 2.7.1+cu118
Is debug build: False
CUDA used to build PyTorch: 11.8
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.3 LTS (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Clang version: Could not collect
CMake version: version 3.31.3
Libc version: glibc-2.35

cc @justinchuby @chauhang @penguinwu @avikchaudhuri @gmagogsfm @zhxchen17 @tugsbayasgalan @angelayi @suo @ydwu4

Metadata

Metadata

Assignees

No one assigned

    Labels

    OSS contribution wantedPR from open source contributors welcome to solve this issue.export-triagedThis tag is used to tag issues that have been looked by PT2 Export team and determined the next stepmodule: onnxRelated to torch.onnxoncall: exportoncall: pt2

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions