-
Notifications
You must be signed in to change notification settings - Fork 6k
[Dy2St][CUDAGraph] Set undefined place for CUDAGraph OP outputs before lowering to avoid unnecessary memcpy && Add CUDAGraph unitest
#75078
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a7575bb
GPU place -> base Place
DrRyanHuang c712f97
update comment
DrRyanHuang 9ef6d97
add unitest
DrRyanHuang 7d0ca66
2020 -> 2025
DrRyanHuang 9e38dbe
add other unitests
DrRyanHuang b3594c1
update unitest && roll back if for coverage
DrRyanHuang 8116081
Merge branch 'PaddlePaddle:develop' into cg
DrRyanHuang 20a2db6
update reason
DrRyanHuang 68a7dc6
remove test_cudagraph on win32
DrRyanHuang 970c194
skip dcu
DrRyanHuang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| # Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import unittest | ||
| from contextlib import contextmanager | ||
|
|
||
| import numpy as np | ||
|
|
||
| import paddle | ||
| from paddle.jit.dy2static.utils import CUDAGraphState | ||
|
|
||
| SEED = 2025 | ||
| np.random.seed(2025) | ||
|
|
||
|
|
||
| class Dy2StCudaGraphManager: | ||
| def __init__(self): | ||
| self.state = CUDAGraphState.DISABLE | ||
| self.captured_batch_size = set() | ||
| self.batch_size = -1 | ||
|
|
||
| def run_impl(self, original_run_impl, inputs, parameters, attrs): | ||
| prog_attrs, cuda_graph_attrs = attrs | ||
| if self.state == CUDAGraphState.REPLAY: | ||
| if self.batch_size not in self.captured_batch_size: | ||
| self.state = CUDAGraphState.DISABLE | ||
| elif self.state == CUDAGraphState.CAPTURE: | ||
| self.captured_batch_size.add(self.batch_size) | ||
|
|
||
| cuda_graph_attrs |= { | ||
| "cuda_graph_state": self.state, | ||
| "cuda_graph_dispatch_key": self.batch_size | ||
| if self.state != CUDAGraphState.DISABLE | ||
| else 0, | ||
| } | ||
| return original_run_impl( | ||
| inputs, parameters, (prog_attrs, cuda_graph_attrs) | ||
| ) | ||
|
|
||
| @contextmanager | ||
| def run_impl_guard(self): | ||
| with paddle.jit.dy2static.pir_partial_program.replace_run_impl_guard( | ||
| self.run_impl, | ||
| ): | ||
| yield | ||
|
|
||
|
|
||
| class CudaGraphRunner: | ||
| def __init__(self, runnable): | ||
| self.runnable = runnable | ||
| self.captured = False | ||
| self.cuda_graph_manager = Dy2StCudaGraphManager() | ||
|
|
||
| def run_static_model(self, x): | ||
| if not self.captured: | ||
| # Capture | ||
| self.cuda_graph_manager.state = CUDAGraphState.CAPTURE | ||
| self.cuda_graph_manager.batch_size = x.shape[0] | ||
| self.captured = True | ||
| with self.cuda_graph_manager.run_impl_guard(): | ||
| self.runnable(x) | ||
| return | ||
DrRyanHuang marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| # Replay | ||
| self.cuda_graph_manager.state = CUDAGraphState.REPLAY | ||
| self.cuda_graph_manager.batch_size = x.shape[0] | ||
| with self.cuda_graph_manager.run_impl_guard(): | ||
| return self.runnable(x) | ||
|
|
||
|
|
||
| class TestCUDAGraph(unittest.TestCase): | ||
| def get_function(self): | ||
| return lambda x: x + x | ||
|
|
||
| def test_cuda_graph(self): | ||
DrRyanHuang marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| x = paddle.rand([32, 64]) | ||
| fn = self.get_function() | ||
| runner = CudaGraphRunner(fn) | ||
DrRyanHuang marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # Captured | ||
DrRyanHuang marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| runner.run_static_model(x) | ||
| # Replay | ||
| y_cg = runner.run_static_model(x) | ||
| y_dy = fn(x) | ||
|
|
||
| self.assertTrue(paddle.allclose(y_dy, y_cg)) | ||
DrRyanHuang marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.