Skip to content

Conversation

@yf225
Copy link
Contributor

@yf225 yf225 commented Sep 25, 2019

Stack from ghstack:

The discussion in #15149 captures it the best, and there are two main benefits of using c10::variant:

  1. Compared to defining a common enum class for all functions, using c10::variant we can constrain the the range of acceptable enums for a particular function.
  2. Compared to using std::string as enum, using c10::variant we can save the string comparison overhead, and also constrain the the range of acceptable enums for a particular function.

Differential Revision: D17579438

[BC-breaking] Use torch::variant for enums in C++ API

gh-metadata: pytorch pytorch 26837 gh/yf225/7/head
@yf225
Copy link
Contributor Author

yf225 commented Sep 25, 2019

@ShahriarSS Would you like to review this PR?

Will Feng added 5 commits September 25, 2019 16:40
[BC-breaking] Use torch::variant for enums in C++ API

gh-metadata: pytorch pytorch 26837 gh/yf225/7/head
[BC-breaking] Use torch::variant for enums in C++ API

gh-metadata: pytorch pytorch 26837 gh/yf225/7/head
[BC-breaking] Use torch::variant for enums in C++ API

gh-metadata: pytorch pytorch 26837 gh/yf225/7/head
[BC-breaking] Use torch::variant for enums in C++ API

gh-metadata: pytorch pytorch 26837 gh/yf225/7/head
[BC-breaking] Use c10::variant for enums in C++ API

gh-metadata: pytorch pytorch 26837 gh/yf225/7/head
@yf225 yf225 changed the title [BC-breaking] Use torch::variant for enums in C++ API [BC-breaking] Use c10::variant for enums in C++ API Sep 25, 2019
Will Feng added 2 commits September 25, 2019 16:52
[BC-breaking] Use c10::variant for enums in C++ API

gh-metadata: pytorch pytorch 26837 gh/yf225/7/head
[BC-breaking] Use c10::variant for enums in C++ API

gh-metadata: pytorch pytorch 26837 gh/yf225/7/head
@ShahriarRezghi
Copy link

