-
Notifications
You must be signed in to change notification settings - Fork 660
【Hackathon 9th No.73】add unit tests for graph_opt_backend #3609
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
gongshaotian
merged 8 commits into
PaddlePaddle:develop
from
ooooo-create:test/add_unit_tests_for_graph_opt_backend
Sep 3, 2025
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
dbdaa9a
test: add unit tests for graph_opt_backend
ooooo-create 0adec77
refactor(tests): improve graph optimization test structure and readab…
ooooo-create 9a6c43a
fix(tests): correct CUDA graph related typos in test files
ooooo-create 7ede3e6
Merge branch 'develop' of https://github.com/PaddlePaddle/FastDeploy …
ooooo-create fdb0135
refactor(test): support attention layer and optimize graph optimizati…
ooooo-create fd9ff8a
remove some func call
ooooo-create 2491d2c
Merge branch 'develop' into test/add_unit_tests_for_graph_opt_backend
gongshaotian cad3e7e
Merge branch 'develop' into test/add_unit_tests_for_graph_opt_backend
luotao1 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| """ | ||
| # 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 | ||
|
|
||
| import numpy as np | ||
| import paddle | ||
| from paddle.nn import functional as F | ||
|
|
||
| from fastdeploy.config import ( | ||
| CacheConfig, | ||
| FDConfig, | ||
| GraphOptimizationConfig, | ||
| ParallelConfig, | ||
| ) | ||
| from fastdeploy.model_executor.forward_meta import ForwardMeta | ||
| from fastdeploy.model_executor.graph_optimization.decorator import ( | ||
| support_graph_optimization, | ||
| ) | ||
|
|
||
|
|
||
| @support_graph_optimization | ||
| class TinyModel(paddle.nn.Layer): | ||
| """Test Model""" | ||
|
|
||
| def __init__(self, fd_config: FDConfig, d_model: int, d_hidden: int): | ||
| super().__init__() | ||
| self.fd_config = fd_config | ||
|
|
||
| self.W1 = paddle.ones([d_model, d_hidden]).astype("float32") | ||
| self.b1 = paddle.ones([d_hidden]).astype("float32") | ||
| self.W2 = paddle.ones([d_hidden, d_model]).astype("float32") | ||
| self.b2 = paddle.ones([d_model]).astype("float32") | ||
|
|
||
| def forward(self, ids_remove_padding, forward_meta: ForwardMeta): | ||
| """Test model forward pass""" | ||
| h = F.relu(F.linear(forward_meta.input_ids, self.W1, self.b1)) | ||
| return forward_meta.input_ids + F.linear(h, self.W2, self.b2) | ||
|
|
||
|
|
||
| def numpy_baseline(d_model: int, d_hidden: int, x: np.ndarray): | ||
gongshaotian marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| W1 = np.ones((d_model, d_hidden), dtype="float32") | ||
| b1 = np.ones(d_hidden, dtype="float32") | ||
| W2 = np.ones((d_hidden, d_model), dtype="float32") | ||
| b2 = np.ones(d_model, dtype="float32") | ||
|
|
||
| h = np.maximum(0, x @ W1 + b1) | ||
| return x + (h @ W2 + b2) | ||
|
|
||
|
|
||
| class TestGrpahOptBackend(unittest.TestCase): | ||
ooooo-create marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
| Test graph_opt_backend | ||
| """ | ||
|
|
||
| def test_graph_opt_backend(self): | ||
| """Run test case""" | ||
| graph_opt_config = GraphOptimizationConfig(args={}) | ||
| graph_opt_config.use_cudagraph = False | ||
| parallel_config = ParallelConfig(args={}) | ||
| parallel_config.max_num_seqs = 1 | ||
| cache_config = CacheConfig({}) | ||
| # Initialize cuda graph capture list | ||
| graph_opt_config._set_cudagraph_sizes(max_num_seqs=parallel_config.max_num_seqs) | ||
| graph_opt_config.init_with_cudagrpah_size(max_num_seqs=parallel_config.max_num_seqs) | ||
ooooo-create marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
gongshaotian marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| fd_config = FDConfig( | ||
| graph_opt_config=graph_opt_config, | ||
| parallel_config=parallel_config, | ||
| cache_config=cache_config, | ||
| test_mode=True, | ||
| ) | ||
| input_np = np.ones([2, 4, 16], dtype="float32") | ||
| # Run Numpy Baseline | ||
| output_numpy = numpy_baseline(16, 32, input_np) | ||
|
|
||
| input_tensor = paddle.ones([2, 4, 16], dtype="float32") | ||
| # Run Test Dynamic Graph | ||
| test_model_dynamic = TinyModel(fd_config=fd_config, d_model=16, d_hidden=32) | ||
| forward_meta = ForwardMeta(input_ids=input_tensor, ids_remove_padding=input_tensor, step_use_cudagraph=True) | ||
| output_dynamic = test_model_dynamic(ids_remove_padding=input_tensor, forward_meta=forward_meta) | ||
| np.testing.assert_allclose(output_numpy, output_dynamic.numpy()) | ||
|
|
||
| # Run Test Static Graph | ||
| graph_opt_config.graph_opt_level = 1 | ||
| fd_config = FDConfig( | ||
| graph_opt_config=graph_opt_config, | ||
| parallel_config=parallel_config, | ||
| cache_config=cache_config, | ||
| test_mode=True, | ||
| ) | ||
| test_model_static = TinyModel(fd_config=fd_config, d_model=16, d_hidden=32) | ||
| output_static = test_model_static(ids_remove_padding=input_tensor, forward_meta=forward_meta) | ||
| np.testing.assert_allclose(output_numpy, output_static.numpy()) | ||
|
|
||
| # Run Test CINN | ||
| graph_opt_config.graph_opt_level = 2 | ||
| fd_config = FDConfig( | ||
| graph_opt_config=graph_opt_config, | ||
| parallel_config=parallel_config, | ||
| cache_config=cache_config, | ||
| test_mode=True, | ||
| ) | ||
| test_model_cinn = TinyModel(fd_config=fd_config, d_model=16, d_hidden=32) | ||
| output_cinn = test_model_cinn(ids_remove_padding=input_tensor, forward_meta=forward_meta) | ||
| np.testing.assert_allclose(output_numpy, output_cinn.numpy()) | ||
|
|
||
| graph_opt_config.use_cudagraph = True | ||
| # Run Test Dynamic + CudaGraph | ||
| graph_opt_config.graph_opt_level = 0 | ||
| fd_config = FDConfig( | ||
| graph_opt_config=graph_opt_config, | ||
| parallel_config=parallel_config, | ||
| cache_config=cache_config, | ||
| test_mode=True, | ||
| ) | ||
| test_model_dynamic_cudagraph = TinyModel(fd_config=fd_config, d_model=16, d_hidden=32) | ||
| output_dynamic_cudagraph = test_model_dynamic_cudagraph( | ||
| ids_remove_padding=input_tensor, forward_meta=forward_meta | ||
| ) | ||
| np.testing.assert_allclose(output_numpy, output_dynamic_cudagraph.numpy()) | ||
|
|
||
| # Run Test Static + CudaGraph | ||
| graph_opt_config.graph_opt_level = 1 | ||
| fd_config = FDConfig( | ||
| graph_opt_config=graph_opt_config, | ||
| parallel_config=parallel_config, | ||
| cache_config=cache_config, | ||
| test_mode=True, | ||
| ) | ||
| test_model_static_cudagraph = TinyModel(fd_config=fd_config, d_model=16, d_hidden=32) | ||
| output_static_cudagraph = test_model_static_cudagraph( | ||
| ids_remove_padding=input_tensor, forward_meta=forward_meta | ||
| ) | ||
| np.testing.assert_allclose(output_numpy, output_static_cudagraph.numpy()) | ||
|
|
||
| # Run Test CINN + CudaGraph | ||
| graph_opt_config.graph_opt_level = 2 | ||
| fd_config = FDConfig( | ||
| graph_opt_config=graph_opt_config, | ||
| parallel_config=parallel_config, | ||
| cache_config=cache_config, | ||
| test_mode=True, | ||
| ) | ||
| test_model_cinn_cudagraph = TinyModel(fd_config=fd_config, d_model=16, d_hidden=32) | ||
| output_cinn_cudagraph = test_model_cinn_cudagraph(ids_remove_padding=input_tensor, forward_meta=forward_meta) | ||
| np.testing.assert_allclose(output_numpy, output_cinn_cudagraph.numpy()) | ||
ooooo-create marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
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.