-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Conversation
你的PR提交成功,感谢你对开源项目的贡献! |
可否请教下目前CI的三个失败。 |
CI-APPROVAL和CI-Static-Check等其他流水线通过后,需要找人review并approve。 |
多谢回复,目前除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()] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
现在有专门的CPU流水线和GPU流水线,为了减少单测时间,当为GPU版本时只跑GPUplace即可。所以后面的循环也是不必要的。下同。
There was a problem hiding this comment.
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]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 这里变量加上后缀1的用意是什么
- 这两行应放到guard外面。下同。
exe1 = paddle.static.Executor(place)
result1 = exe1.run(feed={}, fetch_list=[data1])
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
第一条已修改命名;第二条放到guard外后会报错,就保持缩进没有动了
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 这里变量加上后缀1的用意是什么
- 这两行应放到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)
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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
。下同
There was a problem hiding this comment.
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 应可通过
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好的,我根据参考链接修改此处判断
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已修改,可通过PR-CI-APPROVAL
python/paddle/tensor/creation.py
Outdated
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]] |
There was a problem hiding this comment.
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]]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
几条example都已修改
python/paddle/tensor/creation.py
Outdated
col = row | ||
|
||
if not isinstance(offset, int): | ||
raise TypeError("offset should be a int") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
be a int. 空一格
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里最好加上()突出一下运算优先级。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已修改
好的 |
@betterpig RFC已修改,麻烦审核。PaddlePaddle/community#219 |
@betterpig creation.py 接口已修改麻烦审核 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM for docs
python/paddle/tensor/creation.py
Outdated
- 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里也上下各加一个空行吧,刚才在 review 其他文档发现英文这个上下都有空行应该也是必需的
另外 offset 现在也有默认值了,这里需要加上默认值的描述
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里也上下各加一个空行吧,刚才在 review 其他文档发现英文这个上下都有空行应该也是必需的
另外 offset 现在也有默认值了,这里需要加上默认值的描述
空行已添加
offset的默认值之前在1938行已有描述
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😂 报歉是我眼瞎了
@betterpig @SigureMo |
There was a problem hiding this 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我修改下
There was a problem hiding this 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'): |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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 ofpaddle.tril_indices
?
不好意思 我写错了。已重新提交rfc的PR PaddlePaddle/community#224
python/paddle/tensor/creation.py
Outdated
return out | ||
|
||
if _in_legacy_dygraph(): | ||
out = _C_ops.triu_indices('rows', row, 'cols', col, 'offset', offset, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
andcol
, andpaddle.tril_indices
have same problem which shoud be fix.
triu_indices的代码中已统一将 rows cols 替换成 row col。 tril_indices应该另提一个PR修改?就不在这个PR里做调整了
python/paddle/tensor/creation.py
Outdated
'rows': row, | ||
'cols': col, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same with L1992
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已修改
@jeff41404 提到的问题已修改并通过CI,劳烦审核 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM for docs
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