Skip to content

[PyTorch Edge] Support default args with out arg, flag off #63540

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

Closed
wants to merge 33 commits into from

Conversation

cccclai
Copy link
Contributor

@cccclai cccclai commented Aug 19, 2021

Stack from ghstack:

  1. Allow consuming operators with defaults arguments and out arguments. Flag is off to keep the same behavior as v6, in pr 63651, turn on the flag.
  2. Add two unittests to cover this type of operators.

Differential Revision: D30414156

Differential Revision: D30414156

@facebook-github-bot
Copy link
Contributor

facebook-github-bot commented Aug 19, 2021

🔗 Helpful links

💊 CI failures summary and remediations

As of commit 8351ded (more details on the Dr. CI page):


💚 💚 Looks good so far! There are no failures yet. 💚 💚


This comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.

Please report bugs/suggestions to the (internal) Dr. CI Users group.

Click here to manually regenerate this comment.

@facebook-github-bot facebook-github-bot added oncall: jit Add this issue/PR to JIT oncall triage queue cla signed labels Aug 19, 2021
cccclai pushed a commit that referenced this pull request Aug 19, 2021
Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156/)

ghstack-source-id: 136170423
Pull Request resolved: #63540
cccclai pushed a commit that referenced this pull request Aug 19, 2021
Pull Request resolved: #63540


ghstack-source-id: 136217678

Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156/)
for (size_t i = num_specified_args.value(); i < args.size(); ++i) {
stack.push_back(args[i].default_value());
c10::optional<IValue> out_arg;
if (!args.empty() && args.back().is_out()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there always zero or one out arg? What happens if there are 2 or more out args?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh I didn't know it's possible. My understanding is that out arg is an optional argument to store the output to. The output from inference can only be one. What about having a TORCH_CHECK to check only 1 out arg and we can extend it when we know there might be more than one out arg?

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 there can be more than 1 out arg. We definitely are coding the out arg as a counter not a bool (plus it makes sense it can be > 1).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Update to cover multiple args.

Copy link
Contributor

@raziel raziel left a comment

Choose a reason for hiding this comment

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

Thanks. A few comments, but in particular about why the support in the interpreter is under a flag?

for (size_t i = num_specified_args.value(); i < args.size(); ++i) {
stack.push_back(args[i].default_value());
c10::optional<IValue> out_arg;
if (!args.empty() && args.back().is_out()) {
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 there can be more than 1 out arg. We definitely are coding the out arg as a counter not a bool (plus it makes sense it can be > 1).

@cccclai cccclai changed the title [PyTorch Edge] Support default args with out arg [PyTorch Edge] Support default args with out arg, flag off Aug 21, 2021
Allow consuming operators with defaults arguments and out arguments. Flag is off to keep the same behavior as v6, in pr 63651, turn on the flag.



Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156/)

Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156)

[ghstack-poisoned]
1. Allow consuming operators with defaults arguments and out arguments. Flag is off to keep the same behavior as v6, in pr 63651, turn on the flag.
2. Add two unittests to cover this type of operators.



Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156/)

Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156)

[ghstack-poisoned]
1. Allow consuming operators with defaults arguments and out arguments. Flag is off to keep the same behavior as v6, in pr 63651, turn on the flag.
2. Add two unittests to cover this type of operators.



Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156/)

Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156)

[ghstack-poisoned]
1. Allow consuming operators with defaults arguments and out arguments. Flag is off to keep the same behavior as v6, in pr 63651, turn on the flag.
2. Add two unittests to cover this type of operators.



Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156/)

Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156)

[ghstack-poisoned]
out_args.push_back(stack.back());
stack.pop_back();
}
size_t start_index = num_specified_args.value() - out_args.size();
Copy link
Contributor

Choose a reason for hiding this comment

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

@iseeyuan @raziel @cccclai @gmagogsfm I had to do this calculation many places, so I modified CalculateNecessaryArgs utility method to return two things (pair<int, int>) where the first one is number of default args and the second one is number of out args. Do you think I should communicate this number of args to mobile by either of following ways:

  1. change num_specified_args: int to num_specified_args: pair<int, int>
  2. add another map that says op_to_num_out_args.

Copy link
Contributor

Choose a reason for hiding this comment

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

@tugsbayasgalan I think one number, num_specified_args would be sufficient, because you've already provided is_out. All the out args can be popped and default values can be pushed. Any reason num_out_args is needed in mobile?

