Skip to content

Add CUDAGuard to ATen#9277

Closed
goldsborough wants to merge 8 commits intopytorch:masterfrom
goldsborough:auto-stream
Closed

Add CUDAGuard to ATen#9277
goldsborough wants to merge 8 commits intopytorch:masterfrom
goldsborough:auto-stream

Conversation

@goldsborough
Copy link
Contributor

@goldsborough goldsborough commented Jul 9, 2018

THCStream was recently moved to ATen by @mruberry: #8997. This PR now introduces a guard class that replaces AutoStream from torch/csrc/ and also uses this new stream interface.

I had to extend the CUDAStream interface with unchecked calls, so that we can reset the stream without throwing an exception in the guard's destructor.

@colesbury @apaszke @ezyang

Fixes #7800

@goldsborough goldsborough force-pushed the auto-stream branch 2 times, most recently from 11b423a to 8f9994b Compare July 10, 2018 02:06
@goldsborough
Copy link
Contributor Author

@pytorchbot retest this please

@goldsborough
Copy link
Contributor Author

@pytorchbot retest this please

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

@goldsborough
Copy link
Contributor Author

After discussing a strategy with @mruberry, we agreed that it would be better that CUDAStreamGuard (formerly) not only set the current stream, but also the current device to the one associated with the stream supplied to the guard. In that sense, this kind of guard would actually be more of an extension ofat::DeviceGuard, giving it an understanding of CUDA streams (and maybe in the future, more CUDA specific things). As such, we decided to call it CUDAGuard (like DeviceGuard, but for all CUDA things).

Given the above, it might seem to make sense to inherit CUDAGuard from DeviceGuard. At the moment DeviceGuard is still very CUDA specific, so this would be fine. However in the future DeviceGuard should gain more understanding of other kinds of devices. At that point, a CUDAGuard would not follow an is-a relationship with DeviceGuard anymore (e.g. a DeviceGuard can accept a RandomFPGADevice, but it wouldn't make sense for a CUDAGuard to deal with such an object). So instead of inheritance, I think it's better to use composition and implement the device-specific parts of CUDAGuard using a DeviceGuard. So much for my design thoughts here.

NOTE: I changed AT_CHECK in CUDAStream.cpp to AT_ASSERT. AT_ASSERT should be used for asserts without a message, AT_CHECK for asserts with a message.

@goldsborough goldsborough changed the title Add CUDAStreamGuard to ATen Add CUDAGuard to ATen Jul 13, 2018
@mruberry
Copy link
Collaborator

(summary of offline discussion w/@goldsborough): the entire active stream state must be captured, not just one stream, to preserve the invariant that the stream state resets when the guard is destroyed.

@goldsborough
Copy link
Contributor Author

Now storing the currently active stream for each device upon construction @mruberry
@colesbury could you take a look from PyTorch end?

@mruberry
Copy link
Collaborator

Looks super good to me and I really like the tests you added.

So glad that AutoStream is officially dead.

@mruberry mruberry mentioned this pull request Jul 13, 2018
Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

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

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

@goldsborough
Copy link
Contributor Author

@colesbury I've made both DeviceGuard and CUDAGuard movable, and addressed the nits. Thanks for the review! Let me know if this is better now

@goldsborough goldsborough force-pushed the auto-stream branch 2 times, most recently from 0e99968 to cb96296 Compare July 17, 2018 23:18
@goldsborough
Copy link
Contributor Author

@pytorchbot retest this please

Copy link
Member

@colesbury colesbury left a comment

Choose a reason for hiding this comment

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

lgtm

/// Move-constructs this `CUDAGuard` from another `CUDAGuard`. The
/// moved-from `CUDAGuard` is modified such that its destruction has no
/// effect (does not reset the stream or device).
CUDAGuard(CUDAGuard&& other) noexcept

This comment was marked as off-topic.

@@ -1,10 +1,10 @@
#include "ATen/CUDAStream.h"
#include "ATen/CUDAStream.h"

This comment was marked as off-topic.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@goldsborough is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@goldsborough is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

zdevito pushed a commit to zdevito/ATen that referenced this pull request Jul 18, 2018
Summary:
THCStream was recently moved to ATen by mruberry: pytorch/pytorch#8997. This PR now introduces a guard class that replaces `AutoStream` from `torch/csrc/` and also uses this new stream interface.

I had to extend the `CUDAStream` interface with unchecked calls, so that we can reset the stream without throwing an exception in the guard's destructor.

colesbury apaszke ezyang

Fixes pytorch/pytorch#7800
Pull Request resolved: pytorch/pytorch#9277

Differential Revision: D8865183

Pulled By: goldsborough

fbshipit-source-id: 67c9bc09629d92fa5660286b5eec08fde9108cd7
facebook-github-bot pushed a commit that referenced this pull request Jul 20, 2018
Summary:
ezyang noticed that the CUDAStream files lived under ATen/ despite being CUDA-specific, and suggested porting them to ATen/cuda and exposing them with a new CUDAContext. This PR does that. It also:

- Moves ATen's CUDA-specific exceptions for ATen/cudnn to ATen/cuda for consistency
- Moves getDeviceProperties() and getCurrentCUDASparseHandle() to CUDAContext from CUDAHooks

The separation between CUDAContext and CUDAHooks is straightforward. Files that are in CUDA-only builds should rely on CUDAContext, while CUDAHooks is for runtime dispatch in files that can be included in CPU-only builds. A comment in CUDAContext.h explains this pattern. Acquiring device properties and CUDA-specific handles is something only done in builds with CUDA, for example, so I moved them from CUDAHooks to CUDAContext.

This PR will conflict with #9277 and I will merge with master after #9277 goes in.
Pull Request resolved: #9435

Reviewed By: soumith

Differential Revision: D8917236

Pulled By: ezyang

fbshipit-source-id: 219718864234fdd21a2baff1dd3932ff289b5751
jramseyer pushed a commit to jramseyer/pytorch that referenced this pull request Jul 30, 2018
Summary:
THCStream was recently moved to ATen by mruberry: pytorch#8997. This PR now introduces a guard class that replaces `AutoStream` from `torch/csrc/` and also uses this new stream interface.

I had to extend the `CUDAStream` interface with unchecked calls, so that we can reset the stream without throwing an exception in the guard's destructor.

colesbury apaszke ezyang

Fixes pytorch#7800
Pull Request resolved: pytorch#9277

Differential Revision: D8865183

Pulled By: goldsborough

fbshipit-source-id: 67c9bc09629d92fa5660286b5eec08fde9108cd7
jramseyer pushed a commit to jramseyer/pytorch that referenced this pull request Jul 30, 2018
Summary:
ezyang noticed that the CUDAStream files lived under ATen/ despite being CUDA-specific, and suggested porting them to ATen/cuda and exposing them with a new CUDAContext. This PR does that. It also:

- Moves ATen's CUDA-specific exceptions for ATen/cudnn to ATen/cuda for consistency
- Moves getDeviceProperties() and getCurrentCUDASparseHandle() to CUDAContext from CUDAHooks

The separation between CUDAContext and CUDAHooks is straightforward. Files that are in CUDA-only builds should rely on CUDAContext, while CUDAHooks is for runtime dispatch in files that can be included in CPU-only builds. A comment in CUDAContext.h explains this pattern. Acquiring device properties and CUDA-specific handles is something only done in builds with CUDA, for example, so I moved them from CUDAHooks to CUDAContext.

This PR will conflict with pytorch#9277 and I will merge with master after pytorch#9277 goes in.
Pull Request resolved: pytorch#9435

Reviewed By: soumith

Differential Revision: D8917236

Pulled By: ezyang

fbshipit-source-id: 219718864234fdd21a2baff1dd3932ff289b5751
goodlux pushed a commit to goodlux/pytorch that referenced this pull request Aug 15, 2018
Summary:
THCStream was recently moved to ATen by mruberry: pytorch#8997. This PR now introduces a guard class that replaces `AutoStream` from `torch/csrc/` and also uses this new stream interface.

I had to extend the `CUDAStream` interface with unchecked calls, so that we can reset the stream without throwing an exception in the guard's destructor.

colesbury apaszke ezyang

Fixes pytorch#7800
Pull Request resolved: pytorch#9277

Differential Revision: D8865183

Pulled By: goldsborough

fbshipit-source-id: 67c9bc09629d92fa5660286b5eec08fde9108cd7
goodlux pushed a commit to goodlux/pytorch that referenced this pull request Aug 15, 2018
Summary:
ezyang noticed that the CUDAStream files lived under ATen/ despite being CUDA-specific, and suggested porting them to ATen/cuda and exposing them with a new CUDAContext. This PR does that. It also:

- Moves ATen's CUDA-specific exceptions for ATen/cudnn to ATen/cuda for consistency
- Moves getDeviceProperties() and getCurrentCUDASparseHandle() to CUDAContext from CUDAHooks

The separation between CUDAContext and CUDAHooks is straightforward. Files that are in CUDA-only builds should rely on CUDAContext, while CUDAHooks is for runtime dispatch in files that can be included in CPU-only builds. A comment in CUDAContext.h explains this pattern. Acquiring device properties and CUDA-specific handles is something only done in builds with CUDA, for example, so I moved them from CUDAHooks to CUDAContext.

This PR will conflict with pytorch#9277 and I will merge with master after pytorch#9277 goes in.
Pull Request resolved: pytorch#9435

Reviewed By: soumith

Differential Revision: D8917236

Pulled By: ezyang

fbshipit-source-id: 219718864234fdd21a2baff1dd3932ff289b5751
@ezyang ezyang added the merged label Jun 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CUDA streams in ATen

5 participants