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

[triu_indices] add triu_indices_op #45168

Merged
merged 29 commits into from Aug 25, 2022
Merged

Conversation

Rayman96
Copy link
Contributor

@Rayman96 Rayman96 commented Aug 16, 2022

PR types

New features

PR changes

APIs

Describe

为 Paddle 新增 triu_indices API, 包括 CPU&GPU kernel,python API以及单元测试

RFC的PR链接: PaddlePaddle/community#207
DOC的PR链接: PaddlePaddle/docs#5161

@paddle-bot
Copy link

paddle-bot bot commented Aug 16, 2022

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@Rayman96
Copy link
Contributor Author

可否请教下目前CI的三个失败。
CI-APPROVAL和CI-Static-Check好像是非代码层面的问题;
CI-Kunlun的错误没有理解日志显示的问题

@betterpig
Copy link
Contributor

betterpig commented Aug 18, 2022

可否请教下目前CI的三个失败。
CI-APPROVAL和CI-Static-Check好像是非代码层面的问题;
CI-Kunlun的错误没有理解日志显示的问题

CI-APPROVAL和CI-Static-Check等其他流水线通过后,需要找人review并approve。
CI-Kunlun是网络问题。已经重跑了

@Rayman96
Copy link
Contributor Author

Rayman96 commented Aug 18, 2022

可否请教下目前CI的三个失败。
CI-APPROVAL和CI-Static-Check好像是非代码层面的问题;
CI-Kunlun的错误没有理解日志显示的问题

CI-APPROVAL和CI-Static-Check等其他流水线通过后,需要找人review并approve。 CI-Kunlun是网络问题。已经重跑了

多谢回复,目前除CI-APPROVAL和CI-Static-Check外都已通过,辛苦审核review

def test_static(self):
places = [
paddle.CPUPlace(), paddle.fluid.CUDAPlace(0)
] if fluid.core.is_compiled_with_cuda() else [paddle.CPUPlace()]
Copy link
Contributor

Choose a reason for hiding this comment

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

现在有专门的CPU流水线和GPU流水线,为了减少单测时间,当为GPU版本时只跑GPUplace即可。所以后面的循环也是不必要的。下同。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已修改

paddle.static.Program()):
data1 = paddle.triu_indices(4, 4, -1)
exe1 = paddle.static.Executor(place)
result1 = exe1.run(feed={}, fetch_list=[data1])
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. 这里变量加上后缀1的用意是什么
  2. 这两行应放到guard外面。下同。
exe1 = paddle.static.Executor(place)
result1 = exe1.run(feed={}, fetch_list=[data1])

Copy link
Contributor Author

Choose a reason for hiding this comment

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

第一条已修改命名;第二条放到guard外后会报错,就保持缩进没有动了

Copy link
Contributor Author

@Rayman96 Rayman96 Aug 19, 2022

Choose a reason for hiding this comment

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

  1. 这里变量加上后缀1的用意是什么
  2. 这两行应放到guard外面。下同。
exe1 = paddle.static.Executor(place)
result1 = exe1.run(feed={}, fetch_list=[data1])

将后两行移出后会报错显示, 本地和流水线都可复现:

Traceback (most recent call last):
File "test_triu_indices_op.py", line 73, in test_static
result = exe.run(feed={}, fetch_list=[data])
File "/usr/local/lib/python3.7/dist-packages/paddle/fluid/executor.py", line 1307, in run
six.reraise(*sys.exc_info())
File "/usr/local/lib/python3.7/dist-packages/six.py", line 703, in reraise
raise value
File "/usr/local/lib/python3.7/dist-packages/paddle/fluid/executor.py", line 1303, in run
return_merged=return_merged)
File "/usr/local/lib/python3.7/dist-packages/paddle/fluid/executor.py", line 1582, in _run_impl
return_numpy)
File "/usr/local/lib/python3.7/dist-packages/paddle/fluid/executor.py", line 546, in run
fetch_list)._move_to_list()
RuntimeError: (NotFound) triu_indices_1.tmp_0 not in VariableScope.
[Hint: Expected HasVar(name) == true, but received HasVar(name):0 != true:1.] (at /paddle/paddle/fluid/framework/new_executor/new_executor_defs.cc:677)

Copy link
Contributor

Choose a reason for hiding this comment

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

好的。

exe1 = paddle.static.Executor(place)
result1 = exe1.run(feed={}, fetch_list=[data1])
expected_result1 = np.triu_indices(4, -1, 4)
self.assertTrue(np.allclose(result1, expected_result1))
Copy link
Contributor

Choose a reason for hiding this comment

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

使用assertTrue在结果出现diff时报错不直观,无法看出diff在哪里,差了多少。改为np.testing.assert_array_equal。下同

Copy link
Member

@SigureMo SigureMo Aug 18, 2022

Choose a reason for hiding this comment

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

@Rayman96 ,这里可参考 https://github.com/PaddlePaddle/community/blob/master/rfcs/CodeStyle/20220805_code_style_improvement_for_unittest.md#1%E7%9B%B8%E5%85%B3%E8%83%8C%E6%99%AF ,是近期一个正在进行的一项修改

另外,目测这里的静态图返回值是没有 unpack 的,替换成 np.testing 下的函数可能会报 shape 不匹配的问题,静态图返回值是一个 list,因此如果想要取得其结果应该 unpack 下,修改方式可参考正在进行中的 #45213(只是目测,也可能我看错了 😂)

修改后 PR-CI-APPROVAL 应可通过

Copy link
Contributor Author

Choose a reason for hiding this comment

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

