Ankit Swami
Ankit Swami

Reputation: 1

User warning when exporting Pytorch model to ONNX

I have found some code that directly converts the pytorch model to onnx:

import torch.onnx  
torch.onnx.export(
    model,  
    input,  
    "model.onnx",  
    export_params=True,  
    opset_version=10
)

But it throws UserWarning most of the time :-

/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:2359: UserWarning: floordiv is deprecated, and its behavior will change in a future version of pytorch. It currently rounds toward 0 (like the 'trunc' function NOT 'floor'). This results in incorrect rounding for negative values. To keep the current behavior, use torch.div(a, b, rounding_mode='trunc'), or for actual floor division, use torch.div(a, b, rounding_mode='floor').

_verify_batch_size([input.size(0) * input.size(1) // num_groups, num_groups] + list(input.size()[2:]))

/usr/local/lib/python3.7/dist-packages/torch/onnx/symbolic_opset9.py:1934: UserWarning: ONNX export unsqueeze with negative axis -1 might cause the onnx model to be incorrect. Negative axis is not supported in ONNX. Axis is converted to 1 based on input shape at export time. Passing an tensor of different rank in execution will be incorrect.

"Passing an tensor of different rank in execution will be incorrect.")

/usr/local/lib/python3.7/dist-packages/torch/onnx/symbolic_opset9.py:1934: UserWarning: ONNX export unsqueeze with negative axis -1 might cause the onnx model to be incorrect. Negative axis is not supported in ONNX. Axis is converted to 2 based on input shape at export time. Passing an tensor of different rank in execution will be incorrect.

"Passing an tensor of different rank in execution will be incorrect.")

Can you explain why I am getting this error and is this method correct for exporting to onnx or can you suggest any better method for exporting complex pytorch model to onnx ?

Upvotes: 0

Views: 1422

Answers (1)

Zhilin Lu
Zhilin Lu

Reputation: 11

The reason is given directly in the warning message. Since PyTorch1.10, the floordiv is deprecated. You need to update input.size(1) // num_groups to torch.div(input.size(1), num_groups, rounding_mode='floor') if you wish to avoid the warning.

But it is indeed weird that the // should be considered as torch. floor_divide only when a torch.Tensor is included as operand. This might has something to do with the onnx export logic. Hopefully someone more familar with the underlying logic could give a deeper answer.

Upvotes: 1

Related Questions