未验证 提交 a896d1ce 编写于 作者: B baoachun 提交者: GitHub

update repeated_fc_relu_fuse_pass ut (#37845)

* update repeated_fc_relu_fuse_pass ut

* update ut
上级 f74ebd8a
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
...@@ -12,82 +12,116 @@ ...@@ -12,82 +12,116 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import unittest from auto_scan_test import PassAutoScanTest, SkipReasons
from program_config import TensorConfig, ProgramConfig, OpConfig
import numpy as np import numpy as np
from inference_pass_test import InferencePassTest import paddle.inference as paddle_infer
import paddle.fluid as fluid from functools import partial
import paddle.fluid.core as core from typing import Optional, List, Callable, Dict, Any, Set
from paddle.fluid.core import PassVersionChecker import unittest
import hypothesis
class RepeatedFcReluFusePass3Test(InferencePassTest): from hypothesis import given, settings, seed, example, assume
def setUp(self): import hypothesis.strategies as st
fc_num = 3 from functools import reduce
with fluid.program_guard(self.main_program, self.startup_program):
data = fluid.data(
name="data", shape=[-1, 3, 64, 64], dtype="float32") class TestRepeatedFcReluFusePass(PassAutoScanTest):
param_attr = fluid.ParamAttr( def is_program_valid(self, program_config: ProgramConfig) -> bool:
initializer=fluid.initializer.Xavier(uniform=False), return True
learning_rate=0.001)
conv_out = fluid.layers.conv2d( def sample_program_config(self, draw):
input=data, x_col = draw(st.sampled_from([1]))
num_filters=3, y_col = draw(st.sampled_from([1]))
filter_size=3, axis = draw(st.sampled_from([-1, 1]))
bias_attr=param_attr, batch_size = draw(st.integers(min_value=1, max_value=4))
act=None) dim = draw(st.sampled_from([32, 64, 128]))
fc_outs = []
fc_outs.append( def generate_input():
fluid.layers.fc(input=[conv_out], act="relu", size=1000)) return np.random.random([batch_size, dim]).astype(np.float32)
for i in range(1, fc_num):
fc_outs.append( def generate_weight(shape):
fluid.layers.fc( return np.random.random(shape).astype(np.float32)
input=[fc_outs[i - 1]], act="relu", size=1000))
self.feeds = { attrs = [{
"data": np.random.random([1, 3, 64, 64]).astype("float32"), "x_col": x_col,
} "y_col": y_col
self.fetch_list = [fc_outs[fc_num - 1]] }, {
"axis": axis
def test_check_output(self): }, {
use_gpu = False 'batch_size': batch_size,
self.check_output_with_option(use_gpu) 'dim': dim
}]
self.assertTrue(
PassVersionChecker.IsCompatible('repeated_fc_relu_fuse_pass')) mul_op1 = OpConfig(
type="mul",
inputs={"X": ["input_data"],
class RepeatedFcReluFusePass9Test(InferencePassTest): "Y": ["mul1_weight"]},
def setUp(self): outputs={"Out": ["mul1_output"]},
fc_num = 9 attrs={"x_num_col_dims": x_col,
with fluid.program_guard(self.main_program, self.startup_program): "y_num_col_dims": y_col})
data = fluid.data(
name="data", shape=[-1, 3, 64, 64], dtype="float32") elt_op1 = OpConfig(
param_attr = fluid.ParamAttr( type="elementwise_add",
initializer=fluid.initializer.Xavier(uniform=False), inputs={"X": ["mul1_output"],
learning_rate=0.001) "Y": ["elementwise1_weight"]},
conv_out = fluid.layers.conv2d( outputs={"Out": ["elementwise1_output"]},
input=data, attrs={"axis": axis})
num_filters=3,
filter_size=3, relu_op1 = OpConfig(
bias_attr=param_attr, type="relu",
act=None) inputs={"X": ["elementwise1_output"]},
fc_outs = [] outputs={"Out": ["relu1_output"]},
fc_outs.append( attrs={})
fluid.layers.fc(input=[conv_out], act="relu", size=1000))
for i in range(1, fc_num): mul_op2 = OpConfig(
fc_outs.append( type="mul",
fluid.layers.fc( inputs={"X": ["relu1_output"],
input=[fc_outs[i - 1]], act="relu", size=1000)) "Y": ["mul2_weight"]},
self.feeds = { outputs={"Out": ["mul2_output"]},
"data": np.random.random([1, 3, 64, 64]).astype("float32"), attrs={"x_num_col_dims": x_col,
} "y_num_col_dims": y_col})
self.fetch_list = [fc_outs[fc_num - 1]]
elt_op2 = OpConfig(
def test_check_output(self): type="elementwise_add",
use_gpu = False inputs={"X": ["mul2_output"],
self.check_output_with_option(use_gpu) "Y": ["elementwise2_weight"]},
outputs={"Out": ["elementwise2_output"]},
self.assertTrue( attrs={"axis": axis})
PassVersionChecker.IsCompatible('repeated_fc_relu_fuse_pass'))
relu_op2 = OpConfig(
type="relu",
inputs={"X": ["elementwise2_output"]},
outputs={"Out": ["relu2_output"]},
attrs={})
model_net = [mul_op1, elt_op1, relu_op1, mul_op2, elt_op2, relu_op2]
program_config = ProgramConfig(
ops=model_net,
weights={
"mul1_weight": TensorConfig(data_gen=partial(generate_weight,
[dim, 32])),
"mul2_weight":
TensorConfig(data_gen=partial(generate_weight, [32, 128])),
"elementwise1_weight":
TensorConfig(data_gen=partial(generate_weight, [32])),
"elementwise2_weight":
TensorConfig(data_gen=partial(generate_weight, [128]))
},
inputs={
"input_data": TensorConfig(data_gen=partial(generate_input)),
},
outputs=["relu2_output"])
return program_config
def sample_predictor_configs(self, program_config):
config = self.create_inference_config()
yield config, ["fusion_repeated_fc_relu"], (1e-5, 1e-5)
def test(self):
self.run_and_statis(passes=["repeated_fc_relu_fuse_pass"])
if __name__ == "__main__": if __name__ == "__main__":
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册