Skip to content

Commit

Permalink
clean fluid task: transfer gaussian random api (PaddlePaddle#48529)
Browse files Browse the repository at this point in the history
  • Loading branch information
201716010711 authored Dec 8, 2022
1 parent b2be092 commit da8fb52
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 176 deletions.
8 changes: 5 additions & 3 deletions python/paddle/distribution/normal.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
from paddle.distribution import distribution
from paddle.fluid.data_feeder import check_type, convert_dtype
from paddle.fluid.framework import _non_static_mode
from paddle.fluid.layers import nn, tensor
from paddle.fluid.layers import tensor
from paddle.tensor import random


class Normal(distribution.Distribution):
Expand Down Expand Up @@ -180,16 +181,17 @@ def sample(self, shape=(), seed=0):
self.loc + self.scale, batch_shape + shape, self.dtype, 0.0
)
zero_tmp_reshape = paddle.reshape(zero_tmp, output_shape)

zero_tmp_shape = paddle.shape(zero_tmp_reshape)
normal_random_tmp = nn.gaussian_random(
normal_random_tmp = random.gaussian(
zero_tmp_shape, mean=0.0, std=1.0, seed=seed, dtype=self.dtype
)
output = normal_random_tmp * (zero_tmp_reshape + self.scale)
output = paddle.add(output, self.loc, name=name)
return output
else:
output_shape = shape + batch_shape
output = nn.gaussian_random(
output = random.gaussian(
output_shape, mean=0.0, std=1.0, seed=seed, dtype=self.dtype
) * (tensor.zeros(output_shape, dtype=self.dtype) + self.scale)
output = paddle.add(output, self.loc, name=name)
Expand Down
147 changes: 0 additions & 147 deletions python/paddle/fluid/layers/nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@
'elementwise_div',
'elementwise_sub',
'elementwise_mul',
'gaussian_random',
'clip',
'clip_by_norm',
'mean',
Expand Down Expand Up @@ -2720,152 +2719,6 @@ def relu(x, name=None):
from paddle.fluid.framework import convert_np_dtype_to_dtype_


@deprecated(since="2.0.0", update_to="paddle.normal")
@templatedoc()
def gaussian_random(
shape, mean=0.0, std=1.0, seed=0, dtype='float32', name=None
):
"""
This OP returns a Tensor filled with random values sampled from a Gaussian
distribution, with ``shape`` and ``dtype``.
Args:
shape(list|tuple|Tensor): The shape of the output Tensor. If ``shape``
is a list or tuple, the elements of it should be integers or Tensors
(with the shape [1], and the data type int32 or int64). If ``shape``
is a Tensor, it should be a 1-D Tensor(with the data type int32 or
int64).
mean(float|int, optional): Mean of the output tensor, default is 0.0.
std(float|int, optional): Standard deviation of the output tensor, default
is 1.0.
seed(int, optional): ${seed_comment}
dtype(str|np.dtype|core.VarDesc.VarType, optional): The data type of
the output Tensor. Supported data types: float32, float64.
Default is float32.
name(str, optional): The default value is None. Normally there is no
need for user to set this property. For more information, please
refer to :ref:`api_guide_Name`.
Returns:
Tensor: A Tensor filled with random values sampled from a Gaussian
distribution, with ``shape`` and ``dtype``.
Examples:
.. code-block:: python
import paddle
import paddle.fluid as fluid
paddle.enable_static()
# example 1:
# attr shape is a list which doesn't contain Tensor.
result_1 = fluid.layers.gaussian_random(shape=[3, 4])
# [[-0.31261674, 1.8736548, -0.6274357, 0.96988016],
# [-0.12294637, 0.9554768, 1.5690808, -1.2894802 ],
# [-0.60082096, -0.61138713, 1.5345167, -0.21834975]]
# example 2:
# attr shape is a list which contains Tensor.
dim_1 = fluid.layers.fill_constant([1], "int64", 2)
dim_2 = fluid.layers.fill_constant([1], "int32", 3)
result_2 = fluid.layers.gaussian_random(shape=[dim_1, dim_2])
# [[ 0.51398206, -0.3389769, 0.23597084],
# [ 1.0388143, -1.2015356, -1.0499583 ]]
# example 3:
# attr shape is a Tensor, the data type must be int64 or int32.
var_shape = fluid.data(name='var_shape', shape=[2], dtype="int64")
result_3 = fluid.layers.gaussian_random(var_shape)
# if var_shape's value is [2, 3]
# result_3 is:
# [[-0.12310527, 0.8187662, 1.923219 ]
# [ 0.70721835, 0.5210541, -0.03214082]]
.. code-block:: python
# declarative mode
# required: skiptest
import numpy as np
from paddle import fluid
x = fluid.layers.gaussian_random((2, 3), std=2., seed=10)
place = fluid.CPUPlace()
exe = fluid.Executor(place)
start = fluid.default_startup_program()
main = fluid.default_main_program()
exe.run(start)
x_np, = exe.run(main, feed={}, fetch_list=[x])
x_np
# array([[2.3060477, 2.676496 , 3.9911983],
# [0.9990833, 2.8675377, 2.2279181]], dtype=float32)
.. code-block:: python
# imperative mode
import numpy as np
from paddle import fluid
import paddle.fluid.dygraph as dg
place = fluid.CPUPlace()
with dg.guard(place) as g:
x = fluid.layers.gaussian_random((2, 4), mean=2., dtype="float32", seed=10)
x_np = x.numpy()
x_np
# array([[2.3060477 , 2.676496 , 3.9911983 , 0.9990833 ],
# [2.8675377 , 2.2279181 , 0.79029655, 2.8447366 ]], dtype=float32)
"""
if not isinstance(dtype, core.VarDesc.VarType):
dtype = convert_np_dtype_to_dtype_(dtype)

if in_dygraph_mode():
shape = utils.convert_shape_to_list(shape)
place = _current_expected_place()
return _C_ops.gaussian(
shape, float(mean), float(std), seed, dtype, place
)

if _in_legacy_dygraph():
shape = utils.convert_shape_to_list(shape)
return _legacy_C_ops.gaussian_random(
'shape',
shape,
'mean',
float(mean),
'std',
float(std),
'seed',
seed,
'dtype',
dtype,
)

check_type(shape, 'shape', (list, tuple, Variable), 'gaussian_random/randn')
check_dtype(dtype, 'dtype', ['float32', 'float64'], 'gaussian_random/randn')

inputs = {}
attrs = {
'mean': mean,
'std': std,
'seed': seed,
'dtype': dtype,
'use_mkldnn': False,
}
utils.get_shape_tensor_inputs(
inputs=inputs, attrs=attrs, shape=shape, op_type='gaussian_random/randn'
)

helper = LayerHelper('gaussian_random', **locals())
out = helper.create_variable_for_type_inference(dtype)
helper.append_op(
type='gaussian_random', inputs=inputs, outputs={'Out': out}, attrs=attrs
)

return out


def _elementwise_op(helper):
op_type = helper.layer_type
x = helper.kwargs.get('x', None)
Expand Down
13 changes: 7 additions & 6 deletions python/paddle/fluid/tests/unittests/test_gaussian_random_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import paddle.fluid.core as core
from paddle.fluid.framework import _test_eager_guard
from paddle.fluid.tests.unittests.op_test import OpTest, convert_uint16_to_float
from paddle.tensor import random


class TestGaussianRandomOp(OpTest):
Expand Down Expand Up @@ -228,43 +229,43 @@ def test_api(self):
name="shape_tensor_int64", shape=[2], dtype="int64"
)

out_1 = fluid.layers.gaussian_random(
out_1 = random.gaussian(
shape=[2000, 500], dtype="float32", mean=0.0, std=1.0, seed=10
)

out_2 = fluid.layers.gaussian_random(
out_2 = random.gaussian(
shape=[2000, positive_2_int32],
dtype="float32",
mean=0.0,
std=1.0,
seed=10,
)

out_3 = fluid.layers.gaussian_random(
out_3 = random.gaussian(
shape=[2000, positive_2_int64],
dtype="float32",
mean=0.0,
std=1.0,
seed=10,
)

out_4 = fluid.layers.gaussian_random(
out_4 = random.gaussian(
shape=shape_tensor_int32,
dtype="float32",
mean=0.0,
std=1.0,
seed=10,
)

out_5 = fluid.layers.gaussian_random(
out_5 = random.gaussian(
shape=shape_tensor_int64,
dtype="float32",
mean=0.0,
std=1.0,
seed=10,
)

out_6 = fluid.layers.gaussian_random(
out_6 = random.gaussian(
shape=shape_tensor_int64,
dtype=np.float32,
mean=0.0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import paddle
import paddle.fluid as fluid
from paddle.fluid.framework import _test_eager_guard
from paddle.tensor import random


class AutoPruneLayer0(fluid.Layer):
Expand Down Expand Up @@ -487,7 +488,7 @@ def test_case3_prune_no_grad_branch2(self):

def func_case4_with_no_grad_op_maker(self):
with fluid.dygraph.guard():
out = fluid.layers.gaussian_random(shape=[20, 30])
out = random.gaussian(shape=[20, 30])
loss = paddle.mean(out)
loss.backward()
self.assertIsNone(out._grad_ivar())
Expand Down
2 changes: 1 addition & 1 deletion python/paddle/fluid/tests/unittests/test_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2557,7 +2557,7 @@ def make_gaussian_random(self):
with program_guard(
fluid.default_main_program(), fluid.default_startup_program()
):
out = layers.gaussian_random(shape=[20, 30])
out = random.gaussian(shape=[20, 30])
return out

def make_sum(self):
Expand Down
9 changes: 5 additions & 4 deletions python/paddle/fluid/tests/unittests/test_manual_seed.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,21 @@

import paddle
import paddle.fluid as fluid
from paddle.tensor import random


class TestManualSeed(unittest.TestCase):
def test_seed(self):
fluid.enable_dygraph()

gen = paddle.seed(12312321111)
x = fluid.layers.gaussian_random([10], dtype="float32")
x = random.gaussian([10], dtype="float32")
st1 = gen.get_state()
x1 = fluid.layers.gaussian_random([10], dtype="float32")
x1 = random.gaussian([10], dtype="float32")
gen.set_state(st1)
x2 = fluid.layers.gaussian_random([10], dtype="float32")
x2 = random.gaussian([10], dtype="float32")
gen.manual_seed(12312321111)
x3 = fluid.layers.gaussian_random([10], dtype="float32")
x3 = random.gaussian([10], dtype="float32")
x_np = x.numpy()
x1_np = x1.numpy()
x2_np = x2.numpy()
Expand Down
13 changes: 7 additions & 6 deletions python/paddle/fluid/tests/unittests/test_random_seed.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import paddle.fluid as fluid
import paddle.fluid.core as core
import paddle.fluid.generator as generator
from paddle.tensor import random


class TestGeneratorSeed(unittest.TestCase):
Expand Down Expand Up @@ -148,13 +149,13 @@ def test_generator_gaussian_random_dygraph(self):
fluid.enable_dygraph()

gen = paddle.seed(12312321111)
x = fluid.layers.gaussian_random([10], dtype="float32")
x = random.gaussian([10], dtype="float32")
st1 = gen.get_state()
x1 = fluid.layers.gaussian_random([10], dtype="float32")
x1 = random.gaussian([10], dtype="float32")
gen.set_state(st1)
x2 = fluid.layers.gaussian_random([10], dtype="float32")
x2 = random.gaussian([10], dtype="float32")
gen.manual_seed(12312321111)
x3 = fluid.layers.gaussian_random([10], dtype="float32")
x3 = random.gaussian([10], dtype="float32")
x_np = x.numpy()
x1_np = x1.numpy()
x2_np = x2.numpy()
Expand All @@ -175,8 +176,8 @@ def test_generator_gaussian_random_static(self):
with fluid.program_guard(train_program, startup_program):
# example 1:
# attr shape is a list which doesn't contain tensor Variable.
result_1 = fluid.layers.gaussian_random(shape=[3, 4])
result_2 = fluid.layers.gaussian_random(shape=[3, 4])
result_1 = random.gaussian(shape=[3, 4])
result_2 = random.gaussian(shape=[3, 4])

exe = fluid.Executor(fluid.CPUPlace())
exe.run(startup_program)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import paddle.fluid as fluid

paddle.enable_static()
from paddle.tensor import random


class XPUTestGaussianRandomOp(XPUOpTestWrapper):
Expand Down Expand Up @@ -192,43 +193,43 @@ def test_api(self):
name="shape_tensor_int64", shape=[2], dtype="int64"
)

out_1 = fluid.layers.gaussian_random(
out_1 = random.gaussian(
shape=[2000, 500], dtype="float32", mean=0.0, std=1.0, seed=10
)

out_2 = fluid.layers.gaussian_random(
out_2 = random.gaussian(
shape=[2000, positive_2_int32],
dtype="float32",
mean=0.0,
std=1.0,
seed=10,
)

out_3 = fluid.layers.gaussian_random(
out_3 = random.gaussian(
shape=[2000, positive_2_int64],
dtype="float32",
mean=0.0,
std=1.0,
seed=10,
)

out_4 = fluid.layers.gaussian_random(
out_4 = random.gaussian(
shape=shape_tensor_int32,
dtype="float32",
mean=0.0,
std=1.0,
seed=10,
)

out_5 = fluid.layers.gaussian_random(
out_5 = random.gaussian(
shape=shape_tensor_int64,
dtype="float32",
mean=0.0,
std=1.0,
seed=10,
)

out_6 = fluid.layers.gaussian_random(
out_6 = random.gaussian(
shape=shape_tensor_int64,
dtype=np.float32,
mean=0.0,
Expand Down
Loading

0 comments on commit da8fb52

Please sign in to comment.