Copy link
Contributor

Choose a reason for hiding this comment

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

yeahh I guess you still have to loop from the end to pop out args. Yeah, I confirm mobile doesn't need num_out_args.

Copy link
Contributor

Choose a reason for hiding this comment

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

@tugsbayasgalan Thanks for the confirmation! At least we don't need to change the bytecode schema :D

Copy link
Contributor

@iseeyuan iseeyuan left a comment

Choose a reason for hiding this comment

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

Left a few comments. @cccclai For checking outputs in tests and TORCH_CHECK default_value, please let me know if they make sense.

@tugsbayasgalan Please verify that we don't need explicit num_out_args.

out_args.push_back(stack.back());
stack.pop_back();
}
size_t start_index = num_specified_args.value() - out_args.size();
Copy link
Contributor

Choose a reason for hiding this comment

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

@tugsbayasgalan I think one number, num_specified_args would be sufficient, because you've already provided is_out. All the out args can be popped and default values can be pushed. Any reason num_out_args is needed in mobile?

size_t start_index = num_specified_args.value() - out_args.size();
for (size_t i = (start_index >= 0 ? start_index : 0); i < args.size();
++i) {
if (args[i].default_value().has_value()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It should be a TORCH_CHECK. args[i] must have default value, otherwise the element# in the stack can mismatch.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Resolve in the latest commit.

1. Allow consuming operators with defaults arguments and out arguments. Flag is off to keep the same behavior as v6, in pr 63651, turn on the flag.
2. Add two unittests to cover this type of operators.



Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156/)

Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156)

[ghstack-poisoned]
chenlai and others added 2 commits August 26, 2021 10:10
1. Allow consuming operators with defaults arguments and out arguments. Flag is off to keep the same behavior as v6, in pr 63651, turn on the flag.
2. Add two unittests to cover this type of operators.



Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156/)

Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156)

[ghstack-poisoned]
1. Allow consuming operators with defaults arguments and out arguments. Flag is off to keep the same behavior as v6, in pr 63651, turn on the flag.
2. Add two unittests to cover this type of operators.



Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156/)

Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156)

[ghstack-poisoned]
insertInstruction(OP, operator_table_.size());
}
operator_table_.emplace_back(op.getOperation(node));
}
}

bool emit_default_input_instructions_;
bool support_default_args_before_out_;
Copy link
Contributor

Choose a reason for hiding this comment

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

should we clean up these flags later on? @cccclai @iseeyuan @raziel @gmagogsfm I feel like this will just keep adding more and more edge cases (2^x) to cover if we keep having flags like these.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, after a couple more releases, let's clean them up.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. I add some comments about these two flags in the latest commit, so it's easier to clean up.

Copy link
Contributor

Choose a reason for hiding this comment

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

We could clean them up when we don't need the backport functions.

1. Allow consuming operators with defaults arguments and out arguments. Flag is off to keep the same behavior as v6, in pr 63651, turn on the flag.
2. Add two unittests to cover this type of operators.



Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156/)

Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156)

[ghstack-poisoned]
1. Allow consuming operators with defaults arguments and out arguments. Flag is off to keep the same behavior as v6, in pr 63651, turn on the flag.
2. Add two unittests to cover this type of operators.



Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156/)

Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156)

[ghstack-poisoned]
cccclai pushed a commit that referenced this pull request Aug 27, 2021
1. Allow consuming operators with defaults arguments and out arguments. Flag is off to keep the same behavior as v6, in pr 63651, turn on the flag.
2. Add two unittests to cover this type of operators.

Pull Request resolved: #63540


ghstack-source-id: 136887019

Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156/)
@cccclai cccclai added this to the 1.10.0 milestone Aug 28, 2021
Copy link
Contributor

@iseeyuan iseeyuan left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks!


const int N = 28;
auto input = torch::range(1, N * N, 1);
input[0] = 1; // a more stable matrix
Copy link
Contributor

@iseeyuan iseeyuan Aug 29, 2021

Choose a reason for hiding this comment

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

import torch
N = 3
input = torch.range(1, N * N, 1)
print(input)
tensor([1., 2., 3., 4., 5., 6., 7., 8., 9.])

input[0] = 1 does not change input and the condition number of this matrix is Inf (very unstable) and the solution could vary in different platforms.

