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

add as_complex and as_real op #37784

Merged
merged 6 commits into from
Dec 10, 2021

Conversation

iclementine
Copy link

@iclementine iclementine commented Dec 2, 2021

PR types

New features

PR changes

OPs

Describe

  1. Add as_complex and as_real API, and corresponding operators.

examples

import paddle
x = paddle.arange(12, dtype=paddle.float32).reshape([2, 3, 2])
y = paddle.as_complex(x)
print(y.numpy())

# [[ 0. +1.j  2. +3.j  4. +5.j]
#  [ 6. +7.j  8. +9.j 10.+11.j]]
import paddle
x = paddle.arange(12, dtype=paddle.float32).reshape([2, 3, 2])
y = paddle.as_complex(x)
z = paddle.as_real(y)
print(z.numpy())

# [[[ 0.  1.]
#   [ 2.  3.]
#   [ 4.  5.]]

#  [[ 6.  7.]
#   [ 8.  9.]
#   [10. 11.]]]
  1. fix typos in doc of angle_op

@paddle-bot-old
Copy link

paddle-bot-old bot commented Dec 2, 2021

Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

jeff41404
jeff41404 previously approved these changes Dec 2, 2021
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


const framework::DDim out_dims(in_dims.Get(), input_rank - 1);
ctx->SetOutputDim("Out", out_dims);
ctx->ShareLoD("X", /*->*/ "Out");
Copy link
Contributor

Choose a reason for hiding this comment

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

实数和对应的复数的lod是一样的吗?

Copy link
Author

@iclementine iclementine Dec 2, 2021

Choose a reason for hiding this comment

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

实数和复数的 lod 是一样的。

对于 LoDTensor 来说,view_as_complex 和 view_as_real 直接操作 LoD Tensor 的 underlying Tensor.

比如说一个 underlying 形状为 (L, *base_shape) 的 复数 LoDTensor 来说,经过 view_as_real 会得到一个 LoD Tensor, 其 underlying Tensor 形状为 (L, *base_shape, 2)

而 LoD 信息表示的是递归折叠的端点信息。只要一个操作是局限在 base_shape 内部的操作,那么是不会改变 LoD 信息的,而复数和实数的互相 view,对于复数来说是 element 级别的操作,所以不会改变 LoD 信息。

对于 view_as_complex 来说,因为对输入 Tensor 形状有要求。假定为 (L, *base_shape), 那么每两个相邻的元素为一个单元视作复数,这个操作也不会跨越 base_shape, 所以不会改变 LoD 信息。

return np.stack([x.real, x.imag], -1)


class TestViewAsComplexOp(OpTest):
Copy link
Contributor

Choose a reason for hiding this comment

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

请参考test_view_op_reuse_allocation.py单测,测试下这两个API的view机制是否真的生效(改变输入输出中的某一个tensor值,另一个也会随之改变),同时也测下inplace version等信息。
可以直接在test_view_op_reuse_allocation.py单测中添加新的样例。

Copy link
Author

Choose a reason for hiding this comment

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

okay

Copy link
Author

Choose a reason for hiding this comment

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

加上了测试内存复用机制的单测 case.

pangyoki
pangyoki previously approved these changes Dec 2, 2021
Copy link
Contributor

@pangyoki pangyoki left a comment

Choose a reason for hiding this comment

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

LGTM

jeff41404
jeff41404 previously approved these changes Dec 2, 2021
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

@phlrain phlrain self-requested a review December 2, 2021 12:35
phlrain
phlrain previously approved these changes Dec 2, 2021
@iclementine iclementine changed the title add view_ad_complex and view_as_real op add view_as_complex and view_as_real op Dec 3, 2021
TCChenlong
TCChenlong previously approved these changes Dec 3, 2021
Copy link
Contributor

@TCChenlong TCChenlong left a comment

Choose a reason for hiding this comment

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

LGTM

@iclementine iclementine changed the title add view_as_complex and view_as_real op add as_complex and as_real op Dec 8, 2021
jeff41404
jeff41404 previously approved these changes Dec 9, 2021
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

TCChenlong
TCChenlong previously approved these changes Dec 9, 2021
Copy link
Contributor

@TCChenlong TCChenlong left a comment

Choose a reason for hiding this comment

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

LGTM

XiaoguangHu01
XiaoguangHu01 previously approved these changes Dec 9, 2021
Copy link
Contributor

@XiaoguangHu01 XiaoguangHu01 left a comment

Choose a reason for hiding this comment

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

LG API

Copy link
Contributor

@TCChenlong TCChenlong 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 API Docs

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

@XiaoguangHu01 XiaoguangHu01 left a comment

Choose a reason for hiding this comment

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

LGTM

@iclementine iclementine merged commit ae40370 into PaddlePaddle:develop Dec 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants