Skip to content

Allow forward functions with single output to return Variable #23803

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 5 commits into from

Conversation

malvika2147
Copy link

@malvika2147 malvika2147 commented Aug 5, 2019

Stack from ghstack:

Summary:
Custom forward() can return a Variable in case of single outputs instead of returning a variable_list of size 1.

Test Plan: Modified tests involving single output forward functions.

Differential Revision: D16673857

Summary:

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:
@pytorchbot pytorchbot added module: autograd Related to torch.autograd, and the autograd engine in general module: cpp Related to C++ API labels Aug 5, 2019
malvika2147 pushed a commit that referenced this pull request Aug 5, 2019
Summary:

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

ghstack-source-id: 4f00570
Pull Request resolved: #23803
@malvika2147 malvika2147 requested a review from ezyang August 5, 2019 18:01
@@ -56,10 +56,13 @@ TORCH_API variable_list _wrap_outputs(
// auto y = MyFunction::apply(6, x);
// Example backward call:
// y[0].sum().backward();
template<typename X, typename... Args>
using forward_t = decltype(X::forward(nullptr, std::declval<Args>()...));
Copy link
Contributor

Choose a reason for hiding this comment

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

You probably didn't mean to attach the docblock above to forward_t. (Speaking of which, would be nice to have a doc for forward_t too!)

@ezyang
Copy link
Contributor

ezyang commented Aug 6, 2019

@smessmer could you please help review the C++ template metaprogramming in this diff?

@ezyang ezyang requested a review from smessmer August 6, 2019 13:19
Copy link
Contributor

@ezyang ezyang left a comment

Choose a reason for hiding this comment

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

Seems to work, but I don't know too much about how the template metaprogramming works here.

…ble"


Summary:
Custom `forward()` can return a `Variable` in case of single outputs instead of returning a `variable_list` of size 1. 

Test Plan: Modified tests involving single output forward functions.
malvika2147 pushed a commit that referenced this pull request Aug 6, 2019
Summary:

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

ghstack-source-id: f1ce31a
Pull Request resolved: #23803
…ble"


Summary:
Custom `forward()` can return a `Variable` in case of single outputs instead of returning a `variable_list` of size 1. 

Test Plan: Modified tests involving single output forward functions.

Differential Revision: [D16673857](https://our.internmc.facebook.com/intern/diff/D16673857)
malvika2147 pushed a commit that referenced this pull request Aug 7, 2019
Summary:

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

ghstack-source-id: f7fec79
Pull Request resolved: #23803
Copy link
Contributor

@smessmer smessmer left a comment

Choose a reason for hiding this comment

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

metaprogramming looks good

@@ -182,7 +198,8 @@ variable_list Function<T>::apply(Args&&... args) {
node->input_info_.emplace_back(var);
}

variable_list outputs;
typedef forward_t<X, Args...> forward_return_t;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: using is more readable than typedef

@@ -159,9 +169,15 @@ template <typename... Args>
void extract_vars(std::vector<bool> &is_var, variable_list& list, Args&& ... args) {
}

template <typename T>
typename std::enable_if<std::is_same<T, variable_list>::value, T&>::type to_output_type(variable_list& output_list) { return output_list; }
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: we have guts::enable_if_t to avoid the typename

…ble"


Summary:
Custom `forward()` can return a `Variable` in case of single outputs instead of returning a `variable_list` of size 1. 

Test Plan: Modified tests involving single output forward functions.

Differential Revision: [D16673857](https://our.internmc.facebook.com/intern/diff/D16673857)
malvika2147 pushed a commit that referenced this pull request Aug 8, 2019
Summary:

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

ghstack-source-id: c526831
Pull Request resolved: #23803
…ble"


Summary:
Custom `forward()` can return a `Variable` in case of single outputs instead of returning a `variable_list` of size 1. 

Test Plan: Modified tests involving single output forward functions.

Differential Revision: [D16673857](https://our.internmc.facebook.com/intern/diff/D16673857)
malvika2147 pushed a commit that referenced this pull request Aug 8, 2019
Summary:

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:

Pull Request resolved: #23803
ghstack-source-id: fa3f38e
@zou3519 zou3519 deleted the gh/malvika2147/16/head branch August 9, 2019 18:13
@facebook-github-bot
Copy link
Contributor

@ezyang merged this pull request in 81ba2df.

yf225 pushed a commit to yf225/pytorch that referenced this pull request Aug 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Merged module: autograd Related to torch.autograd, and the autograd engine in general module: cpp Related to C++ API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants