-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Add paddle.device.cuda.stream_guard API #35623
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
Changes from all commits
42b6c9a
565da73
6321ed1
c364df1
19a17c9
70a7cfe
9d7dd65
11fecca
908d6d6
ecc413c
7f4c12a
9729e22
c13f7ee
44b12d3
2b28170
868ad77
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,9 @@ | |
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import paddle | ||
| from paddle.fluid import core | ||
| from paddle.fluid.wrapped_decorator import signature_safe_contextmanager | ||
|
|
||
| from .streams import Stream # noqa: F401 | ||
| from .streams import Event # noqa: F401 | ||
|
|
@@ -24,6 +26,7 @@ | |
| 'synchronize', | ||
| 'device_count', | ||
| 'empty_cache', | ||
| 'stream_guard', | ||
| ] | ||
|
|
||
|
|
||
|
|
@@ -121,7 +124,7 @@ def device_count(): | |
|
|
||
|
|
||
| def empty_cache(): | ||
| """ | ||
| ''' | ||
| Releases idle cached memory held by the allocator so that those can be used in other GPU | ||
| application and visible in `nvidia-smi`. In most cases you don't need to use this function, | ||
| Paddle does not release the memory back to the OS when you remove Tensors on the GPU, | ||
|
|
@@ -137,7 +140,67 @@ def empty_cache(): | |
| tensor = paddle.randn([512, 512, 512], "float") | ||
| del tensor | ||
| paddle.device.cuda.empty_cache() | ||
| """ | ||
| ''' | ||
|
|
||
| if core.is_compiled_with_cuda(): | ||
| core.cuda_empty_cache() | ||
|
|
||
|
|
||
| def _set_current_stream(stream): | ||
| ''' | ||
| Set the current stream. | ||
|
|
||
| Parameters: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Parameters->Args
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 询问了陈龙,Args 或者 Parameters 都可以,为了与本页面其他API 保持统一,不进行修改。 |
||
| stream(paddle.device.cuda.Stream): The selected stream. | ||
|
|
||
| Returns: | ||
| CUDAStream: The previous stream. | ||
|
|
||
| ''' | ||
|
|
||
| if not isinstance(stream, paddle.device.cuda.Stream): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 下面的判断是否可以包含上面
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 我想问,可不可以统一成 TypeError?(其实我不应该写成 ValueError,想统一改成 TypeError) |
||
| raise TypeError("stream type should be paddle.device.cuda.Stream") | ||
|
|
||
| cur_stream = current_stream() | ||
| if id(stream) == id(cur_stream): | ||
| return stream | ||
| return core._set_current_stream(stream) | ||
|
|
||
|
|
||
| @signature_safe_contextmanager | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dygraph_only
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 待定 |
||
| def stream_guard(stream): | ||
| ''' | ||
| **Notes**: | ||
| **This API only supports dygraph mode currently.** | ||
|
|
||
| A context manager that specifies the current stream context by the given stream. | ||
|
|
||
| Parameters: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Paramters->Args
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 同上 |
||
| stream(paddle.device.cuda.Stream): the selected stream. If stream is None, just yield. The default value is None. | ||
|
|
||
| Examples: | ||
| .. code-block:: python | ||
|
|
||
| # required: gpu | ||
| import paddle | ||
|
|
||
| s = paddle.device.cuda.Stream() | ||
| data1 = paddle.ones(shape=[20]) | ||
| data2 = paddle.ones(shape=[20]) | ||
| with paddle.device.cuda.stream_guard(s): | ||
| data3 = data1 + data2 | ||
|
|
||
| ''' | ||
|
|
||
| if stream is not None and not isinstance(stream, paddle.device.cuda.Stream): | ||
| raise TypeError("stream type should be paddle.device.cuda.Stream") | ||
|
|
||
| cur_stream = current_stream() | ||
| if stream is None or id(stream) == id(cur_stream): | ||
| yield | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里单测是不是要加上同样的stream
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 经讨论后不需要修改。 |
||
| else: | ||
| pre_stream = _set_current_stream(stream) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. stream 是否影响分布式环境?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 会进行线下测试,相关结果后续会贴在开头的 comment 中。 |
||
| try: | ||
| yield | ||
| finally: | ||
| stream = _set_current_stream(pre_stream) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the hard code
1means?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1 means non-blocking stream. We init CUDA Stream with default non-blocking property following pytorch implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about using
paddle::platform::stream::StreamFlag::kStreamNonBlockinginstead of1?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done