Skip to content

[BE] Replace M_PI with c10::pi constexpr variable#50819

Closed
malfet wants to merge 4 commits intomasterfrom
malfet/use-constexpr-var
Closed

[BE] Replace M_PI with c10::pi constexpr variable#50819
malfet wants to merge 4 commits intomasterfrom
malfet/use-constexpr-var

Conversation

@malfet
Copy link
Copy Markdown
Contributor

@malfet malfet commented Jan 20, 2021

Also, get rid of MSVC specific _USE_MATH_DEFINES

Test at compile time that c10::pi == M_PI

@malfet malfet added triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module better-engineering Relatively self-contained tasks for better engineering contributors labels Jan 20, 2021
@malfet malfet requested review from a team, albanD and pbelevich January 20, 2021 17:09
Copy link
Copy Markdown
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.

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

Copy link
Copy Markdown
Collaborator

@albanD albanD left a comment

Choose a reason for hiding this comment

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

What is the original motivation for this change?

Comment thread aten/src/ATen/native/Math.h Outdated
namespace at {
namespace native {
//TODO: Replace me with std::numbers::pi when C++20 becomes available
template <typename T> constexpr T pi = 3.14159265358979323846L;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Do we know the impact of replacing M_PI with this value? Could that lead to some precision changes?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

To ensure there are none, I can add .cpp file that include <math.h> and adds static_assert(at:native::pi<double> == M_PI)
The motivation is not to pollute global namespace with M_PI define and also prevent build system brittleness on Windows, where including before <math.h> would result in M_PI not being defined

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

That sounds good.

To ensure there are none, I can add .cpp file that include <math.h> and adds static_assert(at:native::pi == M_PI)

Would that be brittle on windows? haha
It would be nice though to have a dummy PR that adds this check and makes sure it passes on all our CI just as a sanity check.

Comment thread aten/src/ATen/native/cpu/UnaryOpsKernel.cpp Outdated
@malfet malfet force-pushed the malfet/use-constexpr-var branch 3 times, most recently from 3147319 to b377ff9 Compare January 21, 2021 01:25
Copy link
Copy Markdown
Collaborator

@albanD albanD left a comment

Choose a reason for hiding this comment

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

compilation failures look related right?

Also, get rid of MSVC specific `_USE_MATH_DEFINES`
@malfet malfet force-pushed the malfet/use-constexpr-var branch from b377ff9 to f77ff20 Compare January 21, 2021 19:10
@malfet
Copy link
Copy Markdown
Contributor Author

malfet commented Jan 21, 2021

@albanD yes they are, which were mostly due to the fact, that there seem to be no good way of having a specialized constexpr variable (and BFloat16 constructor is not constexpr) But everything is green now.

@codecov
Copy link
Copy Markdown

codecov Bot commented Jan 21, 2021

Codecov Report

Merging #50819 (f77ff20) into master (df96344) will decrease coverage by 0.00%.
The diff coverage is 88.88%.

@@            Coverage Diff             @@
##           master   #50819      +/-   ##
==========================================
- Coverage   81.02%   81.02%   -0.01%     
==========================================
  Files        1916     1916              
  Lines      209343   209343              
==========================================
- Hits       169624   169616       -8     
- Misses      39719    39727       +8     

Copy link
Copy Markdown
Collaborator

@albanD albanD 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 for the update

@malfet malfet changed the title [BE] Replace M_PI with at::native::pi constexpr variable [BE] Replace M_PI with c10::pi constexpr variable Jan 22, 2021
@malfet malfet force-pushed the malfet/use-constexpr-var branch from 403cd48 to 84db488 Compare January 22, 2021 01:51
Copy link
Copy Markdown
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.

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

While trying to access constexpr from kernel, NVCC complains that:
```
identifier "c10::pi<   ::c10::Half> " is undefined in device code
```
Copy link
Copy Markdown
Contributor

@walterddr walterddr left a comment

Choose a reason for hiding this comment

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

looks good. there are several more .cu/cc files in caffe2/operators that uses _USE_MATH_DEFINES for M_PI. Should we address them as well or leave it be?

Copy link
Copy Markdown
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.

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

@malfet
Copy link
Copy Markdown
Contributor Author

malfet commented Jan 22, 2021

@walterddr good point, let me do it in separate PR

@facebook-github-bot
Copy link
Copy Markdown
Contributor

@malfet merged this pull request in de8cd6b.

@malfet malfet deleted the malfet/use-constexpr-var branch January 26, 2021 03:36
Comment on lines +280 to +281
additional_summand = -c10::pi<scalar_t> /
compat_tan(c10::pi<scalar_t> * x);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

FYI this was casting to accscalar_t

laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 24, 2026
Summary:
Also, get rid of MSVC specific `_USE_MATH_DEFINES`

Test at compile time that c10::pi<double> == M_PI

Pull Request resolved: pytorch#50819

Reviewed By: albanD

Differential Revision: D25976330

Pulled By: malfet

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

Labels

better-engineering Relatively self-contained tasks for better engineering contributors cla signed Merged triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants