Skip to content

Conversation

liqunfu
Copy link
Collaborator

@liqunfu liqunfu commented Sep 20, 2022

Signed-off-by: Liqun Fu liqfu@microsoft.com

Description

Add AttributeHasValue op

Motivation and Context

This PR is to solve part of the issue (AttributeHasValue and StaticOptionalHasValue): ##3921

This is an op to help convert primary ops with optional attribute to simply function ops.
This op is a function op thus not a burden for runtimes. It is not simple function ops though.

Signed-off-by: Liqun Fu <liqfu@microsoft.com>
@liqunfu liqunfu requested a review from a team as a code owner September 20, 2022 01:12
@liqunfu liqunfu marked this pull request as draft September 20, 2022 01:12
Signed-off-by: Liqun Fu <liqfu@microsoft.com>
AttributeHasValue,
18,
OpSchema()
.SetDoc(R"DOC(Returns which elements of the input are NaN.)DOC")
Copy link
Contributor

Choose a reason for hiding this comment

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

Change to "Returns true iff at least one of the attribute-value is specified."

const size_t numOutputs = ctx.getNumOutputs();
if (numOutputs != 1) {
fail_type_inference("AttributeHasValue is expected to have 1 output.");
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The checks above are unnecessary and can be omitted. The default-checker will already check this from the opschema information.

auto ints_attr_proto = ctx.getAttribute("ints");
bool has_ints = (nullptr != ints_attr_proto) && ints_attr_proto->ints_size() > 0;
auto int_attr_proto = ctx.getAttribute("int");
bool has_int = (nullptr != int_attr_proto) && int_attr_proto->has_i();
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess you will add a check for all other attributes as well?

Copy link
Contributor

Choose a reason for hiding this comment

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

May be something like:

   for (const char *name : { "int", "ints", ... })
      if ctx.getAttribute(name) != nullptr {
         has_attr = true;
         break;
      }

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes I will find a way to iterate all types defined in proto. For now, I just want to covert ints so that I can validate it in converting reduce ops to function ops.

FunctionProto& functionProto) {
FunctionBuilder builder(functionProto);
auto ints_attr_proto = ctx.getAttribute("ints");
bool has_ints = (nullptr != ints_attr_proto) && ints_attr_proto->ints_size() > 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should omit the second check on ints_size?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think I added the check due to test failure. Let me double check.

// ToTensor does not work with boolean element type. Need to create a Constant int
// and then Cast to tensor of boolean.
if (has_ints) {
builder.Const("output0", 1);
Copy link
Contributor

@gramalingam gramalingam Sep 20, 2022

Choose a reason for hiding this comment

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

I think something like

   builder.Add("output0 = Constant < value = bool {1} > ()");

may work?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

will try

**kwargs,
)

output = np.array(True)
Copy link
Contributor

Choose a reason for hiding this comment

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

May be replace True by bool(kwargs) ... then you can use the same test-case with no-attributes as well as one-or-more attributes.

if (has_ints) {
builder.Const("output0", 1);
} else if (has_int) {
builder.Const("output0", 1);
Copy link
Member

Choose a reason for hiding this comment

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

Merge this condition (has_int) with the condition of has_ints?

Signed-off-by: Liqun Fu <liqfu@microsoft.com>
@liqunfu liqunfu marked this pull request as ready for review September 23, 2022 00:40
Signed-off-by: Liqun Fu <liqfu@microsoft.com>
Signed-off-by: Liqun Fu <liqfu@microsoft.com>
@lgtm-com
Copy link

lgtm-com bot commented Sep 23, 2022

This pull request introduces 1 alert when merging 5e56af2 into 895593a - view on LGTM.com

new alerts:

  • 1 for Unused import

Signed-off-by: Liqun Fu <liqfu@microsoft.com>
@@ -23,7 +26,7 @@ def test_one_attribute(name, **kwargs):
node,
inputs=[],
outputs=[output],
name="test_attribute_has_value_{name}_false".format(name=name),
name=f"test_attribute_has_value_{name}_false",
Copy link
Contributor

Choose a reason for hiding this comment

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

May be change to test_attribute_has_{name}_false ? Or, omit the value_ part in the call-site. Otherwise, we will get value_value_int.

Copy link
Contributor

@gramalingam gramalingam left a comment

Choose a reason for hiding this comment

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

LGTM (except for the nit about repeated value_value in the name), thanks!

Signed-off-by: Liqun Fu <liqfu@microsoft.com>
Signed-off-by: Liqun Fu <liqfu@microsoft.com>
@liqunfu liqunfu requested a review from a team as a code owner September 26, 2022 18:44
@liqunfu liqunfu merged commit f0d6074 into onnx:main Sep 26, 2022
broune pushed a commit to broune/onnx that referenced this pull request May 6, 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.

3 participants