Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【Hackathon 5th No.14】Add combinations API to Paddle #57792

Merged
merged 15 commits into from
Dec 1, 2023
23 changes: 7 additions & 16 deletions python/paddle/tensor/math.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

严格按照模板(包括空行)

Original file line number Diff line number Diff line change
@@ -6953,30 +6953,21 @@ def combinations(x, r=2, with_replacement=False, name=None):
.. code-block:: python

>>> import paddle

>>> # example1
>>> x = paddle.to_tensor([1, 2, 3], dtype='float32')
>>> y = paddle.to_tensor([2, 3, 4], dtype='int32')
>>> res = paddle.ldexp(x, y)
>>> x = paddle.to_tensor([1, 2, 3], dtype='int32')
>>> res = paddle.combinations(x)
>>> print(res)
Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
[4. , 16., 48.])

>>> # example2
>>> x = paddle.to_tensor([1, 2, 3], dtype='float32')
>>> y = paddle.to_tensor([2], dtype='int32')
>>> res = paddle.ldexp(x, y)
>>> print(res)
Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
[4. , 8. , 12.])
Tensor(shape=[3, 2], dtype=int32, place=Place(gpu:0), stop_gradient=True,
[[1, 2],
[1, 3],
[2, 3]])

"""
if len(x.shape) != 1:
raise TypeError(f"Expect a 1-D vector, but got x shape {x.shape}")
if not isinstance(r, int) or r < 0:
raise ValueError(f"Expect a non-negative int, but got r={r}")

if r == 0:
if r == 0 or r > x.shape[0]:
return paddle.empty([0], dtype=x.dtype)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty的形状设计好像有些问题,pytorch在不同情况下的empty的size是不同的。相关设计后续补充到rfc文档中。

Copy link
Contributor Author

@Patrick-Star125 Patrick-Star125 Oct 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我之前错误理解empty表示输入形状为空的tensor了,已添加empty相关测试


if r > 1:
10 changes: 9 additions & 1 deletion test/legacy_test/test_combinations.py
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@
paddle.enable_static()


def convert_combinations_to_array(x, r, with_replacement):
def convert_combinations_to_array(x, r=2, with_replacement=False):
if r == 0:
return np.array([]).astype(x.dtype)
if with_replacement:
@@ -114,3 +114,11 @@ def modify_setting(self):
self.x_shape = [10]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

缺少了输入为empty情况下的单测

self.r = 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

缺少r>x_shape情况的单测

self.with_replacement = True


class TestIndexFillAPI3(TestCombinationsAPIBase):
def modify_setting(self):
self.dtype_np = 'float32'
self.x_shape = [0]
self.r = 10
self.with_replacement = False