From c6a2b0fd92c8c6760bfcceabb477036f1be1b887 Mon Sep 17 00:00:00 2001 From: 201716010711 <87008376+201716010711@users.noreply.github.com> Date: Wed, 7 Dec 2022 11:09:10 +0800 Subject: [PATCH] delete sampling_id api (#48543) --- python/paddle/fluid/layers/nn.py | 40 ---------- python/paddle/fluid/layers/rnn.py | 34 -------- .../unittests/npu/test_sampling_id_op_npu.py | 52 ------------- .../fluid/tests/unittests/test_layers.py | 15 ---- .../fluid/tests/unittests/test_random_seed.py | 77 ------------------- .../tests/unittests/test_sampling_id_op.py | 45 ----------- 6 files changed, 263 deletions(-) delete mode 100644 python/paddle/fluid/tests/unittests/npu/test_sampling_id_op_npu.py delete mode 100644 python/paddle/fluid/tests/unittests/test_sampling_id_op.py diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index 39d4d678ab..474bccc162 100644 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -86,7 +86,6 @@ __all__ = [ 'elementwise_sub', 'elementwise_mul', 'gaussian_random', - 'sampling_id', 'clip', 'clip_by_norm', 'mean', @@ -3190,45 +3189,6 @@ def gaussian_random( return out -@templatedoc() -def sampling_id(x, min=0.0, max=1.0, seed=0, dtype='float32'): - """ - This op is used for sampling id from multinomial distribution from the input, sampling one id for one sample. - - Parameters: - x (Variable): 2-D tensor, [batch_size, input_feature_dimensions] - min (Float): minimum , default 0.0. - max (Float): maximum, default 1.0. - seed (Float): Random seed, default 0. if seed is not 0, will generate same number every time. - dtype(np.dtype|core.VarDesc.VarType|str): The type of output data : float32, float_16, int etc - - Returns: - Variable: sampling tensor. - - Examples: - .. code-block:: python - - import paddle.fluid as fluid - x = fluid.data( - name="X", - shape=[13, 11], - dtype='float32') - - out = fluid.layers.sampling_id(x) - """ - - helper = LayerHelper('sampling_id', **locals()) - out = helper.create_variable_for_type_inference(dtype) - helper.append_op( - type='sampling_id', - inputs={'X': x}, - outputs={'Out': out}, - attrs={'min': min, 'max': max, 'seed': seed}, - ) - - return out - - def _elementwise_op(helper): op_type = helper.layer_type x = helper.kwargs.get('x', None) diff --git a/python/paddle/fluid/layers/rnn.py b/python/paddle/fluid/layers/rnn.py index 8b5721438d..6786f04292 100644 --- a/python/paddle/fluid/layers/rnn.py +++ b/python/paddle/fluid/layers/rnn.py @@ -2306,40 +2306,6 @@ class SampleEmbeddingHelper(GreedyEmbeddingHelper): ) self.seed = seed - def sample(self, time, outputs, states): - r""" - Perform sampling from a categorical distribution, and the distribution - is computed by `softmax(outputs/softmax_temperature)`. - - Parameters: - time(Variable): An `int64` tensor with shape `[1]` provided by the - caller, representing the current time step number of decoding. - outputs(Variable): A tensor variable. Usually it's data type is float32 - or float64, and it's shape is `[batch_size, vocabulary_size]`, - representing the predicted logits of current step. It is same as - `outputs` returned by `BasicDecoder.output_fn(BasicDecoder.cell.call())`. - states(Variable): A (possibly nested structure of) tensor variable[s]. - It is same as `new_states` returned by `BasicDecoder.cell.call()`. - - Returns: - Variable: An `int64` tensor with shape `[batch_size]`, representing \ - the sampled ids. - """ - logits = ( - (outputs / self.softmax_temperature) - if self.softmax_temperature is not None - else outputs - ) - probs = paddle.nn.functional.softmax(logits) - # TODO: remove this stop_gradient. The stop_gradient of sample_ids can - # not pass to probs, since sampling_id op does not have corresponding - # grad op and thus can not pass. - probs.stop_gradient = True - sample_ids = nn.sampling_id( - probs, seed=self.seed, dtype=self.start_tokens.dtype - ) - return sample_ids - class BasicDecoder(Decoder): """ diff --git a/python/paddle/fluid/tests/unittests/npu/test_sampling_id_op_npu.py b/python/paddle/fluid/tests/unittests/npu/test_sampling_id_op_npu.py deleted file mode 100644 index d354e39bcf..0000000000 --- a/python/paddle/fluid/tests/unittests/npu/test_sampling_id_op_npu.py +++ /dev/null @@ -1,52 +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 -import numpy as np -import sys - -sys.path.append("..") - -from op_test import OpTest, _set_use_system_allocator -import paddle.fluid.core as core -import paddle.fluid as fluid -from paddle.fluid.op import Operator -import paddle - -_set_use_system_allocator(False) - - -class TestSamplingIdShape(unittest.TestCase): - def test_shape(self): - paddle.enable_static() - x = fluid.layers.data(name='x', shape=[3], dtype='float32') - output = fluid.layers.sampling_id(x) - - place = fluid.NPUPlace(0) - exe = fluid.Executor(place=place) - exe.run(fluid.default_startup_program()) - - feed = { - 'x': np.array([[0.2, 0.3, 0.5], [0.2, 0.3, 0.4]], dtype='float32') - } - output_np = exe.run(feed=feed, fetch_list=[output])[0] - - self.assertEqual(output.shape[0], -1) - self.assertEqual(len(output.shape), 1) - self.assertEqual(output_np.shape[0], 2) - self.assertEqual(len(output_np.shape), 1) - - -if __name__ == "__main__": - unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_layers.py b/python/paddle/fluid/tests/unittests/test_layers.py index 9297666eea..043321bf56 100644 --- a/python/paddle/fluid/tests/unittests/test_layers.py +++ b/python/paddle/fluid/tests/unittests/test_layers.py @@ -2308,7 +2308,6 @@ class TestBook(LayerTest): { "make_gaussian_random", "make_kldiv_loss", - "make_sampling_id", "make_uniform_random_batch_size_like", } ) @@ -2794,20 +2793,6 @@ class TestBook(LayerTest): out = layers.gaussian_random(shape=[20, 30]) return out - def make_sampling_id(self): - with program_guard( - fluid.default_main_program(), fluid.default_startup_program() - ): - x = self._get_data( - name="X", - shape=[13, 11], - dtype='float32', - append_batch_size=False, - ) - - out = layers.sampling_id(x) - return out - def make_sum(self): with program_guard( fluid.default_main_program(), fluid.default_startup_program() diff --git a/python/paddle/fluid/tests/unittests/test_random_seed.py b/python/paddle/fluid/tests/unittests/test_random_seed.py index 5a3e92eb9f..44b3688895 100644 --- a/python/paddle/fluid/tests/unittests/test_random_seed.py +++ b/python/paddle/fluid/tests/unittests/test_random_seed.py @@ -372,83 +372,6 @@ class TestGeneratorSeed(unittest.TestCase): np.testing.assert_allclose(out1_res2, out2_res2, rtol=1e-05) self.assertTrue(not np.allclose(out1_res2, out1_res1)) - def test_generator_sampling_id_dygraph(self): - """Test Generator seed.""" - gen = paddle.seed(12312321111) - - fluid.enable_dygraph() - - gen.manual_seed(12312321111) - x = fluid.layers.uniform_random( - [10, 10], dtype="float32", min=0.0, max=1.0 - ) - y = fluid.layers.sampling_id(x) - - st1 = gen.get_state() - x1 = fluid.layers.uniform_random( - [10, 10], dtype="float32", min=0.0, max=1.0 - ) - y1 = fluid.layers.sampling_id(x) - - gen.set_state(st1) - x2 = fluid.layers.uniform_random( - [10, 10], dtype="float32", min=0.0, max=1.0 - ) - y2 = fluid.layers.sampling_id(x) - - gen.manual_seed(12312321111) - x3 = fluid.layers.uniform_random( - [10, 10], dtype="float32", min=0.0, max=1.0 - ) - y3 = fluid.layers.sampling_id(x) - - x_np = y.numpy() - x1_np = y1.numpy() - x2_np = y2.numpy() - x3_np = y3.numpy() - - if not core.is_compiled_with_cuda(): - print(">>>>>>> sampling id dygraph >>>>>>>") - np.testing.assert_allclose(x1_np, x2_np, rtol=1e-05) - np.testing.assert_allclose(x_np, x3_np, rtol=1e-05) - - def test_generator_randperm_static_1(self): - - fluid.disable_dygraph() - - paddle.seed(123123143) - - startup_program = fluid.Program() - train_program = fluid.Program() - with fluid.program_guard(train_program, startup_program): - # example 1: - # attr shape is a list which doesn't contain tensor Variable. - x = fluid.layers.uniform_random(shape=[10, 10]) - result_1 = fluid.layers.sampling_id(x) - result_2 = fluid.layers.sampling_id(x) - - exe = fluid.Executor(fluid.CPUPlace()) - exe.run(startup_program) - out1 = exe.run( - train_program, feed={}, fetch_list=[result_1, result_2] - ) - - paddle.seed(123123143) - out2 = exe.run( - train_program, feed={}, fetch_list=[result_1, result_2] - ) - - out1_res1 = np.array(out1[0]) - out1_res2 = np.array(out1[1]) - out2_res1 = np.array(out2[0]) - out2_res2 = np.array(out2[1]) - - if not core.is_compiled_with_cuda(): - print(">>>>>>> sampling id static >>>>>>>") - np.testing.assert_allclose(out1_res1, out2_res1, rtol=1e-05) - np.testing.assert_allclose(out1_res2, out2_res2, rtol=1e-05) - self.assertTrue(not np.allclose(out1_res2, out1_res1)) - def test_gen_TruncatedNormal_initializer(self): fluid.disable_dygraph() diff --git a/python/paddle/fluid/tests/unittests/test_sampling_id_op.py b/python/paddle/fluid/tests/unittests/test_sampling_id_op.py deleted file mode 100644 index ae84e98aaa..0000000000 --- a/python/paddle/fluid/tests/unittests/test_sampling_id_op.py +++ /dev/null @@ -1,45 +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 - -import paddle -import paddle.fluid as fluid - - -class TestSamplingIdShape(unittest.TestCase): - def test_shape(self): - paddle.enable_static() - x = fluid.layers.data(name='x', shape=[3], dtype='float32') - output = fluid.layers.sampling_id(x) - - place = fluid.CPUPlace() - exe = fluid.Executor(place=place) - exe.run(fluid.default_startup_program()) - - feed = { - 'x': np.array([[0.2, 0.3, 0.5], [0.2, 0.3, 0.4]], dtype='float32') - } - output_np = exe.run(feed=feed, fetch_list=[output])[0] - - self.assertEqual(output.shape[0], -1) - self.assertEqual(len(output.shape), 1) - self.assertEqual(output_np.shape[0], 2) - self.assertEqual(len(output_np.shape), 1) - - -if __name__ == "__main__": - unittest.main() -- GitLab