##############################
edit:
Chen: Thanks. Address in the latest commit.

for (size_t i = (start_index >= 0 ? start_index : 0); i < args.size();
++i) {
auto default_val = args[i].default_value();
TORCH_CHECK(
Copy link
Contributor

Choose a reason for hiding this comment

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

@raziel Got it. Thanks!

insertInstruction(OP, operator_table_.size());
}
operator_table_.emplace_back(op.getOperation(node));
}
}

bool emit_default_input_instructions_;
bool support_default_args_before_out_;
Copy link
Contributor

Choose a reason for hiding this comment

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

We could clean them up when we don't need the backport functions.

1. Allow consuming operators with defaults arguments and out arguments. Flag is off to keep the same behavior as v6, in pr 63651, turn on the flag.
2. Add two unittests to cover this type of operators.



Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156/)

Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156)

[ghstack-poisoned]
Copy link
Contributor

@raziel raziel left a comment

Choose a reason for hiding this comment

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

Thanks!

chenlai added 11 commits August 30, 2021 20:57
1. Allow consuming operators with defaults arguments and out arguments. Flag is off to keep the same behavior as v6, in pr 63651, turn on the flag.
2. Add two unittests to cover this type of operators.



Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156/)

Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156)

[ghstack-poisoned]
1. Allow consuming operators with defaults arguments and out arguments. Flag is off to keep the same behavior as v6, in pr 63651, turn on the flag.
2. Add two unittests to cover this type of operators.



Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156/)

Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156)

[ghstack-poisoned]
1. Allow consuming operators with defaults arguments and out arguments. Flag is off to keep the same behavior as v6, in pr 63651, turn on the flag.
2. Add two unittests to cover this type of operators.



Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156/)

Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156)

[ghstack-poisoned]
1. Allow consuming operators with defaults arguments and out arguments. Flag is off to keep the same behavior as v6, in pr 63651, turn on the flag.
2. Add two unittests to cover this type of operators.



Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156/)

Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156)

[ghstack-poisoned]
1. Allow consuming operators with defaults arguments and out arguments. Flag is off to keep the same behavior as v6, in pr 63651, turn on the flag.
2. Add two unittests to cover this type of operators.



Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156/)

Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156)

[ghstack-poisoned]
1. Allow consuming operators with defaults arguments and out arguments. Flag is off to keep the same behavior as v6, in pr 63651, turn on the flag.
2. Add two unittests to cover this type of operators.



Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156/)

Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156)

[ghstack-poisoned]
1. Allow consuming operators with defaults arguments and out arguments. Flag is off to keep the same behavior as v6, in pr 63651, turn on the flag.
2. Add two unittests to cover this type of operators.



Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156/)

Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156)

[ghstack-poisoned]
1. Allow consuming operators with defaults arguments and out arguments. Flag is off to keep the same behavior as v6, in pr 63651, turn on the flag.
2. Add two unittests to cover this type of operators.



Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156/)

Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156)

[ghstack-poisoned]
1. Allow consuming operators with defaults arguments and out arguments. Flag is off to keep the same behavior as v6, in pr 63651, turn on the flag.
2. Add two unittests to cover this type of operators.



Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156/)

Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156)

[ghstack-poisoned]
1. Allow consuming operators with defaults arguments and out arguments. Flag is off to keep the same behavior as v6, in pr 63651, turn on the flag.
2. Add two unittests to cover this type of operators.



Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156/)

Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156)

[ghstack-poisoned]
1. Allow consuming operators with defaults arguments and out arguments. Flag is off to keep the same behavior as v6, in pr 63651, turn on the flag.
2. Add two unittests to cover this type of operators.



Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156/)

Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156)

[ghstack-poisoned]
cccclai pushed a commit that referenced this pull request Sep 1, 2021
1. Allow consuming operators with defaults arguments and out arguments. Flag is off to keep the same behavior as v6, in pr 63651, turn on the flag.
2. Add two unittests to cover this type of operators.

Pull Request resolved: #63540


ghstack-source-id: 137211562

Differential Revision: [D30414156](https://our.internmc.facebook.com/intern/diff/D30414156/)
@facebook-github-bot
Copy link
Contributor

This pull request has been merged in 8d5b950.

@facebook-github-bot facebook-github-bot deleted the gh/cccclai/103/head branch September 5, 2021 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla signed Merged oncall: jit Add this issue/PR to JIT oncall triage queue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants