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

refine stride flag #57005

Merged
Merged
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
36 changes: 28 additions & 8 deletions paddle/phi/core/kernel_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

#include "paddle/phi/core/kernel_factory.h"

#include <regex>
#include <string>
#include <unordered_set>

#include "glog/logging.h"
#include "paddle/phi/core/enforce.h"
#include "paddle/utils/flags.h"
Expand All @@ -33,6 +37,10 @@ PHI_DEFINE_EXPORTED_bool(use_stride_kernel,
true,
"Whether to use strdie kernel if op support stride.");

PHI_DEFINE_EXPORTED_string(stride_kernel_blacklist,
Copy link
Contributor

Choose a reason for hiding this comment

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

flags 加到 phi/flags.cc 是不是好一些?

"",
"It controls the strided kernel subset do not use.");

PD_DECLARE_int32(low_precision_op_list);
PD_DECLARE_bool(enable_api_kernel_fallback);
PD_DECLARE_bool(run_kp_kernel);
Expand Down Expand Up @@ -226,14 +234,26 @@ KernelResult KernelFactory::SelectKernelOrThrowError(
phi::errors::NotFound("The kernel `%s` is not registered.", kernel_name));

if (FLAGS_use_stride_kernel && use_strided_kernel) {
auto stride_kernel_iter = iter->second.find(
{const_kernel_key.backend() == paddle::experimental::Backend::GPUDNN
? paddle::experimental::Backend::GPU
: const_kernel_key.backend(),
phi::DataLayout::STRIDED,
const_kernel_key.dtype()});
if (stride_kernel_iter != iter->second.end()) {
return {stride_kernel_iter->second, false, true};
std::regex reg(",");
std::unordered_set<std::string> elems{
std::sregex_token_iterator(FLAGS_stride_kernel_blacklist.begin(),
FLAGS_stride_kernel_blacklist.end(),
reg,
-1),
std::sregex_token_iterator()};
elems.erase("");

if (!elems.count(kernel_name)) {
auto stride_kernel_iter = iter->second.find(
{const_kernel_key.backend() == paddle::experimental::Backend::GPUDNN
? paddle::experimental::Backend::GPU
: const_kernel_key.backend(),
phi::DataLayout::STRIDED,
const_kernel_key.dtype()});
if (stride_kernel_iter != iter->second.end()) {
VLOG(1) << "use strided kernel, kernel_name = " << kernel_name;
return {stride_kernel_iter->second, false, true};
}
}
}

Expand Down