Skip to content

inductor: avoiding moving constructor to cuda when it would cause h2d sync in index_put_ fallback - #130338

Closed
bdhirsh wants to merge 3 commits into
gh/bdhirsh/584/basefrom
gh/bdhirsh/584/head
Closed

bdhirsh wants to merge 3 commits into
gh/bdhirsh/584/basefrom
gh/bdhirsh/584/head

Conversation

@bdhirsh

@bdhirsh bdhirsh commented Jul 9, 2024 •

Copy link
Copy Markdown
Collaborator

My attempt at a fix for #130335, see issue for more details / internal xref. Any feedback from inductor folks is appreciated. I attempted to make the move-constructors-to-cuda pass a bit less aggressive by detecting when the movement would incur a H2D sync for aten.index_put_. I'm not sure if there are any other ops that inductor falls back to eager on, that may-or-may-not incur a H2D sync if we change any of their inputs from cpu to cuda.

Stack from ghstack (oldest at bottom):

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @peterbell10 @ipiszy @yf225 @chenyang78 @kadeng @muchulee8 @ColinPeppler @amjames @desertfire @chauhang

… sync in index_put_ fallback

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Jul 9, 2024 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/130338

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 5e6f3a2 with merge base 81df076 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@eellison eellison left a comment

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.

Looks good ! Test ?

Comment thread torch/_inductor/fx_passes/post_grad.py Outdated
# Therefore, when inductor sees an index_put_ with byte tensor indices,
# it should *not* convert the cpu scalar value into a cuda tensor.
any_byte_bool_indices = False
indices = node.args[1]

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: use operator_schemas.normalize_function instead of directly accessing args

Comment thread torch/_inductor/fx_passes/post_grad.py Outdated


def is_index_put_and_requires_h2d_sync_for_cuda_value(node):
if node.target is not torch.ops.aten.index_put.default:

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.

do we want this for index_put_ as well ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I figured it would just never get hit since functionalization has already run, and reinplacing runs in a later pass. But probably doesn't hurt to add, can add it

@bdhirsh

bdhirsh commented Jul 9, 2024

Copy link
Copy Markdown
Collaborator Author

Looks good ! Test ?

hmmm let me know if you have better testing ideas - the idea of a "no h2d sync happened" test feels a bit fragile (e.g. I could have the test run the profiler and parse the output to ensure that nonzero() never shows up, but that doesn't feel great).

Alternatively, I could just add an expecttest test where I assert that the final inductor graph properly keeps the scalar-tensor on cpu when the above conditions apply, as a proxy for the H2D sync test (i'll probably default to that)

@eellison

eellison commented Jul 9, 2024

Copy link
Copy Markdown
Contributor

@bdhirsh yea sg, that's what i was thinking

bdhirsh added 2 commits July 9, 2024 14:06
…d cause h2d sync in index_put_ fallback"

My attempt at a fix for #130335, see issue for more details / internal xref. Any feedback from inductor folks is appreciated. I attempted to make the move-constructors-to-cuda pass a bit less aggressive by detecting when the movement would incur a H2D sync for `aten.index_put_`. I'm not sure if there are any other ops that inductor falls back to eager on, that may-or-may-not incur a H2D sync if we change any of their inputs from cpu to cuda.




cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx peterbell10 ipiszy yf225 chenyang78 kadeng muchulee8 ColinPeppler amjames desertfire chauhang

[ghstack-poisoned]
…d cause h2d sync in index_put_ fallback"

My attempt at a fix for #130335, see issue for more details / internal xref. Any feedback from inductor folks is appreciated. I attempted to make the move-constructors-to-cuda pass a bit less aggressive by detecting when the movement would incur a H2D sync for `aten.index_put_`. I'm not sure if there are any other ops that inductor falls back to eager on, that may-or-may-not incur a H2D sync if we change any of their inputs from cpu to cuda.




cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx peterbell10 ipiszy yf225 chenyang78 kadeng muchulee8 ColinPeppler amjames desertfire chauhang

[ghstack-poisoned]
@eellison

Copy link
Copy Markdown
Contributor

land ?

@bdhirsh

bdhirsh commented Jul 15, 2024

Copy link
Copy Markdown
Collaborator Author

@pytorchbot merge

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Jul 15, 2024
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

mlazos pushed a commit that referenced this pull request Jul 18, 2024
… sync in index_put_ fallback (#130338)

My attempt at a fix for #130335, see issue for more details / internal xref. Any feedback from inductor folks is appreciated. I attempted to make the move-constructors-to-cuda pass a bit less aggressive by detecting when the movement would incur a H2D sync for `aten.index_put_`. I'm not sure if there are any other ops that inductor falls back to eager on, that may-or-may-not incur a H2D sync if we change any of their inputs from cpu to cuda.

Pull Request resolved: #130338
Approved by: https://github.com/eellison
xuhancn pushed a commit to xuhancn/pytorch that referenced this pull request Jul 25, 2024
… sync in index_put_ fallback (pytorch#130338)

My attempt at a fix for pytorch#130335, see issue for more details / internal xref. Any feedback from inductor folks is appreciated. I attempted to make the move-constructors-to-cuda pass a bit less aggressive by detecting when the movement would incur a H2D sync for `aten.index_put_`. I'm not sure if there are any other ops that inductor falls back to eager on, that may-or-may-not incur a H2D sync if we change any of their inputs from cpu to cuda.

Pull Request resolved: pytorch#130338
Approved by: https://github.com/eellison
@github-actions
github-actions Bot deleted the gh/bdhirsh/584/head branch August 15, 2024 01:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants