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

[XPU] Add fast_layernorm + leaky_relu fusion #57113

Merged
merged 1 commit into from
Sep 19, 2023

Conversation

frank-oops
Copy link
Contributor

PR types

Performance optimization

PR changes

OPs

Description

add fast_layternorm and act(leaky_relu) fusion

@paddle-bot
Copy link

paddle-bot bot commented Sep 8, 2023

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@paddle-bot paddle-bot bot added the contributor External developers label Sep 8, 2023
@@ -291,6 +291,7 @@ if(WITH_XPU)
${XPU_PASS_DEPS})
pass_library(gather_squeeze_pass inference DIR xpu DEPS ${XPU_PASS_DEPS})
pass_library(fast_where_xpu_fuse_pass inference DIR xpu DEPS ${XPU_PASS_DEPS})
pass_library(fln_act_xpu_fuse_pass inference DIR xpu DEPS ${XPU_PASS_DEPS})
Copy link
Contributor

Choose a reason for hiding this comment

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

layer_norm_act_xpu_fuse_pass

@@ -0,0 +1,212 @@
// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
Copy link
Contributor

Choose a reason for hiding this comment

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

layer_norm_act_fuse_pass.cc

After the pass is applied:
x
|
fln_act_xpu
Copy link
Contributor

Choose a reason for hiding this comment

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

fln 用 layer_norm 替换

graph:
x
|
layernorm_xpu
Copy link
Contributor

Choose a reason for hiding this comment

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

layer_norm

const std::string& name_scope,
const std::string& act_type);
// declare operator node's name
PATTERN_DECL_NODE(ln);
Copy link
Contributor

Choose a reason for hiding this comment

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

ln 改成 layer_norm,下同

output
*/

struct FLNActXPUPattern : public PatternBase {
Copy link
Contributor

Choose a reason for hiding this comment

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

FLN改成 LayerNorm

@@ -101,6 +101,15 @@
data_type : x
optional : bias, x_max

- op : fln_act_xpu
Copy link
Contributor

Choose a reason for hiding this comment

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

layer_norm_act_xpu

@hong19860320 hong19860320 requested a review from wz1qqx September 13, 2023 01:59
->assert_is_op_input("layer_norm", "Scale");
auto ln_out = pattern->NewNode(ln_out_repr())
->AsOutput()
->assert_is_op_output("layer_norm", "Y");
Copy link
Contributor

Choose a reason for hiding this comment

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

->assert_is_op_input(act_type_, "X"); 加到这里来
另外,需要判断 ln_out 只有一个 output

@@ -1014,6 +1014,20 @@ void AddCMulXPUInferMeta(const MetaTensor& x,
out->set_layout(x.layout());
}

void FLNActXPUInferMeta(const MetaTensor& x,
Copy link
Contributor

Choose a reason for hiding this comment

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

LayerNormActInferMeta

@@ -226,6 +226,15 @@ void AddCMulXPUInferMeta(const MetaTensor& x,
const MetaTensor& w,
MetaTensor* out);

void FLNActXPUInferMeta(const MetaTensor& x,
Copy link
Contributor

Choose a reason for hiding this comment

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

同上

namespace fusion {

template <typename T, typename Context>
void FLNActXPUKernel(const Context& ctx,
Copy link
Contributor

Choose a reason for hiding this comment

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

同上

act.hard_sigmoid_slope = act_param;
}
#ifdef PADDLE_WITH_XPU_PLUGIN
int r = xpu::plugin::fln_act_fusion(ctx.x_context(),
Copy link
Contributor

Choose a reason for hiding this comment

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

fln 是否能统一调整为 layer_norm_act_fusion

args : (Tensor x, Tensor scale, Tensor bias, int begin_norm_axis, float epsilon, int act_type, float act_param)
output : Tensor(out)
infer_meta :
func : LayerNormActInferMeta
Copy link
Contributor

Choose a reason for hiding this comment

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

LayerNormActInferMeta 改成 LayerNormActXPUInferMeta
函数名称和算子名称保持一致,下同

namespace patterns {

/*
change layernorm op to fast_layernorm op
Copy link
Contributor

Choose a reason for hiding this comment

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

描述改一下,第一个字母大写

@@ -70,6 +70,8 @@ XPUOpMap& get_kl2_ops() {
XPUKernelSet({phi::DataType::FLOAT32, phi::DataType::FLOAT16})},
{"batch_norm",
XPUKernelSet({phi::DataType::FLOAT32, phi::DataType::FLOAT16})},
{"layer_norm_act_xpu",
Copy link
Contributor

Choose a reason for hiding this comment

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

按照字母顺序?或者 fuse 类的算子放一起?

@frank-oops frank-oops force-pushed the fln_act-1 branch 2 times, most recently from 2305a00 to cd13575 Compare September 18, 2023 06:18
@hong19860320 hong19860320 changed the title add fast_layternorm and act(leaky_relu) fusion [XPU] Add fast_layternorm + leaky_relu fusion Sep 19, 2023
Copy link
Contributor

@hong19860320 hong19860320 left a comment

Choose a reason for hiding this comment

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

LGTM

@hong19860320 hong19860320 merged commit ad32cca into PaddlePaddle:develop Sep 19, 2023
@hong19860320 hong19860320 changed the title [XPU] Add fast_layternorm + leaky_relu fusion [XPU] Add fast_layernorm + leaky_relu fusion Sep 19, 2023
Frida-a pushed a commit to Frida-a/Paddle that referenced this pull request Oct 14, 2023
danleifeng pushed a commit to danleifeng/Paddle that referenced this pull request Nov 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor External developers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants