Skip to content

Port masked_fill CUDA kernel to ATen - #38061

Closed
crcrpar wants to merge 5 commits into
pytorch:masterfrom
crcrpar:port-masked-fill-cuda
Closed

crcrpar wants to merge 5 commits into
pytorch:masterfrom
crcrpar:port-masked-fill-cuda

Conversation

@crcrpar

@crcrpar crcrpar commented May 7, 2020

Copy link
Copy Markdown
Collaborator

Related issue: #24303

This PR changes CPU implementation of #33330 though I don't see terrible regression as follows.

Profiling

CPU

Benchmark is executed with https://github.com/mingfeima/op_bench-py on the machine with 12 Intel(R) Core(TM) i7-7800X CPU @ 3.50GHz.

input size contiguous this pr [ms] v1.5 [ms] speedup
128 1000 TRUE 0.469 0.466 0.9936034115
256 1000 TRUE 0.94 0.932 0.9914893617
512 1000 TRUE 1.87 1.875 1.002673797
1024 1000 TRUE 3.735 3.748 1.003480589
128 1000 FALSE 0.467 0.469 1.004282655
256 1000 FALSE 0.937 0.94 1.003201708
512 1000 FALSE 1.873 1.878 1.002669514
1024 1000 FALSE 3.748 3.761 1.003468517

GPU

I see some regression in GPU kernel so investigating now.

# This PR
torch.version = 1.6.0a0+b30e60d, torch.path = /home/mkozuki/ghq/github.com/crcrpar/pyt-1/torch
Size: 65536, Forward: 1.1904239654541016e-05, Effective BW (GB/s): 16.5157965317845
Size: 131072, Forward: 1.2037754058837891e-05, Effective BW (GB/s): 32.66522958336304
Size: 262144, Forward: 1.218557357788086e-05, Effective BW (GB/s): 64.53795506413618
Size: 524288, Forward: 1.201152801513672e-05, Effective BW (GB/s): 130.9462041813418
Size: 1048576, Forward: 3.201484680175781e-05, Effective BW (GB/s): 98.25841177622878
Size: 2097152, Forward: 6.691217422485351e-05, Effective BW (GB/s): 94.02558014118655
Size: 4194304, Forward: 0.00012657642364501953, Effective BW (GB/s): 99.40960281267283
Size: 8388608, Forward: 0.0002461743354797363, Effective BW (GB/s): 102.22765078641397
Size: 16777216, Forward: 0.0004850029945373535, Effective BW (GB/s): 103.77595307016936
Size: 33554432, Forward: 0.0009639811515808106, Effective BW (GB/s): 104.42454796437107
# v1.5
torch.version = 1.5.0, torch.path = /home/mkozuki/.pyenv/versions/anaconda3-2020.02/envs/torch15/lib/python3.7/site-packages/torch
Size: 65536, Forward: 1.4140605926513672e-05, Effective BW (GB/s): 13.903788919777442
Size: 131072, Forward: 1.4185905456542969e-05, Effective BW (GB/s): 27.718780532168072
Size: 262144, Forward: 1.4300346374511718e-05, Effective BW (GB/s): 54.99391269303102
Size: 524288, Forward: 1.4100074768066406e-05, Effective BW (GB/s): 111.55004678146771
Size: 1048576, Forward: 2.8598308563232423e-05, Effective BW (GB/s): 109.99699485879117
Size: 2097152, Forward: 5.365610122680664e-05, Effective BW (GB/s): 117.25518358864254
Size: 4194304, Forward: 0.00010152578353881836, Effective BW (GB/s): 123.93809297900103
Size: 8388608, Forward: 0.00019715070724487304, Effective BW (GB/s): 127.6476475873989
Size: 16777216, Forward: 0.00038855552673339844, Effective BW (GB/s): 129.53527755258082
Size: 33554432, Forward: 0.0007711434364318847, Effective BW (GB/s): 130.53770705197672

@dr-ci

dr-ci Bot commented May 7, 2020 •

Copy link
Copy Markdown

💊 CI failures summary and remediations

As of commit 417533d (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 on the GitHub issue tracker.

See how this bot performed.

This comment has been revised 11 times.

@vincentqb

Copy link
Copy Markdown
Contributor

@mruberry -- do you want to review this PR?

@mruberry

Copy link
Copy Markdown
Collaborator

@VitalyFedyunin is probably a better reviewer for porting PRs.

@vincentqb
vincentqb requested a review from VitalyFedyunin May 12, 2020 21:08
@vincentqb vincentqb added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label May 12, 2020

@VitalyFedyunin VitalyFedyunin 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.

Overall good, just small comments.

iter.dtype(), "masked_fill_cuda", [&] {
scalar_t scalar_val = value.to<scalar_t>();
auto mask_dtype = iter.input_dtype(1);
if (mask_dtype == at::ScalarType::Bool) {

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.

Please keep TORCH_WARN("masked_fill_ received a mask with dtype torch.uint8, this behavior is now deprecated," \ "please use a mask with dtype torch.bool instead.");

Maybe change it to TORCH_WARN_ONCE

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.

Or better move this warning to common (CPU,CUDA) code.

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.

@VitalyFedyunin

Copy link
Copy Markdown
Contributor

Did you had chance to figure out the issue with smaller sizes perf on GPU?

@facebook-github-bot facebook-github-bot 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.

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

@crcrpar

crcrpar commented May 13, 2020

Copy link
Copy Markdown
Collaborator Author

Did you had chance to figure out the issue with smaller sizes perf on GPU?

No idea, unfortunately...

facebook-github-bot pushed a commit that referenced this pull request Jun 30, 2020
Summary:
This is the prototype for the modular utils that we've been discussing. It is admittedly a large PR, but a good fraction of that is documentation and examples. I've trimmed a bit on the edges since we last discussed this design (for instance Timer is no longer Fuzzer aware), but it's mostly the same.

In addition to the library and hermetic examples, I've included `examples.end_to_end` which tests #38061 over a variety of shapes, dtypes, degrees of broadcasting, and layouts. (CC crcrpar)  I only did CPU as I'm not set up on a GPU machine yet. [Results from my devserver](https://gist.github.com/robieta/d1a8e1980556dc3f4f021c9f7c3738e2)

Key takeaways:
  1) For contiguous Tensors, larger dtypes (fp32 and fp64) and lots of reuse of the mask due to broadcasting, improvements are significant. (Presumably due to better vectorization?)
  2) There is an extra ~1.5 us overhead, which dominates small kernels.
  3) Cases with lower write intensity (int8, lower mask fraction, etc) or non-contiguous seem to suffer.

Hopefully this demonstrates the proof-of-concept for how this tooling can be used to tune kernels and assess PRs. Looking forward to thoughts and feedback.
Pull Request resolved: #38338

Differential Revision: D21551048

Pulled By: robieta

fbshipit-source-id: 6c50e5439a04eac98b8a2355ef731852ba0500db
@ngimel

ngimel commented Jul 20, 2021

Copy link
Copy Markdown
Collaborator

masked_fill is ported

@ngimel ngimel closed this Jul 20, 2021
@crcrpar
crcrpar deleted the port-masked-fill-cuda branch July 20, 2021 23:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla signed open source 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.

7 participants