Skip to content

Conversation

ClifHouck
Copy link
Contributor

Description

onnx seems to ignore certain possibilities when it comes to otherwise valid IEEE float values. So far this change just adds functionality to the parser to recognize inf or -inf as floats. However, this seems to point towards more issues. Should onnx recognize any string parse-able by std::stof as a float literal?

Motivation and Context

The current state of this PR does address #5102, although not in a very elegant fashion.

@ClifHouck ClifHouck force-pushed the clif/fix_negative_inf_float_parsing/5102 branch from 3d28307 to e0f192f Compare September 6, 2023 19:31
@ClifHouck ClifHouck marked this pull request as ready for review September 6, 2023 19:39
@ClifHouck ClifHouck requested a review from a team as a code owner September 6, 2023 19:39
@ClifHouck ClifHouck force-pushed the clif/fix_negative_inf_float_parsing/5102 branch from 32bd959 to cc43d77 Compare September 6, 2023 21:15
@justinchuby
Copy link
Member

D:\a\1\s\onnx\defs\parser.cc(72,7): warning C4101: 'e': unreferenced local variable [D:\a\1\s.setuptools-cmake-build\onnx.vcxproj]

https://dev.azure.com/onnx-pipelines/onnx/_build/results?buildId=51364&view=logs&j=ce189759-b1eb-58fb-a270-a52102be8778&t=9db78ef0-98f3-5f2e-158d-66f6d2a6591b&l=596

@ClifHouck ClifHouck force-pushed the clif/fix_negative_inf_float_parsing/5102 branch from 9b12086 to 697e217 Compare September 7, 2023 13:49
@ClifHouck
Copy link
Contributor Author

@gramalingam it seems like the parser treats any attribute value starting with an isalpha character as tensor, graph, or proto type, see https://github.com/onnx/onnx/blob/main/onnx/defs/parser.cc#L421. So I can't really discriminate against inf, infinity, or nan at the ::Parse(Literal&) level. Not sure how to proceed here. Should I add specific checking for valid float alphabetic literals in ParseSingleAttributeValue? Are we explicitly banning the use of inf, nan, and infinity as anything but float literals?

@gramalingam
Copy link
Contributor

the parser treats any attribute value starting with an isalpha character as tensor, graph, or proto type

Hmmm, that's a good point. The tensor/proto is not a concern, because that is a fixed list of type-identifiers that does not clash with inf/nan. The concern is the graph-name, specifically if users want to give a graph-attribute graph a name like "inf".

I think checking for a valid float alphabetic literal is a reasonable choice. This does not ban the use of "inf" etc. as a valid name in other places (like a tensor variable name), it only restricts its use as a graph-name for a graph-attribute. Given that we are already restricting the graph-identifier (it can't use type names like "float"), this may be okay.

Further, we can provide an "escape mechanism" to allow users who really want to produce a graph with name "inf" ... users can write attributes with or without type-annotation: e.g., they can write "mygraph : graph = ..." or "mygraph = ...". It is only in the latter situation that we need to make a guess. In the former case, we can use the type-annotation to guide the parser (which doesn't exist currently, but we can add it easily enough).

Does that sound reasonable? Thanks!

@ClifHouck ClifHouck changed the title WIP: Getting onnx to treat inf/-inf as float literals. Getting onnx to treat inf/-inf as float literals. Sep 14, 2023
@ClifHouck ClifHouck force-pushed the clif/fix_negative_inf_float_parsing/5102 branch from 36e6ca4 to 1e7d0b0 Compare September 19, 2023 14:54
@ClifHouck
Copy link
Contributor Author

I believe everything has been addressed. Please take another look. Thanks for the reviews!

auto-merge was automatically disabled September 19, 2023 18:15

Head branch was pushed to by a user without write access

Signed-off-by: Clif Houck <me@clifhouck.com>
Signed-off-by: Clif Houck <me@clifhouck.com>
Signed-off-by: Clif Houck <me@clifhouck.com>
Signed-off-by: Clif Houck <me@clifhouck.com>
Signed-off-by: Clif Houck <me@clifhouck.com>
Signed-off-by: Clif Houck <me@clifhouck.com>
Signed-off-by: Clif Houck <me@clifhouck.com>
Signed-off-by: Clif Houck <me@clifhouck.com>
Signed-off-by: Clif Houck <me@clifhouck.com>
Signed-off-by: Clif Houck <me@clifhouck.com>
Signed-off-by: Clif Houck <me@clifhouck.com>
Signed-off-by: Clif Houck <me@clifhouck.com>
Signed-off-by: Clif Houck <me@clifhouck.com>
@ClifHouck ClifHouck force-pushed the clif/fix_negative_inf_float_parsing/5102 branch from 03047f1 to 5d5e1a9 Compare September 19, 2023 18:48
@gramalingam gramalingam added this pull request to the merge queue Sep 19, 2023
Merged via the queue into onnx:main with commit 3d01541 Sep 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants