Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If-operator has unexpected semantics if condition input has anything but a single element #18549

Closed
cbourjau opened this issue Nov 22, 2023 · 8 comments
Labels
core runtime issues related to core runtime

Comments

@cbourjau
Copy link
Contributor

Describe the issue

The implementation of the If operator has surprising semantics if the condition is either empty or a tensor with more than one element. I would expect a crash or at least a warning in either case. However, the current behavior is that an empty condition evaluates to false. If a condition tensor with more than one element is provided only the first element is used.

To reproduce

Run the attached onnx model with different 1D boolean inputs.
if.onnx.gz

Urgency

No response

Platform

Mac

OS Version

12.3.1

ONNX Runtime Installation

Released Package

ONNX Runtime Version or Commit ID

1.16.1

ONNX Runtime API

Python

Architecture

ARM64

Execution Provider

Default CPU

Execution Provider Library Version

No response

@yihonglyu yihonglyu added the core runtime issues related to core runtime label Nov 27, 2023
@hariharans29
Copy link
Member

But doesn't the ONNX spec explicitly state that the condition input MUST contain only a single element (https://github.com/onnx/onnx/blob/main/docs/Operators.md#If) ? having said that, maybe we could add a check here to ensure that there really is only one element -

auto condition = *ctx->Input<Tensor>(0)->Data<bool>();

@yuslepukhin
Copy link
Member

The model is invalid, and we need to detect this.

@hariharans29
Copy link
Member

The model is invalid, and we need to detect this.

The model isn't invalid - it is valid. The model has a dynamic shape graph input of type boolean and 1-D tensor shape [N] which is then fed as the input to the If operator. So far, so good. The input sent in is invalid in the context of it being used in the If operator iff N > 1. The input sent in SHOULD be a 1-D tensor of shape [1] (N should be 1). To detect invalid inputs to the If operator, the above proposed check may be added.

@cbourjau
Copy link
Contributor Author

Arguable, the standard should be changed such that the condition of the If node must be a scalar, but for the time being @hariharans29 's assessment is on point.

@hariharans29
Copy link
Member

hariharans29 commented Nov 30, 2023

'Arguable, the standard should be changed such that the condition of the If node must be a scalar,'

Oh but doesn't it already do that? It says "Condition for the if. The tensor must contain a single element."

@cbourjau
Copy link
Contributor Author

cbourjau commented Dec 1, 2023

Oh but doesn't it already do that? It says "Condition for the if. The tensor must contain a single element."

Yes, but it should instead constrain the rank of the tensor to be rank-0 aka a scalar.

yuslepukhin added a commit that referenced this issue Dec 7, 2023
### Description
<!-- Describe your changes. -->

### Motivation and Context
#18549
@yuslepukhin
Copy link
Member

@cbourjau Please, close this ticket. If you feel standard chance is needed, open one at ONNX. Thx!

@cbourjau
Copy link
Contributor Author

Thanks! Closing as fixed with #18733

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core runtime issues related to core runtime
Projects
None yet
Development

No branches or pull requests

4 participants