Skip to content
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] avoid malloc redundant memory when creating context in comm man… #64139

Merged
merged 1 commit into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions paddle/phi/backends/xpu/xpu_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,13 @@ XPUContext::XPUContext() : DeviceContext() {
}
}

XPUContext::XPUContext(const XPUPlace& place) : DeviceContext() {
if (std::getenv("XPU_CDNN_CLUSTER_PARALLEL") != nullptr) {
XPUContext::XPUContext(const XPUPlace& place, bool is_comm_context)
: DeviceContext() {
if (is_comm_context) {
// for communication context init, with gm_size=1 and l3_size=1
impls_.push_back(std::make_unique<Impl>(place));
impls_[0]->Init(1, 1);
} else if (std::getenv("XPU_CDNN_CLUSTER_PARALLEL") != nullptr) {
int default_num_stream = 4;
if (std::getenv("XPU_CDNN_CLUSTER_PARALLEL_STREAM_NUMBER") != nullptr) {
default_num_stream =
Expand Down
3 changes: 2 additions & 1 deletion paddle/phi/backends/xpu/xpu_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ class XPUContext : public DeviceContext,
public:
XPUContext();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

默认构造函数是不是也一起加上这个选项


explicit XPUContext(const XPUPlace&);
// is_comm_context = 1 for init comm context with gm_size=1 and l3_size=1
explicit XPUContext(const XPUPlace&, bool is_comm_context = 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里为啥默认值不直接置为false而要用int类型0呢?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

编译的时候应该会被优化掉


virtual ~XPUContext();

Expand Down
5 changes: 3 additions & 2 deletions paddle/phi/core/distributed/comm_context_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,9 @@ void CommContextManager::CreateBKCLCommContext(
std::make_unique<BKCLCommContext>(rank, size, bkcl_id);

if (CommContextManager::device_id != -1) {
std::unique_ptr<phi::XPUContext> dev_ctx(
new phi::XPUContext(phi::XPUPlace(CommContextManager::device_id)));
bool is_comm_context = 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上,另外感觉这个变量只是作为参数传进构造函数好像没啥必要

Copy link
Contributor Author

@cqulilujia cqulilujia May 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里可以不创建变量,我改掉

std::unique_ptr<phi::XPUContext> dev_ctx(new phi::XPUContext(
phi::XPUPlace(CommContextManager::device_id), is_comm_context));
dev_ctx->SetAllocator(phi::memory_utils::GetAllocator(
CommContextManager::device_id, dev_ctx->stream()));
dev_ctx->SetHostAllocator(phi::memory_utils::GetHostAllocator());
Expand Down