top_p_sampling

paddle.Tensor. top_p_sampling ( x, ps, threshold=None, seed=None, name=None )

Get the TopP scores and ids according to the cumulative threshold ps.

Parameters
  • x (Tensor) – A N-D Tensor with type float32, float16 and bfloat16.

  • ps (Tensor) – A 1-D Tensor with type float32, float16 and bfloat16. it is the cumulative probability threshold to limit low probability input.

  • threshold (Tensor) – A 1-D Tensor with type float32, float16 and bfloat16. it is the absolute probability threshold to limit input, it will take effect simultaneously with ps, if not set, the default value is 0.f.

  • seed (int, optional) – the random seed,

  • name (str, optional) – For details, please refer to Name. Generally, no setting is required. Default: None.

Returns

tuple(Tensor), return the values and indices. The value data type is the same as the input x. The indices data type is int64.

Examples

>>> 
>>> import paddle

>>> paddle.device.set_device('gpu')
>>> paddle.seed(2023)
>>> x = paddle.randn([2,3])
>>> print(x)
Tensor(shape=[2, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
 [[-0.32012719, -0.07942779,  0.26011357],
  [ 0.79003978, -0.39958701,  1.42184138]])
>>> paddle.seed(2023)
>>> ps = paddle.randn([2])
>>> print(ps)
Tensor(shape=[2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
 [-0.32012719, -0.07942779])
>>> value, index = paddle.tensor.top_p_sampling(x, ps)
>>> print(value)
Tensor(shape=[2, 1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
 [[0.26011357],
  [1.42184138]])
>>> print(index)
Tensor(shape=[2, 1], dtype=int64, place=Place(gpu:0), stop_gradient=True,
 [[2],
  [2]])