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

update mkldnn scale_matmul fuse pass ut (#37210)

* update mkldnn scale_matmul fuse pass ut

* update mkldnn scale_matmul_fuse_pass ut
上级 0456e003
# 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,61 +12,147 @@ ...@@ -12,61 +12,147 @@
# 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.
from __future__ import print_function from auto_scan_test import PassAutoScanTest, SkipReasons
from program_config import TensorConfig, ProgramConfig
import unittest
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 AnalysisConfig import unittest
from paddle.fluid.core import PassVersionChecker
import hypothesis
from hypothesis import given, settings, seed, example, assume
class ScaleMatmulMkldnnFusePassTest(InferencePassTest): import hypothesis.strategies as st
def setUp(self):
self.set_params()
with fluid.program_guard(self.main_program, self.startup_program): class TestScaleMatmulMkldnnFusePass(PassAutoScanTest):
data = fluid.data( def is_program_valid(self, program_config: ProgramConfig) -> bool:
name="data", shape=[1, 3, 100, 100], dtype="float32") return True
weight = fluid.layers.create_parameter(
shape=[1, 3, 100, 100], dtype="float32") def sample_program_config(self, draw):
scale = fluid.layers.scale(data, scale=self.scale_scale) scale = draw(st.floats(min_value=0.01, max_value=2))
matmul = fluid.layers.matmul( bias = 0.0
scale, bias_after_scale = draw(st.booleans())
weight, transpose_X = draw(st.booleans())
transpose_x=self.transpose_x, transpose_Y = draw(st.booleans())
transpose_y=self.transpose_y) alpha = draw(st.floats(min_value=0.01, max_value=2))
batch_size = draw(st.integers(min_value=1, max_value=4))
self.fetch_list = [matmul] channel = draw(st.integers(min_value=1, max_value=64))
self.enable_mkldnn = True input_dim = draw(st.sampled_from([1, 32, 64]))
def set_params(self): def generate_input(attrs, type):
self.feeds = { if attrs[1]['transpose_X'] and attrs[1]['transpose_Y']:
"data": np.random.random((1, 3, 100, 100)).astype("float32") shape_x = [
} attrs[2]['batch_size'], attrs[2]['channel'],
self.scale_scale = 2.0 attrs[2]['input_dim'], 32
self.transpose_x = False ]
self.transpose_y = False shape_y = [
self.pass_name = "scale_matmul_fuse_pass" attrs[2]['batch_size'], attrs[2]['channel'], 64,
attrs[2]['input_dim']
def test_check_output(self): ]
use_gpu = False elif attrs[1]['transpose_X']:
self.check_output_with_option(use_gpu) shape_x = [
attrs[2]['batch_size'], attrs[2]['channel'],
def test_pass_compatible(self): attrs[2]['input_dim'], 32
self.assertTrue(PassVersionChecker.IsCompatible(self.pass_name)) ]
shape_y = [
attrs[2]['batch_size'], attrs[2]['channel'],
class ScaleMatmulMkldnnFusePassTest_1(ScaleMatmulMkldnnFusePassTest): attrs[2]['input_dim'], 64
def set_params(self): ]
self.feeds = { elif attrs[1]['transpose_Y']:
"data": np.random.random((1, 3, 100, 100)).astype("float32") shape_x = [
} attrs[2]['batch_size'], attrs[2]['channel'], 32,
self.scale_scale = 5.0 attrs[2]['input_dim']
self.transpose_x = True ]
self.transpose_y = True shape_y = [
self.pass_name = "scale_matmul_fuse_pass" attrs[2]['batch_size'], attrs[2]['channel'], 8,
attrs[2]['input_dim']
]
else:
shape_x = [
attrs[2]['batch_size'], attrs[2]['channel'], 32,
attrs[2]['input_dim']
]
shape_y = [
attrs[2]['batch_size'], attrs[2]['channel'],
attrs[2]['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)
attrs = [{
"scale": scale,
"bias": bias,
"bias_after_scale": bias_after_scale
}, {
"transpose_X": transpose_X,
"transpose_Y": transpose_Y,
"alpha": alpha
}, {
'batch_size': batch_size,
'channel': channel,
'input_dim': input_dim
}]
ops_config = [{
"op_type": "scale",
"op_inputs": {
"X": ["input_data1"]
},
"op_outputs": {
"Out": ["scale_output"]
},
"op_attrs": {
"scale": attrs[0]['scale'],
"bias": attrs[0]['bias'],
"bias_after_scale": attrs[0]['bias_after_scale']
},
}, {
"op_type": "matmul",
"op_inputs": {
"X": ["scale_output"],
"Y": ["input_data2"]
},
"op_outputs": {
"Out": ["matmul_output"]
},
"op_attrs": {
'transpose_X': attrs[1]['transpose_X'],
'transpose_Y': attrs[1]['transpose_Y'],
'alpha': attrs[1]['alpha'],
"fused_reshape_X": [],
"fused_reshape_Y": [],
"fused_transpose_X": [],
"fused_transpose_Y": [],
"fused_reshape_Out": [],
"fused_transpose_Out": []
}
}]
ops = self.generate_op_config(ops_config)
program_config = ProgramConfig(
ops=ops,
weights={},
inputs={
"input_data1":
TensorConfig(data_gen=partial(generate_input, attrs, "x")),
"input_data2":
TensorConfig(data_gen=partial(generate_input, attrs, "y"))
},
outputs=["matmul_output"])
return program_config
def sample_predictor_configs(self, program_config):
config = self.create_inference_config(use_mkldnn=True)
yield config, ['matmul'], (1e-5, 1e-5)
def test(self):
self.run_and_statis(quant=False, passes=["scale_matmul_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.
先完成此消息的编辑!
想要评论请 注册