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

fix RunWithExternalStream contex switch bug #57629

Merged
merged 3 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions paddle/fluid/inference/api/analysis_predictor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
#include "paddle/fluid/platform/device_context.h"
#include "paddle/fluid/platform/place.h"
#include "paddle/fluid/platform/profiler.h"
#include "paddle/phi/api/include/context_pool.h"
#include "paddle/phi/api/include/tensor.h"
#include "paddle/phi/common/backend.h"
#include "paddle/phi/common/data_type.h"
Expand Down Expand Up @@ -2219,6 +2220,8 @@ bool AnalysisPredictor::ExpRunWithExternalStream(const gpuStream_t stream) {
UpdatePrivateDeviceContext(gpu_context, gpu_resource, place_);
return std::unique_ptr<phi::DeviceContext>(gpu_context);
}));
auto &pool = paddle::experimental::DeviceContextPool::Instance();
pool.Update(place_);
}

return ZeroCopyRun();
Expand Down
2 changes: 2 additions & 0 deletions paddle/phi/api/include/context_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class PADDLE_API DeviceContextPool {

phi::DeviceContext* GetMutable(const Place& place);

void Update(const Place& place);
Copy link
Contributor

Choose a reason for hiding this comment

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

如果确实需要增加的话,建议再斟酌一下接口名,仅看代码Update不太清楚是更新了什么,可以具体一点,我理解这里应该是底层的DeviceContext更新了,但是没有同步到API层对吧,所以是不是SyncDeviceContextPointer之类的更准确,仅供参考,根据实际语义命名即可

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已修改


template <AllocationType T>
const typename DefaultDeviceContextType<T>::TYPE* Get(const Place& place) {
return reinterpret_cast<const typename DefaultDeviceContextType<T>::TYPE*>(
Expand Down
12 changes: 12 additions & 0 deletions paddle/phi/api/lib/context_pool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ limitations under the License. */
namespace paddle {
namespace experimental {

void DeviceContextPool::Update(const Place& place) {
if (!phi::DeviceContextPool::IsInitialized()) {
phi::memory_utils::InitDevices();
}
// only when we need the specific DeviceContext, get and cache it
auto* dev_ctx = phi::DeviceContextPool::Instance().Get(place);
{
std::lock_guard<std::mutex> lock(mutex_);
context_map_[place] = dev_ctx;
}
}

DeviceContextPool& DeviceContextPool::Instance() {
static DeviceContextPool g_device_context_pool;
return g_device_context_pool;
Expand Down