Skip to content

[CUDA] Add experimental green context support for SM carveout #159104

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

Open
wants to merge 17 commits into
base: main
Choose a base branch
from

Conversation

eqy
Copy link
Collaborator

@eqy eqy commented Jul 25, 2025

Low-level PyTorch APIs should be usable/stable enough at this point but we might move the underlying driver API usage a bit from here...

Built on top of @drisspg 's branch

cc @ptrblck @msaroufim @jerryzh168

@eqy eqy requested a review from syed-ahmed as a code owner July 25, 2025 00:02
@eqy eqy added module: cuda Related to torch.cuda, and CUDA support in general matrix multiplication labels Jul 25, 2025
Copy link

pytorch-bot bot commented Jul 25, 2025

🔗 Helpful Links

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

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

❌ 8 New Failures

As of commit 6db03ba with merge base 5f5f508 (image):

NEW FAILURES - The following jobs have failed:

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

Copy link
Contributor

This PR needs a release notes: label

If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@@ -1501,6 +1502,27 @@ def test_honor_sm_carveout(self) -> None:
self.assertNotEqual(no_carveout, carveout_66)
self.assertNotEqual(carveout_66, carveout_0)

@unittest.skipIf(not _get_torch_cuda_version() >= (12, 8), "Green Context only tested on 12.8+")
def test_greencontext_caveout(self):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
def test_greencontext_caveout(self):
def test_greencontext_carveout(self):

@unittest.skipIf(not _get_torch_cuda_version() >= (12, 8), "Green Context only tested on 12.8+")
def test_greencontext_caveout(self):
a = torch.randn(4096, 4096, device='cuda', dtype=torch.bfloat16)
ctx = torch.cuda.green_contexts.GreenContext.create(0, 1)
Copy link
Collaborator

Choose a reason for hiding this comment

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

want to make it _green_contexts or are we shore enough?

Copy link
Collaborator

Choose a reason for hiding this comment

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

One thing I could think of, num_sms should be the first arg, with the second defaulting to current device. Also, maybe use kwargs in the test to make it more obvious

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not sure if this is the final API that we really want to expose over e.g., a context-manager API so users are not manually creating/destroying contexts or calling pybind'ed methods directly

#if defined(CUDA_VERSION) && CUDA_VERSION >= 12080
CUdevice device;
C10_CUDA_DRIVER_CHECK(
c10::cuda::DriverAPI::get()->cuDeviceGet_(&device, device_id));
Copy link
Collaborator

Choose a reason for hiding this comment

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

would this work if someone tries to create GreenContext before initializing cuda context

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

nope, that was indeed broken

c10::cuda::DriverAPI::get()->cuCtxPushCurrent_(context_));
}
// currently hardcoes the new green context to use the default stream
// TODO(eqy): consider creating a new stream if e.g., it allows interop
Copy link
Collaborator

Choose a reason for hiding this comment

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

anything preventing us from using a different stream now?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not that I'm aware of, but I think it gets messy as say the user is already in a stream context manager---in that case the new context implicitly switches to another "side" stream (even if it's the default stream from the perspective of the new context). We'll still sync appropriately before and after switching to the green context, but if they are checking things like torch.cuda.current_stream it may cause some confusion

Copy link
Collaborator

Choose a reason for hiding this comment

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

but it would still be as confusing to get 0 for torch.cuda.current_stream() which I think would happen now?

Copy link
Collaborator

@Skylion007 Skylion007 left a comment

Choose a reason for hiding this comment

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

Nits


// Implement move operations
GreenContext(GreenContext&& other) noexcept
: green_ctx_(other.green_ctx_),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: can all done with member initializers using std::exchange

@HDCharles HDCharles added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Jul 28, 2025
// Convert to regular context
C10_CUDA_DRIVER_CHECK(
c10::cuda::DriverAPI::get()->cuCtxFromGreenCtx_(&context_, green_ctx_));
TORCH_INTERNAL_ASSERT(
Copy link
Collaborator

Choose a reason for hiding this comment

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

is it possible that C10_CUDA_DRIVER_CHECK is successful but conversion still fails? It should still be TORCH_CHECK in this case though, right? It's not "report a bug to pytorch", it's "something's wrong with your system"

c10::cuda::DriverAPI::get()->cuCtxPushCurrent_(context_));
}
// currently hardcoes the new green context to use the default stream
// TODO(eqy): consider creating a new stream if e.g., it allows interop
Copy link
Collaborator

Choose a reason for hiding this comment

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

but it would still be as confusing to get 0 for torch.cuda.current_stream() which I think would happen now?

Comment on lines 172 to 174
cudaEvent_t ev;
C10_CUDA_CHECK(cudaEventCreate(&ev));
C10_CUDA_CHECK(cudaEventRecord(ev, NULL));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
cudaEvent_t ev;
C10_CUDA_CHECK(cudaEventCreate(&ev));
C10_CUDA_CHECK(cudaEventRecord(ev, NULL));
CUDAEvent ev;
ev.record(0);

Did you mean to hardcode NULL stream here, or should it be current_stream?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
matrix multiplication module: cuda Related to torch.cuda, and CUDA support in general 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.

6 participants