-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
[XPU] merge memery pool for Paddle and XHPC by using alloc overloading #63924
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3331c39
fuse xhpc and paddle buffer
lj970926 69f7a3a
refine code
lj970926 a744eea
bug fix
lj970926 c1d5a74
bug fix
lj970926 a153008
bug fix
lj970926 c4b9202
bug fix
lj970926 683f9f0
bug fix
lj970926 308c0c6
rename test
lj970926 f3f153d
bug fix
lj970926 a59bedd
refine code
lj970926 e491a73
bug fix
lj970926 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 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 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 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 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 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 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 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 |
---|---|---|
@@ -1 +1,2 @@ | ||
paddle_test(enforce_xpu_test SRCS enforce_xpu_test.cc) | ||
paddle_test(overload_xpu_alloc_test SRCS overload_xpu_alloc_test.cc) |
This file contains 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,104 @@ | ||
// Copyright (c) 2024 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. | ||
|
||
#include "paddle/fluid/memory/allocation/allocator.h" | ||
#include "paddle/fluid/memory/stats.h" | ||
#include "paddle/phi/backends/xpu/enforce_xpu.h" | ||
#include "paddle/phi/backends/xpu/xpu_context.h" | ||
|
||
#include "gtest/gtest.h" | ||
|
||
namespace paddle { | ||
namespace memory { | ||
TEST(XPUOverloadAllocTest, EnvTest) { | ||
setenv("XPUAPI_DEFAULT_SIZE", "4096", 1); | ||
// use alloc overload | ||
unsetenv("XPU_PADDLE_DISABLE_ALLOC_OVERLOAD"); | ||
phi::XPUContext dev_ctx_overload( | ||
phi::XPUPlace(phi::backends::xpu::GetXPUCurrentDeviceId())); | ||
EXPECT_STREQ(dev_ctx_overload.x_context()->get_option("XPUAPI_DEFAULT_SIZE"), | ||
"1"); | ||
EXPECT_NE(dev_ctx_overload.x_context()->overload_alloc_gm, nullptr); | ||
// do not use alloc overload | ||
setenv("XPU_PADDLE_DISABLE_ALLOC_OVERLOAD", "1", 1); | ||
phi::XPUContext dev_ctx_origin( | ||
phi::XPUPlace(phi::backends::xpu::GetXPUCurrentDeviceId())); | ||
EXPECT_STREQ(dev_ctx_origin.x_context()->get_option("XPUAPI_DEFAULT_SIZE"), | ||
"4096"); | ||
EXPECT_EQ(dev_ctx_origin.x_context()->overload_alloc_gm, nullptr); | ||
unsetenv("XPU_PADDLE_DISABLE_ALLOC_OVERLOAD"); | ||
unsetenv("XPUAPI_DEFAULT_SIZE"); | ||
} | ||
|
||
TEST(XPUOverloadAllocTest, BasicTest) { | ||
phi::XPUContext dev_ctx( | ||
phi::XPUPlace(phi::backends::xpu::GetXPUCurrentDeviceId())); | ||
int numel = 64; | ||
int alignment = phi::backends::xpu::XPUMinChunkSize(); | ||
int expected_alloc_size = | ||
allocation::AlignedSize(numel * sizeof(int), alignment); | ||
xpu::ctx_guard RAII_GUARD(dev_ctx.x_context()); | ||
int pre_alloc_value = DEVICE_MEMORY_STAT_CURRENT_VALUE( | ||
Allocated, dev_ctx.GetPlace().GetDeviceId()); | ||
int* buffer = RAII_GUARD.alloc<int>(numel); | ||
int after_alloc_value = DEVICE_MEMORY_STAT_CURRENT_VALUE( | ||
Allocated, dev_ctx.GetPlace().GetDeviceId()); | ||
EXPECT_NE(buffer, nullptr); | ||
EXPECT_EQ(after_alloc_value - pre_alloc_value, expected_alloc_size); | ||
} | ||
|
||
TEST(XPUOverloadAllocTest, NestedScopeTest) { | ||
phi::XPUContext dev_ctx( | ||
phi::XPUPlace(phi::backends::xpu::GetXPUCurrentDeviceId())); | ||
xpu::ctx_guard RAII_GUARD1(dev_ctx.x_context()); | ||
int pre_alloc_value = DEVICE_MEMORY_STAT_CURRENT_VALUE( | ||
Allocated, dev_ctx.GetPlace().GetDeviceId()); | ||
int* buffer_outter = RAII_GUARD1.alloc<int>(64); | ||
EXPECT_NE(buffer_outter, nullptr); | ||
{ | ||
// The destruction of inner guard should not free the memory allocated from | ||
// outter guard. | ||
xpu::ctx_guard RAII_GUARD2(dev_ctx.x_context()); | ||
int* buffer_inner = RAII_GUARD2.alloc<int>(64); | ||
EXPECT_NE(buffer_inner, nullptr); | ||
} | ||
int post_alloc_value = DEVICE_MEMORY_STAT_CURRENT_VALUE( | ||
Allocated, dev_ctx.GetPlace().GetDeviceId()); | ||
EXPECT_NE(post_alloc_value, pre_alloc_value); | ||
} | ||
|
||
TEST(XPUOverloadAllocTest, MultiStreamTest) { | ||
// Test whether stream 1 use the memory poll of stream 0. | ||
int size = 64; | ||
setenv("XPU_CDNN_CLUSTER_PARALLEL", "1", 1); | ||
phi::XPUContext dev_ctx( | ||
phi::XPUPlace(phi::backends::xpu::GetXPUCurrentDeviceId())); | ||
xpu::ctx_guard RAII_GUARD0(dev_ctx.x_context(0)); | ||
xpu::ctx_guard RAII_GUARD1(dev_ctx.x_context(1)); | ||
int pre_alloc_value = DEVICE_MEMORY_STAT_CURRENT_VALUE( | ||
Allocated, dev_ctx.GetPlace().GetDeviceId()); | ||
int* buffer0 = RAII_GUARD1.alloc<int>(size); | ||
EXPECT_NE(buffer0, nullptr); | ||
{ | ||
int* buffer1 = RAII_GUARD0.alloc<int>(size); | ||
EXPECT_NE(buffer1, nullptr); | ||
} | ||
int post_alloc_value = DEVICE_MEMORY_STAT_CURRENT_VALUE( | ||
Allocated, dev_ctx.GetPlace().GetDeviceId()); | ||
|
||
EXPECT_NE(pre_alloc_value, post_alloc_value); | ||
unsetenv("XPU_CDNN_CLUSTER_PARALLEL"); | ||
} | ||
} // namespace memory | ||
} // namespace paddle |
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.
这里是不是还会有那个BufferMgr align to 64的warning
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.