Skip to content

[PyTorch] Use plain old function pointer for RecordFunctionCallback (reapply)#49408

Closed
swolchok wants to merge 3 commits intogh/swolchok/47/basefrom
gh/swolchok/47/head
Closed

[PyTorch] Use plain old function pointer for RecordFunctionCallback (reapply)#49408
swolchok wants to merge 3 commits intogh/swolchok/47/basefrom
gh/swolchok/47/head

Conversation

@swolchok
Copy link
Copy Markdown
Contributor

@swolchok swolchok commented Dec 15, 2020

Stack from ghstack:

Nearly every non-test callsite doesn't need to capture any variables anyway, and this saves 48 bytes per callback.

Differential Revision: D25563207

NOTE FOR REVIEWERS: This PR has internal Facebook specific changes or comments, please review them on Phabricator!

…reapply)

Nearly every non-test callsite doesn't need to capture any variables anyway, and this saves 48 bytes per callback.

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

**NOTE FOR REVIEWERS**: This PR has internal Facebook specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D25563207/)!

[ghstack-poisoned]
@swolchok swolchok requested a review from albanD as a code owner December 15, 2020 17:48
@facebook-github-bot facebook-github-bot added cla signed oncall: jit Add this issue/PR to JIT oncall triage queue labels Dec 15, 2020
@swolchok swolchok requested review from dhruvbird and malfet December 15, 2020 17:49
…nCallback (reapply)"

Nearly every non-test callsite doesn't need to capture any variables anyway, and this saves 48 bytes per callback.

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

**NOTE FOR REVIEWERS**: This PR has internal Facebook specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D25563207/)!

[ghstack-poisoned]
swolchok added a commit that referenced this pull request Dec 15, 2020
…reapply)

Pull Request resolved: #49408

Nearly every non-test callsite doesn't need to capture any variables anyway, and this saves 48 bytes per callback.
ghstack-source-id: 118625284

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

**NOTE FOR REVIEWERS**: This PR has internal Facebook specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D25563207/)!
try {
if (is_start) {
ctx = rfcb.start()(rf);
ctx = rfcb.start() ? rfcb.start()(rf) : nullptr;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

How about something like?

Suggested change
ctx = rfcb.start() ? rfcb.start()(rf) : nullptr;
if (auto& start = rcbf.start()) ctx = start(rf) else ctx = nullptr;

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.

there's no reason to complicate the code like this; start() is an inlineable load of a single pointer

Comment on lines +283 to +285
if (rfcb.end()) {
rfcb.end()(rf, ctx.get());
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit

Suggested change
if (rfcb.end()) {
rfcb.end()(rf, ctx.get());
}
if (auto& end = rfcb.end()) {
end(rf, ctx.get());
}

[](const RecordFunction&, ObserverContext*) {}):
start_(std::move(start)),
end_(std::move(end)) {
StartCallback start,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why not pass by reference here?(or keep std::move() when member variable is initialized

Suggested change
StartCallback start,
const StartCallback& start,

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.

because function pointers should be passed by value, just like void* or int, for efficiency reasons. Similarly, there is no need to move them.

}

inline const std::function<std::unique_ptr<ObserverContext>(const RecordFunction&)>& start() const {
inline StartCallback start() const {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why do we want a copy here?

Suggested change
inline StartCallback start() const {
inline const StartCallback& start() const {

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.

because function pointers are cheap to copy, like int or void*

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

std::function is not just a function pointer, is it?

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.

No, but this diff changes StartCallback to be a function pointer above.

Comment on lines +22 to +23
at::RecordFunctionCallback::StartCallback fn =
[](const at::RecordFunction&) -> std::unique_ptr<at::ObserverContext> { return nullptr; }) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why do we need a non-trivial initializer here?

Suggested change
at::RecordFunctionCallback::StartCallback fn =
[](const at::RecordFunction&) -> std::unique_ptr<at::ObserverContext> { return nullptr; }) {
at::RecordFunctionCallback::StartCallback fn = at::RecordFunctionCallback::StartCallback()

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.

I just left it the way the existing code had it, don't think it's expensive either way because it's a constant expression (address of a function)

@facebook-github-bot
Copy link
Copy Markdown
Contributor

facebook-github-bot commented Dec 15, 2020

💊 CI failures summary and remediations

As of commit 62d7d46 (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.

This comment has been revised 4 times.

@codecov
Copy link
Copy Markdown

codecov bot commented Dec 15, 2020

Codecov Report

Merging #49408 (62d7d46) into gh/swolchok/47/base (9234f50) will increase coverage by 0.00%.
The diff coverage is 96.87%.

@@                 Coverage Diff                  @@
##           gh/swolchok/47/base   #49408   +/-   ##
====================================================
  Coverage                80.56%   80.56%           
====================================================
  Files                     1875     1875           
  Lines                   202701   202697    -4     
====================================================
- Hits                    163309   163308    -1     
+ Misses                   39392    39389    -3     

…nCallback (reapply)"

Nearly every non-test callsite doesn't need to capture any variables anyway, and this saves 48 bytes per callback.

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

**NOTE FOR REVIEWERS**: This PR has internal Facebook specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D25563207/)!

[ghstack-poisoned]
swolchok added a commit that referenced this pull request Dec 15, 2020
…reapply)

Pull Request resolved: #49408

Nearly every non-test callsite doesn't need to capture any variables anyway, and this saves 48 bytes per callback.
ghstack-source-id: 118665808

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

**NOTE FOR REVIEWERS**: This PR has internal Facebook specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D25563207/)!
@facebook-github-bot
Copy link
Copy Markdown
Contributor

This pull request has been merged in 22c6daf.

@facebook-github-bot facebook-github-bot deleted the gh/swolchok/47/head branch December 19, 2020 15:18
hwangdeyu pushed a commit to hwangdeyu/pytorch that referenced this pull request Jan 6, 2021
…reapply) (pytorch#49408)

Summary:
Pull Request resolved: pytorch#49408

Nearly every non-test callsite doesn't need to capture any variables anyway, and this saves 48 bytes per callback.
ghstack-source-id: 118665808

Test Plan:
Wait for GitHub CI since we had C++14-specific issues with
this one in previous PR pytorch#48629

Reviewed By: malfet

Differential Revision: D25563207

fbshipit-source-id: 6a2831205917d465f8248ca37429ba2428d5626d
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.

3 participants