diff --git a/paddle/fluid/operators/im2sequence_op.cc b/paddle/fluid/operators/im2sequence_op.cc deleted file mode 100644 index d11734c1a6c99..0000000000000 --- a/paddle/fluid/operators/im2sequence_op.cc +++ /dev/null @@ -1,209 +0,0 @@ -/* Copyright (c) 2016 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 "paddle/fluid/operators/im2sequence_op.h" - -#include -#include -#include - -namespace paddle { -namespace operators { - -class Im2SequenceOp : public framework::OperatorWithKernel { - public: - using framework::OperatorWithKernel::OperatorWithKernel; - - protected: - void InferShape(framework::InferShapeContext* ctx) const override { - PADDLE_ENFORCE_EQ( - ctx->HasInput("X"), - true, - phi::errors::NotFound("The input 'X' of Im2SequenceOp is not found.")); - PADDLE_ENFORCE_EQ(ctx->HasOutput("Out"), - true, - phi::errors::NotFound( - "The output 'Out' of Im2SequenceOp is not found.")); - auto in_dim = ctx->GetInputDim("X"); - - PADDLE_ENFORCE_EQ(in_dim.size(), - 4, - phi::errors::InvalidArgument( - "The dimensions size of input 'X' in Im2SequenceOp " - "should be 4. But " - "received dimensions size=[%d], dimensions=[%s].", - in_dim.size(), - in_dim)); - auto img_channels = in_dim[1]; - - auto kernels = ctx->Attrs().Get>("kernels"); - auto strides = ctx->Attrs().Get>("strides"); - auto paddings = ctx->Attrs().Get>("paddings"); - if (!ctx->IsRuntime()) { - // set lod level for compile-time - framework::VarDesc* out_desc = - PADDLE_GET(framework::VarDesc*, ctx->GetOutputVarPtrs("Out")[0]); - out_desc->SetLoDLevel(1); - } - - ctx->SetOutputDim("Out", - {in_dim[0], img_channels * kernels[0] * kernels[1]}); - } -}; - -class Im2SequenceOpMaker : public framework::OpProtoAndCheckerMaker { - public: - void Make() override { - AddInput("X", - "(Tensor) The input tensor has NCHW format." - "N: batch size" - "C: channels" - "H: height" - "W: width"); - AddInput("Y", - "(Tensor) The input tensor of image real size(H, W)." - "2-D with shape [batchsize, 2]") - .AsDispensable(); - AddOutput("Out", "(LodTensor) The output data of im2sequence op,"); - AddAttr>("kernels", - "(vector), the " - "kernels(kernel_height, kernel_width)"); - AddAttr>("strides", - "(vector default:{1, 1}), the " - "strides(h_stride, w_stride)") - .SetDefault({1, 1}); - AddAttr>("paddings", - "(vector default:{0, 0, 0, 0}), the " - "paddings(up_pad, left_pad, down_pad, right_pad)") - .SetDefault({0, 0, 0, 0}); - AddAttr>("out_stride", - "the attribute is valid only when input(Y)" - "is not NULL.this attribute represents the" - "scaling of the pic through the CNN" - "(vector default:{1,1}),the out_stride" - " (out_stride_height, out_stride_width)") - .SetDefault({1, 1}); - AddComment(R"DOC( -This op uses kernels to scan images and converts these images to sequences. -After expanding, The number of time steps are output_height * output_width -and the dimension of each time step is kernel_height * kernel_width * channels, -in which: - -output_height = - 1 + (padding_height + padding_down + img_height - kernel_height + stride_height - 1) / - stride_height; -output_width = - 1 + (padding_left + padding+right + img_width - kernel_width + stride_width - 1) / - stride_width; - -This op can be used after convolution neural network, and before recurrent neural network. - -Given: - -x = [[[[ 6. 2. 1.] - [ 8. 3. 5.] - [ 0. 2. 6.]] - - [[ 2. 4. 4.] - [ 6. 3. 0.] - [ 6. 4. 7.]]] - - [[[ 6. 7. 1.] - [ 5. 7. 9.] - [ 2. 4. 8.]] - - [[ 1. 2. 1.] - [ 1. 3. 5.] - [ 9. 0. 8.]]]] -x.dims = {2, 2, 3, 3} - -And: - -kernels = [2, 2] -strides = [1, 1] -paddings = [0, 0, 0, 0] - -Then: - -output.data = [[ 6. 2. 8. 3. 2. 4. 6. 3.] - [ 2. 1. 3. 5. 4. 4. 3. 0.] - [ 8. 3. 0. 2. 6. 3. 6. 4.] - [ 3. 5. 2. 6. 3. 0. 4. 7.] - [ 6. 7. 5. 7. 1. 2. 1. 3.] - [ 7. 1. 7. 9. 2. 1. 3. 5.] - [ 5. 7. 2. 4. 1. 3. 9. 0.] - [ 7. 9. 4. 8. 3. 5. 0. 8.]] -output.dims = {8, 8} -output.lod = [[0, 4, 8]] - -)DOC"); - } -}; - -class Im2SequenceGradOp : public framework::OperatorWithKernel { - public: - using framework::OperatorWithKernel::OperatorWithKernel; - - protected: - void InferShape(framework::InferShapeContext* ctx) const override { - PADDLE_ENFORCE_EQ(ctx->HasInput("X"), - true, - phi::errors::NotFound( - "The input 'X' of Im2SequenceGradOp is not found.")); - PADDLE_ENFORCE_EQ( - ctx->HasInput(framework::GradVarName("Out")), - true, - phi::errors::NotFound("The input %s of Im2SequenceGradOp is not found.", - framework::GradVarName("Out"))); - ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X")); - } -}; - -template -class Im2SequenceGradMaker : public framework::SingleGradOpMaker { - public: - using framework::SingleGradOpMaker::SingleGradOpMaker; - - protected: - void Apply(GradOpPtr op) const override { - op->SetType("im2sequence_grad"); - op->SetInput("X", this->Input("X")); - op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out")); - op->SetOutput(framework::GradVarName("X"), this->InputGrad("X")); - op->SetAttrMap(this->Attrs()); - } -}; - -} // namespace operators -} // namespace paddle - -namespace ops = paddle::operators; -REGISTER_OPERATOR(im2sequence, - ops::Im2SequenceOp, - ops::Im2SequenceOpMaker, - ops::Im2SequenceGradMaker, - ops::Im2SequenceGradMaker); -REGISTER_OPERATOR(im2sequence_grad, ops::Im2SequenceGradOp); - -PD_REGISTER_STRUCT_KERNEL( - im2sequence, CPU, ALL_LAYOUT, ops::Im2SequenceKernel, float) {} -PD_REGISTER_STRUCT_KERNEL( - im2sequence_grad, CPU, ALL_LAYOUT, ops::Im2SequenceGradKernel, float) {} - -#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) -PD_REGISTER_STRUCT_KERNEL( - im2sequence, GPU, ALL_LAYOUT, ops::Im2SequenceKernel, float) {} -PD_REGISTER_STRUCT_KERNEL( - im2sequence_grad, GPU, ALL_LAYOUT, ops::Im2SequenceGradKernel, float) {} -#endif diff --git a/paddle/fluid/operators/im2sequence_op.h b/paddle/fluid/operators/im2sequence_op.h deleted file mode 100644 index 5fb689d5b1be0..0000000000000 --- a/paddle/fluid/operators/im2sequence_op.h +++ /dev/null @@ -1,203 +0,0 @@ -/* Copyright (c) 2016 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 -#include - -#include "paddle/fluid/framework/data_layout.h" -#include "paddle/fluid/framework/eigen.h" -#include "paddle/fluid/framework/op_registry.h" -#include "paddle/fluid/operators/eigen/eigen_function.h" -#include "paddle/phi/kernels/funcs/im2col.h" -#include "paddle/phi/kernels/funcs/math_function.h" - -namespace paddle { -namespace operators { - -inline int Im2SeqOutputSize( - int input_size, int filter_size, int padding_0, int padding_1, int stride) { - const int output_size = - (input_size + padding_0 + padding_1 - filter_size) / stride + 1; - return output_size; -} - -template -class Im2SequenceKernel : public framework::OpKernel { - public: - void Compute(const framework::ExecutionContext& ctx) const override { - const phi::DenseTensor* in = ctx.Input("X"); - phi::DenseTensor* out = ctx.Output("Out"); - auto in_dim = in->dims(); - int batch_size = in_dim[0]; - int img_channels = in_dim[1]; - int img_height = in_dim[2]; - int img_width = in_dim[3]; - auto kernels = ctx.Attr>("kernels"); - auto strides = ctx.Attr>("strides"); - auto paddings = ctx.Attr>("paddings"); - if (ctx.HasInput("Y") && batch_size > 1) { - const phi::DenseTensor* img_real_size = ctx.Input("Y"); - auto out_stride = ctx.Attr>("out_stride"); - phi::DenseTensor cpu_shape_tensor; - paddle::framework::TensorCopySync( - *img_real_size, platform::CPUPlace(), &cpu_shape_tensor); - std::vector img_real_h; - std::vector img_real_w; - std::vector output_height; - std::vector output_width; - int result = 0; - for (int i = 0; i < batch_size; i++) { - int tmp_real_h = static_cast((cpu_shape_tensor.data())[2 * i]); - int tmp_real_w = - static_cast((cpu_shape_tensor.data())[2 * i + 1]); - if (tmp_real_h % out_stride[0] == 0) { - tmp_real_h = tmp_real_h / out_stride[0]; - } else { - tmp_real_h = tmp_real_h / out_stride[0] + 1; - } - if (tmp_real_w % out_stride[1] == 0) { - tmp_real_w = tmp_real_w / out_stride[1]; - } else { - tmp_real_w = tmp_real_w / out_stride[1] + 1; - } - img_real_h.push_back(tmp_real_h); - img_real_w.push_back(tmp_real_w); - output_height.push_back(Im2SeqOutputSize( - img_real_h[i], kernels[0], paddings[0], paddings[2], strides[0])); - output_width.push_back(Im2SeqOutputSize( - img_real_w[i], kernels[1], paddings[1], paddings[3], strides[1])); - result += output_height[i] * output_width[i]; - } - - out->mutable_data({result, img_channels * kernels[0] * kernels[1]}, - ctx.GetPlace()); - - const std::vector dilations({1, 1}); - int offset_out = 0; - for (int i = 0; i < batch_size; i++) { - const phi::DenseTensor src = - in->Slice(i, i + 1).Resize({img_channels, img_height, img_width}); - phi::DenseTensor dst = - out->Slice(offset_out, - offset_out + output_height[i] * output_width[i]) - .Resize({output_height[i], - output_width[i], - img_channels, - kernels[0], - kernels[1]}); - offset_out += output_height[i] * output_width[i]; - - phi::funcs::Im2ColFunctor - f; - auto& dev_ctx = ctx.template device_context(); - f(dev_ctx, src, dilations, strides, paddings, &dst); - } - framework::LoD lod(1); - lod[0].reserve(batch_size + 1); - int offset = 0; - lod[0].push_back(offset); - for (int i = 0; i < batch_size; ++i) { - offset += output_height[i] * output_width[i]; - lod[0].push_back(offset); - } - out->set_lod(lod); - } else { - int output_height = Im2SeqOutputSize( - img_height, kernels[0], paddings[0], paddings[2], strides[0]); - int output_width = Im2SeqOutputSize( - img_width, kernels[1], paddings[1], paddings[3], strides[1]); - out->mutable_data( - {static_cast(batch_size) * output_height * output_width, - static_cast(img_channels) * kernels[0] * kernels[1]}, - ctx.GetPlace()); - const std::vector dilations({1, 1}); - auto out_dims = out->dims(); - out->Resize({batch_size, out->numel() / batch_size}); - for (int i = 0; i < batch_size; i++) { - const phi::DenseTensor src = - in->Slice(i, i + 1).Resize({img_channels, img_height, img_width}); - phi::DenseTensor dst = out->Slice(i, i + 1).Resize({output_height, - output_width, - img_channels, - kernels[0], - kernels[1]}); - - phi::funcs::Im2ColFunctor - f; - auto& dev_ctx = ctx.template device_context(); - f(dev_ctx, src, dilations, strides, paddings, &dst); - } - out->Resize(out_dims); - framework::LoD lod(1); - lod[0].reserve(batch_size + 1); - int offset = 0; - lod[0].push_back(offset); - for (int i = 0; i < batch_size; ++i) { - offset += output_height * output_width; - lod[0].push_back(offset); - } - out->set_lod(lod); - } - } -}; - -template -class Im2SequenceGradKernel : public framework::OpKernel { - public: - void Compute(const framework::ExecutionContext& ctx) const override { - auto* in = ctx.Input("X"); - phi::DenseTensor* d_out = const_cast( - ctx.Input(framework::GradVarName("Out"))); - auto* d_x = ctx.Output(framework::GradVarName("X")); - d_x->mutable_data(ctx.GetPlace()); - - auto x_v = framework::EigenVector::Flatten(*d_x); - auto& place = *ctx.template device_context().eigen_device(); - EigenConstant, T, 1>::Eval(place, x_v, 0.0); - - auto in_dim = in->dims(); - int batch_size = in_dim[0]; - int img_channels = in_dim[1]; - int img_height = in_dim[2]; - int img_width = in_dim[3]; - - auto kernels = ctx.Attr>("kernels"); - auto strides = ctx.Attr>("strides"); - auto paddings = ctx.Attr>("paddings"); - int output_height = Im2SeqOutputSize( - img_height, kernels[0], paddings[0], paddings[2], strides[0]); - int output_width = Im2SeqOutputSize( - img_width, kernels[1], paddings[1], paddings[3], strides[1]); - - const std::vector dilations({1, 1}); - - auto d_out_dims = d_out->dims(); - d_out->Resize({batch_size, d_out->numel() / batch_size}); - for (int i = 0; i < batch_size; i++) { - phi::DenseTensor dst = - d_x->Slice(i, i + 1).Resize({img_channels, img_height, img_width}); - const phi::DenseTensor src = d_out->Slice(i, i + 1).Resize( - {output_height, output_width, img_channels, kernels[0], kernels[1]}); - phi::funcs::Col2ImFunctor - f; - auto& dev_ctx = ctx.template device_context(); - f(dev_ctx, src, dilations, strides, paddings, &dst); - } - d_out->Resize(d_out_dims); - } -}; - -} // namespace operators -} // namespace paddle diff --git a/test/ir/inference/CMakeLists.txt b/test/ir/inference/CMakeLists.txt index a0db9d85e1bd5..488d2e863e322 100755 --- a/test/ir/inference/CMakeLists.txt +++ b/test/ir/inference/CMakeLists.txt @@ -57,9 +57,6 @@ if(WIN32) "test_trt_explicit_quantization_mobilenet") endif() -# Only for cpu(mkl + openblas) -set(TEST_INFERENCE_CPU_UT "test_mul_lstm_fuse_pass" "test_mul_gru_fuse_pass") - foreach(CPU_UT ${TEST_INFERENCE_CPU_UT}) list(REMOVE_ITEM TEST_INFERENCE_IR_PASSES ${CPU_UT}) endforeach() @@ -160,9 +157,6 @@ if(NOT WITH_ONEDNN py_test_modules(${target} MODULES ${target}) set_tests_properties(${target} PROPERTIES LABELS "RUN_TYPE=INFER") endforeach() - - set_tests_properties(test_mul_lstm_fuse_pass PROPERTIES TIMEOUT 1000) - set_tests_properties(test_mul_gru_fuse_pass PROPERTIES TIMEOUT 600) endif() if(WITH_GPU AND TENSORRT_FOUND) diff --git a/test/ir/inference/test_mul_gru_fuse_pass.py b/test/ir/inference/test_mul_gru_fuse_pass.py deleted file mode 100644 index 0ccbe46724608..0000000000000 --- a/test/ir/inference/test_mul_gru_fuse_pass.py +++ /dev/null @@ -1,142 +0,0 @@ -# Copyright (c) 2021 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 -from functools import partial - -import hypothesis.strategies as st -import numpy as np -from auto_scan_test import PassAutoScanTest -from program_config import OpConfig, ProgramConfig, TensorConfig - - -class TestMulGruFusePass(PassAutoScanTest): - def is_program_valid(self, program_config: ProgramConfig) -> bool: - return True - - def sample_program_config(self, draw): - x_col = draw(st.sampled_from([1])) - y_col = draw(st.sampled_from([1])) - activation = draw(st.sampled_from(['sigmoid', 'tanh'])) - is_reverse = draw(st.booleans()) - has_origin_mode = draw(st.booleans()) - origin_mode = False - gate_activation = draw(st.sampled_from(['sigmoid', 'tanh'])) - batch_size = draw(st.integers(min_value=1, max_value=40)) - - def generate_input(): - shape = [batch_size, 128, 6, 120] - return np.full(shape, 0.001).astype(np.float32) - - def generate_weight(shape): - return np.full(shape, 0.0001).astype(np.float32) - - im2sequence_op = OpConfig( - type="im2sequence", - inputs={"X": ["input_data"]}, - outputs={"Out": ["seq_out"]}, - attrs={ - "kernels": [6, 1], - "out_stride": [1, 1], - "paddings": [0, 0, 0, 0], - "strides": [1, 1], - }, - ) - - mul_op = OpConfig( - type="mul", - inputs={"X": ["seq_out"], "Y": ["mul_weight"]}, - outputs={"Out": ["mul_out"]}, - attrs={"x_num_col_dims": x_col, "y_num_col_dims": y_col}, - ) - - if has_origin_mode: - gru_op = OpConfig( - type="gru", - inputs={ - "Input": ["mul_out"], - "Weight": ["gru_weight"], - "Bias": ["gru_bias"], - }, - outputs={ - "BatchGate": ["batch_gate"], - "BatchHidden": ["batch_hidden"], - "BatchResetHiddenPrev": ["batch_reset"], - "Hidden": ["hidden"], - }, - attrs={ - 'activation': activation, - 'is_reverse': is_reverse, - 'gate_activation': gate_activation, - 'is_test': True, - 'origin_mode': origin_mode, - }, - ) - else: - gru_op = OpConfig( - type="gru", - inputs={ - "Input": ["mul_out"], - "Weight": ["gru_weight"], - "Bias": ["gru_bias"], - }, - outputs={ - "BatchGate": ["batch_gate"], - "BatchHidden": ["batch_hidden"], - "BatchResetHiddenPrev": ["batch_reset"], - "Hidden": ["hidden"], - }, - attrs={ - 'activation': activation, - 'is_reverse': is_reverse, - 'gate_activation': gate_activation, - 'is_test': True, - }, - ) - - model_net = [im2sequence_op, mul_op, gru_op] - - program_config = ProgramConfig( - ops=model_net, - weights={ - "mul_weight": TensorConfig( - data_gen=partial(generate_weight, [768, 600]) - ), - "gru_weight": TensorConfig( - data_gen=partial(generate_weight, [200, 600]) - ), - "gru_bias": TensorConfig( - data_gen=partial(generate_weight, [1, 600]) - ), - }, - inputs={ - "input_data": TensorConfig(data_gen=partial(generate_input)) - }, - outputs=["hidden"], - ) - - return program_config - - def sample_predictor_configs(self, program_config): - config = self.create_inference_config() - yield config, ["im2sequence", "fusion_gru"], (1e-5, 1e-5) - - def test(self): - self.run_and_statis( - quant=False, max_duration=600, passes=["mul_gru_fuse_pass"] - ) - - -if __name__ == "__main__": - unittest.main() diff --git a/test/ir/inference/test_mul_lstm_fuse_pass.py b/test/ir/inference/test_mul_lstm_fuse_pass.py deleted file mode 100644 index fec34311604ee..0000000000000 --- a/test/ir/inference/test_mul_lstm_fuse_pass.py +++ /dev/null @@ -1,128 +0,0 @@ -# Copyright (c) 2021 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 -from functools import partial - -import hypothesis.strategies as st -import numpy as np -from auto_scan_test import PassAutoScanTest -from program_config import OpConfig, ProgramConfig, TensorConfig - - -class TestMulLstmFusePass(PassAutoScanTest): - def is_program_valid(self, program_config: ProgramConfig) -> bool: - return True - - def sample_program_config(self, draw): - x_col = draw(st.sampled_from([1])) - y_col = draw(st.sampled_from([1])) - use_peepholes = draw(st.booleans()) - is_reverse = draw(st.booleans()) - gate_activation = draw(st.sampled_from(["sigmoid"])) - cell_activation = draw(st.sampled_from(["tanh", "relu", "identity"])) - candidate_activation = draw( - st.sampled_from(["tanh", "relu", "identity"]) - ) - batch_size = draw(st.integers(min_value=1, max_value=40)) - - def generate_input(): - shape = [batch_size, 128, 6, 120] - return np.full(shape, 0.01).astype(np.float32) - - def generate_weight(shape): - return np.full(shape, 0.0001).astype(np.float32) - - im2sequence_op = OpConfig( - type="im2sequence", - inputs={"X": ["input_data"]}, - outputs={"Out": ["seq_out"]}, - attrs={ - "kernels": [6, 1], - "out_stride": [1, 1], - "paddings": [0, 0, 0, 0], - "strides": [1, 1], - }, - ) - - mul_op = OpConfig( - type="mul", - inputs={"X": ["seq_out"], "Y": ["mul_weight"]}, - outputs={"Out": ["mul_out"]}, - attrs={"x_num_col_dims": x_col, "y_num_col_dims": y_col}, - ) - - lstm_op = OpConfig( - type="lstm", - inputs={ - "Input": ["mul_out"], - "Weight": ["lstm_weight"], - "Bias": ["lstm_bias"], - }, - outputs={ - "Hidden": ["lstm_hidden"], - "Cell": ["lstm_cell"], - "BatchGate": ["lstm_gate"], - "BatchCellPreAct": ["lstm_batch_cell"], - }, - attrs={ - 'use_peepholes': use_peepholes, - 'is_reverse': is_reverse, - 'gate_activation': gate_activation, - 'cell_activation': cell_activation, - 'candidate_activation': candidate_activation, - 'is_test': True, - }, - ) - - model_net = [im2sequence_op, mul_op, lstm_op] - - if use_peepholes: - lstm_bias_shape = [1, 1050] - else: - lstm_bias_shape = [1, 600] - - program_config = ProgramConfig( - ops=model_net, - weights={ - "mul_weight": TensorConfig( - data_gen=partial(generate_weight, [768, 600]) - ), - "lstm_weight": TensorConfig( - data_gen=partial(generate_weight, [150, 600]) - ), - "lstm_bias": TensorConfig( - data_gen=partial(generate_weight, lstm_bias_shape) - ), - }, - inputs={ - "input_data": TensorConfig(data_gen=partial(generate_input)), - }, - outputs=["lstm_hidden"], - ) - - return program_config - - def sample_predictor_configs(self, program_config): - config = self.create_inference_config() - yield config, ["im2sequence", "fusion_lstm"], (1e-5, 1e-5) - - def test(self): - self.run_and_statis( - quant=False, max_duration=1000, passes=["mul_lstm_fuse_pass"] - ) - - -if __name__ == "__main__": - unittest.main() diff --git a/test/ir/inference/test_seqconv_eltadd_relu_fuse_pass.py b/test/ir/inference/test_seqconv_eltadd_relu_fuse_pass.py deleted file mode 100644 index b31533ac958d0..0000000000000 --- a/test/ir/inference/test_seqconv_eltadd_relu_fuse_pass.py +++ /dev/null @@ -1,120 +0,0 @@ -# Copyright (c) 2021 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 -from functools import partial - -import hypothesis.strategies as st -import numpy as np -from auto_scan_test import PassAutoScanTest -from program_config import OpConfig, ProgramConfig, TensorConfig - - -class TestSeqconvEltaddReluFusePass(PassAutoScanTest): - def is_program_valid(self, program_config: ProgramConfig) -> bool: - return True - - def sample_program_config(self, draw): - contextLength = draw(st.sampled_from([1, 2, 3, 4])) - contextStart = draw(st.sampled_from([1, 2, 3])) - contextStride = draw(st.sampled_from([1])) - paddingTrainable = False - axis = draw(st.sampled_from([1])) - batch_size = draw(st.integers(min_value=1, max_value=4)) - - def generate_input(): - shape = [batch_size, 128, 6, 120] - return np.random.random(shape).astype(np.float32) - - def generate_weight(shape): - return np.random.random(shape).astype(np.float32) - - im2sequence_op = OpConfig( - type="im2sequence", - inputs={"X": ["input_data"]}, - outputs={"Out": ["seq_out"]}, - attrs={ - "kernels": [6, 1], - "out_stride": [1, 1], - "paddings": [0, 0, 0, 0], - "strides": [1, 1], - }, - ) - - sequence_conv_op = OpConfig( - type="sequence_conv", - inputs={"X": ["seq_out"], "Filter": ["conv_weight"]}, - outputs={"Out": ["conv_out"]}, - attrs={ - "contextLength": contextLength, - "contextStart": contextStart, - "contextStride": contextStride, - "paddingTrainable": paddingTrainable, - }, - ) - - elementwise_add_op = OpConfig( - type="elementwise_add", - inputs={"X": ["conv_out"], "Y": ["elt_weight"]}, - outputs={"Out": ["elt_output"]}, - attrs={'axis': axis}, - ) - - relu_op = OpConfig( - type="relu", - inputs={"X": ["elt_output"]}, - outputs={"Out": ["relu_output"]}, - attrs={}, - ) - - model_net = [ - im2sequence_op, - sequence_conv_op, - elementwise_add_op, - relu_op, - ] - - program_config = ProgramConfig( - ops=model_net, - weights={ - "conv_weight": TensorConfig( - data_gen=partial(generate_weight, [768 * contextLength, 16]) - ), - "elt_weight": TensorConfig( - data_gen=partial(generate_weight, [16]) - ), - }, - inputs={ - "input_data": TensorConfig(data_gen=partial(generate_input)) - }, - outputs=["relu_output"], - ) - - return program_config - - def sample_predictor_configs(self, program_config): - config = self.create_inference_config() - yield config, ["im2sequence", "fusion_seqconv_eltadd_relu"], ( - 1e-5, - 1e-5, - ) - - def test(self): - self.run_and_statis( - quant=False, passes=["seqconv_eltadd_relu_fuse_pass"] - ) - - -if __name__ == "__main__": - unittest.main() diff --git a/test/ir/inference/test_seqpool_cvm_concat_fuse_pass_py.py b/test/ir/inference/test_seqpool_cvm_concat_fuse_pass_py.py deleted file mode 100644 index 123dad50ae865..0000000000000 --- a/test/ir/inference/test_seqpool_cvm_concat_fuse_pass_py.py +++ /dev/null @@ -1,157 +0,0 @@ -# Copyright (c) 2021 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 -from functools import partial - -import hypothesis.strategies as st -import numpy as np -from auto_scan_test import PassAutoScanTest -from program_config import OpConfig, ProgramConfig, TensorConfig - - -class TestSeqpoolCvmConcatFusePass(PassAutoScanTest): - def is_program_valid(self, program_config: ProgramConfig) -> bool: - return True - - def sample_program_config(self, draw): - is_test = True - pooltype = "SUM" - pad_value1 = draw(st.floats()) - pad_value2 = draw(st.floats()) - pad_value3 = draw(st.floats()) - use_cvm = True - axis = draw(st.sampled_from([1])) - batch_size = draw(st.integers(min_value=1, max_value=4)) - - def generate_input1(): - shape = [batch_size, 128, 6, 120] - return np.random.random(shape).astype(np.float32) - - def generate_input2(): - shape = [batch_size, 2] - return np.random.random(shape).astype(np.float32) - - def generate_input3(): - return np.random.random([1, 768]).astype(np.float32) - - im2sequence_op = OpConfig( - type="im2sequence", - inputs={"X": ["input_data1"]}, - outputs={"Out": ["seq_out"]}, - attrs={ - "kernels": [6, 1], - "out_stride": [1, 1], - "paddings": [0, 0, 0, 0], - "strides": [1, 1], - }, - ) - - sequence_pool_op1 = OpConfig( - type="sequence_pool", - inputs={"X": ["seq_out"]}, - outputs={"Out": ["seq_pool1_out"], "MaxIndex": ["index1_out"]}, - attrs={ - "is_test": is_test, - "pooltype": pooltype, - "pad_value": pad_value1, - }, - ) - - sequence_pool_op2 = OpConfig( - type="sequence_pool", - inputs={"X": ["seq_out"]}, - outputs={"Out": ["seq_pool2_out"], "MaxIndex": ["index2_out"]}, - attrs={ - "is_test": is_test, - "pooltype": pooltype, - "pad_value": pad_value2, - }, - ) - - sequence_pool_op3 = OpConfig( - type="sequence_pool", - inputs={"X": ["seq_out"]}, - outputs={"Out": ["seq_pool3_out"], "MaxIndex": ["index3_out"]}, - attrs={ - "is_test": is_test, - "pooltype": pooltype, - "pad_value": pad_value3, - }, - ) - - cvm_op1 = OpConfig( - type="cvm", - inputs={"X": ["seq_pool1_out"], "CVM": ["input_data2"]}, - outputs={"Y": ["cvm1_out"]}, - attrs={"use_cvm": use_cvm}, - ) - - cvm_op2 = OpConfig( - type="cvm", - inputs={"X": ["seq_pool2_out"], "CVM": ["input_data2"]}, - outputs={"Y": ["cvm2_out"]}, - attrs={"use_cvm": use_cvm}, - ) - - cvm_op3 = OpConfig( - type="cvm", - inputs={"X": ["seq_pool3_out"], "CVM": ["input_data2"]}, - outputs={"Y": ["cvm3_out"]}, - attrs={"use_cvm": use_cvm}, - ) - - concat_op = OpConfig( - type="concat", - inputs={"X": ["cvm1_out", "cvm2_out", "cvm3_out"]}, - outputs={"Out": ["concat_output"]}, - attrs={'axis': axis}, - ) - - model_net = [ - im2sequence_op, - sequence_pool_op1, - sequence_pool_op2, - sequence_pool_op3, - cvm_op1, - cvm_op2, - cvm_op3, - concat_op, - ] - - program_config = ProgramConfig( - ops=model_net, - weights={}, - inputs={ - "input_data1": TensorConfig(data_gen=partial(generate_input1)), - "input_data2": TensorConfig(data_gen=partial(generate_input2)), - "input_data3": TensorConfig(data_gen=partial(generate_input3)), - }, - outputs=["concat_output"], - ) - - return program_config - - def sample_predictor_configs(self, program_config): - config = self.create_inference_config() - yield config, ["im2sequence", "fusion_seqpool_cvm_concat"], (1e-5, 1e-5) - - def test(self): - self.run_and_statis( - quant=False, passes=["seqpool_cvm_concat_fuse_pass"] - ) - - -if __name__ == "__main__": - unittest.main() diff --git a/test/legacy_test/test_im2sequence_op.py b/test/legacy_test/test_im2sequence_op.py deleted file mode 100644 index b118418711249..0000000000000 --- a/test/legacy_test/test_im2sequence_op.py +++ /dev/null @@ -1,326 +0,0 @@ -# Copyright (c) 2018 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 numpy as np -from op_test import OpTest, skip_check_grad_ci - - -def get_output_shape(attrs, in_shape, img_real_size): - batchsize = in_shape[0] - img_height = in_shape[2] - img_width = in_shape[3] - paddings = np.array(attrs['paddings']).astype("int32") - kernels = np.array(attrs['kernels']).astype("int32") - strides = np.array(attrs['strides']).astype("int32") - output_height = np.zeros((1, batchsize)).astype("int32") - output_width = np.zeros((1, batchsize)).astype("int32") - if len(img_real_size): - out_stride = np.array(attrs['out_stride']).astype("int32") - imgreal_h = 0 - imgreal_w = 0 - for index in range(batchsize): - if img_real_size[index, 0] % out_stride[0] == 0: - imgreal_h = img_real_size[index, 0] / out_stride[0] - else: - imgreal_h = img_real_size[index, 0] / out_stride[0] + 1 - if img_real_size[index, 0] % out_stride[1] == 0: - imgreal_w = img_real_size[index, 1] / out_stride[1] - else: - imgreal_w = img_real_size[index, 0] / out_stride[1] + 1 - output_height[0, index] = ( - 1 - + ( - imgreal_h - + paddings[0] - + paddings[2] - - kernels[0] - + strides[0] - - 1 - ) - / strides[0] - ) - - output_width[0, index] = ( - 1 - + ( - imgreal_w - + paddings[1] - + paddings[3] - - kernels[1] - + strides[1] - - 1 - ) - / strides[1] - ) - else: - for index in range(batchsize): - output_height[0, index] = ( - 1 - + ( - img_height - + paddings[0] - + paddings[2] - - kernels[0] - + strides[0] - - 1 - ) - / strides[0] - ) - - output_width[0, index] = ( - 1 - + ( - img_width - + paddings[1] - + paddings[3] - - kernels[1] - + strides[1] - - 1 - ) - / strides[1] - ) - - return output_height, output_width - - -def im2col(attrs, im, col): - """ - im: {CHW} - col: - {outputHeight, outputWidth, inputChannels, filterHeight, filterWidth} - """ - input_channels, input_height, input_width = im.shape - output_height, output_width, _, filter_height, filter_width = col.shape - - stride_height, stride_width = attrs['strides'] - padding_height, padding_width = attrs['paddings'][0:2] - - for col_row_idx in range(0, output_height): - for col_col_idx in range(0, output_width): - for channel in range(0, input_channels): - for filter_row_idx in range(0, filter_height): - for filter_col_idx in range(0, filter_width): - im_row_offset = ( - col_row_idx * stride_height - + filter_row_idx - - padding_height - ) - - im_col_offset = ( - col_col_idx * stride_width - + filter_col_idx - - padding_width - ) - - if ( - im_row_offset < 0 - or im_row_offset >= input_height - or im_col_offset < 0 - or im_col_offset >= input_width - ): - col[col_row_idx][col_col_idx][channel][ - filter_row_idx - ][filter_col_idx] = 0.0 - else: - im_offset = ( - channel * input_height + im_row_offset - ) * input_width + im_col_offset - - col[col_row_idx][col_col_idx][channel][ - filter_row_idx - ][filter_col_idx] = im[channel][im_row_offset][ - im_col_offset - ] - - -def Im2Sequence(inputs, img_real_size, attrs): - output_height, output_width = get_output_shape( - attrs, inputs.shape, img_real_size - ) - img_channels = inputs.shape[1] - batch_size = inputs.shape[0] - out = [] - for index in range(batch_size): - tmp = np.zeros( - [ - output_height[0, index], - output_width[0, index], - img_channels, - attrs['kernels'][0], - attrs['kernels'][1], - ] - ).astype("float32") - out.append(tmp) - for index in range(len(inputs)): - im2col(attrs, inputs[index], out[index]) - out[index] = out[index].reshape( - [ - output_height[0, index] * output_width[0, index], - img_channels * attrs['kernels'][0] * attrs['kernels'][1], - ] - ) - out = np.concatenate(out, axis=0) - return out - - -class TestBlockExpandOp(OpTest): - def config(self): - self.batch_size = 1 - self.img_channels = 3 - self.img_height = 4 - self.img_width = 10 - self.attrs = { - 'kernels': [2, 2], - 'strides': [1, 1], - 'paddings': [1, 1, 1, 1], - } - - def setUp(self): - self.config() - self.op_type = "im2sequence" - x = np.random.uniform( - 0.1, - 1, - [ - self.batch_size, - self.img_channels, - self.img_height, - self.img_width, - ], - ).astype("float32") - real_size = np.array([]).astype("float32") - out = Im2Sequence(x, real_size, self.attrs) - self.inputs = {'X': x} - self.outputs = {'Out': out} - - def test_check_output(self): - # NODE(yjjiang11): This op will be deprecated. - self.check_output(check_dygraph=False) - - def test_check_grad_normal(self): - self.check_grad(['X'], 'Out', check_dygraph=False) - - -class TestBlockExpandOpCase2(TestBlockExpandOp): - def config(self): - self.batch_size = 2 - self.img_channels = 3 - self.img_height = 4 - self.img_width = 5 - self.attrs = { - 'kernels': [2, 1], - 'strides': [2, 1], - 'paddings': [2, 1, 2, 1], - } - - -class TestBlockExpandOpCase3(TestBlockExpandOp): - def config(self): - self.batch_size = 6 - self.img_channels = 1 - self.img_height = 4 - self.img_width = 5 - self.attrs = { - 'kernels': [2, 1], - 'strides': [2, 1], - 'paddings': [2, 0, 2, 0], - } - - -class TestBlockExpandOpCase4(TestBlockExpandOp): - def config(self): - self.batch_size = 6 - self.img_channels = 2 - self.img_height = 3 - self.img_width = 3 - self.attrs = { - 'kernels': [2, 2], - 'strides': [1, 1], - 'paddings': [0, 0, 0, 0], - } - - -@skip_check_grad_ci( - reason="Since 'real_size' is used just in forward computation, we don't test the gradient here." -) -class TestBlockExpandOpCase5(OpTest): - def config(self): - self.batch_size = 1 - self.img_channels = 3 - self.img_height = 4 - self.img_width = 5 - self.attrs = { - 'kernels': [2, 1], - 'strides': [2, 1], - 'paddings': [2, 1, 2, 1], - 'out_stride': [2, 2], - } - self.real_size = np.array([[8, 10], [5, 8]]).astype("float32") - - def setUp(self): - self.config() - self.op_type = "im2sequence" - x = np.random.uniform( - 0.1, - 1, - [ - self.batch_size, - self.img_channels, - self.img_height, - self.img_width, - ], - ).astype("float32") - out = np.array(Im2Sequence(x, self.real_size, self.attrs)) - self.inputs = {'X': x, 'Y': self.real_size} - self.outputs = {'Out': out} - - def test_check_output(self): - # NODE(yjjiang11): This op will be deprecated. - self.check_output(check_dygraph=False) - - -class TestBlockExpandOpCase6(TestBlockExpandOpCase5): - def config(self): - self.batch_size = 3 - self.img_channels = 1 - self.img_height = 4 - self.img_width = 5 - self.attrs = { - 'kernels': [2, 1], - 'strides': [1, 1], - 'paddings': [0, 0, 0, 0], - 'out_stride': [1, 1], - } - self.real_size = np.array([[8, 10], [5, 8], [5, 8]]).astype("float32") - - -class TestBlockExpandOpCase7(TestBlockExpandOpCase6): - def config(self): - self.batch_size = 2 - self.img_channels = 2 - self.img_height = 3 - self.img_width = 3 - self.attrs = { - 'kernels': [2, 2], - 'strides': [1, 1], - 'paddings': [1, 0, 1, 0], - 'out_stride': [2, 2], - } - self.real_size = np.array([[6, 6], [4, 4]]).astype("float32") - - -if __name__ == '__main__': - unittest.main()