好的,我根据参考链接修改此处判断

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已修改,可通过PR-CI-APPROVAL

data1 = paddle.triu_indices(4,4,0)
print(data1)
# [[0, 0, 0, 0, 1, 1, 1, 2, 2, 3],
# [0, 1, 2, 3, 1, 2, 3, 2, 3, 3]]
Copy link
Contributor

Choose a reason for hiding this comment

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

内层中括号对齐-->

# [[0, 0, 0, 0, 1, 1, 1, 2, 2, 3],
#  [0, 1, 2, 3, 1, 2, 3, 2, 3, 3]]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

几条example都已修改

col = row

if not isinstance(offset, int):
raise TypeError("offset should be a int")
Copy link
Contributor

Choose a reason for hiding this comment

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

be a int. 空一格

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已修改

auto b = -1 - f;
auto cX4 = x << 3; // 4 * c = 4 * (2x) = 8x;
*row = resolve_root_int<T>(b, cX4, x, -1);
*col = x - ((f - *row + 1) * *row >> 1) + *row;
Copy link
Contributor

Choose a reason for hiding this comment

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

这里最好加上()突出一下运算优先级。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已修改

@Rayman96
Copy link
Contributor Author

麻烦 @betterpig 大佬看下,这里的col允许输入为None以获得默认正方形矩阵是合理的吗

合理,这里是对齐了numpy的接口,简化方阵的输入。就按支持col为None来写接口和文档。

好的

@Rayman96
Copy link
Contributor Author

@betterpig RFC已修改,麻烦审核。PaddlePaddle/community#219

@Rayman96
Copy link
Contributor Author

@betterpig creation.py 接口已修改麻烦审核
@SigureMo 中英文的文档已修改麻烦审核 https://github.com/PaddlePaddle/docs/pull/5161。

SigureMo
SigureMo previously approved these changes Aug 23, 2022
Copy link
Member

@SigureMo SigureMo 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

Comment on lines 1939 to 1941
- If offset = 0, all elements on and below the main diagonal are retained.
- If offset > 0, include just as few diagonals above the main diagonal.
- If offset < 0, excludes just as few diagonals below the main diagonal.
Copy link
Member

Choose a reason for hiding this comment

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

这里也上下各加一个空行吧,刚才在 review 其他文档发现英文这个上下都有空行应该也是必需的

另外 offset 现在也有默认值了,这里需要加上默认值的描述

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这里也上下各加一个空行吧,刚才在 review 其他文档发现英文这个上下都有空行应该也是必需的

另外 offset 现在也有默认值了,这里需要加上默认值的描述

空行已添加
offset的默认值之前在1938行已有描述

Copy link
Member

Choose a reason for hiding this comment

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

😂 报歉是我眼瞎了

@Rayman96
Copy link
Contributor Author

@betterpig @SigureMo
修改后CI已通过,文档看起来也ok的,辛苦大佬们再审核看是否可以通过

betterpig
betterpig previously approved these changes Aug 24, 2022
SigureMo
SigureMo previously approved these changes Aug 24, 2022
Copy link
Member

@SigureMo SigureMo 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,麻烦 @Ligoml 最终确认下

Return the indices of the upper triangular part of the 2-D matrix
whose row and col is known. Indices are ordered based on row and then columns.
The upper triangular part of the matrix is defined as the elements on
and above the diagonal.
Copy link
Contributor

Choose a reason for hiding this comment

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

image

这里有一些报错,应该需要在 API 介绍和 Args: 之间空一行

Copy link
Contributor Author

Choose a reason for hiding this comment

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

我修改下

@Rayman96 Rayman96 dismissed stale reviews from SigureMo and betterpig via 5b4af77 August 24, 2022 07:14
Ligoml
Ligoml previously approved these changes Aug 24, 2022
Copy link
Contributor

@Ligoml Ligoml 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

@@ -1923,3 +1923,88 @@ def tril_indices(row, col, offset=0, dtype='int64'):
'dtype': dtype
})
return out


def triu_indices(row, col=None, offset=0, dtype='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 title of rfc should be paddle.triu_indices instead of paddle.tril_indices?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the title of rfc should be paddle.triu_indices instead of paddle.tril_indices?

不好意思 我写错了。已重新提交rfc的PR PaddlePaddle/community#224

return out

if _in_legacy_dygraph():
out = _C_ops.triu_indices('rows', row, 'cols', col, 'offset', offset,
Copy link
Contributor

Choose a reason for hiding this comment

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

In order to reuse documents, the name of python API parameters should be consistent with C++ API's, so should be row and col, and paddle.tril_indices have same problem which shoud be fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In order to reuse documents, the name of python API parameters should be consistent with C++ API's, so should be row and col, and paddle.tril_indices have same problem which shoud be fix.

triu_indices的代码中已统一将 rows cols 替换成 row col。 tril_indices应该另提一个PR修改?就不在这个PR里做调整了

Comment on lines 2005 to 2006
'rows': row,
'cols': col,
Copy link
Contributor

Choose a reason for hiding this comment

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

same with L1992

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已修改


template <typename T>
__device__ inline int resolve_root_int(int b, int cX4, int x, int32_t sign) {
int bXb_cX4 = b * b - cX4;
Copy link
Contributor

Choose a reason for hiding this comment

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

to multiply two int32 numbers, it's better to use int64_t or long long int to avoid overflow?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已修改

@Rayman96
Copy link
Contributor Author

@jeff41404 提到的问题已修改并通过CI,劳烦审核

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

Copy link
Contributor

@Ligoml Ligoml 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

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.

None yet

6 participants