-
Notifications
You must be signed in to change notification settings - Fork 4
add RecordStreamTest.cpp #47
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
youge325
wants to merge
2
commits into
PFCCLab:master
Choose a base branch
from
youge325:cRecord
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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
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,247 @@ | ||
| #include <ATen/ATen.h> | ||
| #include <ATen/core/Tensor.h> | ||
| #include <ATen/ops/zeros.h> | ||
| #include <c10/core/Stream.h> | ||
| #include <c10/cuda/CUDAStream.h> | ||
| #include <gtest/gtest.h> | ||
| #include <torch/all.h> | ||
|
|
||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| #include "../src/file_manager.h" | ||
|
|
||
| extern paddle_api_test::ThreadSafeParam g_custom_param; | ||
|
|
||
| namespace at { | ||
| namespace test { | ||
|
|
||
| using paddle_api_test::FileManerger; | ||
| using paddle_api_test::ThreadSafeParam; | ||
|
|
||
| class RecordStreamTest : public ::testing::Test { | ||
| protected: | ||
| void SetUp() override { cpu_tensor = at::zeros({2, 3}, at::kFloat); } | ||
| at::Tensor cpu_tensor; | ||
| }; | ||
|
|
||
| static at::Stream get_default_cuda_stream() { | ||
| return c10::cuda::getCurrentCUDAStream(0); | ||
| } | ||
|
|
||
| // --- 基础功能测试:CUDA tensor + CUDA stream --- | ||
|
|
||
| // kFloat, shape {2,3} (small) | ||
| TEST_F(RecordStreamTest, CudaFloat2x3) { | ||
| auto file_name = g_custom_param.get(); | ||
| FileManerger file(file_name); | ||
| file.createFile(); | ||
| file << "CudaFloat2x3 "; | ||
| if (!torch::cuda::is_available()) { | ||
| file << "no_cuda"; | ||
| file << "\n"; | ||
| file.saveFile(); | ||
| return; | ||
| } | ||
| try { | ||
| at::Tensor t = cpu_tensor.cuda(); | ||
| at::Stream stream = get_default_cuda_stream(); | ||
| t.record_stream(stream); | ||
| file << "1"; | ||
| } catch (const std::exception& e) { | ||
| file << "exception"; | ||
| } | ||
| file << "\n"; | ||
| file.saveFile(); | ||
| } | ||
|
|
||
| // kDouble, shape {4} (small, different dtype) | ||
| TEST_F(RecordStreamTest, CudaDouble4) { | ||
| auto file_name = g_custom_param.get(); | ||
| FileManerger file(file_name); | ||
| file.openAppend(); | ||
| file << "CudaDouble4 "; | ||
| if (!torch::cuda::is_available()) { | ||
| file << "no_cuda"; | ||
| file << "\n"; | ||
| file.saveFile(); | ||
| return; | ||
| } | ||
| try { | ||
| at::Tensor t = at::zeros({4}, at::kDouble).cuda(); | ||
| at::Stream stream = get_default_cuda_stream(); | ||
| t.record_stream(stream); | ||
| file << "1"; | ||
| } catch (const std::exception& e) { | ||
| file << "exception"; | ||
| } | ||
| file << "\n"; | ||
| file.saveFile(); | ||
| } | ||
|
|
||
| // kInt, shape {100,100} (large, >= 10000 elements) | ||
| TEST_F(RecordStreamTest, CudaInt100x100) { | ||
| auto file_name = g_custom_param.get(); | ||
| FileManerger file(file_name); | ||
| file.openAppend(); | ||
| file << "CudaInt100x100 "; | ||
| if (!torch::cuda::is_available()) { | ||
| file << "no_cuda"; | ||
| file << "\n"; | ||
| file.saveFile(); | ||
| return; | ||
| } | ||
| try { | ||
| at::Tensor t = at::zeros({100, 100}, at::kInt).cuda(); | ||
| at::Stream stream = get_default_cuda_stream(); | ||
| t.record_stream(stream); | ||
| file << "1"; | ||
| } catch (const std::exception& e) { | ||
| file << "exception"; | ||
| } | ||
| file << "\n"; | ||
| file.saveFile(); | ||
| } | ||
|
|
||
| // kLong, shape {} (0-d scalar tensor) | ||
| TEST_F(RecordStreamTest, CudaLongScalar) { | ||
| auto file_name = g_custom_param.get(); | ||
| FileManerger file(file_name); | ||
| file.openAppend(); | ||
| file << "CudaLongScalar "; | ||
| if (!torch::cuda::is_available()) { | ||
| file << "no_cuda"; | ||
| file << "\n"; | ||
| file.saveFile(); | ||
| return; | ||
| } | ||
| try { | ||
| at::Tensor t = at::zeros({}, at::kLong).cuda(); | ||
| at::Stream stream = get_default_cuda_stream(); | ||
| t.record_stream(stream); | ||
| file << "1"; | ||
| } catch (const std::exception& e) { | ||
| file << "exception"; | ||
| } | ||
| file << "\n"; | ||
| file.saveFile(); | ||
| } | ||
|
|
||
| // kFloat, shape {0} (空 tensor,边界 shape) | ||
| TEST_F(RecordStreamTest, CudaEmptyShape) { | ||
| auto file_name = g_custom_param.get(); | ||
| FileManerger file(file_name); | ||
| file.openAppend(); | ||
| file << "CudaEmptyShape "; | ||
| if (!torch::cuda::is_available()) { | ||
| file << "no_cuda"; | ||
| file << "\n"; | ||
| file.saveFile(); | ||
| return; | ||
| } | ||
| try { | ||
| at::Tensor t = at::zeros({0}, at::kFloat).cuda(); | ||
| at::Stream stream = get_default_cuda_stream(); | ||
| t.record_stream(stream); | ||
| file << "1"; | ||
| } catch (const std::exception& e) { | ||
| file << "exception"; | ||
| } | ||
| file << "\n"; | ||
| file.saveFile(); | ||
| } | ||
|
|
||
| // kFloat, shape {1,1,1} (全一维度,边界 shape) | ||
| TEST_F(RecordStreamTest, CudaAllOnes) { | ||
| auto file_name = g_custom_param.get(); | ||
| FileManerger file(file_name); | ||
| file.openAppend(); | ||
| file << "CudaAllOnes "; | ||
| if (!torch::cuda::is_available()) { | ||
| file << "no_cuda"; | ||
| file << "\n"; | ||
| file.saveFile(); | ||
| return; | ||
| } | ||
| try { | ||
| at::Tensor t = at::zeros({1, 1, 1}, at::kFloat).cuda(); | ||
| at::Stream stream = get_default_cuda_stream(); | ||
| t.record_stream(stream); | ||
| file << "1"; | ||
| } catch (const std::exception& e) { | ||
| file << "exception"; | ||
| } | ||
| file << "\n"; | ||
| file.saveFile(); | ||
| } | ||
|
|
||
| // kFloat, 非连续 tensor(经 transpose) | ||
| TEST_F(RecordStreamTest, CudaNonContiguous) { | ||
| auto file_name = g_custom_param.get(); | ||
| FileManerger file(file_name); | ||
| file.openAppend(); | ||
| file << "CudaNonContiguous "; | ||
| if (!torch::cuda::is_available()) { | ||
| file << "no_cuda"; | ||
| file << "\n"; | ||
| file.saveFile(); | ||
| return; | ||
| } | ||
| try { | ||
| at::Tensor base = at::zeros({3, 4}, at::kFloat).cuda(); | ||
| at::Tensor t = base.transpose(0, 1); // 非连续 | ||
| at::Stream stream = get_default_cuda_stream(); | ||
| t.record_stream(stream); | ||
| file << "1"; | ||
| } catch (const std::exception& e) { | ||
| file << "exception"; | ||
| } | ||
| file << "\n"; | ||
| file.saveFile(); | ||
| } | ||
|
|
||
| // --- 异常路径:CPU tensor + CUDA stream(如有 CUDA) --- | ||
| // record_stream 在两个框架下对 CPU tensor 的处理行为 | ||
| TEST_F(RecordStreamTest, CpuTensorCudaStream) { | ||
| auto file_name = g_custom_param.get(); | ||
| FileManerger file(file_name); | ||
| file.openAppend(); | ||
| file << "CpuTensorCudaStream "; | ||
| if (!torch::cuda::is_available()) { | ||
| file << "no_cuda"; | ||
| file << "\n"; | ||
| file.saveFile(); | ||
| return; | ||
| } | ||
| try { | ||
| at::Stream stream = get_default_cuda_stream(); | ||
| cpu_tensor.record_stream(stream); | ||
| file << "1"; | ||
| } catch (const std::exception& e) { | ||
| file << "exception"; | ||
| } | ||
| file << "\n"; | ||
| file.saveFile(); | ||
| } | ||
|
|
||
| // --- 异常路径:CPU tensor + CPU stream(无 CUDA 依赖) --- | ||
| // record_stream 是 CUDA-only API,CPU stream 应触发异常 | ||
| TEST_F(RecordStreamTest, CpuTensorCpuStream) { | ||
| auto file_name = g_custom_param.get(); | ||
| FileManerger file(file_name); | ||
| file.openAppend(); | ||
| file << "CpuTensorCpuStream "; | ||
| c10::Stream stream(c10::Stream::DEFAULT, | ||
| c10::Device(c10::DeviceType::CPU, 0)); | ||
| try { | ||
| cpu_tensor.record_stream(stream); | ||
| file << "1"; | ||
| } catch (const std::exception& e) { | ||
| file << "exception"; | ||
| } | ||
| file << "\n"; | ||
| file.saveFile(); | ||
| } | ||
|
|
||
| } // namespace test | ||
| } // namespace at |
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.
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.
find_package(CUDAToolkit QUIET)+ conditional include path append can still leave builds failing later with missingcuda_runtime_api.h(e.g., CUDA-enabled libtorch headers included but CUDA toolkit not found), and the failure will be a compiler error with no actionable CMake message. Consider adding a clearmessage(FATAL_ERROR ...)(or at leastmessage(WARNING ...)) when CUDA-dependent headers/tests are being built butCUDAToolkitis not found, so the configuration fails early and is easier to debug.