Skip to content

Commit

Permalink
delete sampling_id api (#48543)
Browse files Browse the repository at this point in the history
  • Loading branch information
201716010711 authored Dec 7, 2022
1 parent ddd5656 commit c6a2b0f
Show file tree
Hide file tree
Showing 6 changed files with 0 additions and 263 deletions.
40 changes: 0 additions & 40 deletions python/paddle/fluid/layers/nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@
'elementwise_sub',
'elementwise_mul',
'gaussian_random',
'sampling_id',
'clip',
'clip_by_norm',
'mean',
Expand Down Expand Up @@ -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)
Expand Down
34 changes: 0 additions & 34 deletions python/paddle/fluid/layers/rnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -2306,40 +2306,6 @@ def __init__(
)
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):
"""
Expand Down
52 changes: 0 additions & 52 deletions python/paddle/fluid/tests/unittests/npu/test_sampling_id_op_npu.py

This file was deleted.

15 changes: 0 additions & 15 deletions python/paddle/fluid/tests/unittests/test_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2308,7 +2308,6 @@ def setUp(self):
{
"make_gaussian_random",
"make_kldiv_loss",
"make_sampling_id",
"make_uniform_random_batch_size_like",
}
)
Expand Down Expand Up @@ -2794,20 +2793,6 @@ def make_gaussian_random(self):
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()
Expand Down
77 changes: 0 additions & 77 deletions python/paddle/fluid/tests/unittests/test_random_seed.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,83 +372,6 @@ def test_generator_randperm_static(self):
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()

Expand Down
45 changes: 0 additions & 45 deletions python/paddle/fluid/tests/unittests/test_sampling_id_op.py

This file was deleted.

0 comments on commit c6a2b0f

Please # to comment.