Skip to content

[Bug Fix] Fix NLP-Bert model performance loss #50333

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
merged 2 commits into from
Feb 10, 2023
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
31 changes: 27 additions & 4 deletions paddle/fluid/framework/op_kernel_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ limitations under the License. */
#include "paddle/fluid/framework/data_type.h"
#include "paddle/fluid/framework/library_type.h"
#include "paddle/fluid/platform/place.h"
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/core/device_context.h"
#include "paddle/phi/core/enforce.h"
#include "paddle/phi/core/kernel_factory.h"

namespace paddle {
Expand Down Expand Up @@ -131,10 +133,31 @@ inline bool backends_are_same_class(const phi::Backend& l,
return phi::TransToPhiPlace(l) == phi::TransToPhiPlace(r);
}

inline bool NeedTransform(const phi::KernelKey& l, const phi::KernelKey& r) {
return !backends_are_same_class(l.backend(), r.backend()) ||
NeedTransformDataType(l, r) ||
NeedTransformLayout(l.layout(), r.layout());
inline bool NeedTransformBackend(const phi::Backend& type_for_var_backend,
const phi::Backend& expected_backend,
const phi::DenseTensor& tensor) {
// NOTE(jiahongyu): KernelKey does not hold place information, so we need to
// explicitly transform CUDAPinnedPlace->CUDAPlace
if (type_for_var_backend != phi::Backend::ALL_BACKEND &&
paddle::platform::is_cuda_pinned_place(tensor.place()) &&
expected_backend != phi::Backend::CPU) {
Copy link
Contributor

Choose a reason for hiding this comment

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

这里是不是变成expected_backend == phi::Backend::GPU更明确一点

Copy link
Contributor Author

Choose a reason for hiding this comment

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

此处参考静态图的实现,非 CPU 的 backend 都是需要 transform 的

Ref. data_transfer.h (line 86-89), All backends need transform except CPU.

VLOG(3) << "Transform Variable " << tensor.name() << " from "
<< tensor.place() << " to "
<< phi::TransToPhiPlace(expected_backend);
return true;
}
return !backends_are_same_class(type_for_var_backend, expected_backend);
}

inline bool NeedTransform(const phi::KernelKey& kernel_type_for_var,
const phi::KernelKey& expected_kernel_key,
const phi::DenseTensor& tensor) {
return NeedTransformBackend(kernel_type_for_var.backend(),
expected_kernel_key.backend(),
tensor) ||
NeedTransformDataType(kernel_type_for_var, expected_kernel_key) ||
NeedTransformLayout(kernel_type_for_var.layout(),
expected_kernel_key.layout());
}

} // namespace framework
Expand Down
4 changes: 2 additions & 2 deletions paddle/fluid/imperative/prepared_operator.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ std::shared_ptr<NameVarMap<VarType>> PrepareData(
if (tensor && tensor->IsInitialized() && (tensor->memory_size() != 0)) {
auto kernel_type_for_var = op.GetKernelTypeForVar(
name_pair.first, *tensor, expected_kernel_key);
if (!framework::NeedTransform(kernel_type_for_var,
expected_kernel_key)) {
if (!framework::NeedTransform(
kernel_type_for_var, expected_kernel_key, *tensor)) {
continue;
} else {
VLOG(3) << "Transform Variable " << GetNameFromVar(template_var)
Expand Down