diff --git a/paddle/fluid/operators/uniform_random_op.cc b/paddle/fluid/operators/uniform_random_op.cc index 2fda636e8809bc4bb8ad99b381de84b772684b97..5324b9697c94a28de0efa98dea0c72f53546a7a2 100644 --- a/paddle/fluid/operators/uniform_random_op.cc +++ b/paddle/fluid/operators/uniform_random_op.cc @@ -182,9 +182,11 @@ uniform distribution. The random result is in set [min, max). AddAttr>("shape", "The shape of the output tensor") .SetDefault({}); AddAttr("min", "Minimum value of uniform random. [default -1.0].") - .SetDefault(-1.0f); + .SetDefault(-1.0f) + .SupportTensor(); AddAttr("max", "Maximun value of uniform random. [default 1.0].") - .SetDefault(1.0f); + .SetDefault(1.0f) + .SupportTensor(); AddAttr("seed", "Random seed used for generating samples. " "0 means use a seed generated by the system." diff --git a/paddle/phi/api/yaml/legacy_api.yaml b/paddle/phi/api/yaml/legacy_api.yaml index b02ffd319ce050008b28345e75bb2411189d636f..9dd54e7fb44a5cc3ccbe47eb8166ad599681d747 100755 --- a/paddle/phi/api/yaml/legacy_api.yaml +++ b/paddle/phi/api/yaml/legacy_api.yaml @@ -2754,11 +2754,11 @@ backward : unfold_grad - api : uniform_random - args : (IntArray shape, DataType dtype, float min, float max, int seed, Place place={}) + args : (IntArray shape, DataType dtype, Scalar min, Scalar max, int seed, Place place={}) output : Tensor(out) infer_meta : func : UniformRandomInferMeta - param: [shape, dtype, min, max, seed] + param: [shape, dtype] kernel : func : uniform_random param: [shape, dtype, min, max, seed] diff --git a/paddle/phi/infermeta/nullary.cc b/paddle/phi/infermeta/nullary.cc index 4d11c462743d361d0530ac7ffbad4f8eff9c9059..442e62473238601f2fe06599e811fe252f691bbc 100644 --- a/paddle/phi/infermeta/nullary.cc +++ b/paddle/phi/infermeta/nullary.cc @@ -79,9 +79,6 @@ void RandpermInferMeta(int n, DataType dtype, MetaTensor* out) { void UniformRandomInferMeta(const IntArray& shape, DataType dtype, - float min, - float max, - int seed, MetaTensor* out) { auto out_dims = phi::make_ddim(shape.GetData()); out->set_dims(out_dims); diff --git a/paddle/phi/infermeta/nullary.h b/paddle/phi/infermeta/nullary.h index 3ac2b0a7cf393020b915e691567775c5f9312ea9..59673ba8bcf3687294f7103838c4308d6ec95bbf 100644 --- a/paddle/phi/infermeta/nullary.h +++ b/paddle/phi/infermeta/nullary.h @@ -67,9 +67,6 @@ void TruncatedGaussianRandomInferMeta(const std::vector& shape, void UniformRandomInferMeta(const IntArray& shape, DataType dtype, - float min, - float max, - int seed, MetaTensor* out); void TrilIndicesInferMeta( diff --git a/paddle/phi/kernels/cpu/uniform_random_kernel.cc b/paddle/phi/kernels/cpu/uniform_random_kernel.cc index a09812363f1d8d81fa7c81fec7f8db2041e8dc87..e5b25fc055437fb7c603f24bcbea615bd50c085a 100644 --- a/paddle/phi/kernels/cpu/uniform_random_kernel.cc +++ b/paddle/phi/kernels/cpu/uniform_random_kernel.cc @@ -47,8 +47,8 @@ template void UniformRandomRawKernel(const Context &dev_ctx, const IntArray &shape, DataType dtype, - float min, - float max, + const Scalar &min, + const Scalar &max, int seed, int diag_num, int diag_step, @@ -64,7 +64,8 @@ void UniformRandomRawKernel(const Context &dev_ctx, } else { engine = dev_ctx.GetGenerator()->GetCPUEngine(); } - UniformRealDistribution(data, size, min, max, engine); + UniformRealDistribution( + data, size, min.to(), max.to(), engine); if (diag_num > 0) { PADDLE_ENFORCE_GT( size, @@ -88,8 +89,8 @@ template void UniformRandomKernel(const Context &dev_ctx, const IntArray &shape, DataType dtype, - float min, - float max, + const Scalar &min, + const Scalar &max, int seed, DenseTensor *out) { UniformRandomRawKernel( diff --git a/paddle/phi/kernels/gpu/uniform_random_kernel.cu b/paddle/phi/kernels/gpu/uniform_random_kernel.cu index ee486976a450c2f7c756130f78ee686c09dcd218..36382f40ea11d9c4044a4d5a8a5d83bfe17cd17d 100644 --- a/paddle/phi/kernels/gpu/uniform_random_kernel.cu +++ b/paddle/phi/kernels/gpu/uniform_random_kernel.cu @@ -57,8 +57,8 @@ template void UniformRandomRawKernel(const Context& dev_ctx, const IntArray& shape, DataType dtype, - float min, - float max, + const Scalar& min, + const Scalar& max, int seed, int diag_num, int diag_step, @@ -70,12 +70,12 @@ void UniformRandomRawKernel(const Context& dev_ctx, // Use global Generator seed using MT = typename kps::details::MPTypeTrait::Type; funcs::uniform_distribution dist; - funcs::uniform_real_transform trans(min, max); + funcs::uniform_real_transform trans(min.to(), max.to()); funcs::distribution_and_transform(dev_ctx, out, dist, trans); } else { // Use OP seed - auto func = - UniformGenerator(min, max, seed, diag_num, diag_step, diag_val); + auto func = UniformGenerator( + min.to(), max.to(), seed, diag_num, diag_step, diag_val); IndexKernel>(dev_ctx, out, func); } } @@ -84,8 +84,8 @@ template void UniformRandomKernel(const Context& dev_ctx, const IntArray& shape, DataType dtype, - float min, - float max, + const Scalar& min, + const Scalar& max, int seed, DenseTensor* out) { UniformRandomRawKernel( diff --git a/paddle/phi/kernels/selected_rows/uniform_random_kernel.cc b/paddle/phi/kernels/selected_rows/uniform_random_kernel.cc index ce2bbc533f703e73ff03f5e4c57f1a66103ab8e9..c304fcf67705e7fb099a593aef740604fc6215b7 100644 --- a/paddle/phi/kernels/selected_rows/uniform_random_kernel.cc +++ b/paddle/phi/kernels/selected_rows/uniform_random_kernel.cc @@ -26,8 +26,8 @@ template void UniformRandomRawKernel(const Context& dev_ctx, const IntArray& shape, DataType dtype, - float min, - float max, + const Scalar& min, + const Scalar& max, int seed, int diag_num, int diag_step, @@ -49,8 +49,8 @@ template void UniformRandomKernel(const Context& dev_ctx, const IntArray& shape, DataType dtype, - float min, - float max, + const Scalar& min, + const Scalar& max, int seed, SelectedRows* out) { phi::UniformRandomKernel( diff --git a/paddle/phi/kernels/selected_rows/uniform_random_kernel.h b/paddle/phi/kernels/selected_rows/uniform_random_kernel.h index d0a9084b40b3755760eebbebfadd99ee7c33d243..237b01532c7bd824b4ae36ae85fe588c76defa59 100644 --- a/paddle/phi/kernels/selected_rows/uniform_random_kernel.h +++ b/paddle/phi/kernels/selected_rows/uniform_random_kernel.h @@ -15,6 +15,7 @@ #pragma once #include "paddle/phi/common/int_array.h" +#include "paddle/phi/common/scalar.h" #include "paddle/phi/core/selected_rows.h" namespace phi { @@ -24,8 +25,8 @@ template void UniformRandomRawKernel(const Context& dev_ctx, const IntArray& shape, DataType dtype, - float min, - float max, + const Scalar& min, + const Scalar& max, int seed, int diag_num, int diag_step, @@ -36,8 +37,8 @@ template void UniformRandomKernel(const Context& dev_ctx, const IntArray& shape, DataType dtype, - float min, - float max, + const Scalar& min, + const Scalar& max, int seed, SelectedRows* out); diff --git a/paddle/phi/kernels/uniform_random_kernel.h b/paddle/phi/kernels/uniform_random_kernel.h index 03eca83db03acda88f40a6d6797ba45fa2465890..1395a4663b91400343b41314dc6988e52b25e789 100644 --- a/paddle/phi/kernels/uniform_random_kernel.h +++ b/paddle/phi/kernels/uniform_random_kernel.h @@ -15,6 +15,7 @@ #pragma once #include "paddle/phi/common/int_array.h" +#include "paddle/phi/common/scalar.h" #include "paddle/phi/core/dense_tensor.h" #include "paddle/phi/core/device_context.h" @@ -24,8 +25,8 @@ template void UniformRandomRawKernel(const Context& dev_ctx, const IntArray& shape, DataType dtype, - float min, - float max, + const Scalar& min, + const Scalar& max, int seed, int diag_num, int diag_step, @@ -36,8 +37,8 @@ template void UniformRandomKernel(const Context& dev_ctx, const IntArray& shape, DataType dtype, - float min, - float max, + const Scalar& min, + const Scalar& max, int seed, DenseTensor* out); diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index 6b0fb3f62c7ab5312eeaba08edb096d9ce2769da..60f065530b52868bb98de710efc8198d3c77b34f 100755 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -15767,6 +15767,8 @@ def uniform_random(shape, check_type(shape, 'shape', (list, tuple, Variable), 'uniform_random/rand') check_dtype(dtype, 'dtype', ('float32', 'float64', 'uint16'), 'uniform_random/rand') + check_type(min, 'min', (float, int, Variable), 'uniform_random/rand') + check_type(max, 'max', (float, int, Variable), 'uniform_random/rand') inputs = dict() attrs = {'seed': seed, 'min': min, 'max': max, 'dtype': dtype} diff --git a/python/paddle/fluid/tests/unittests/test_uniform_random_op.py b/python/paddle/fluid/tests/unittests/test_uniform_random_op.py index a368697c934cca7ea5add61ce04c9dd24db53707..7aba65f9d1120a8f238b9ddca6c31ae5c9d7f903 100644 --- a/python/paddle/fluid/tests/unittests/test_uniform_random_op.py +++ b/python/paddle/fluid/tests/unittests/test_uniform_random_op.py @@ -28,6 +28,8 @@ import paddle.fluid as fluid from paddle.fluid import Program, program_guard from paddle.fluid.framework import _test_eager_guard +from test_attribute_var import UnittestBase + def output_hist(out): hist, _ = np.histogram(out, range=(-5, 10)) @@ -648,5 +650,49 @@ class TestRandomValue(unittest.TestCase): 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() + starup_prog = Program() + with program_guard(main_prog, starup_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.fluid.layers.uniform_random([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(starup_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/python/paddle/tensor/random.py b/python/paddle/tensor/random.py index fa3e2fa8fed5118fabe0c881d4f15620257f351c..709f61d5ca5d1a1e7ee9afa856844547246f4346 100644 --- a/python/paddle/tensor/random.py +++ b/python/paddle/tensor/random.py @@ -568,6 +568,8 @@ def uniform(shape, dtype=None, min=-1.0, max=1.0, seed=0, name=None): check_type(shape, 'shape', (list, tuple, Variable), 'uniform/rand') check_dtype(dtype, 'dtype', ('float32', 'float64'), 'uniform/rand') + check_type(min, 'min', (float, int, Variable), 'uniform/rand') + check_type(max, 'max', (float, int, Variable), 'uniform/rand') inputs = dict() attrs = {'seed': seed, 'min': min, 'max': max, 'dtype': dtype}