Skip to content

Conversation

@jon-tow
Copy link
Contributor

@jon-tow jon-tow commented Oct 22, 2019

Adds interpolate functional and Upsample module support for the C++ API.

Issue: #25883

Reviewer: @yf225

Copy link
Contributor

@yf225 yf225 left a comment

Choose a reason for hiding this comment

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

@jon-tow Thanks so much for the awesome work and I really appreciated it. :D I left some minor comments

@yf225
Copy link
Contributor

yf225 commented Oct 23, 2019

  1. Allow for constructing UpsampleOptions size and scale_factor with just
    int64_t or double, respectively. This is to match the Python API.
    E.g. UpsampleOptions().scale_factor(0.5).

I feel that we can probably relax the requirement on this and allow using {0.5} to represent scalar value 0.5, to make the implementation easier :D

@jon-tow jon-tow changed the title [WIP] C++ API parity: Upsample C++ API parity: Upsample Oct 23, 2019
@jon-tow
Copy link
Contributor Author

jon-tow commented Oct 23, 2019

As always, thank you for the help and guidance @yf225 :) .
I believe I've addressed each comment properly. Please let me know if there's anything that needs consideration, notably the handling of UpsampleOptions and its usage here:

Tensor UpsampleImpl::forward(const Tensor& input) {
return F::interpolate(
input,
InterpolateOptions()
.size(options.size())
.scale_factor(options.scale_factor())
.mode(decltype(InterpolateOptions().mode())(options.mode()))
.align_corners(
options.align_corners().has_value() ? options.align_corners()
: c10::nullopt));
}

@jon-tow jon-tow requested a review from yf225 October 23, 2019 21:39
Copy link
Contributor

@yf225 yf225 left a comment

Choose a reason for hiding this comment

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

@jon-tow Thanks so much for the awesome work! I left some minor comments :D It would be awesome if we could mark Implementation Parity as YES for for Upsample in test/cpp_api_parity/parity-tracker.md as well. Thanks again!

InterpolateOptions()
.size(options.size())
.scale_factor(options.scale_factor())
.mode(decltype(InterpolateOptions().mode())(options.mode()))
Copy link
Contributor

Choose a reason for hiding this comment

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

curious does .mode(options.mode()) work?

Copy link
Contributor Author

@jon-tow jon-tow Oct 24, 2019

Choose a reason for hiding this comment

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

The problem here is that UpsampleOptions::mode_t and InterpolateOptions::mode_t differ by one variant member: torch::kArea (in Python "area"). According to the Python torch.nn.Upsample documentation, "area" is not an acceptable mode option, as "area" is commonly used as a decimation/downsampling mechanism. It happens to be passable because it is not checked in the module's __init__.

Would you like me to add torch::kArea as a variant member in Upsample::mode_t? This would make your suggestion possible. :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah that makes sense, thanks for the catch! One thing that's a bit worrying to me is that I am not sure if the conversion from UpsampleOptions::mode_t to InterpolateOptions::mode_t is always well-defined :( Maybe we can create a temporary variable and use if-branching to set the value to be passed into F::interpolate explicitly :D

Copy link
Contributor Author

@jon-tow jon-tow Oct 25, 2019

Choose a reason for hiding this comment

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

Note: The TORCH_ARG-macro carries the private: access modifier over to the next line so that any following non-TORCH_ARG declaration gets "secretly" privatized. In this case, it hides
typedef c10::variant<...> mode_t (and similarly other option variants such as the reduction_ts) from users.

I had to place the InterpolateOptions::mode_t type alias declaration before any TORCH_ARG to expose the type publicly. This allowed me to create a temporary InterpolateOptions::mode_t mode; and ergonomically make the necessary update based on your suggestion.

Just thought I'd mention it because it may be useful to let users have access to these types: modes, reductions, etc. :D. Otherwise, decltype seems to be the only way to get this type information. Let me know what you think! :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks so much for the catch! Yes I think we should establish the convention throughout the codebase that typedef c10::variant<...> mode_t should always happen in the first line of any Options that uses c10::variant (It is not enforced everywhere in the codebase right now, I opened a PR for PadOptions in #28760) :D

@yf225
Copy link
Contributor

yf225 commented Oct 24, 2019

We might need to resolve the conflict with the changes in upstream test/cpp_api_parity/parity-tracker.md :/

Copy link
Contributor

@yf225 yf225 left a comment

Choose a reason for hiding this comment

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

@jon-tow Thanks so much for the awesome work! :D

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@yf225 has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

facebook-github-bot pushed a commit that referenced this pull request Oct 27, 2019
Summary:
Based on the discussion in #28413 (comment), putting anything that's not tagged as `public:` under a `TORCH_ARG` line would hide it under `private:`. To get around this problem, we should move the `mode_t` declaration at the top of the PadOptions declaration.
Pull Request resolved: #28760

Differential Revision: D18165117

Pulled By: yf225

fbshipit-source-id: cf39c0a893822264cd6a64cd887729afcd84dbd0
@jon-tow
Copy link
Contributor Author

jon-tow commented Oct 28, 2019

No problem, @yf225! Thanks for your time and advice. Glad to lend a hand :)

@facebook-github-bot
Copy link
Contributor

@yf225 merged this pull request in 52dd587.

@jon-tow jon-tow deleted the c++-api/upsample branch October 29, 2019 21:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants