-
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
【complex op】No.12、14 add complex support for square & reciprocal #60821
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3414,6 +3414,11 @@ def setUp(self): | |
|
||
np.random.seed(1024) | ||
x = np.random.uniform(1, 2, self.shape).astype(self.dtype) | ||
if self.dtype == np.complex64 or self.dtype == np.complex128: | ||
x = ( | ||
np.random.uniform(-1, 1, self.shape) | ||
+ 1j * np.random.uniform(-1, 1, self.shape) | ||
).astype(self.dtype) | ||
out = np.reciprocal(x) | ||
|
||
self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} | ||
|
@@ -3423,12 +3428,29 @@ def setUp(self): | |
def test_check_grad(self): | ||
if self.dtype == np.float16: | ||
return | ||
self.check_grad(['X'], 'Out', max_relative_error=0.01, check_pir=True) | ||
if self.dtype == np.complex64 or self.dtype == np.complex128: | ||
self.check_grad( | ||
['X'], 'Out', max_relative_error=0.03, check_pir=True | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. 这里我在本机测试的是0.02多一点,所以扩大到0.03了 |
||
) | ||
else: | ||
self.check_grad( | ||
['X'], 'Out', max_relative_error=0.01, check_pir=True | ||
) | ||
|
||
def test_check_output(self): | ||
self.check_output(check_pir=True) | ||
|
||
|
||
class TestReciprocal_Complex64(TestReciprocal): | ||
def init_dtype(self): | ||
self.dtype = np.complex64 | ||
|
||
|
||
class TestReciprocal_Complex128(TestReciprocal): | ||
def init_dtype(self): | ||
self.dtype = np.complex128 | ||
|
||
|
||
class TestReciprocal_ZeroDim(TestReciprocal): | ||
def init_shape(self): | ||
self.shape = [] | ||
|
@@ -3799,6 +3821,11 @@ def setUp(self): | |
|
||
np.random.seed(1024) | ||
x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype) | ||
if self.dtype == np.complex64 or self.dtype == np.complex128: | ||
x = ( | ||
np.random.uniform(-1, 1, self.shape) | ||
+ 1j * np.random.uniform(-1, 1, self.shape) | ||
).astype(self.dtype) | ||
out = np.square(x) | ||
|
||
self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} | ||
|
@@ -3814,6 +3841,16 @@ def test_check_output(self): | |
self.check_output(check_pir=True) | ||
|
||
|
||
class TestSquare_Complex64(TestSquare): | ||
def init_dtype(self): | ||
self.dtype = np.complex64 | ||
|
||
|
||
class TestSquare_Complex128(TestSquare): | ||
def init_dtype(self): | ||
self.dtype = np.complex128 | ||
|
||
|
||
class TestSquare_ZeroDim(TestSquare): | ||
def init_shape(self): | ||
self.shape = [] | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
这里是我学习cumprod的代码时发现的,在cpu端梯度反向传播时会进行复数的
*=
操作,然后产生错误。比如说这张图中,x_grad[0][0]的梯度应该是conj(1 + x[1][0]) = conj(1 + 2 + 3j)=3-3j
,但是这里用了*=
,相应的计算逻辑是conj(1 + 1 *= x[1][0])=conj(1 + [(1 * 2 - 0*3) + (0*2+3*2)j])=conj(3+6j)
。应该就是因为在计算a.imag时使用了新的a.real导致的。