@yf225 I think you forgot about this one and this one (Although the second one is in detail namespace so maybe it's not that important).

@yf225
Copy link
Contributor Author

yf225 commented Sep 25, 2019

@ShahriarSS Yes those are skipped intentionally because we likely need to rewrite how RNN are implemented in C++ API. I will fix the enum issue when I work on parity for the RNN layers.

[BC-breaking] Use c10::variant for enums in C++ API

gh-metadata: pytorch pytorch 26837 gh/yf225/7/head
@ShahriarRezghi
Copy link

ShahriarRezghi commented Sep 25, 2019

@yf225 Then this looks good to me. Just one thing: Is it a bad idea to have a macro that defines the enum and it's extern const like TORCH_ARG or TORCH_MODULE (is it even possible)?

[BC-breaking] Use c10::variant for enums in C++ API

gh-metadata: pytorch pytorch 26837 gh/yf225/7/head
@yf225
Copy link
Contributor Author

yf225 commented Sep 25, 2019

@ShahriarSS Using macro is a great idea - I will add it in this PR.

@smessmer
Copy link
Contributor

Can you give some background on why we want to do this? enum class seems like less effort to use.

@yf225
Copy link
Contributor Author

yf225 commented Sep 25, 2019

@smessmer I think the discussion in #15149 captures it the best, and I think there are three main benefits of using c10::variant:

  1. Compared to defining an enum class for each function, using c10::variant we can declare the range of acceptable enums inline with the function declaration, which makes the code easier to read.
  2. Compared to defining a common enum class for all functions, using c10::variant we can constrain the the range of acceptable enums for a particular function.
  3. Compared to using std::string as enum, using c10::variant we can save the string comparison overhead, and also constrain the the range of acceptable enums for a particular function.

I will update the PR description to reflect this rationale.

[BC-breaking] Use c10::variant for enums in C++ API

gh-metadata: pytorch pytorch 26837 gh/yf225/7/head
@pytorchbot pytorchbot added caffe2 module: build Build system issues labels Sep 25, 2019
[BC-breaking] Use c10::variant for enums in C++ API

gh-metadata: pytorch pytorch 26837 gh/yf225/7/head
namespace torch {

/// Variable of `Nonlinearity` type can take one of the following values:
/// - `torch::kLinear`

Choose a reason for hiding this comment

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

@yf225 Aren't these comments redundant? the accepted values are defined in the body of declaration itself.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am a bit worried that people won't directly see the mapping from enumtype::Linear to torch::kLinear, and this comment should help them discover that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ShahriarSS I changed the enum type from enumtype::Linear to enumtype::torch::kLinear, so that people can more easily figure out that they should use torch::kLinear.

>;

/// Variable of `FanMode` type can take one of the following values:
/// - `torch::kFanIn`

Choose a reason for hiding this comment

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

Same thing here about being redundant.

@yf225 yf225 mentioned this pull request Sep 26, 2019
[BC-breaking] Use c10::variant for enums in C++ API

gh-metadata: pytorch pytorch 26837 gh/yf225/7/head
Will Feng added 4 commits October 14, 2019 20:57
Add c10::variant-based enums in C++ API

gh-metadata: pytorch pytorch 26837 gh/yf225/7/head
Add c10::variant-based enums in C++ API

gh-metadata: pytorch pytorch 26837 gh/yf225/7/head
Add c10::variant-based enums in C++ API

gh-metadata: pytorch pytorch 26837 gh/yf225/7/head
Add c10::variant-based enums in C++ API

gh-metadata: pytorch pytorch 26837 gh/yf225/7/head

#ifdef MPARK_VARIABLE_TEMPLATES
constexpr variant_in_place_t in_place{};
constexpr variant_in_place_t variant_in_place{};
Copy link
Contributor

Choose a reason for hiding this comment

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

What's going on here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's for avoiding conflict with the same variable name in c10/util/Optional.h. I added a comment in the c10-specific change log for better clarity.

Copy link
Contributor

Choose a reason for hiding this comment

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

We should just make variant.h use the optional.h definition. in_place is part of the standard.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I moved the common definition for in_place into c10/util/in_place.h, so that both variant.h and optional.h can use it.


#define TORCH_ENUM_DECLARE(name) \
namespace torch { \
namespace enumtype { \
Copy link
Contributor

Choose a reason for hiding this comment

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

How'd you decide on this name? Is it just an internal name or is it visible to users?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is only an internal name - the type of the enum is e.g. torch::enumtype::kFanIn, and the variable that has this type is torch::kFanIn, which is what's used by the end user.

@ezyang
Copy link
Contributor

ezyang commented Oct 15, 2019

The way of referencing the enums got a lot wordier in this PR. Was this intentional?

Add c10::variant-based enums in C++ API

gh-metadata: pytorch pytorch 26837 gh/yf225/7/head
@yf225
Copy link
Contributor Author

yf225 commented Oct 15, 2019

The way of referencing the enums got a lot wordier in this PR. Was this intentional?

Yes it was intentional - they will be changed to the short form in #27933.

@yf225 yf225 requested a review from ezyang October 15, 2019 16:30
Add c10::variant-based enums in C++ API

gh-metadata: pytorch pytorch 26837 gh/yf225/7/head
Will Feng added 5 commits October 16, 2019 14:03
Add c10::variant-based enums in C++ API

gh-metadata: pytorch pytorch 26837 gh/yf225/7/head
Add c10::variant-based enums in C++ API

gh-metadata: pytorch pytorch 26837 gh/yf225/7/head
Add c10::variant-based enums in C++ API

gh-metadata: pytorch pytorch 26837 gh/yf225/7/head
Add c10::variant-based enums in C++ API

gh-metadata: pytorch pytorch 26837 gh/yf225/7/head
Add c10::variant-based enums in C++ API

gh-metadata: pytorch pytorch 26837 gh/yf225/7/head
@yf225
Copy link
Contributor Author

yf225 commented Oct 16, 2019

@pytorchbot rebase this please

Will Feng added 2 commits October 16, 2019 18:01
Add c10::variant-based enums in C++ API

gh-metadata: pytorch pytorch 26837 gh/yf225/7/head
Add c10::variant-based enums in C++ API

gh-metadata: pytorch pytorch 26837 gh/yf225/7/head
@facebook-github-bot
Copy link
Contributor

@yf225 merged this pull request in aad5071.

@facebook-github-bot facebook-github-bot deleted the gh/yf225/7/head branch October 28, 2019 22:22
pdlive215 pushed a commit to pdlive215/pytorch that referenced this pull request Nov 27, 2019
Summary:
This PR adds temporary declarations for `torch::k{name}` enums, so that we can submit a PR to rename the enum usage in torchvision. And then, after the changes to torchvision is done, we can remove the temporary declarations in pytorch#26837 to officially move over to using `c10::variant` for enums.
Pull Request resolved: pytorch#27051

Differential Revision: D17672220

Pulled By: yf225

fbshipit-source-id: 4ae77634e8c7efa3404698f7c1a69177cbb5dab3
thiagocrepaldi pushed a commit to thiagocrepaldi/pytorch that referenced this pull request Feb 4, 2020
Summary:
This PR adds temporary declarations for `torch::k{name}` enums, so that we can submit a PR to rename the enum usage in torchvision. And then, after the changes to torchvision is done, we can remove the temporary declarations in pytorch#26837 to officially move over to using `c10::variant` for enums.
Pull Request resolved: pytorch#27051

Differential Revision: D17672220

Pulled By: yf225

fbshipit-source-id: 4ae77634e8c7efa3404698f7c1a69177cbb5dab3
thiagocrepaldi pushed a commit to thiagocrepaldi/pytorch that referenced this pull request Feb 4, 2020
Summary: Pull Request resolved: pytorch#26837

Test Plan: Imported from OSS

Differential Revision: D17579438

Pulled By: yf225

fbshipit-source-id: 9ac59df28a317fdb3be2cc02c65962ad99117127
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

caffe2 Merged module: build Build system issues module: cpp Related to C++ API

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants