Skip to content

[cuda] Add new gamma beta backwards kernel #147773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from

Conversation

ahmadsharif1
Copy link
Contributor

@ahmadsharif1 ahmadsharif1 commented Feb 24, 2025

Context:
Prior to this PR we had 3 non-ROCM CUDA kernels to handle GammaBeta backwards pass:

  1. For small M
  2. 32x32 faster kernel for shapes that were divisible by 32 for both M and N
  3. All other cases

This approach had several weaknesses:

  1. For non-32x32 case, the performance was slow because we were not using warp shuffles there
  2. For small M we were not doing coalesced loads so performance was poor in that case (though the total runtime is quite small in those cases so perhaps it doesn't matter much)
  3. For large M and small N, we were only using few SMs in the GPU because we were only exploiting parallelism in the N dimension, not in the M dimension
  4. We had to maintain 3 different kernels.

This PR:

  1. Adds a single templatized kernel that can technically replace all 3 kernels and get equal or faster performance. The only reason I left out the simple kernel is because USE_ROCM case was using that and I couldn't test my kernel with USE_ROCM
  2. Depending on template parameters, this kernel can either fully reduce the grad values or partially reduce them. In the partial reduction case, a second kernel is needed to fully reduce them.
  3. For the large M and small N case, we can launch the partial reduction kernel followed by a .sum() to do the full reduction. The advantage is the partial reduction can fully utilize all SMs on the GPU as we parallelize across the M dimension. This can lead to pretty dramatic performance gains -- for instance, I saw 10x+ performance improvement for M=7e6 and N=32 (which was from a real model).

Full performance results are shown below on my H100:

image

Copy link

pytorch-bot bot commented Feb 24, 2025

🔗 Helpful Links

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

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

❌ 15 New Failures

As of commit a634482 with merge base ffa19b9 (image):

NEW FAILURES - The following jobs have failed:

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

@pytorch-bot pytorch-bot bot added the release notes: nn release notes category label Feb 24, 2025
Copy link
Contributor

Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as Stale.
Feel free to remove the Stale label if you feel this was a mistake.
If you are unable to remove the Stale label please contact a maintainer in order to do so.
If you want the bot to never mark this PR stale again, add the no-stale label.
Stale pull requests will automatically be closed after 30 days of inactivity.

@github-actions github-actions bot added the Stale label Apr 26, 2025
@github-actions github-actions bot closed this May 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release notes: nn release notes category Stale
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant