Skip to content

Commit

Permalink
support auto fallback to cpu kernel for cusom device (#44639)
Browse files Browse the repository at this point in the history
  • Loading branch information
zyfncg authored Jul 27, 2022
1 parent 42d58dd commit 15c0c9d
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions paddle/phi/core/kernel_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ const Kernel& KernelFactory::SelectKernelOrThrowError(
<< "] is not registered.";
}
#endif

auto kernel_iter = iter->second.find(kernel_key);
// TODO(chenweihang): polish refind impl here
if (kernel_iter == iter->second.end() &&
Expand All @@ -118,6 +119,22 @@ const Kernel& KernelFactory::SelectKernelOrThrowError(
kernel_key.backend(), phi::DataLayout::ALL_LAYOUT, kernel_key.dtype());
kernel_iter = iter->second.find(any_layout_kernel_key);
}

#ifdef PADDLE_WITH_CUSTOM_DEVICE
if (kernel_iter == iter->second.end()) {
// Fallback CPU backend
phi::KernelKey cpu_kernel_key(
phi::Backend::CPU, kernel_key.layout(), kernel_key.dtype());
kernel_iter = iter->second.find(cpu_kernel_key);
if (kernel_iter == iter->second.end() &&
kernel_key.layout() != phi::DataLayout::ALL_LAYOUT) {
phi::KernelKey any_layout_kernel_key(
phi::Backend::CPU, phi::DataLayout::ALL_LAYOUT, kernel_key.dtype());
kernel_iter = iter->second.find(any_layout_kernel_key);
}
}
#endif

PADDLE_ENFORCE_NE(
kernel_iter,
iter->second.end(),
Expand Down

0 comments on commit 15c0c9d

Please sign in to comment.