Skip to content

add a specialized check for gradients (RequiresGradCheck) to speed up runtime assumptions check #50392

Closed
Krovatkin wants to merge 7 commits intopytorch:masterfrom
Krovatkin:krovatkin/diff_check2
Closed

add a specialized check for gradients (RequiresGradCheck) to speed up runtime assumptions check #50392
Krovatkin wants to merge 7 commits intopytorch:masterfrom
Krovatkin:krovatkin/diff_check2

Conversation

@Krovatkin
Copy link
Copy Markdown
Contributor

@Krovatkin Krovatkin commented Jan 11, 2021

This change replaces general-case TypeChecks on DifferentiableGraphs addded in #49433 with specialized RequiresGradChecks that only checks if the profiled requires_gradients properties match tensors' requires_gard at runtime.
This change improves perf by 3-4% on fastrnns.

@facebook-github-bot facebook-github-bot added cla signed oncall: jit Add this issue/PR to JIT oncall triage queue labels Jan 11, 2021
@facebook-github-bot
Copy link
Copy Markdown
Contributor

facebook-github-bot commented Jan 11, 2021

💊 CI failures summary and remediations

As of commit 786a2cd539 (more details on the Dr. CI page):


  • 1/1 failures possibly* introduced in this PR
    • 1/1 non-CircleCI failure(s)

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.

Copy link
Copy Markdown
Contributor

@bertmaher bertmaher left a comment

Choose a reason for hiding this comment

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

Test failures are probably related?

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.

Just to be sure, this std::vector is only constructed once, right, and then cached inside the lambda below? I just want to make sure we're not allocating on every RequiresGradCheck.

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.

yup!

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.

I don't think rg-props should ever contain c10::nullopt, that wouldn't be checking anything at all. (it also makes it so we have to do an extra unwrap of the bool). Can we change std::vector<c10::optional<bool>> -> std::vector<bool> ?

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.

Yeah, we can, I just double check that Thomas indeed is conservatively setting requiresGrad to true if we it changed during profiling. Let me update it

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.

Any reason to check num_inputs > 0 here? Since this is a static property of the RequiresGradCheck op, let's catch it at construction time. (I also have mixed feelings about TORCH_INTERNAL_ASSERT in the interpreter loop period, but I can set that aside for now ;-) )

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.

err, that was a copy-pasta from TypeCheck! I think I should update both.

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.

auto t will make a copy of the Tensor. It seems like you should be able to use auto const& t but at least do auto& t.

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.

ah! a good catch! fixed

@Krovatkin Krovatkin requested a review from bertmaher January 12, 2021 19:19
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: profile (expected) shape is copy-pasta

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.

hehe, will fix.

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.

just wondering: is there any worry here about undefined tensor? (potentially not) what happens when you call undefined.requires_grad() ?

Copy link
Copy Markdown
Contributor Author

@Krovatkin Krovatkin Jan 12, 2021

Choose a reason for hiding this comment

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

undefined tensors shouldn't reach here: even with higher-order derivatives, we either guard AutogradAdd and AutogradAnyNonZero (they become aten::Add) and similar ops or we don't differentiate them?

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 did you add this here ?

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.

you are right. it doesn't make too much sense since all RequiresGradCheck should be outside :-) or differentiable graphs

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.

removed it.

Copy link
Copy Markdown
Contributor

@eellison eellison left a comment

Choose a reason for hiding this comment

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

nice !

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.

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

@codecov
Copy link
Copy Markdown

codecov Bot commented Jan 13, 2021

Codecov Report

Merging #50392 (685f51e) into master (9efe153) will decrease coverage by 0.00%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master   #50392      +/-   ##
==========================================
- Coverage   80.67%   80.67%   -0.01%     
==========================================
  Files        1910     1910              
  Lines      207842   207857      +15     
==========================================
+ Hits       167684   167689       +5     
- Misses      40158    40168      +10     

@Krovatkin Krovatkin force-pushed the krovatkin/diff_check2 branch from 786a2cd to 685f51e Compare January 15, 2021 05:03
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.

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

@facebook-github-bot
Copy link
Copy Markdown
Contributor

@Krovatkin merged this pull request in 8e60bf9.

@Krovatkin Krovatkin changed the title add RequiresGradCheck add a specialized check for gradients (RequiresGradCheck) to speed up runtime assumptions check Feb 24, 2021
laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 24, 2026
Summary:
This change improves perf by 3-4% on fastrnns.

Pull Request resolved: pytorch#50392

Reviewed By: izdeby

Differential Revision: D25891392

Pulled By: Krovatkin

fbshipit-source-id: 44d9b6907d3975742c9d77102fe6a85aab2c08c0
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.

4 participants