From 310f4320b705458131a2ef6f98a4b9acec57447b Mon Sep 17 00:00:00 2001 From: 201716010711 <87008376+201716010711@users.noreply.github.com> Date: Thu, 1 Dec 2022 14:33:45 +0800 Subject: [PATCH] clean fluid task: delete sum api (#48438) --- python/paddle/fluid/layers/nn.py | 73 ------------ python/paddle/fluid/optimizer.py | 4 +- .../tests/unittests/ipu/test_sum_op_ipu.py | 4 +- .../unittests/ir/test_ir_fusion_group_pass.py | 4 +- .../fluid/tests/unittests/test_layers.py | 2 +- .../tests/unittests/test_optimizer_grad.py | 2 +- .../test_paddle_fluid_modelaverage.py | 109 ++++++++++++++++++ .../fluid/tests/unittests/test_sum_op.py | 6 +- .../tests/unittests/xpu/test_sum_op_xpu.py | 6 +- python/paddle/hapi/model.py | 4 +- .../paddle/incubate/optimizer/modelaverage.py | 4 +- 11 files changed, 127 insertions(+), 91 deletions(-) create mode 100644 python/paddle/fluid/tests/unittests/test_paddle_fluid_modelaverage.py diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index 15eada61cf0..03059afb191 100644 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -104,7 +104,6 @@ __all__ = [ 'elementwise_mul', 'gaussian_random', 'sampling_id', - 'sum', 'shape', 'clip', 'clip_by_norm', @@ -5439,78 +5438,6 @@ def sampling_id(x, min=0.0, max=1.0, seed=0, dtype='float32'): return out -@templatedoc() -def sum(x): - """ - ${comment} - - Case 1: - :: - Input: - Input. Shape = [2, 3] - Input = [[1, 2, 3], - [4, 5, 6]] - - Output: - The output. Shape = [2, 3] - Output = [[1, 2, 3], - [4, 5, 6]] - - Case 2: - :: - Input: - First input: - Input1. Shape = [2, 3] - Input1 = [[1, 2, 3], - [4, 5, 6]] - - The second input: - Input2. Shape = [2, 3] - Input2 = [[7, 8, 9], - [10, 11, 12]] - - Output: - The output. Shape = [2, 3] - Output = [[8, 10, 12], - [14, 16, 18]] - - Args: - x (Variable|list(Variable)): ${x_comment} - - Returns: - Variable: ${out_comment} - - Examples: - .. code-block:: python - - import paddle.fluid as fluid - - input0 = fluid.layers.fill_constant(shape=[2, 3], dtype='int64', value=5) - input1 = fluid.layers.fill_constant(shape=[2, 3], dtype='int64', value=3) - sum = fluid.layers.sum([input0, input1]) - - # You can print out 'sum' via executor. - out = fluid.layers.Print(sum, message="the sum of input0 and input1: ") - exe = fluid.Executor(fluid.CPUPlace()) - exe.run(fluid.default_main_program()) - - # The printed result is: - # 1570701754 the sum of input0 and input1: The place is:CPUPlace - # Tensor[sum_0.tmp_0] - # shape: [2,3,] - # dtype: l - # data: 8,8,8,8,8,8, - - # the sum of input0 and input1 is 2-D Tensor with shape [2,3]. - # dtype is the corresponding C++ data type, which may vary in different environments. - # Eg: if the data type of tensor is int64, then the corresponding C++ data type is int64_t, - # so the dtype value is typeid(int64_t).Name(), which is 'x' on MacOS, 'l' on Linux, - # and '__int64' on Windows. They both represent 64-bit integer variables. - """ - - return paddle.add_n(x) - - def shape(input): """ :alias_main: paddle.shape diff --git a/python/paddle/fluid/optimizer.py b/python/paddle/fluid/optimizer.py index c7a817e1d75..8c9a940d846 100755 --- a/python/paddle/fluid/optimizer.py +++ b/python/paddle/fluid/optimizer.py @@ -3980,8 +3980,8 @@ class ModelAverage(Optimizer): # backup param value to grad layers.assign(input=param, output=grad) # param = (sum_1 + sum_2 + sum_3) / (num_accumulates + old_num_accumulates) - tmp = layers.sum(x=[num_accumulates, old_num_accumulates]) - sum = layers.sum(x=[sum_1, sum_2, sum_3]) + tmp = paddle.add_n([num_accumulates, old_num_accumulates]) + sum = paddle.add_n([sum_1, sum_2, sum_3]) tmp = layers.cast( x=tmp, dtype='float32' if self._dtype is None else self._dtype ) diff --git a/python/paddle/fluid/tests/unittests/ipu/test_sum_op_ipu.py b/python/paddle/fluid/tests/unittests/ipu/test_sum_op_ipu.py index c0bc022f057..91c7596a8f9 100644 --- a/python/paddle/fluid/tests/unittests/ipu/test_sum_op_ipu.py +++ b/python/paddle/fluid/tests/unittests/ipu/test_sum_op_ipu.py @@ -51,7 +51,7 @@ class TestBase(IPUOpTest): y = paddle.static.data( name=self.feed_list[1], shape=self.feed_shape[1], dtype='float32' ) - out = paddle.fluid.layers.sum([x, y], **self.attrs) + out = paddle.add_n([x, y], **self.attrs) self.fetch_list = [out.name] def run_model(self, exec_mode): @@ -92,7 +92,7 @@ class TestCase1(TestBase): z = paddle.static.data( name=self.feed_list[2], shape=self.feed_shape[2], dtype='float32' ) - out = paddle.fluid.layers.sum([x, y, z], **self.attrs) + out = paddle.add_n([x, y, z], **self.attrs) self.fetch_list = [out.name] diff --git a/python/paddle/fluid/tests/unittests/ir/test_ir_fusion_group_pass.py b/python/paddle/fluid/tests/unittests/ir/test_ir_fusion_group_pass.py index 19754fd6f2d..1538bac16ff 100644 --- a/python/paddle/fluid/tests/unittests/ir/test_ir_fusion_group_pass.py +++ b/python/paddle/fluid/tests/unittests/ir/test_ir_fusion_group_pass.py @@ -165,13 +165,13 @@ class FusionGroupPassSumTest(FusionGroupPassTest): ) # subgraph with 2 op nodes - tmp_0 = layers.sum( + tmp_0 = paddle.add_n( [self.feed_vars[0], self.feed_vars[1], self.feed_vars[2]] ) tmp_1 = paddle.sqrt(tmp_0) tmp_2 = layers.mul(tmp_0, self.feed_vars[3]) # subgraph with 2 op nodes - tmp_3 = paddle.square(layers.sum([tmp_1, tmp_2])) + tmp_3 = paddle.square(paddle.add_n([tmp_1, tmp_2])) self.append_gradients(tmp_3) diff --git a/python/paddle/fluid/tests/unittests/test_layers.py b/python/paddle/fluid/tests/unittests/test_layers.py index eaf7acaba59..7750f6b613b 100644 --- a/python/paddle/fluid/tests/unittests/test_layers.py +++ b/python/paddle/fluid/tests/unittests/test_layers.py @@ -3534,7 +3534,7 @@ class TestBook(LayerTest): name="input", shape=[13, 11], dtype='float32' ) - out = layers.sum(input) + out = paddle.add_n(input) return out def make_slice(self): diff --git a/python/paddle/fluid/tests/unittests/test_optimizer_grad.py b/python/paddle/fluid/tests/unittests/test_optimizer_grad.py index d5e5a7a200c..acdc43659d8 100644 --- a/python/paddle/fluid/tests/unittests/test_optimizer_grad.py +++ b/python/paddle/fluid/tests/unittests/test_optimizer_grad.py @@ -122,7 +122,7 @@ class SimpleNetWithCond: cond_i = fluid.layers.assign(np.array([cond_i], dtype='float32')) sum_cond = fluid.layers.cond(cond_i > 1.0, cond_true, cond_false) - sum_all = fluid.layers.sum([sum_xy, sub_yz, sum_cond]) + sum_all = paddle.add_n([sum_xy, sub_yz, sum_cond]) mean_out = paddle.mean(sum_all) if use_bf16: import paddle.static.amp as amp diff --git a/python/paddle/fluid/tests/unittests/test_paddle_fluid_modelaverage.py b/python/paddle/fluid/tests/unittests/test_paddle_fluid_modelaverage.py new file mode 100644 index 00000000000..7be33d31fd4 --- /dev/null +++ b/python/paddle/fluid/tests/unittests/test_paddle_fluid_modelaverage.py @@ -0,0 +1,109 @@ +# Copyright (c) 2020 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 +import paddle.fluid as fluid +import paddle + + +class TestModelAverage(unittest.TestCase): + def test_model_average_static(self): + paddle.enable_static() + place = fluid.CPUPlace() + shape = [2, 3, 8, 8] + exe = fluid.Executor(place) + train_program = fluid.Program() + startup = fluid.Program() + test_program = fluid.Program() + with fluid.program_guard(train_program, startup): + with fluid.unique_name.guard(): + data = fluid.data(name='X', shape=[None, 1], dtype='float32') + hidden = fluid.layers.fc(input=data, size=10) + loss = paddle.mean(hidden) + test_program = train_program.clone() + optimizer = paddle.optimizer.Momentum( + learning_rate=0.2, momentum=0.1 + ) + + optimizer.minimize(loss) + # build ModelAverage optimizer + model_average = paddle.fluid.optimizer.ModelAverage( + 0.15, min_average_window=2, max_average_window=10 + ) + + exe.run(startup) + for i in range(10): + x = np.random.random(size=(10, 1)).astype('float32') + ( + latest_b, + sum_1, + sum_2, + sum_3, + num_accumulates, + old_num_accumulates, + num_updates, + ) = exe.run( + program=train_program, + feed={'X': x}, + fetch_list=[ + 'fc_0.b_0', + 'fc_0.b_0_sum_1_0', + 'fc_0.b_0_sum_2_0', + 'fc_0.b_0_sum_3_0', + 'fc_0.b_0_num_accumulates_0', + 'fc_0.b_0_old_num_accumulates_0', + 'fc_0.b_0_num_updates_0', + ], + ) + self.assertTrue( + np.equal(sum_1, np.zeros(shape=[10], dtype='float32')).all() + ) + self.assertTrue( + np.equal(sum_2, np.zeros(shape=[10], dtype='float32')).all() + ) + self.assertTrue( + np.equal(num_accumulates, np.array([0], dtype='int64')).all() + ) + self.assertTrue( + np.equal(old_num_accumulates, np.array([2], dtype='int64')).all() + ) + self.assertTrue( + np.equal(num_updates, np.array([10], dtype='int64')).all() + ) + + average_b = (sum_1 + sum_2 + sum_3) / ( + num_accumulates + old_num_accumulates + ) + # apply ModelAverage + with model_average.apply(exe): + x = np.random.random(size=(10, 1)).astype('float32') + outs, b = exe.run( + program=test_program, + feed={'X': x}, + fetch_list=[loss.name, 'fc_0.b_0'], + ) + self.assertAlmostEqual(np.mean(average_b), np.mean(b)) + + x = np.random.random(size=(10, 1)).astype('float32') + outs, b = exe.run( + program=test_program, + feed={'X': x}, + fetch_list=[loss.name, 'fc_0.b_0'], + ) + self.assertAlmostEqual(np.mean(latest_b), np.mean(b)) + + +if __name__ == "__main__": + unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_sum_op.py b/python/paddle/fluid/tests/unittests/test_sum_op.py index 68fdfcb9908..9626a872e2f 100644 --- a/python/paddle/fluid/tests/unittests/test_sum_op.py +++ b/python/paddle/fluid/tests/unittests/test_sum_op.py @@ -426,20 +426,20 @@ class API_Test_Add_n(unittest.TestCase): class TestRaiseSumError(unittest.TestCase): def test_errors(self): def test_type(): - fluid.layers.sum([11, 22]) + paddle.add_n([11, 22]) self.assertRaises(TypeError, test_type) def test_dtype(): data1 = fluid.data(name="input1", shape=[10], dtype="int8") data2 = fluid.data(name="input2", shape=[10], dtype="int8") - fluid.layers.sum([data1, data2]) + paddle.add_n([data1, data2]) self.assertRaises(TypeError, test_dtype) def test_dtype1(): data1 = fluid.data(name="input1", shape=[10], dtype="int8") - fluid.layers.sum(data1) + paddle.add_n(data1) self.assertRaises(TypeError, test_dtype1) diff --git a/python/paddle/fluid/tests/unittests/xpu/test_sum_op_xpu.py b/python/paddle/fluid/tests/unittests/xpu/test_sum_op_xpu.py index ec615324bcc..b400bd12d3f 100644 --- a/python/paddle/fluid/tests/unittests/xpu/test_sum_op_xpu.py +++ b/python/paddle/fluid/tests/unittests/xpu/test_sum_op_xpu.py @@ -130,20 +130,20 @@ class API_Test_Add_n(unittest.TestCase): class TestRaiseSumError(unittest.TestCase): def test_errors(self): def test_type(): - fluid.layers.sum([11, 22]) + paddle.add_n([11, 22]) self.assertRaises(TypeError, test_type) def test_dtype(): data1 = fluid.data(name="input1", shape=[10], dtype="int8") data2 = fluid.data(name="input2", shape=[10], dtype="int8") - fluid.layers.sum([data1, data2]) + paddle.add_n([data1, data2]) self.assertRaises(TypeError, test_dtype) def test_dtype1(): data1 = fluid.data(name="input1", shape=[10], dtype="int8") - fluid.layers.sum(data1) + paddle.add_n(data1) self.assertRaises(TypeError, test_dtype1) diff --git a/python/paddle/hapi/model.py b/python/paddle/hapi/model.py index 025abd9acc9..116f433c8f0 100644 --- a/python/paddle/hapi/model.py +++ b/python/paddle/hapi/model.py @@ -637,7 +637,7 @@ class StaticGraphAdapter: metrics.append(to_list(metric.compute(*(outputs + labels)))) if mode == 'train' and self.model._optimizer: - self._loss_endpoint = fluid.layers.sum(losses) + self._loss_endpoint = paddle.add_n(losses) if self._nranks > 1: role = role_maker.PaddleCloudRoleMaker(is_collective=True) fleet.init(role) @@ -795,7 +795,7 @@ class DynamicGraphAdapter: losses = self.model._loss(*(to_list(outputs) + labels)) losses = to_list(losses) - final_loss = fluid.layers.sum(losses) + final_loss = paddle.add_n(losses) if self._amp_level != "O0": scaled = self.model._scaler.scale(final_loss) diff --git a/python/paddle/incubate/optimizer/modelaverage.py b/python/paddle/incubate/optimizer/modelaverage.py index 21b17657340..52bf1ac4f34 100644 --- a/python/paddle/incubate/optimizer/modelaverage.py +++ b/python/paddle/incubate/optimizer/modelaverage.py @@ -548,8 +548,8 @@ class ModelAverage(Optimizer): # backup param value to grad layers.assign(input=param, output=grad) # param = (sum_1 + sum_2 + sum_3) / (num_accumulates + old_num_accumulates) - tmp = layers.sum(x=[num_accumulates, old_num_accumulates]) - sum = layers.sum(x=[sum_1, sum_2, sum_3]) + tmp = paddle.add_n([num_accumulates, old_num_accumulates]) + sum = paddle.add_n([sum_1, sum_2, sum_3]) tmp = layers.cast( x=tmp, dtype='float32' if self._dtype is None else self._dtype ) -- GitLab