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

Adding Optional ops to opset 15 #3567

Merged
merged 11 commits into from Jul 14, 2021
Merged

Conversation

neginraoof
Copy link
Contributor

Description
Adding operators for optional type to opset 15:
Optional (constructor)
OptionalHasElement
OptionalGetElement

Motivation and Context
These ops are necessary for enabling export of customer models with optional type.

@neginraoof neginraoof requested review from a team as code owners July 8, 2021 21:52
Signed-off-by: neginraoof <neginmr@utexas.edu>
@askhade
Copy link
Contributor

askhade commented Jul 9, 2021

@postrational can you help review this. We need to get this in onnx 1.10

These ops were reviewed and validated in onnxruntime as contrib ops: microsoft/onnxruntime#7946

@askhade askhade added this to the 1.10 milestone Jul 9, 2021

def optional_has_element_reference_implementation(optional): # type: (Optional[np.ndarray]) -> np.ndarray
if optional is None:
return np.array(False)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this map to a one-dimensional tensor in ONNX? I think we want a scalar (zero-dimensional tensor), right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a zero dimension numpy scalar. The ONNX model output value info does not have shape (only boolean dtype).

onnx/numpy_helper.py Outdated Show resolved Hide resolved
Signed-off-by: neginraoof <neginmr@utexas.edu>
Signed-off-by: neginraoof <neginmr@utexas.edu>
def _extract_value_info(input, name, type_proto=None): # type: (Union[List[Any], np.ndarray, None], Text, TypeProto) -> onnx.ValueInfoProto
if type_proto is None:
if isinstance(input, list):
elem_type = onnx.mapping.NP_TYPE_TO_TENSOR_TYPE[input[0].dtype]
Copy link
Contributor

@askhade askhade Jul 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add this comment back :

TODO: Account for recursive sequence case. Right now, this function supports
Sequences of Tensors.

we are still only supporting Sequence of tensors

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the case where type_proto is None there is no else block to set type_proto... meaning if control does not enter
if isinstance(input, list) or elif isinstance(input, np.ndarray) or np.isscalar(input) then type_proto will be None when the control reaches line 118.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how will this work in the case when optional proto holds None value? type_proto will be None?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree. Better to have an assertion/check if the value is None.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@askhade I don't think we need the comment back. The function now does support sequence of sequence or other recursive types if the type_proto for such type is provided. So users can create this kind of type_proto and then use _extract_value_info to create the value info.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@askhade In the case where optional_proto holds None, we rely on the user to provide a corresponding type_proto. For a None tensor, the type_proto has elem_type (depending on the None tensor data type).
I'll add a check to fail if both input and type_proto are None.

onnx/numpy_helper.py Outdated Show resolved Hide resolved
@gramalingam gramalingam added the operator Issues related to ONNX operators label Jul 12, 2021
Signed-off-by: neginraoof <neginmr@utexas.edu>
Signed-off-by: neginraoof <neginmr@utexas.edu>
Signed-off-by: neginraoof <neginmr@utexas.edu>
@neginraoof neginraoof force-pushed the neraoof/optional branch 3 times, most recently from 6c73720 to 75e3475 Compare July 13, 2021 18:01
Signed-off-by: neginraoof <neginmr@utexas.edu>
@neginraoof neginraoof force-pushed the neraoof/optional branch 2 times, most recently from 35549fa to d629005 Compare July 13, 2021 23:00
Signed-off-by: neginraoof <neginmr@utexas.edu>
…tional

Signed-off-by: neginraoof <neginmr@utexas.edu>
docs/Operators.md Outdated Show resolved Hide resolved
Signed-off-by: neginraoof <neginmr@utexas.edu>
@askhade askhade enabled auto-merge (squash) July 14, 2021 20:59
Copy link
Contributor

@hariharans29 hariharans29 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR !

@askhade askhade merged commit 48a4f08 into onnx:master Jul 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
operator Issues related to ONNX operators
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants