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 5 No.2】Add index_fill / index_fill_ API to Paddle -part #57416

Merged
merged 15 commits into from
Nov 3, 2023

Conversation

Patrick-Star125
Copy link
Contributor

@Patrick-Star125 Patrick-Star125 commented Sep 16, 2023

PR types

New features

PR changes

APIs

Description

Add index_fill/index_fill_ API to Paddle

Link

Rfc PR: PaddlePaddle/community#621
docs PR: PaddlePaddle/docs#6284

待完成

  • RFC文档修改
  • 中文文档

@Patrick-Star125 Patrick-Star125 changed the title init index_fill API 【Hackathon 5 No.2】Add index_fill / index_fill_ API to Paddle Sep 16, 2023
@paddle-bot paddle-bot bot added the contributor External developers label Sep 16, 2023
@paddle-ci-bot
Copy link

paddle-ci-bot bot commented Sep 29, 2023

Sorry to inform you that c49cd3d's CIs have passed for more than 7 days. To prevent PR conflicts, you need to re-run all CIs manually.

@paddle-ci-bot
Copy link

paddle-ci-bot bot commented Oct 7, 2023

Sorry to inform you that dfb2947's CIs have passed for more than 7 days. To prevent PR conflicts, you need to re-run all CIs manually.

Copy link
Contributor

@zoooo0820 zoooo0820 left a comment

Choose a reason for hiding this comment

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

static-check中提示示例代码格式问题,辛苦也再根据报错信息中给出的参考文档修改下


if inplace:
x[:] = out
return x
Copy link
Contributor

Choose a reason for hiding this comment

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

此处,如果是inplace,是否可以不要调用clone+setitem赋值,而是直接使用index_put_赋值; 如果非inplace,是否可以不需要额外的clone操作

Copy link
Contributor

@jeff41404 jeff41404 Oct 17, 2023

Choose a reason for hiding this comment

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

the implementation solution in rfc shoule be also changed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

current implementation is same with rfc API design already

Copy link
Contributor

Choose a reason for hiding this comment

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

in rfc this line: out = paddle.clone(x) at else branch need to be delete ?

self.x_shape = (10, 15, 10)
self.axis = 1


Copy link
Contributor

Choose a reason for hiding this comment

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

补充下complex类型的测试吧

Copy link
Contributor Author

Choose a reason for hiding this comment

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

index_put不支持complex类型的输入

增加了float16类型的测试

ref_res = compute_index_put_ref(
self.x_np, self.axis, self.index_np, self.value
)
np.testing.assert_allclose(ref_res, pd_res, atol=1e-5)
Copy link
Contributor

Choose a reason for hiding this comment

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

这几个assert能否使用默认的atol阈值

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@Patrick-Star125
Copy link
Contributor Author

Done

@zoooo0820
Copy link
Contributor

Done

@Patrick-Star125 辛苦再看下static-check中显示的示例代码格式,以及单测中atol阈值设置这两个问题呢

@Patrick-Star125
Copy link
Contributor Author

@zoooo0820 似乎static-check中报错不太明确,我主要参考这个PR的写法,样例代码在我线下都是可以正常执行的

@sunzhongkai588
Copy link
Contributor

sunzhongkai588 commented Oct 13, 2023

@zoooo0820 似乎static-check中报错不太明确,我主要参考这个PR的写法,样例代码在我线下都是可以正常执行的

@Patrick-Star125

  • static-check 代码示例没通过,报以下错,应该是没 import numpy。请尽量不使用 numpy,直接创建paddle tensor。
image
  • 另外,示例代码检查默认是cpu环境,输出需要和cpu环境下一致。

详细规范请务必参考 python文档示例代码规范

@Patrick-Star125
Copy link
Contributor Author

Done

Copy link
Contributor

@zoooo0820 zoooo0820 left a comment

Choose a reason for hiding this comment

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

LGTM

if axis < 0:
axis = axis + x_dim

if not (isinstance(axis, int)) or (axis > x_dim - 1) or axis < -x_dim:
Copy link
Contributor

@jeff41404 jeff41404 Oct 17, 2023

Choose a reason for hiding this comment

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

The negative axis has been processed in L5183 above, so the judgment condition here should be axis < 0 not axis < -x_dim.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment on lines 5240 to 5242
axis (int): The dimension along which to index.
index (Tensor): The 1-D Tensor containing the indices to index.
The data type of ``index`` must be int32 or int64.
Copy link
Contributor

Choose a reason for hiding this comment

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

The order of parameters in the document should be consistent with the order of parameters in the function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

paddle.enable_static()


def compute_index_put_ref(x, axis, index, value):
Copy link
Contributor

Choose a reason for hiding this comment

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

compute_index_fill_ref is more suitable?

Copy link
Contributor

Choose a reason for hiding this comment

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

Is the logic of compute_index_put_ref too complex? and it is better to use Numpy's fancy indexing, eg, x=np.transpose(x, perm); x[index] = value; x=np.transpose(x, perm) be simpler and easier to understand?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

jeff41404
jeff41404 previously approved these changes Oct 19, 2023
Copy link
Contributor

@jeff41404 jeff41404 left a comment

Choose a reason for hiding this comment

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

LGTM

Co-authored-by: zachary sun <70642955+sunzhongkai588@users.noreply.github.com>
@paddle-ci-bot
Copy link

paddle-ci-bot bot commented Oct 28, 2023

Sorry to inform you that 59e28c3's CIs have passed for more than 7 days. To prevent PR conflicts, you need to re-run all CIs manually.

@luotao1
Copy link
Contributor

luotao1 commented Nov 2, 2023

请解决下冲突以及codestyle流水线的问题

Copy link
Contributor

@sunzhongkai588 sunzhongkai588 left a comment

Choose a reason for hiding this comment

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

LGTM for docs

@luotao1 luotao1 merged commit c1a72a4 into PaddlePaddle:develop Nov 3, 2023
28 checks passed
@luotao1 luotao1 changed the title 【Hackathon 5 No.2】Add index_fill / index_fill_ API to Paddle 【Hackathon 5 No.2】Add index_fill / index_fill_ API to Paddle -part Nov 3, 2023
sunzhongkai588

This comment was marked as abuse.

zeroRains pushed a commit to zeroRains/Paddle that referenced this pull request Nov 8, 2023
…addle#57416)

* init index_fill API

* modify api design

* format code

* optimize process

* format code

* adjust computation process

* sample code format

* fix code example

* modify test reference

* Update documentation

Co-authored-by: zachary sun <70642955+sunzhongkai588@users.noreply.github.com>

---------

Co-authored-by: zachary sun <70642955+sunzhongkai588@users.noreply.github.com>
danleifeng pushed a commit to danleifeng/Paddle that referenced this pull request Nov 14, 2023
…addle#57416)

* init index_fill API

* modify api design

* format code

* optimize process

* format code

* adjust computation process

* sample code format

* fix code example

* modify test reference

* Update documentation

Co-authored-by: zachary sun <70642955+sunzhongkai588@users.noreply.github.com>

---------

Co-authored-by: zachary sun <70642955+sunzhongkai588@users.noreply.github.com>
@co63oc
Copy link
Contributor

co63oc commented Dec 9, 2023

import paddle
x = paddle.eye(num_rows=3, num_columns=4)
indices = paddle.to_tensor(data=[0, 2])
value = -1
result = x.index_fill(axis=1, index=indices, value=value)
print(result)

result = x.index_fill_(axis=1, index=indices, value=value)
print(result)
print(x)
Tensor(shape=[3, 4], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       [[-1.,  0., -1.,  0.],
        [-1.,  1., -1.,  0.],
        [-1.,  0., -1.,  0.]])
Tensor(shape=[3, 4], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       [[-1., -1., -1., -1.],
        [ 0.,  1.,  0.,  0.],
        [-1., -1., -1., -1.]])
Tensor(shape=[3, 4], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       [[-1., -1., -1., -1.],
        [ 0.,  1.,  0.,  0.],
        [-1., -1., -1., -1.]])

The index_fill and index_fill_ versions have different calculation results

@Patrick-Star125

@Patrick-Star125
Copy link
Contributor Author

@co63oc this pr has been submitted for this bug, thank you for your feedback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants