Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
32e36b15
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
32e36b15
编写于
5月 16, 2023
作者:
S
sprouteer
提交者:
GitHub
5月 16, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[XPU] Add sigmoid_elementmul_xpu_fuse_pass (#53580)
上级
c2c3bd43
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
262 addition
and
0 deletion
+262
-0
paddle/fluid/framework/ir/CMakeLists.txt
paddle/fluid/framework/ir/CMakeLists.txt
+1
-0
paddle/fluid/framework/ir/sigmoid_elementmul_fuse_pass.cc
paddle/fluid/framework/ir/sigmoid_elementmul_fuse_pass.cc
+127
-0
paddle/fluid/framework/ir/sigmoid_elementmul_fuse_pass.h
paddle/fluid/framework/ir/sigmoid_elementmul_fuse_pass.h
+62
-0
paddle/fluid/inference/api/paddle_pass_builder.cc
paddle/fluid/inference/api/paddle_pass_builder.cc
+1
-0
test/ir/inference/test_xpu_sigmoid_elementmul_fuse_pass.py
test/ir/inference/test_xpu_sigmoid_elementmul_fuse_pass.py
+71
-0
未找到文件。
paddle/fluid/framework/ir/CMakeLists.txt
浏览文件 @
32e36b15
...
...
@@ -127,6 +127,7 @@ pass_library(dense_multihead_matmul_to_sparse_pass inference)
pass_library
(
delete_cast_op_pass inference
)
pass_library
(
delete_elementwise_mul_op_pass inference
)
pass_library
(
delete_repeated_ops_pass inference
)
pass_library
(
sigmoid_elementmul_fuse_pass inference
)
pass_library
(
generate_pass DEPS pass_desc_proto
)
target_link_libraries
(
generate_pass pass_desc_proto
)
...
...
paddle/fluid/framework/ir/sigmoid_elementmul_fuse_pass.cc
0 → 100644
浏览文件 @
32e36b15
// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <string>
#include "glog/logging.h"
#include "paddle/fluid/framework/ir/fuse_pass_base.h"
#include "paddle/fluid/framework/ir/graph_pattern_detector.h"
#include "paddle/fluid/framework/ir/pass.h"
#include "paddle/fluid/framework/ir/sigmoid_elementmul_fuse_pass.h"
#include "paddle/fluid/framework/op_version_registry.h"
#include "paddle/fluid/platform/enforce.h"
namespace
paddle
{
namespace
framework
{
namespace
ir
{
namespace
patterns
{
struct
SigmoidElementmulFusePattern
:
public
PatternBase
{
SigmoidElementmulFusePattern
(
PDPattern
*
pattern
,
const
std
::
string
&
name_scope
);
// declare operator node's name
PATTERN_DECL_NODE
(
sigmoid
);
PATTERN_DECL_NODE
(
elementwise_mul
);
// declare variable node's name
PATTERN_DECL_NODE
(
sigmoid_x
);
PATTERN_DECL_NODE
(
sigmoid_out
);
PATTERN_DECL_NODE
(
elemul_out
);
};
SigmoidElementmulFusePattern
::
SigmoidElementmulFusePattern
(
PDPattern
*
pattern
,
const
std
::
string
&
name_scope
)
:
PatternBase
(
pattern
,
name_scope
,
name_scope
)
{
auto
*
sigmoid_x
=
pattern
->
NewNode
(
sigmoid_x_repr
())
->
assert_is_op_input
(
"sigmoid"
,
"X"
)
->
assert_var_not_persistable
();
auto
*
sigmoid_op
=
pattern
->
NewNode
(
sigmoid_repr
())
->
assert_is_op
(
"sigmoid"
);
auto
*
sigmoid_out
=
pattern
->
NewNode
(
sigmoid_out_repr
())
->
assert_is_op_output
(
"sigmoid"
,
"Out"
)
->
assert_var_not_persistable
();
auto
*
elemul_op
=
pattern
->
NewNode
(
elementwise_mul_repr
())
->
assert_is_op
(
"elementwise_mul"
);
auto
*
elemul_out
=
pattern
->
NewNode
(
elemul_out_repr
())
->
assert_is_op_output
(
"elementwise_mul"
,
"Out"
)
->
assert_var_not_persistable
();
sigmoid_op
->
LinksFrom
({
sigmoid_x
}).
LinksTo
({
sigmoid_out
});
elemul_op
->
LinksFrom
({
sigmoid_x
,
sigmoid_out
}).
LinksTo
({
elemul_out
});
}
}
// namespace patterns
SigmoidElementmulFusePass
::
SigmoidElementmulFusePass
()
{}
void
SigmoidElementmulFusePass
::
ApplyImpl
(
ir
::
Graph
*
graph
)
const
{
PADDLE_ENFORCE_NOT_NULL
(
graph
,
platform
::
errors
::
PreconditionNotMet
(
"graph should not be null."
));
Init
(
name_scope_
,
graph
);
GraphPatternDetector
gpd
;
patterns
::
SigmoidElementmulFusePattern
pattern
(
gpd
.
mutable_pattern
(),
name_scope_
);
int
found_subgraph_count
=
0
;
auto
handler
=
[
&
](
const
GraphPatternDetector
::
subgraph_t
&
subgraph
,
Graph
*
graph
)
{
VLOG
(
4
)
<<
"handle SigmoidElementmulFusePass fuse"
;
#define GET_IR_NODE(node_) GET_IR_NODE_FROM_SUBGRAPH(node_, node_, pattern)
GET_IR_NODE
(
sigmoid_x
);
GET_IR_NODE
(
sigmoid
);
GET_IR_NODE
(
sigmoid_out
);
GET_IR_NODE
(
elementwise_mul
);
GET_IR_NODE
(
elemul_out
);
#undef GET_IR_NODE
auto
*
block
=
sigmoid
->
Op
()
->
Block
();
std
::
string
elemul_out_name
=
elemul_out
->
Name
();
// Generate swish op
framework
::
OpDesc
swish_op_desc
(
block
);
swish_op_desc
.
SetType
(
"swish"
);
swish_op_desc
.
SetInput
(
"X"
,
{
sigmoid_x
->
Name
()});
swish_op_desc
.
SetAttr
(
"beta"
,
1.
f
);
swish_op_desc
.
SetOutput
(
"Out"
,
{
elemul_out_name
});
auto
*
swish
=
graph
->
CreateOpNode
(
&
swish_op_desc
);
IR_NODE_LINK_TO
(
sigmoid_x
,
swish
);
IR_NODE_LINK_TO
(
swish
,
elemul_out
);
// delete useless node
std
::
unordered_set
<
const
Node
*>
delete_nodes
;
delete_nodes
=
{
sigmoid
,
sigmoid_out
,
elementwise_mul
};
GraphSafeRemoveNodes
(
graph
,
delete_nodes
);
found_subgraph_count
++
;
};
gpd
(
graph
,
handler
);
AddStatis
(
found_subgraph_count
);
}
}
// namespace ir
}
// namespace framework
}
// namespace paddle
REGISTER_PASS
(
sigmoid_elementmul_fuse_pass
,
paddle
::
framework
::
ir
::
SigmoidElementmulFusePass
);
REGISTER_PASS_CAPABILITY
(
sigmoid_elementmul_fuse_pass
)
.
AddCombination
(
paddle
::
framework
::
compatible
::
OpVersionComparatorCombination
().
EQ
(
"swish"
,
0
));
paddle/fluid/framework/ir/sigmoid_elementmul_fuse_pass.h
0 → 100644
浏览文件 @
32e36b15
/* Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#pragma once
#include "paddle/fluid/framework/ir/fuse_pass_base.h"
namespace
paddle
{
namespace
framework
{
namespace
ir
{
class
Graph
;
/*
1. fuse sigmoid + elementwise_mul into swish
Origin subgraph:
input
/ \
| |
| sigmoid
| |
| |
elementwise_mul
|
|
out
Fused subgraph:
input
|
|
swish
|
|
out
*/
class
SigmoidElementmulFusePass
:
public
FusePassBase
{
public:
SigmoidElementmulFusePass
();
virtual
~
SigmoidElementmulFusePass
()
{}
protected:
void
ApplyImpl
(
ir
::
Graph
*
graph
)
const
override
;
private:
const
std
::
string
name_scope_
{
"sigmoid_elementmul_fuse_pass"
};
};
}
// namespace ir
}
// namespace framework
}
// namespace paddle
paddle/fluid/inference/api/paddle_pass_builder.cc
浏览文件 @
32e36b15
...
...
@@ -522,6 +522,7 @@ XpuPassStrategy::XpuPassStrategy() : PassStrategy({}) {
"delete_cast_op_pass"
,
"stack_fuse_pass"
,
"fused_multi_transformer_xpu_pass"
,
"sigmoid_elementmul_fuse_pass"
,
"fc_xpu_fuse_pass"
,
"conv2d_xpu_fuse_pass"
,
"link_xpu_op_max_pass"
,
...
...
test/ir/inference/test_xpu_sigmoid_elementmul_fuse_pass.py
0 → 100644
浏览文件 @
32e36b15
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
unittest
import
hypothesis.strategies
as
st
import
numpy
as
np
from
auto_scan_test
import
PassAutoScanTest
from
program_config
import
OpConfig
,
ProgramConfig
,
TensorConfig
class
TestSigmoidElementmulFusePass
(
PassAutoScanTest
):
def
sample_predictor_configs
(
self
,
program_config
):
config
=
self
.
create_inference_config
(
use_xpu
=
True
)
yield
config
,
[
"swish"
],
(
1e-3
,
1e-3
)
def
sample_program_config
(
self
,
draw
):
# 1. sigmoid
sigmoid_x_shape
=
draw
(
st
.
lists
(
st
.
integers
(
min_value
=
1
,
max_value
=
4
),
min_size
=
2
,
max_size
=
4
)
)
sigmoid_op
=
OpConfig
(
"sigmoid"
,
inputs
=
{
"X"
:
[
"sigmoid_x"
]},
outputs
=
{
"Out"
:
[
"sigmoid_out"
]},
trans_x
=
False
,
trans_y
=
False
,
)
mul_op
=
OpConfig
(
"elementwise_mul"
,
inputs
=
{
"X"
:
[
"sigmoid_x"
],
"Y"
:
[
"sigmoid_out"
]},
outputs
=
{
"Out"
:
[
"out"
]},
axis
=-
1
,
)
ops
=
[
sigmoid_op
,
mul_op
]
program_config
=
ProgramConfig
(
ops
=
ops
,
weights
=
{},
inputs
=
{
"sigmoid_x"
:
TensorConfig
(
shape
=
sigmoid_x_shape
),
},
outputs
=
ops
[
-
1
].
outputs
[
"Out"
],
)
return
program_config
def
test
(
self
):
self
.
run_and_statis
(
quant
=
False
,
max_examples
=
25
,
passes
=
[
"sigmoid_elementmul_fuse_pass"
],
)
if
__name__
==
"__main__"
:
np
.
random
.
seed
(
200
)
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录