From a6c74cf502cb96a86c2e6cd5b69fe75df7233b45 Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Tue, 28 May 2024 02:46:48 +0000 Subject: [PATCH 1/2] pir support uniform_random --- .../pir/dialect/op_generator/ops_api_gen.py | 1 - python/paddle/tensor/random.py | 16 +++++ .../test_uniform_random_op_deprecated.py | 67 +++++++++++++++++++ .../test_uniform_random_bf16_op.py | 0 .../legacy_test/test_uniform_random_op.py | 43 ------------ 5 files changed, 83 insertions(+), 44 deletions(-) create mode 100644 test/deprecated/legacy_test/test_uniform_random_op_deprecated.py rename test/{deprecated => }/legacy_test/test_uniform_random_bf16_op.py (100%) rename test/{deprecated => }/legacy_test/test_uniform_random_op.py (94%) diff --git a/paddle/fluid/pir/dialect/op_generator/ops_api_gen.py b/paddle/fluid/pir/dialect/op_generator/ops_api_gen.py index c542bdbf26312..e9ac28e56bee1 100644 --- a/paddle/fluid/pir/dialect/op_generator/ops_api_gen.py +++ b/paddle/fluid/pir/dialect/op_generator/ops_api_gen.py @@ -188,7 +188,6 @@ 'sparse_momentum', 'tdm_sampler', 'soft_relu', - 'uniform_random_batch_size_like', 'match_matrix_tensor', 'c_reduce_max', 'c_reduce_max_', diff --git a/python/paddle/tensor/random.py b/python/paddle/tensor/random.py index 7987a02af1cea..097f62f594864 100644 --- a/python/paddle/tensor/random.py +++ b/python/paddle/tensor/random.py @@ -463,6 +463,22 @@ def uniform_random_batch_size_like( >>> print(out_2.shape) [2, 3] """ + if in_dynamic_or_pir_mode(): + dtype = convert_np_dtype_to_dtype_(dtype) + return _C_ops.uniform_random_batch_size_like( + input, + shape, + input_dim_idx, + output_dim_idx, + min, + max, + seed, + 0, + 0, + 1.0, + dtype, + ) + check_variable_and_dtype( input, 'Input', diff --git a/test/deprecated/legacy_test/test_uniform_random_op_deprecated.py b/test/deprecated/legacy_test/test_uniform_random_op_deprecated.py new file mode 100644 index 0000000000000..0e21865131bc0 --- /dev/null +++ b/test/deprecated/legacy_test/test_uniform_random_op_deprecated.py @@ -0,0 +1,67 @@ +# 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 os +import unittest + +import numpy as np +from test_attribute_var import UnittestBase + +import paddle +from paddle.base import Program, program_guard + + +class TestUniformMinMaxTensor(UnittestBase): + def init_info(self): + self.shapes = [[2, 3, 4]] + self.save_path = os.path.join(self.temp_dir.name, self.path_prefix()) + + def test_static(self): + main_prog = Program() + startup_prog = Program() + with program_guard(main_prog, startup_prog): + fc = paddle.nn.Linear(4, 10) + x = paddle.randn([2, 3, 4]) + x.stop_gradient = False + feat = fc(x) # [2,3,10] + min_v = paddle.to_tensor([0.1]) + max_v = paddle.to_tensor([0.9]) + y = paddle.uniform([2, 3, 10], min=min_v, max=max_v) + z = paddle.uniform([2, 3, 10], min=min_v, max=max_v) + + out = feat + y + z + + sgd = paddle.optimizer.SGD() + sgd.minimize(paddle.mean(out)) + self.assertTrue(self.var_prefix() in str(main_prog)) + + exe = paddle.static.Executor() + exe.run(startup_prog) + res = exe.run(fetch_list=[out]) + np.testing.assert_array_equal(res[0].shape, [2, 3, 10]) + + paddle.static.save_inference_model(self.save_path, [x], [out], exe) + # Test for Inference Predictor + infer_out = self.infer_prog() + np.testing.assert_array_equal(res[0].shape, [2, 3, 10]) + + def path_prefix(self): + return 'uniform_random' + + def var_prefix(self): + return "Var[" + + +if __name__ == "__main__": + unittest.main() diff --git a/test/deprecated/legacy_test/test_uniform_random_bf16_op.py b/test/legacy_test/test_uniform_random_bf16_op.py similarity index 100% rename from test/deprecated/legacy_test/test_uniform_random_bf16_op.py rename to test/legacy_test/test_uniform_random_bf16_op.py diff --git a/test/deprecated/legacy_test/test_uniform_random_op.py b/test/legacy_test/test_uniform_random_op.py similarity index 94% rename from test/deprecated/legacy_test/test_uniform_random_op.py rename to test/legacy_test/test_uniform_random_op.py index b7424d4cf2e30..ac981f38b13c2 100644 --- a/test/deprecated/legacy_test/test_uniform_random_op.py +++ b/test/legacy_test/test_uniform_random_op.py @@ -12,13 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np from op import Operator from op_test import OpTest, convert_uint16_to_float -from test_attribute_var import UnittestBase import paddle from paddle import base @@ -733,46 +731,5 @@ def test_fixed_random_number(self): paddle.enable_static() -class TestUniformMinMaxTensor(UnittestBase): - def init_info(self): - self.shapes = [[2, 3, 4]] - self.save_path = os.path.join(self.temp_dir.name, self.path_prefix()) - - def test_static(self): - main_prog = Program() - startup_prog = Program() - with program_guard(main_prog, startup_prog): - fc = paddle.nn.Linear(4, 10) - x = paddle.randn([2, 3, 4]) - x.stop_gradient = False - feat = fc(x) # [2,3,10] - min_v = paddle.to_tensor([0.1]) - max_v = paddle.to_tensor([0.9]) - y = paddle.uniform([2, 3, 10], min=min_v, max=max_v) - z = paddle.uniform([2, 3, 10], min=min_v, max=max_v) - - out = feat + y + z - - sgd = paddle.optimizer.SGD() - sgd.minimize(paddle.mean(out)) - self.assertTrue(self.var_prefix() in str(main_prog)) - - exe = paddle.static.Executor() - exe.run(startup_prog) - res = exe.run(fetch_list=[out]) - np.testing.assert_array_equal(res[0].shape, [2, 3, 10]) - - paddle.static.save_inference_model(self.save_path, [x], [out], exe) - # Test for Inference Predictor - infer_out = self.infer_prog() - np.testing.assert_array_equal(res[0].shape, [2, 3, 10]) - - def path_prefix(self): - return 'uniform_random' - - def var_prefix(self): - return "Var[" - - if __name__ == "__main__": unittest.main() From f001b718c85c094ad12693ca0a8fb8c288eb6167 Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Tue, 28 May 2024 11:02:28 +0000 Subject: [PATCH 2/2] refine --- test/deprecated/legacy_test/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/test/deprecated/legacy_test/CMakeLists.txt b/test/deprecated/legacy_test/CMakeLists.txt index 9129199f30850..18891bc1cb65e 100644 --- a/test/deprecated/legacy_test/CMakeLists.txt +++ b/test/deprecated/legacy_test/CMakeLists.txt @@ -735,6 +735,7 @@ set_tests_properties(test_cumprod_op PROPERTIES TIMEOUT 300) set_tests_properties(test_split_program PROPERTIES TIMEOUT 120) set_tests_properties(test_graph_send_ue_recv_op PROPERTIES TIMEOUT 60) set_tests_properties(test_graph_send_uv_op PROPERTIES TIMEOUT 60) +set_tests_properties(test_uniform_random_op_deprecated PROPERTIES TIMEOUT 60) set_tests_properties(test_pretrained_model PROPERTIES TIMEOUT 120) set_tests_properties(test_model PROPERTIES TIMEOUT 300)