Skip to content

PyTorch example - #579

Merged
leofang merged 19 commits into
NVIDIA:mainfrom
msaroufim:pytorch_example
Apr 29, 2025
Merged

PyTorch example#579
leofang merged 19 commits into
NVIDIA:mainfrom
msaroufim:pytorch_example

Conversation

@msaroufim

@msaroufimmsaroufim commented Apr 25, 2025

Copy link
Copy Markdown
Contributor

Description

This PR provides an end to end example of integrating PyTorch with cuda-python. In particular this PR

  1. Ensures cuda-python reuses the same CUDA stream as PyTorch similarly to what Triton does. This logic is likely useful enough to encapsulate into its own helper on the core Stream object in cuda-python
  2. Shows how to pass in pointers to torch tensors to a kernel

Limitation

  1. scalars needs to be encapsulated in a torch.Tensor otherwise I was running into correctness issues

Example of running this locally

(cuda-p) [marksaroufim@devvm3083]~/cuda-python/cuda_core/examples% python pytorch_example.py PyTorch stream: <torch.cuda.Stream device=cuda:0 cuda_stream=0x0>
Using PyTorch stream ID: 0
Successfully created CUDA Python stream from PyTorch stream
Single precision test passed!
Double precision test passed!
All tests passed successfully!

Checklist

  • New or existing tests cover these changes.
  • The documentation is up to date with these changes.

@copy-pr-bot

Copy link
Copy Markdown
Contributor

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

Mark Saroufim added 2 commits April 25, 2025 14:20
Signed-off-by: Mark Saroufim <marksaroufim@meta.com>
@leofangleofang added P1 Medium priority - Should do cuda.core Everything related to the cuda.core module example Improvements or additions to code examples labels Apr 26, 2025
@leofang
leofang self-requested a review April 26, 2025 03:48
@leofangleofang assigned msaroufim and unassigned msaroufimApr 26, 2025
@leofangleofang added this to the cuda.core beta 4 milestone Apr 26, 2025
@leofang

Copy link
Copy Markdown
Member

Many thanks to your contributions, @msaroufim! 🙏 Please give us a bit of time to unblock you -- This repository, including cuda.core, originally was not allowed to accept external contributions. However, we've received legal approval to make changes for cuda.core, and #583 is the last step to open up the gate. Once we finalize the license change we can review/merge your PRs!

@leofangleofang added the blocked This task is currently blocked by other tasks label Apr 26, 2025
@msaroufim

Copy link
Copy Markdown
ContributorAuthor

No worries @leofang I understand, great repo! So looking forward to contributing more soon!

@leofangleofang removed the blocked This task is currently blocked by other tasks label Apr 28, 2025
@msaroufim

Copy link
Copy Markdown
ContributorAuthor

@leofang is this ready for a review? FWIW this is the PR I'm most interested in of the 3 I sent

@kkraus14

Copy link
Copy Markdown
Collaborator

@msaroufim apologies for the delay in reviewing this.

The examples are run as part of the test suite so I think we need to add pytorch to the test dependencies: the requirements-cuXX.txt here: https://github.com/NVIDIA/cuda-python/tree/main/cuda_core/tests

And then also modify the test utilities to properly skip if torch isn't installed in the environment:

@msaroufim

msaroufim commented Apr 29, 2025

Copy link
Copy Markdown
ContributorAuthor

Thanks @kkraus14 mind kicking off a test run for me please?

@msaroufim

Copy link
Copy Markdown
ContributorAuthor

/ok to test 1378e6a

Comment threadcuda_core/tests/requirements-cu11.txt Outdated
Comment threadcuda_core/tests/requirements-cu12.txt Outdated
@kkraus14

kkraus14 commented Apr 29, 2025

Copy link
Copy Markdown
Collaborator

/ok to test 1378e6a

The testing runs on infrastructure that requires an NVIDIA maintainer to trigger.

@kkraus14

Copy link
Copy Markdown
Collaborator

/ok to test 1378e6a

Comment threadcuda_core/examples/pytorch_example.py Outdated
Comment threadcuda_core/examples/pytorch_example.py Outdated
msaroufimand others added 2 commits April 29, 2025 12:13
Co-authored-by: Keith Kraus <keith.j.kraus@gmail.com>
Co-authored-by: Keith Kraus <keith.j.kraus@gmail.com>
@github-actions

This comment has been minimized.

@kkraus14

Copy link
Copy Markdown
Collaborator

/ok to test 01ad808

@leofangleofang left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Great addition, thanks @msaroufim! I'll add another StridedMemoryView based example later (after #150 is done) and it will also be able to handle PyTorch tensors too, without users fetching the pointer as kernel arg. Will ping you for review.

Comment threadcuda_core/examples/pytorch_example.py Outdated
Comment threadcuda_core/examples/pytorch_example.py Outdated
Comment threadcuda_core/examples/pytorch_example.py Outdated
Comment threadcuda_core/examples/pytorch_example.py
Comment threadcuda_core/tests/requirements-cu12.txt Outdated
Comment threadcuda_core/tests/requirements-cu11.txt Outdated
Comment threadcuda_core/tests/requirements-cu12.txt Outdated
msaroufimand others added 7 commits April 29, 2025 14:07
Co-authored-by: Keith Kraus <keith.j.kraus@gmail.com>
Co-authored-by: Keith Kraus <keith.j.kraus@gmail.com>
Co-authored-by: Leo Fang <leo80042@gmail.com>
Co-authored-by: Leo Fang <leo80042@gmail.com>
Co-authored-by: Leo Fang <leo80042@gmail.com>
@msaroufim

Copy link
Copy Markdown
ContributorAuthor

Regarding the testing changes @leofang please feel free to revert depending on what makes sense to you, I just tested your changes now locally and everything worked well

Comment threadcuda_core/tests/requirements-cu11.txt Outdated
Comment threadcuda_core/tests/requirements-cu12.txt Outdated
@leofang

Copy link
Copy Markdown
Member

/ok to test 7d3582f

@leofang
leofang enabled auto-merge April 29, 2025 22:51
@leofang
leofang merged commit 19df0d9 into NVIDIA:mainApr 29, 2025
@github-actions

Copy link
Copy Markdown
Doc Preview CI
Preview removed because the pull request was closed or merged.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cuda.coreEverything related to the cuda.core moduleexampleImprovements or additions to code examplesP1Medium priority - Should do

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@msaroufim@leofang@kkraus14