Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
aadc8674
P
Paddle
项目概览
Crayon鑫
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
aadc8674
编写于
12月 21, 2021
作者:
B
baoachun
提交者:
GitHub
12月 21, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update squared_mat_sub_fuse_pass ut (#37838)
* update squared_mat_sub_fuse_pass ut * update ut * update ut
上级
dc7597e3
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
149 addition
and
39 deletion
+149
-39
paddle/fluid/framework/ir/squared_mat_sub_fuse_pass.cc
paddle/fluid/framework/ir/squared_mat_sub_fuse_pass.cc
+5
-2
python/paddle/fluid/tests/unittests/ir/inference/test_squared_mat_sub_fuse_pass.py
.../unittests/ir/inference/test_squared_mat_sub_fuse_pass.py
+144
-37
未找到文件。
paddle/fluid/framework/ir/squared_mat_sub_fuse_pass.cc
浏览文件 @
aadc8674
...
...
@@ -398,8 +398,7 @@ SquaredMatSubFusePass::SquaredMatSubFusePass() {
.
IsTensor
()
.
End
()
.
AddAttr
(
"alpha"
)
.
IsNumGE
(
0.99
f
)
.
IsNumLE
(
1.01
f
)
.
IsNumEQ
(
1.0
f
)
.
End
()
.
AddAttr
(
"transpose_X"
)
.
IsBoolEQ
(
false
)
...
...
@@ -465,6 +464,10 @@ SquaredMatSubFusePass::SquaredMatSubFusePass() {
.
End
()
// type:float,there is no restriction
.
AddAttr
(
"value"
)
.
End
()
.
AddAttr
(
"str_value"
)
.
IsStringEQ
(
""
)
.
IsOptional
()
.
End
();
}
...
...
python/paddle/fluid/tests/unittests/ir/inference/test_squared_mat_sub_fuse_pass.py
浏览文件 @
aadc8674
# Copyright (c) 202
0
PaddlePaddle Authors. All Rights Reserved.
# Copyright (c) 202
1
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.
...
...
@@ -12,53 +12,160 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from
__future__
import
print_function
import
unittest
from
auto_scan_test
import
PassAutoScanTest
,
SkipReasons
from
program_config
import
TensorConfig
,
ProgramConfig
,
OpConfig
import
numpy
as
np
from
inference_pass_test
import
InferencePassTest
import
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid.core
as
core
from
paddle.fluid.core
import
AnalysisConfig
from
paddle.fluid.core
import
PassVersionChecker
import
paddle.inference
as
paddle_infer
from
functools
import
partial
from
typing
import
Optional
,
List
,
Callable
,
Dict
,
Any
,
Set
import
unittest
import
hypothesis
from
hypothesis
import
given
,
settings
,
seed
,
example
,
assume
import
hypothesis.strategies
as
st
class
TestSquaredMatSubFusePass
(
PassAutoScanTest
):
def
is_program_valid
(
self
,
program_config
:
ProgramConfig
)
->
bool
:
return
True
def
sample_program_config
(
self
,
draw
):
transpose_X
=
False
transpose_Y
=
False
alpha1
=
1.0
alpha2
=
1.0
axis1
=
draw
(
st
.
sampled_from
([
-
1
,
0
]))
place_type
=
draw
(
st
.
sampled_from
([
-
1
,
0
]))
has_str_value
=
draw
(
st
.
booleans
())
str_value
=
''
value
=
draw
(
st
.
floats
(
min_value
=-
10
,
max_value
=
10
))
shape
=
draw
(
st
.
sampled_from
([[
1
]]))
axis2
=
draw
(
st
.
sampled_from
([
-
1
,
0
]))
input_dim
=
draw
(
st
.
sampled_from
([
32
,
64
]))
def
generate_input
(
type
):
shape_x
=
[
32
,
input_dim
]
shape_y
=
[
input_dim
,
16
]
if
type
==
"x"
:
return
np
.
random
.
random
(
shape_x
).
astype
(
np
.
float32
)
else
:
return
np
.
random
.
random
(
shape_y
).
astype
(
np
.
float32
)
matmul_op1
=
OpConfig
(
type
=
"matmul"
,
inputs
=
{
"X"
:
[
"input_data1"
],
"Y"
:
[
"input_data2"
]},
outputs
=
{
"Out"
:
[
"matmul1_output"
]},
attrs
=
{
"transpose_X"
:
transpose_X
,
"transpose_Y"
:
transpose_Y
,
"alpha"
:
alpha1
,
"fused_reshape_X"
:
[],
"fused_reshape_Y"
:
[],
"fused_transpose_X"
:
[],
"fused_transpose_Y"
:
[],
"fused_reshape_Out"
:
[],
"fused_transpose_Out"
:
[]
})
square_op1
=
OpConfig
(
type
=
"square"
,
inputs
=
{
"X"
:
[
"matmul1_output"
]},
outputs
=
{
"Out"
:
[
"square1_output"
]},
attrs
=
{})
square_op2
=
OpConfig
(
type
=
"square"
,
inputs
=
{
"X"
:
[
"input_data1"
]},
outputs
=
{
"Out"
:
[
"square2_output"
]},
attrs
=
{})
square_op3
=
OpConfig
(
type
=
"square"
,
inputs
=
{
"X"
:
[
"input_data2"
]},
outputs
=
{
"Out"
:
[
"square3_output"
]},
attrs
=
{})
class
SquaredMatSubFusePassTest
(
InferencePassTest
):
def
setUp
(
self
):
with
fluid
.
program_guard
(
self
.
main_program
,
self
.
startup_program
):
data_a
=
fluid
.
data
(
name
=
"data_a"
,
shape
=
[
128
,
1
],
dtype
=
"float32"
)
data_b
=
fluid
.
data
(
name
=
"data_b"
,
shape
=
[
256
,
1
],
dtype
=
"float32"
)
matmul_op2
=
OpConfig
(
type
=
"matmul"
,
inputs
=
{
"X"
:
[
"square2_output"
],
"Y"
:
[
"square3_output"
]},
outputs
=
{
"Out"
:
[
"matmul2_output"
]},
attrs
=
{
"transpose_X"
:
transpose_X
,
"transpose_Y"
:
transpose_Y
,
"alpha"
:
alpha2
,
"fused_reshape_X"
:
[],
"fused_reshape_Y"
:
[],
"fused_transpose_X"
:
[],
"fused_transpose_Y"
:
[],
"fused_reshape_Out"
:
[],
"fused_transpose_Out"
:
[]
})
fc_a
=
fluid
.
layers
.
fc
(
data_a
,
size
=
256
)
fc_b
=
fluid
.
layers
.
fc
(
data_b
,
size
=
64
)
elt_sub_op
=
OpConfig
(
type
=
"elementwise_sub"
,
inputs
=
{
"X"
:
[
"square1_output"
],
"Y"
:
[
"matmul2_output"
]},
outputs
=
{
"Out"
:
[
"sub_out"
]},
attrs
=
{
"axis"
:
axis1
})
data_a_square
=
paddle
.
square
(
fc_a
)
data_b_square
=
paddle
.
square
(
fc_b
)
if
has_str_value
:
fill_constant_op
=
OpConfig
(
type
=
"fill_constant"
,
inputs
=
{},
outputs
=
{
"Out"
:
[
"constant_out"
]},
attrs
=
{
"dtype"
:
5
,
"place_type"
:
place_type
,
"str_value"
:
str_value
,
"value"
:
value
,
"shape"
:
shape
})
else
:
fill_constant_op
=
OpConfig
(
type
=
"fill_constant"
,
inputs
=
{},
outputs
=
{
"Out"
:
[
"constant_out"
]},
attrs
=
{
"dtype"
:
5
,
"place_type"
:
place_type
,
"value"
:
value
,
"shape"
:
shape
})
matmul_ab
=
paddle
.
matmul
(
fc_a
,
fc_b
)
matmul_ab_square
=
paddle
.
square
(
matmul_ab
)
matmul_square_ab
=
paddle
.
matmul
(
data_a_square
,
data_b_square
)
elt_mul_op
=
OpConfig
(
type
=
"elementwise_mul"
,
inputs
=
{
"X"
:
[
"sub_out"
],
"Y"
:
[
"constant_out"
]},
outputs
=
{
"Out"
:
[
"mul_out"
]},
attrs
=
{
"axis"
:
axis2
})
scale
=
paddle
.
fluid
.
layers
.
fill_constant
(
shape
=
[
1
],
value
=
0.5
,
dtype
=
'float32'
)
model_net
=
[
matmul_op1
,
square_op1
,
square_op2
,
square_op3
,
matmul_op2
,
elt_sub_op
,
fill_constant_op
,
elt_mul_op
]
sub_val
=
paddle
.
fluid
.
layers
.
elementwise_sub
(
matmul_ab_square
,
matmul_square_ab
)
squared_mat_sub_out
=
fluid
.
layers
.
elementwise_mul
(
sub_val
,
scale
)
program_config
=
ProgramConfig
(
ops
=
model_net
,
weights
=
{},
inputs
=
{
"input_data1"
:
TensorConfig
(
data_gen
=
partial
(
generate_input
,
"x"
)),
"input_data2"
:
TensorConfig
(
data_gen
=
partial
(
generate_input
,
"y"
))
},
outputs
=
[
"mul_out"
])
self
.
feeds
=
{
"data_a"
:
np
.
random
.
random
((
128
,
1
)).
astype
(
"float32"
),
"data_b"
:
np
.
random
.
random
((
256
,
1
)).
astype
(
"float32"
)
}
self
.
fetch_list
=
[
squared_mat_sub_out
]
return
program_config
def
test_check_output
(
self
):
use_gpu
=
False
self
.
check_output_with_option
(
use_gpu
)
def
sample_predictor_configs
(
self
,
program_config
):
config
=
self
.
create_inference_config
()
yield
config
,
[
"fusion_squared_mat_sub"
],
(
1e-5
,
1e-5
)
self
.
assertTrue
(
PassVersionChecker
.
IsCompatible
(
'squared_mat_sub_fuse_pass'
)
)
def
test
(
self
):
self
.
run_and_statis
(
quant
=
False
,
passes
=
[
"squared_mat_sub_fuse_pass"
]
)
if
__name__
==
"__main__"
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录