From d38f8d21a175405220cc31f0f29516a00a9a5c78 Mon Sep 17 00:00:00 2001 From: wanghuancoder Date: Mon, 9 Oct 2023 12:41:12 +0800 Subject: [PATCH] [CleanOps]Delete squeeze op (#57884) * del squeeze op --- paddle/fluid/operators/squeeze_op.cc | 269 --------------------- paddle/fluid/operators/squeeze_op.cu.cc | 45 ---- paddle/fluid/operators/squeeze_op.h | 93 ++++--- test/legacy_test/test_squeeze_op.py | 307 ------------------------ test/xpu/test_squeeze_op_xpu.py | 126 ---------- tools/parallel_UT_rule.py | 2 - 6 files changed, 59 insertions(+), 783 deletions(-) delete mode 100644 paddle/fluid/operators/squeeze_op.cc delete mode 100644 paddle/fluid/operators/squeeze_op.cu.cc delete mode 100755 test/legacy_test/test_squeeze_op.py delete mode 100644 test/xpu/test_squeeze_op_xpu.py diff --git a/paddle/fluid/operators/squeeze_op.cc b/paddle/fluid/operators/squeeze_op.cc deleted file mode 100644 index 115901d3ee2ee..0000000000000 --- a/paddle/fluid/operators/squeeze_op.cc +++ /dev/null @@ -1,269 +0,0 @@ -/* Copyright (c) 2019 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/squeeze_op.h" - -#include -#include -#include -#include - -#include "paddle/fluid/framework/infershape_utils.h" -#include "paddle/fluid/framework/op_registry.h" -#include "paddle/phi/infermeta/unary.h" - -namespace paddle { -namespace operators { - -framework::DDim GetOutputShape(const std::vector squeeze_dims, - const framework::DDim &in_dims, - bool is_runtime) { - size_t num_squeeze_dims = squeeze_dims.size(); - std::vector should_squeeze(in_dims.size(), false); - - // Mark dimensions need to be squeezed. - if (num_squeeze_dims == 0) { - for (int i = 0; i < in_dims.size(); ++i) { - if (in_dims[i] == 1) { - should_squeeze[i] = true; - } - } - } else { - for (size_t i = 0; i < num_squeeze_dims; ++i) { - int current = squeeze_dims[i] < 0 ? squeeze_dims[i] + in_dims.size() - : squeeze_dims[i]; - - PADDLE_ENFORCE_GE( - current, - 0, - platform::errors::InvalidArgument( - "Each axis in Attr(axes) should be in the range of [%d, %d]" - "But current axis is:%d, input tensor's shape = [%s].", - -in_dims.size(), - in_dims.size() - 1, - current, - in_dims)); - PADDLE_ENFORCE_LT( - current, - in_dims.size(), - platform::errors::InvalidArgument( - "Each axis in Attr(axes) should be in the range of [%d, %d]" - "But current axis is:%d, input tensor's shape = [%s].", - -in_dims.size(), - in_dims.size() - 1, - current, - in_dims)); - - if (!should_squeeze[current]) { - if (is_runtime) { - // At run time, dim of 1 is allowed to squeeze - if (in_dims[current] == 1) { - should_squeeze[current] = true; - } - } else { - // At compile time, dim of -1 or 1 is allowed to squeeze - if (in_dims[current] == 1 || in_dims[current] == -1) { - should_squeeze[current] = true; - } - } - } - } - } - // Make output dimensions - std::vector output_shape; - for (int i = 0; i < in_dims.size(); ++i) { - if (!should_squeeze[i]) { - output_shape.push_back(in_dims[i]); - } - } - return phi::make_ddim(output_shape); -} - -class SqueezeOp : public framework::OperatorWithKernel { - public: - using framework::OperatorWithKernel::OperatorWithKernel; - - void InferShape(framework::InferShapeContext *ctx) const override { - OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "Squeeze"); - OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "Squeeze"); - - const auto &x_dims = ctx->GetInputDim("X"); - // Check input tensor dims (<6) Eigen limit. - PADDLE_ENFORCE_LE(x_dims.size(), - 6, - platform::errors::InvalidArgument( - "The dimensions of Input(X) " - "should be in the range of [1, 6] (Eigen limit)." - "But received X's dimensions = %d, X's shape=[%s].", - x_dims.size(), - x_dims)); - - const auto &axes = ctx->Attrs().Get>("axes"); - auto out_dims = GetOutputShape(axes, x_dims, false); - ctx->SetOutputDim("Out", out_dims); - if (x_dims[0] == out_dims[0]) { - // Only pass LoD when the first dimension of output and Input(X) - // are the same. - ctx->ShareLoD("X", "Out"); - } - } - - protected: - phi::KernelKey GetExpectedKernelType( - const framework::ExecutionContext &ctx) const override { - auto input_data_type = - framework::OperatorWithKernel::IndicateVarDataType(ctx, "X"); - return phi::KernelKey(input_data_type, ctx.GetPlace()); - } -}; - -class SqueezeGradOp : public framework::OperatorWithKernel { - public: - using framework::OperatorWithKernel::OperatorWithKernel; - - void InferShape(framework::InferShapeContext *context) const override { - context->SetOutputDim(framework::GradVarName("X"), - context->GetInputDim("X")); - context->ShareLoD("X", framework::GradVarName("X")); - } - - protected: - phi::KernelKey GetExpectedKernelType( - const framework::ExecutionContext &ctx) const override { - auto input_data_type = framework::OperatorWithKernel::IndicateVarDataType( - ctx, framework::GradVarName("Out")); - return phi::KernelKey(input_data_type, ctx.GetPlace()); - } -}; - -class SqueezeOpMaker : public framework::OpProtoAndCheckerMaker { - public: - void Make() override { - AddInput("X", "(Tensor). The input tensor of squeeze operator."); - AddOutput("Out", "(Tensor). The output tensor of squeeze operator."); - AddAttr>("axes", - "(std::vector). List of integers," - " indicating the dimensions to squeeze.") - .SetDefault({}) - .SupportTensor(); - AddAttr("use_mkldnn", - "(bool, default false) Only used in mkldnn kernel") - .SetDefault(false) - .AsExtra(); - AddAttr( - "mkldnn_data_type", - "(string, default \"float32\"). Data type of mkldnn kernel") - .SetDefault("float32") - .InEnum({"float32", "bfloat16"}) - .AsExtra(); - AddComment(R"DOC( - Squeeze Operator. - - Remove single-dimensional entries from the shape of a tensor. - Takes a parameter axes with a list of axes to squeeze. - If axes is not provided, all the single dimensions will be removed from the shape. - If an axis is selected with shape entry not equal to one, an error is raised. - - Examples: - Case 1: - Given - X.shape = (1, 3, 1, 5) - and - axes = [0] - we get: - Out.shape = (3, 1, 5) - - Case 2: - Given - X.shape = (1, 3, 1, 5) - and - axes = [] - we get: - Out.shape = (3, 5) - )DOC"); - } -}; - -template -class SqueezeGradOpMaker : public framework::SingleGradOpMaker { - public: - using framework::SingleGradOpMaker::SingleGradOpMaker; - - void Apply(GradOpPtr grad_op) const override { - grad_op->SetType("squeeze_grad"); - grad_op->SetInput("X", this->Input("X")); - grad_op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out")); - grad_op->SetOutput(framework::GradVarName("X"), this->InputGrad("X")); - grad_op->SetAttrMap(this->Attrs()); - } -}; - -template -class SqueezeDoubleGradOpMaker : public framework::SingleGradOpMaker { - public: - using framework::SingleGradOpMaker::SingleGradOpMaker; - - void Apply(GradOpPtr grad_op) const override { - grad_op->SetType("squeeze"); - grad_op->SetInput("X", this->OutputGrad(framework::GradVarName("X"))); - grad_op->SetOutput("Out", this->InputGrad(framework::GradVarName("Out"))); - grad_op->SetAttrMap(this->Attrs()); - } -}; - -DECLARE_INPLACE_OP_INFERER(SqueezeInplaceInferer, {"X", "Out"}); -DECLARE_INPLACE_OP_INFERER(SqueezeGradInplaceInferer, - {framework::GradVarName("Out"), - framework::GradVarName("X")}); -DECLARE_NO_NEED_BUFFER_VARS_INFERER(SqueezeGradNoNeedBufferVarsInferer, "X"); -} // namespace operators -} // namespace paddle - -namespace ops = paddle::operators; - -REGISTER_OPERATOR(squeeze, - ops::SqueezeOp, - ops::SqueezeOpMaker, - ops::SqueezeGradOpMaker, - ops::SqueezeGradOpMaker); -REGISTER_OPERATOR(squeeze_grad, - ops::SqueezeGradOp, - ops::SqueezeDoubleGradOpMaker, - ops::SqueezeDoubleGradOpMaker, - ops::SqueezeGradNoNeedBufferVarsInferer); - -REGISTER_OP_CPU_KERNEL( - squeeze, - ops::SqueezeKernel, - ops::SqueezeKernel, - ops::SqueezeKernel, - ops::SqueezeKernel, - ops::SqueezeKernel, - ops::SqueezeKernel, - ops::SqueezeKernel, - ops::SqueezeKernel>, - ops::SqueezeKernel>, - ops::SqueezeKernel); -REGISTER_OP_CPU_KERNEL( - squeeze_grad, - ops::SqueezeGradKernel, - ops::SqueezeGradKernel, - ops::SqueezeGradKernel, - ops::SqueezeGradKernel, - ops::SqueezeGradKernel, - ops::SqueezeGradKernel, - ops::SqueezeGradKernel, - ops::SqueezeGradKernel>, - ops::SqueezeGradKernel>, - ops::SqueezeGradKernel); diff --git a/paddle/fluid/operators/squeeze_op.cu.cc b/paddle/fluid/operators/squeeze_op.cu.cc deleted file mode 100644 index a77b369c40373..0000000000000 --- a/paddle/fluid/operators/squeeze_op.cu.cc +++ /dev/null @@ -1,45 +0,0 @@ -/* Copyright (c) 2019 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/squeeze_op.h" - -namespace ops = paddle::operators; -namespace plat = paddle::platform; - -REGISTER_OP_CUDA_KERNEL( - squeeze, - ops::SqueezeKernel, - ops::SqueezeKernel, - ops::SqueezeKernel, - ops::SqueezeKernel, - ops::SqueezeKernel, - ops::SqueezeKernel, - ops::SqueezeKernel, - ops::SqueezeKernel, - ops::SqueezeKernel, - ops::SqueezeKernel>, - ops::SqueezeKernel>); -REGISTER_OP_CUDA_KERNEL( - squeeze_grad, - ops::SqueezeGradKernel, - ops::SqueezeGradKernel, - ops::SqueezeGradKernel, - ops::SqueezeGradKernel, - ops::SqueezeGradKernel, - ops::SqueezeGradKernel, - ops::SqueezeGradKernel, - ops::SqueezeGradKernel, - ops::SqueezeGradKernel, - ops::SqueezeGradKernel>, - ops::SqueezeGradKernel>); diff --git a/paddle/fluid/operators/squeeze_op.h b/paddle/fluid/operators/squeeze_op.h index 0c5b5dfd4c8b0..6f0da1d42e546 100644 --- a/paddle/fluid/operators/squeeze_op.h +++ b/paddle/fluid/operators/squeeze_op.h @@ -26,42 +26,67 @@ namespace operators { framework::DDim GetOutputShape(const std::vector squeeze_dims, const framework::DDim &in_dims, - bool is_runtime); - -template -class SqueezeKernel : public framework::OpKernel { - public: - void Compute(const framework::ExecutionContext &context) const override { - auto *in = context.Input("X"); - auto *out = context.Output("Out"); - - auto &axes = context.Attr>("axes"); - auto x_dims = in->dims(); - auto out_dims = GetOutputShape(axes, x_dims, true); - - out->mutable_data(context.GetPlace(), in->type()); - framework::TensorCopy( - *in, - context.GetPlace(), - context.template device_context(), - out); - out->Resize(out_dims); + bool is_runtime) { + size_t num_squeeze_dims = squeeze_dims.size(); + std::vector should_squeeze(in_dims.size(), false); + + // Mark dimensions need to be squeezed. + if (num_squeeze_dims == 0) { + for (int i = 0; i < in_dims.size(); ++i) { + if (in_dims[i] == 1) { + should_squeeze[i] = true; + } + } + } else { + for (size_t i = 0; i < num_squeeze_dims; ++i) { + int current = squeeze_dims[i] < 0 ? squeeze_dims[i] + in_dims.size() + : squeeze_dims[i]; + + PADDLE_ENFORCE_GE( + current, + 0, + platform::errors::InvalidArgument( + "Each axis in Attr(axes) should be in the range of [%d, %d]" + "But current axis is:%d, input tensor's shape = [%s].", + -in_dims.size(), + in_dims.size() - 1, + current, + in_dims)); + PADDLE_ENFORCE_LT( + current, + in_dims.size(), + platform::errors::InvalidArgument( + "Each axis in Attr(axes) should be in the range of [%d, %d]" + "But current axis is:%d, input tensor's shape = [%s].", + -in_dims.size(), + in_dims.size() - 1, + current, + in_dims)); + + if (!should_squeeze[current]) { + if (is_runtime) { + // At run time, dim of 1 is allowed to squeeze + if (in_dims[current] == 1) { + should_squeeze[current] = true; + } + } else { + // At compile time, dim of -1 or 1 is allowed to squeeze + if (in_dims[current] == 1 || in_dims[current] == -1) { + should_squeeze[current] = true; + } + } + } + } } -}; - -template -class SqueezeGradKernel : public framework::OpKernel { - public: - void Compute(const framework::ExecutionContext &ctx) const override { - auto *d_out = ctx.Input(framework::GradVarName("Out")); - auto *d_x = ctx.Output(framework::GradVarName("X")); - auto in_dims = ctx.Input("X")->dims(); - - d_x->mutable_data(ctx.GetPlace(), d_out->type()); - framework::TensorCopySync(*d_out, ctx.GetPlace(), d_x); - d_x->Resize(in_dims); + // Make output dimensions + std::vector output_shape; + for (int i = 0; i < in_dims.size(); ++i) { + if (!should_squeeze[i]) { + output_shape.push_back(in_dims[i]); + } } -}; + return phi::make_ddim(output_shape); +} template class Squeeze2Kernel : public framework::OpKernel { diff --git a/test/legacy_test/test_squeeze_op.py b/test/legacy_test/test_squeeze_op.py deleted file mode 100755 index 294a86db6dd04..0000000000000 --- a/test/legacy_test/test_squeeze_op.py +++ /dev/null @@ -1,307 +0,0 @@ -# Copyright (c) 2019 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 gradient_checker -import numpy as np -from decorator_helper import prog_scope -from op_test import OpTest, convert_float_to_uint16 - -import paddle -from paddle import base -from paddle.base import Program, core, program_guard - -paddle.enable_static() - - -# Correct: General. -class TestSqueezeOp(OpTest): - def setUp(self): - self.op_type = "squeeze" - self.init_test_case() - self.inputs = {"X": np.random.random(self.ori_shape).astype("float64")} - self.init_attrs() - self.outputs = { - "Out": self.inputs["X"].reshape(self.new_shape), - } - - def test_check_output(self): - self.check_output() - - def test_check_grad(self): - self.check_grad(["X"], "Out") - - def init_test_case(self): - self.ori_shape = (1, 3, 1, 40) - self.axes = (0, 2) - self.new_shape = (3, 40) - - def init_attrs(self): - self.attrs = {"axes": self.axes} - - -class TestSqueezeFP16Op(OpTest): - def setUp(self): - self.op_type = "squeeze" - self.init_test_case() - self.inputs = {"X": np.random.random(self.ori_shape).astype("float16")} - self.init_attrs() - self.outputs = { - "Out": self.inputs["X"].reshape(self.new_shape), - } - - def test_check_output(self): - self.check_output() - - def test_check_grad(self): - self.check_grad(["X"], "Out") - - def init_test_case(self): - self.ori_shape = (1, 3, 1, 40) - self.axes = (0, 2) - self.new_shape = (3, 40) - - def init_attrs(self): - self.attrs = {"axes": self.axes} - - -class TestSqueezeBF16Op(OpTest): - def setUp(self): - self.op_type = "squeeze" - self.dtype = np.uint16 - self.init_test_case() - x = np.random.random(self.ori_shape).astype("float32") - out = x.reshape(self.new_shape) - self.inputs = {"X": convert_float_to_uint16(x)} - self.init_attrs() - self.outputs = {"Out": convert_float_to_uint16(out)} - - def test_check_output(self): - self.check_output() - - def test_check_grad(self): - self.check_grad(["X"], "Out") - - def init_test_case(self): - self.ori_shape = (1, 3, 1, 40) - self.axes = (0, 2) - self.new_shape = (3, 40) - - def init_attrs(self): - self.attrs = {"axes": self.axes} - - -# Correct: There is mins axis. -class TestSqueezeOp1(TestSqueezeOp): - def init_test_case(self): - self.ori_shape = (1, 3, 1, 40) - self.axes = (0, -2) - self.new_shape = (3, 40) - - -# Correct: No axes input. -class TestSqueezeOp2(TestSqueezeOp): - def init_test_case(self): - self.ori_shape = (1, 20, 1, 5) - self.axes = () - self.new_shape = (20, 5) - - -# Correct: Just part of axes be squeezed. -class TestSqueezeOp3(TestSqueezeOp): - def init_test_case(self): - self.ori_shape = (6, 1, 5, 1, 4, 1) - self.axes = (1, -1) - self.new_shape = (6, 5, 1, 4) - - -# Correct: The demension of axis is not of size 1 remains unchanged. -class TestSqueezeOp4(TestSqueezeOp): - def init_test_case(self): - self.ori_shape = (6, 1, 5, 1, 4, 1) - self.axes = (1, 2) - self.new_shape = (6, 5, 1, 4, 1) - - -class TestSqueezeOpError(unittest.TestCase): - def test_errors(self): - paddle.enable_static() - with program_guard(Program(), Program()): - # The input type of softmax_op must be Variable. - x1 = base.create_lod_tensor( - np.array([[-1]]), [[1]], paddle.CPUPlace() - ) - self.assertRaises(TypeError, paddle.squeeze, x1) - # The input axes of squeeze must be list. - x2 = paddle.static.data(name='x2', shape=[4], dtype="int32") - self.assertRaises(TypeError, paddle.squeeze, x2, axes=0) - # The input dtype of squeeze not support float16. - x3 = paddle.static.data(name='x3', shape=[4], dtype="float16") - self.assertRaises(TypeError, paddle.squeeze, x3, axes=0) - - -class API_TestSqueeze(unittest.TestCase): - def setUp(self): - self.executed_api() - - def executed_api(self): - self.squeeze = paddle.squeeze - - def test_out(self): - paddle.enable_static() - with paddle.static.program_guard( - paddle.static.Program(), paddle.static.Program() - ): - data1 = paddle.static.data( - 'data1', shape=[-1, 1, 10], dtype='float64' - ) - result_squeeze = self.squeeze(data1, axis=[1]) - place = paddle.CPUPlace() - exe = paddle.static.Executor(place) - input1 = np.random.random([5, 1, 10]).astype('float64') - (result,) = exe.run( - feed={"data1": input1}, fetch_list=[result_squeeze] - ) - expected_result = np.squeeze(input1, axis=1) - np.testing.assert_allclose(expected_result, result, rtol=1e-05) - - -class API_TestStaticSqueeze_(API_TestSqueeze): - def executed_api(self): - self.squeeze = paddle.squeeze_ - - -class API_TestDygraphSqueeze(unittest.TestCase): - def setUp(self): - self.executed_api() - - def executed_api(self): - self.squeeze = paddle.squeeze - - def test_out(self): - paddle.disable_static() - input_1 = np.random.random([5, 1, 10]).astype("int32") - input = paddle.to_tensor(input_1) - output = self.squeeze(input, axis=[1]) - out_np = output.numpy() - expected_out = np.squeeze(input_1, axis=1) - np.testing.assert_allclose(expected_out, out_np, rtol=1e-05) - - def test_out_int8(self): - paddle.disable_static() - input_1 = np.random.random([5, 1, 10]).astype("int8") - input = paddle.to_tensor(input_1) - output = self.squeeze(input, axis=[1]) - out_np = output.numpy() - expected_out = np.squeeze(input_1, axis=1) - np.testing.assert_allclose(expected_out, out_np, rtol=1e-05) - - def test_out_uint8(self): - paddle.disable_static() - input_1 = np.random.random([5, 1, 10]).astype("uint8") - input = paddle.to_tensor(input_1) - output = self.squeeze(input, axis=[1]) - out_np = output.numpy() - expected_out = np.squeeze(input_1, axis=1) - np.testing.assert_allclose(expected_out, out_np, rtol=1e-05) - - def test_axis_not_list(self): - paddle.disable_static() - input_1 = np.random.random([5, 1, 10]).astype("int32") - input = paddle.to_tensor(input_1) - output = self.squeeze(input, axis=1) - out_np = output.numpy() - expected_out = np.squeeze(input_1, axis=1) - np.testing.assert_allclose(expected_out, out_np, rtol=1e-05) - - def test_dimension_not_1(self): - paddle.disable_static() - input_1 = np.random.random([5, 1, 10]).astype("int32") - input = paddle.to_tensor(input_1) - output = self.squeeze(input, axis=(1, 0)) - out_np = output.numpy() - expected_out = np.squeeze(input_1, axis=1) - np.testing.assert_allclose(expected_out, out_np, rtol=1e-05) - - -class API_TestDygraphSqueezeInplace(API_TestDygraphSqueeze): - def executed_api(self): - self.squeeze = paddle.squeeze_ - - -class TestSqueezeDoubleGradCheck(unittest.TestCase): - def squeeze_wrapper(self, x): - return paddle.squeeze(x[0]) - - @prog_scope() - def func(self, place): - # the shape of input variable should be clearly specified, not inlcude -1. - eps = 0.005 - dtype = np.float32 - - data = paddle.static.data('data', [2, 3], dtype) - data.persistable = True - out = paddle.squeeze(data) - data_arr = np.random.uniform(-1, 1, data.shape).astype(dtype) - - gradient_checker.double_grad_check( - [data], out, x_init=[data_arr], place=place, eps=eps - ) - gradient_checker.double_grad_check_for_dygraph( - self.squeeze_wrapper, [data], out, x_init=[data_arr], place=place - ) - - def test_grad(self): - paddle.enable_static() - places = [base.CPUPlace()] - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: - self.func(p) - - -class TestSqueezeTripleGradCheck(unittest.TestCase): - def squeeze_wrapper(self, x): - return paddle.squeeze(x[0]) - - @prog_scope() - def func(self, place): - # the shape of input variable should be clearly specified, not inlcude -1. - eps = 0.005 - dtype = np.float32 - - data = paddle.static.data('data', [2, 3], dtype) - data.persistable = True - out = paddle.squeeze(data) - data_arr = np.random.uniform(-1, 1, data.shape).astype(dtype) - - gradient_checker.triple_grad_check( - [data], out, x_init=[data_arr], place=place, eps=eps - ) - gradient_checker.triple_grad_check_for_dygraph( - self.squeeze_wrapper, [data], out, x_init=[data_arr], place=place - ) - - def test_grad(self): - paddle.enable_static() - places = [base.CPUPlace()] - if core.is_compiled_with_cuda(): - places.append(base.CUDAPlace(0)) - for p in places: - self.func(p) - - -if __name__ == "__main__": - unittest.main() diff --git a/test/xpu/test_squeeze_op_xpu.py b/test/xpu/test_squeeze_op_xpu.py deleted file mode 100644 index c5b9efce7a770..0000000000000 --- a/test/xpu/test_squeeze_op_xpu.py +++ /dev/null @@ -1,126 +0,0 @@ -# Copyright (c) 2022 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 get_test_cover_info import ( - XPUOpTestWrapper, - create_test_class, - get_xpu_op_support_types, -) -from op_test_xpu import XPUOpTest - -import paddle -from paddle import base -from paddle.base import Program, program_guard - -paddle.enable_static() - - -class XPUTestSqueezeOp(XPUOpTestWrapper): - def __init__(self): - self.op_name = "squeeze" - self.use_dynamic_create_class = False - - # Correct: General. - class TestSqueezeOp(XPUOpTest): - def setUp(self): - self.op_type = "squeeze" - self.__class__.op_type = "squeeze" - self.use_mkldnn = False - self.init_dtype() - self.init_test_case() - self.inputs = { - "X": np.random.random(self.ori_shape).astype(self.dtype) - } - self.init_attrs() - self.outputs = { - "Out": self.inputs["X"].reshape(self.new_shape), - } - - def init_dtype(self): - self.dtype = self.in_type - - def test_check_output(self): - place = paddle.XPUPlace(0) - self.check_output_with_place(place) - - def test_check_grad(self): - place = paddle.XPUPlace(0) - if self.dtype == np.bool_: - return - else: - self.check_grad_with_place(place, ['X'], 'Out') - - def init_test_case(self): - self.ori_shape = (1, 3, 1, 40) - self.axes = (0, 2) - self.new_shape = (3, 40) - - def init_attrs(self): - self.attrs = {"axes": self.axes} - - # Correct: There is mins axis. - class TestSqueezeOp1(TestSqueezeOp): - def init_test_case(self): - self.ori_shape = (1, 3, 1, 40) - self.axes = (0, -2) - self.new_shape = (3, 40) - - # Correct: No axes input. - class TestSqueezeOp2(TestSqueezeOp): - def init_test_case(self): - self.ori_shape = (1, 20, 1, 5) - self.axes = () - self.new_shape = (20, 5) - - # Correct: Just part of axes be squeezed. - class TestSqueezeOp3(TestSqueezeOp): - def init_test_case(self): - self.ori_shape = (6, 1, 5, 1, 4, 1) - self.axes = (1, -1) - self.new_shape = (6, 5, 1, 4) - - # Correct: The demension of axis is not of size 1 remains unchanged. - class TestSqueezeOp4(TestSqueezeOp): - def init_test_case(self): - self.ori_shape = (6, 1, 5, 1, 4, 1) - self.axes = (1, 2) - self.new_shape = (6, 5, 1, 4, 1) - - -class TestSqueezeOpError(unittest.TestCase): - def test_errors(self): - paddle.enable_static() - with program_guard(Program(), Program()): - # The input type of softmax_op must be Variable. - x1 = base.create_lod_tensor( - np.array([[-1]]), [[1]], paddle.XPUPlace(0) - ) - self.assertRaises(TypeError, paddle.squeeze, x1) - # The input axes of squeeze must be list. - x2 = paddle.static.data(name='x2', shape=[4], dtype="int32") - self.assertRaises(TypeError, paddle.squeeze, x2, axes=0) - # The input dtype of squeeze not support float16. - x3 = paddle.static.data(name='x3', shape=[4], dtype="float16") - self.assertRaises(TypeError, paddle.squeeze, x3, axes=0) - - -support_types = get_xpu_op_support_types("squeeze") -for stype in support_types: - create_test_class(globals(), XPUTestSqueezeOp, stype) - -if __name__ == "__main__": - unittest.main() diff --git a/tools/parallel_UT_rule.py b/tools/parallel_UT_rule.py index e7efc6cb9f1e5..cb715c64dd48a 100755 --- a/tools/parallel_UT_rule.py +++ b/tools/parallel_UT_rule.py @@ -1492,7 +1492,6 @@ 'test_yolov3_loss_op', 'test_decayed_adagrad_op', 'test_split_mkldnn_op', - 'test_squeeze_op', 'test_save_inference_model', 'test_smooth_l1_loss', 'test_bilateral_slice_op', @@ -2373,7 +2372,6 @@ 'test_atan2_op', 'test_tensor_fill_', 'test_std_layer', - 'test_squeeze_op', 'test_split_op', 'test_sign_op', 'test_set_value_op',