From dab1896d487967d2557b05cf3e089250b4ce12a8 Mon Sep 17 00:00:00 2001 From: HongyuJia Date: Thu, 1 Dec 2022 23:59:51 +0800 Subject: [PATCH] clean elem_arithmetic part4 unittest (#48465) --- .../tests/unittests/ipu/test_assign_op_ipu.py | 4 ++-- .../tests/unittests/ipu/test_dropout_op_ipu.py | 2 +- .../unittests/ipu/test_elemetwise_x_op_ipu.py | 6 +++--- .../unittests/ipu/test_fill_any_like_op_ipu.py | 4 ++-- .../unittests/ipu/test_fill_constant_op_ipu.py | 2 +- .../unittests/ipu/test_reshape_inplace_op_ipu.py | 2 +- .../tests/unittests/ipu/test_share_data_op_ipu.py | 4 ++-- .../unittests/ipu/test_varname_inplace_ipu.py | 4 ++-- .../paddle/fluid/tests/unittests/ps_dnn_model.py | 4 +--- .../paddle/fluid/tests/unittests/seresnext_net.py | 2 +- .../fluid/tests/unittests/test_argsort_op.py | 2 +- .../paddle/fluid/tests/unittests/test_assign_op.py | 4 ++-- .../paddle/fluid/tests/unittests/test_backward.py | 6 ++---- python/paddle/fluid/tests/unittests/test_case.py | 4 ++-- .../fluid/tests/unittests/test_complex_variable.py | 2 +- python/paddle/fluid/tests/unittests/test_cond.py | 14 +++++++------- .../paddle/fluid/tests/unittests/test_dataset.py | 12 +++--------- .../test_executor_return_tensor_not_overwriting.py | 4 ++-- 18 files changed, 36 insertions(+), 46 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/ipu/test_assign_op_ipu.py b/python/paddle/fluid/tests/unittests/ipu/test_assign_op_ipu.py index 324bac452c..42b817da6f 100644 --- a/python/paddle/fluid/tests/unittests/ipu/test_assign_op_ipu.py +++ b/python/paddle/fluid/tests/unittests/ipu/test_assign_op_ipu.py @@ -43,7 +43,7 @@ class TestBase(IPUOpTest): name=self.feed_list[0], shape=self.feed_shape[0], dtype='float32' ) x = paddle.assign(x) - out = paddle.fluid.layers.elementwise_add(x, x) + out = paddle.add(x, x) self.fetch_list = [out.name] def run_model(self, exec_mode): @@ -72,7 +72,7 @@ class TestAssignFp32Value(TestBase): name=self.feed_list[0], shape=self.feed_shape[0], dtype='float32' ) assign = paddle.assign(self.assign_fp32) - out = paddle.fluid.layers.elementwise_add(x, assign) + out = paddle.add(x, assign) self.fetch_list = [out.name] diff --git a/python/paddle/fluid/tests/unittests/ipu/test_dropout_op_ipu.py b/python/paddle/fluid/tests/unittests/ipu/test_dropout_op_ipu.py index 3c1dc5c07e..ad560f36a2 100644 --- a/python/paddle/fluid/tests/unittests/ipu/test_dropout_op_ipu.py +++ b/python/paddle/fluid/tests/unittests/ipu/test_dropout_op_ipu.py @@ -51,7 +51,7 @@ class TestBase(IPUOpTest): name=self.feed_list[0], shape=self.feed_shape[0], dtype='float32' ) x = paddle.fluid.layers.dropout(x, **self.attrs) - out = paddle.fluid.layers.elementwise_add(x, x) + out = paddle.add(x, x) self.fetch_list = [out.name] def run_model(self, exec_mode): diff --git a/python/paddle/fluid/tests/unittests/ipu/test_elemetwise_x_op_ipu.py b/python/paddle/fluid/tests/unittests/ipu/test_elemetwise_x_op_ipu.py index 908538a9d6..62d0376e58 100644 --- a/python/paddle/fluid/tests/unittests/ipu/test_elemetwise_x_op_ipu.py +++ b/python/paddle/fluid/tests/unittests/ipu/test_elemetwise_x_op_ipu.py @@ -126,17 +126,17 @@ class TestMul(IPUOpTest): class TestAdd(TestMul): def set_test_op(self): - self.op = paddle.fluid.layers.elementwise_add + self.op = paddle.add class TestSub(TestMul): def set_test_op(self): - self.op = paddle.fluid.layers.elementwise_sub + self.op = paddle.subtract class TestDiv(TestMul): def set_test_op(self): - self.op = paddle.fluid.layers.elementwise_div + self.op = paddle.divide class TestMin(TestMul): diff --git a/python/paddle/fluid/tests/unittests/ipu/test_fill_any_like_op_ipu.py b/python/paddle/fluid/tests/unittests/ipu/test_fill_any_like_op_ipu.py index a5df129993..14a8d69a8e 100644 --- a/python/paddle/fluid/tests/unittests/ipu/test_fill_any_like_op_ipu.py +++ b/python/paddle/fluid/tests/unittests/ipu/test_fill_any_like_op_ipu.py @@ -47,7 +47,7 @@ class TestBase(IPUOpTest): name=self.feed_list[0], shape=self.feed_shape[0], dtype='float32' ) x_fill = paddle.full_like(x, **self.attrs) - out = paddle.fluid.layers.elementwise_add(x_fill, x_fill) + out = paddle.add(x_fill, x_fill) self.fetch_list = [out.name] def run_model(self, exec_mode): @@ -71,7 +71,7 @@ class TestError(TestBase): def build_model(self): x = paddle.fluid.data('x', [-1, 3, 13], 'float32') x_fill = paddle.full_like(x, **self.attrs) - out = paddle.fluid.layers.elementwise_add(x_fill, x_fill) + out = paddle.add(x_fill, x_fill) self.fetch_list = [out.name] def test(self): diff --git a/python/paddle/fluid/tests/unittests/ipu/test_fill_constant_op_ipu.py b/python/paddle/fluid/tests/unittests/ipu/test_fill_constant_op_ipu.py index 71dfb2d409..f1e2c79cc6 100644 --- a/python/paddle/fluid/tests/unittests/ipu/test_fill_constant_op_ipu.py +++ b/python/paddle/fluid/tests/unittests/ipu/test_fill_constant_op_ipu.py @@ -47,7 +47,7 @@ class TestBase(IPUOpTest): @IPUOpTest.static_graph def build_model(self): x = paddle.fluid.layers.fill_constant(**self.attrs) - out = paddle.fluid.layers.elementwise_add(x, x) + out = paddle.add(x, x) self.fetch_list = [out.name] def run_model(self, exec_mode): diff --git a/python/paddle/fluid/tests/unittests/ipu/test_reshape_inplace_op_ipu.py b/python/paddle/fluid/tests/unittests/ipu/test_reshape_inplace_op_ipu.py index c12b8919d3..223e821fac 100644 --- a/python/paddle/fluid/tests/unittests/ipu/test_reshape_inplace_op_ipu.py +++ b/python/paddle/fluid/tests/unittests/ipu/test_reshape_inplace_op_ipu.py @@ -50,7 +50,7 @@ class TestBase(IPUOpTest): x = paddle.static.data( name=self.feed_list[0], shape=self.feed_shape[0], dtype='float32' ) - add = paddle.fluid.layers.elementwise_add(x, x) + add = paddle.add(x, x) out = paddle.reshape(add, **self.attrs) self.fetch_list = [out.name] diff --git a/python/paddle/fluid/tests/unittests/ipu/test_share_data_op_ipu.py b/python/paddle/fluid/tests/unittests/ipu/test_share_data_op_ipu.py index 94e2fd353b..132da198f6 100644 --- a/python/paddle/fluid/tests/unittests/ipu/test_share_data_op_ipu.py +++ b/python/paddle/fluid/tests/unittests/ipu/test_share_data_op_ipu.py @@ -48,7 +48,7 @@ class TestBase(IPUOpTest): self.main_prog.global_block().append_op( type="share_data", inputs={"X": x}, outputs={'Out': y} ) - out = paddle.fluid.layers.elementwise_add(y, y) + out = paddle.add(y, y) self.fetch_list = [out.name] def run_model(self, exec_mode): @@ -82,7 +82,7 @@ class TestCase1(TestBase): self.main_prog.global_block().append_op( type="share_data", inputs={"X": x}, outputs={'Out': y} ) - out = paddle.fluid.layers.elementwise_add(x, y) + out = paddle.add(x, y) self.fetch_list = [out.name] diff --git a/python/paddle/fluid/tests/unittests/ipu/test_varname_inplace_ipu.py b/python/paddle/fluid/tests/unittests/ipu/test_varname_inplace_ipu.py index 61b38e80e7..0f956e16d4 100644 --- a/python/paddle/fluid/tests/unittests/ipu/test_varname_inplace_ipu.py +++ b/python/paddle/fluid/tests/unittests/ipu/test_varname_inplace_ipu.py @@ -59,9 +59,9 @@ class TestBase(IPUOpTest): shape=self.feed_shape[0], dtype=self.feed_dtype[0], ) - add1 = paddle.fluid.layers.elementwise_add(x, x) + add1 = paddle.add(x, x) reshape = paddle.reshape(add1, **self.attrs) - add2 = paddle.fluid.layers.elementwise_add(reshape, reshape) + add2 = paddle.add(reshape, reshape) scale1 = paddle.scale(add2) scale2 = paddle.scale(scale1, scale=1.3, bias=0.5) scale3 = paddle.scale(scale2, scale=2, bias=0.7) diff --git a/python/paddle/fluid/tests/unittests/ps_dnn_model.py b/python/paddle/fluid/tests/unittests/ps_dnn_model.py index 9cb61f24a0..c5c29e2299 100755 --- a/python/paddle/fluid/tests/unittests/ps_dnn_model.py +++ b/python/paddle/fluid/tests/unittests/ps_dnn_model.py @@ -230,9 +230,7 @@ class FlDNNLayer(nn.Layer): with paddle.fluid.device_guard( self.PART_B_JOINT_OP_DEVICE_FlAG ): # joint point - interactive = paddle.fluid.layers.elementwise_add( - bottom_a, bottom_b - ) + interactive = paddle.add(bottom_a, bottom_b) return interactive def top_layer(self, interactive, label_input): diff --git a/python/paddle/fluid/tests/unittests/seresnext_net.py b/python/paddle/fluid/tests/unittests/seresnext_net.py index 7d96ea40ef..146cd58bcf 100644 --- a/python/paddle/fluid/tests/unittests/seresnext_net.py +++ b/python/paddle/fluid/tests/unittests/seresnext_net.py @@ -121,7 +121,7 @@ def bottleneck_block(input, num_filters, stride, cardinality, reduction_ratio): short = shortcut(input, num_filters * 2, stride) - return fluid.layers.elementwise_add(x=short, y=scale, act='relu') + return paddle.nn.functional.relu(paddle.add(x=short, y=scale)) img_shape = [3, 224, 224] diff --git a/python/paddle/fluid/tests/unittests/test_argsort_op.py b/python/paddle/fluid/tests/unittests/test_argsort_op.py index f939462a64..87ab960467 100644 --- a/python/paddle/fluid/tests/unittests/test_argsort_op.py +++ b/python/paddle/fluid/tests/unittests/test_argsort_op.py @@ -95,7 +95,7 @@ class TestArgsortOpCPU(unittest.TestCase): input=x, axis=self.axis, descending=self.descending ) self.sorted_x.stop_gradient = False - loss = fluid.layers.elementwise_mul(self.sorted_x, label) + loss = paddle.multiply(self.sorted_x, label) self.loss = paddle.sum(loss) def forward(self): diff --git a/python/paddle/fluid/tests/unittests/test_assign_op.py b/python/paddle/fluid/tests/unittests/test_assign_op.py index 4c5921a5f2..e2325733d1 100644 --- a/python/paddle/fluid/tests/unittests/test_assign_op.py +++ b/python/paddle/fluid/tests/unittests/test_assign_op.py @@ -90,7 +90,7 @@ class TestAssignOpWithLoDTensorArray(unittest.TestCase): y = fluid.layers.fill_constant( shape=[100, 10], dtype='float32', value=1 ) - z = fluid.layers.elementwise_add(x=x, y=y) + z = paddle.add(x=x, y=y) i = fluid.layers.fill_constant(shape=[1], dtype='int64', value=0) init_array = fluid.layers.array_write(x=z, i=i) array = fluid.layers.assign(init_array) @@ -144,7 +144,7 @@ class TestAssignOApi(unittest.TestCase): y = fluid.layers.fill_constant( shape=[100, 10], dtype='float32', value=1 ) - z = fluid.layers.elementwise_add(x=x, y=y) + z = paddle.add(x=x, y=y) i = fluid.layers.fill_constant(shape=[1], dtype='int64', value=0) init_array = fluid.layers.array_write(x=z, i=i) array = paddle.assign(init_array) diff --git a/python/paddle/fluid/tests/unittests/test_backward.py b/python/paddle/fluid/tests/unittests/test_backward.py index 9ba1cf884a..c98fd7dff5 100644 --- a/python/paddle/fluid/tests/unittests/test_backward.py +++ b/python/paddle/fluid/tests/unittests/test_backward.py @@ -243,10 +243,8 @@ class SimpleNet(BackwardNet): x3, size=[100, 64], param_attr=fluid.ParamAttr(name='w2v') ) # merge layers - x_merge = fluid.layers.elementwise_add(x_emb, x2_emb, name='x_add_x2') - x2_merge = fluid.layers.elementwise_add( - x2_emb, x3_emb, name='x2_add_x3' - ) + x_merge = paddle.add(x_emb, x2_emb, name='x_add_x2') + x2_merge = paddle.add(x2_emb, x3_emb, name='x2_add_x3') # shared fc_w predict = fluid.layers.fc( input=x_merge, diff --git a/python/paddle/fluid/tests/unittests/test_case.py b/python/paddle/fluid/tests/unittests/test_case.py index 7008544d17..777db3a348 100644 --- a/python/paddle/fluid/tests/unittests/test_case.py +++ b/python/paddle/fluid/tests/unittests/test_case.py @@ -299,12 +299,12 @@ class TestMutiTask(unittest.TestCase): adagrad = optimizer.Adagrad(learning_rate=0.001) def fn_1(): - sum = layers.elementwise_mul(x, y) + sum = paddle.multiply(x, y) loss = paddle.mean(sum, name="f_1_loss") adam.minimize(loss) def fn_2(): - sum = layers.elementwise_mul(x, y) + sum = paddle.multiply(x, y) loss = paddle.mean(sum, name="f_2_loss") adagrad.minimize(loss) diff --git a/python/paddle/fluid/tests/unittests/test_complex_variable.py b/python/paddle/fluid/tests/unittests/test_complex_variable.py index 588f87b3b2..57774ac5f0 100644 --- a/python/paddle/fluid/tests/unittests/test_complex_variable.py +++ b/python/paddle/fluid/tests/unittests/test_complex_variable.py @@ -33,7 +33,7 @@ class TestComplexVariable(unittest.TestCase): with dg.guard(): x = dg.to_variable(a, "x") y = dg.to_variable(b) - out = paddle.fluid.layers.elementwise_add(x, y) + out = paddle.add(x, y) self.assertIsNotNone("{}".format(out)) np.testing.assert_allclose(out.numpy(), a + b, rtol=1e-05) diff --git a/python/paddle/fluid/tests/unittests/test_cond.py b/python/paddle/fluid/tests/unittests/test_cond.py index 41bfcff736..d31ac885b1 100644 --- a/python/paddle/fluid/tests/unittests/test_cond.py +++ b/python/paddle/fluid/tests/unittests/test_cond.py @@ -310,15 +310,15 @@ class TestCondNestedControlFlow(unittest.TestCase): def less_than_branch(i, a): return layers.cond( i >= 3.0, - lambda: layers.elementwise_add(a, a), - lambda: layers.elementwise_sub(a, a), + lambda: paddle.add(a, a), + lambda: paddle.subtract(a, a), ) def greater_equal_branch(i, a): return layers.cond( i < 8.0, - lambda: layers.elementwise_mul(a, a), - lambda: layers.elementwise_div(a, a), + lambda: paddle.multiply(a, a), + lambda: paddle.divide(a, a), ) main_program = Program() @@ -374,12 +374,12 @@ class TestCondNestedControlFlow(unittest.TestCase): a < b, lambda: fluid.layers.cond( a - b < -1.0, - lambda: fluid.layers.elementwise_add(a, b), - lambda: fluid.layers.elementwise_mul(a, b), + lambda: paddle.add(a, b), + lambda: paddle.multiply(a, b), ), lambda: fluid.layers.cond( a == b, - lambda: fluid.layers.elementwise_sub(a, b), + lambda: paddle.subtract(a, b), lambda: paddle.pow(a, b), ), ) diff --git a/python/paddle/fluid/tests/unittests/test_dataset.py b/python/paddle/fluid/tests/unittests/test_dataset.py index 95e252c12f..ab126c4378 100644 --- a/python/paddle/fluid/tests/unittests/test_dataset.py +++ b/python/paddle/fluid/tests/unittests/test_dataset.py @@ -1029,9 +1029,7 @@ class TestDataset2(unittest.TestCase): name=slot, shape=[1], dtype="float32", lod_level=1 ) slots_vars.append(var) - fake_cost = fluid.layers.elementwise_sub( - slots_vars[0], slots_vars[-1] - ) + fake_cost = paddle.subtract(slots_vars[0], slots_vars[-1]) fake_cost = paddle.mean(fake_cost) with fluid.scope_guard(scope): place = fluid.CPUPlace() @@ -1101,9 +1099,7 @@ class TestDataset2(unittest.TestCase): name=slot, shape=[1], dtype="float32", lod_level=1 ) slots_vars.append(var) - fake_cost = fluid.layers.elementwise_sub( - slots_vars[0], slots_vars[-1] - ) + fake_cost = paddle.subtract(slots_vars[0], slots_vars[-1]) fake_cost = paddle.mean(fake_cost) with fluid.scope_guard(scope): place = fluid.CPUPlace() @@ -1234,9 +1230,7 @@ class TestDataset2(unittest.TestCase): name=slot, shape=[1], dtype="float32", lod_level=1 ) slots_vars.append(var) - fake_cost = fluid.layers.elementwise_sub( - slots_vars[0], slots_vars[-1] - ) + fake_cost = paddle.subtract(slots_vars[0], slots_vars[-1]) fake_cost = paddle.mean(fake_cost) with fluid.scope_guard(scope): place = fluid.CPUPlace() diff --git a/python/paddle/fluid/tests/unittests/test_executor_return_tensor_not_overwriting.py b/python/paddle/fluid/tests/unittests/test_executor_return_tensor_not_overwriting.py index 954d47287b..5e8179886e 100644 --- a/python/paddle/fluid/tests/unittests/test_executor_return_tensor_not_overwriting.py +++ b/python/paddle/fluid/tests/unittests/test_executor_return_tensor_not_overwriting.py @@ -75,7 +75,7 @@ class TestExecutorReturnTensorNotOverOverwritingWithLayers(unittest.TestCase): def calc_add_out(self, place=None, parallel=None): x = paddle.ones(shape=[3, 3], dtype='float32') y = paddle.ones(shape=[3, 3], dtype='float32') - out = fluid.layers.elementwise_add(x=x, y=y) + out = paddle.add(x=x, y=y) program = fluid.default_main_program() if parallel: program = fluid.CompiledProgram(program).with_data_parallel( @@ -88,7 +88,7 @@ class TestExecutorReturnTensorNotOverOverwritingWithLayers(unittest.TestCase): def calc_sub_out(self, place=None, parallel=None): x = paddle.ones(shape=[2, 2], dtype='float32') y = paddle.ones(shape=[2, 2], dtype='float32') - out = fluid.layers.elementwise_sub(x=x, y=y) + out = paddle.subtract(x=x, y=y) program = fluid.default_main_program() if parallel: program = fluid.CompiledProgram(program).with_data_parallel( -- GitLab