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

[inference] Add FusedBiasActKernel #55301

Merged
merged 21 commits into from
Jul 13, 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
11 changes: 11 additions & 0 deletions paddle/phi/api/yaml/fused_ops.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@
data_type : x
optional : bias, x_max

- op : fused_bias_act
args : (Tensor x, Tensor bias, Tensor dequant_scales, Tensor shift, Tensor smooth, str act_method = "gelu", str compute_dtype = "default", int rows = -1, int cols = -1, float quant_scale = -1, int quant_round_type = 1, float quant_max_bound = 127.0, float quant_min_bound = -127.0)
output : Tensor(out)
infer_meta :
func: FusedBiasActInferMeta
kernel :
func : fused_bias_act
data_type : x
optional : bias, dequant_scales, shift, smooth
support_dygraph_mode : true

- op : fused_dropout_add
args : (Tensor x, Tensor y, Tensor seed_tensor, Scalar p, bool is_test, str mode, int seed = 0, bool fix_seed = false)
optional : seed_tensor
Expand Down
130 changes: 130 additions & 0 deletions paddle/phi/infermeta/multiary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1335,6 +1335,136 @@ void EditDistanceInferMeta(const MetaTensor& hyps,
sequencenum->set_dtype(DataType::FLOAT32);
}

void FusedBiasActInferMeta(const MetaTensor& x,
const MetaTensor& bias,
const MetaTensor& dequant_scales,
const MetaTensor& shift,
const MetaTensor& smooth,
const std::string& act_method,
const std::string& compute_dtype,
int rows,
int cols,
float quant_scale,
int quant_round_type,
float quant_max_bound,
float quant_min_bound,
MetaTensor* out) {
auto x_dims = x.dims();
PADDLE_ENFORCE_EQ(x_dims.size(),
2,
phi::errors::InvalidArgument(
"The size of Input(x) must be 2: %s", x_dims));
auto token_num = x_dims[0];
auto dim = x_dims[1];

PADDLE_ENFORCE_GT(
rows, 0, phi::errors::InvalidArgument("The size of Attr(rows) must > 0"));

PADDLE_ENFORCE_GT(
cols, 0, phi::errors::InvalidArgument("The size of Attr(cols) must > 0"));

if (act_method == "geglu" || act_method == "swiglu") {
PADDLE_ENFORCE_EQ(
dim % 2,
0,
phi::errors::InvalidArgument(
"The seconde dimension of x must be even, but receive %d", dim));
dim /= 2;
out->set_dims(phi::make_ddim({token_num, dim}));
} else if (act_method == "gelu") {
out->set_dims(phi::make_ddim({token_num, dim}));
} else {
PADDLE_THROW(
errors::InvalidArgument("act_method must be geglu, swiglu or gelu, "
"but get act_method (%s)",
act_method));
}

auto FBADtypeCheck = [](const MetaTensor& check_tensor,
const std::string& tensor_name,
const std::string& compute_dtype) {
if (compute_dtype == "bf16") {
PADDLE_ENFORCE_EQ(
check_tensor.dtype(),
phi::DataType::BFLOAT16,
phi::errors::InvalidArgument(
"Input(%s) dtype must be the same with Attr(compute_dtype)",
tensor_name));
} else if (compute_dtype == "fp16") {
PADDLE_ENFORCE_EQ(
check_tensor.dtype(),
phi::DataType::FLOAT16,
phi::errors::InvalidArgument(
"Input(%s) dtype must be the same with Attr(compute_dtype)",
tensor_name));
} else if (compute_dtype == "fp32") {
PADDLE_ENFORCE_EQ(
check_tensor.dtype(),
phi::DataType::FLOAT32,
phi::errors::InvalidArgument(
"Input(%s) dtype must be the same with Attr(compute_dtype)",
tensor_name));
}
};

// In the case of quantization enabled, the dtype for computation is
// determined based on compute_dtype.
if (x.dtype() == phi::DataType::INT32) {
PADDLE_ENFORCE_NE(
compute_dtype,
"default",
phi::errors::InvalidArgument(
"If Input(x) dtype is INT32, Attr(compute_dtype) must be set."));

if (bias) {
FBADtypeCheck(bias, "bias", compute_dtype);
}

if (quant_scale > 0) {
out->set_dtype(phi::DataType::INT8);
} else {
if (compute_dtype == "bf16") {
out->set_dtype(phi::DataType::BFLOAT16);
} else if (compute_dtype == "fp16") {
out->set_dtype(phi::DataType::FLOAT16);
} else if (compute_dtype == "fp32") {
out->set_dtype(phi::DataType::FLOAT32);
} else {
PADDLE_THROW(phi::errors::InvalidArgument(
"In the case of quantization enabled with Input(x) INT32, "
"Attr(compute_dtype) must be set in (bf16, fp16, fp32), "
"but get compute_dtype (%s)",
compute_dtype));
}
}
} else {
// x.dtype() != phi::DataType::INT32
if (bias) {
if (compute_dtype != "default") {
FBADtypeCheck(bias, "bias", compute_dtype);
FBADtypeCheck(x, "x", compute_dtype);
} else {
PADDLE_ENFORCE_EQ(
x.dtype(),
bias.dtype(),
phi::errors::InvalidArgument("Input(x) and Input(bias) must be the "
"same dtype in this situation"));
}
} else {
// bias not exist
if (compute_dtype != "default") {
FBADtypeCheck(x, "x", compute_dtype);
}
}
if (quant_scale > 0) {
out->set_dtype(phi::DataType::INT8);
} else {
out->set_dtype(x.dtype());
}
}
out->set_layout(x.layout());
}

void FusedLinearParamGradAddInferMeta(const MetaTensor& x,
const MetaTensor& dout,
const MetaTensor& dweight,
Expand Down
15 changes: 15 additions & 0 deletions paddle/phi/infermeta/multiary.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,21 @@ void EditDistanceInferMeta(const MetaTensor& hyps,
MetaTensor* sequencenum,
MetaTensor* out);

void FusedBiasActInferMeta(const MetaTensor& x,
const MetaTensor& bias,
const MetaTensor& dequant_scales,
const MetaTensor& shift,
const MetaTensor& smooth,
const std::string& act_method,
const std::string& compute_dtype,
int rows,
int cols,
float quant_scale,
int quant_round_type,
float quant_max_bound,
float quant_min_bound,
MetaTensor* out);

void FusedLinearParamGradAddInferMeta(const MetaTensor& x,
const MetaTensor& dout,
const MetaTensor& dweight,
Expand Down
2 changes: 1 addition & 1 deletion paddle/phi/kernels/funcs/activation_functor.h
Original file line number Diff line number Diff line change
Expand Up @@ -3923,7 +3923,7 @@ template <typename T>
struct CudaSwishFunctor : public BaseActivationFunctor<T> {
using MPType = typename phi::dtype::MPTypeTrait<T>::Type;
MPType one = static_cast<MPType>(1.0f);
float beta;
float beta = 1.0;

typename BaseActivationFunctor<T>::AttrPair GetAttrs() {
return {{"beta", &beta}};
Expand Down
Loading